Make sure that primary and replicas are correctly labelled as such Returns true if the instance needed updating
( ctx context.Context, cluster *apiv1.Cluster, instance *corev1.Pod, )
| 164 | // |
| 165 | // Returns true if the instance needed updating |
| 166 | func updateRoleLabels( |
| 167 | ctx context.Context, |
| 168 | cluster *apiv1.Cluster, |
| 169 | instance *corev1.Pod, |
| 170 | ) bool { |
| 171 | contextLogger := log.FromContext(ctx) |
| 172 | |
| 173 | // No current primary, no work to do |
| 174 | if cluster.Status.CurrentPrimary == "" { |
| 175 | return false |
| 176 | } |
| 177 | |
| 178 | if !utils.IsPodActive(*instance) { |
| 179 | contextLogger.Trace("Ignoring not active Pod during label update", |
| 180 | "pod", instance.Name, "status", instance.Status) |
| 181 | return false |
| 182 | } |
| 183 | |
| 184 | if instance.Labels == nil { |
| 185 | instance.Labels = make(map[string]string) |
| 186 | } |
| 187 | |
| 188 | // it is important to note that even if utils.ClusterRoleLabelName is deprecated, |
| 189 | // we still ensure that the values are aligned between the two fields |
| 190 | //nolint:staticcheck |
| 191 | podRole, hasRole := instance.Labels[utils.ClusterRoleLabelName] |
| 192 | newPodRole, newHasRole := instance.Labels[utils.ClusterInstanceRoleLabelName] |
| 193 | |
| 194 | switch instance.Name { |
| 195 | case cluster.Status.CurrentPrimary: |
| 196 | if !hasRole || podRole != specs.ClusterRoleLabelPrimary || !newHasRole || |
| 197 | newPodRole != specs.ClusterRoleLabelPrimary { |
| 198 | contextLogger.Info("Setting primary label", "pod", instance.Name) |
| 199 | utils.SetInstanceRole(&instance.ObjectMeta, specs.ClusterRoleLabelPrimary) |
| 200 | return true |
| 201 | } |
| 202 | |
| 203 | default: |
| 204 | // This intentionally overwrites the transient ClusterRoleLabelUnhealthy value |
| 205 | // that the failover path sets on the old primary. This function is only reached |
| 206 | // once CurrentPrimary == TargetPrimary (the failover guard in the reconcile loop |
| 207 | // returns early otherwise), so by the time we get here the old primary has been |
| 208 | // demoted and "replica" is the correct label. |
| 209 | if !hasRole || podRole != specs.ClusterRoleLabelReplica || !newHasRole || |
| 210 | newPodRole != specs.ClusterRoleLabelReplica { |
| 211 | contextLogger.Info("Setting replica label", "pod", instance.Name) |
| 212 | utils.SetInstanceRole(&instance.ObjectMeta, specs.ClusterRoleLabelReplica) |
| 213 | return true |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return false |
| 218 | } |
| 219 | |
| 220 | // updateOperatorLabels ensures that the instances are labelled as instances, |
| 221 | // and have the correct instance name |
no test coverage detected