fdb-kubernetes-monitor: Ensure that the annotations are updated when the correct configuration is already loaded (#11486)

This commit is contained in:
Johannes Scheuermann 2024-07-04 12:50:31 +02:00 committed by GitHub
parent e52fc3621f
commit 2753f4e2b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 15 deletions

View File

@ -239,11 +239,6 @@ func (podClient *kubernetesClient) updateAnnotationsOnPod(annotationChanges map[
if podClient.podMetadata == nil {
return fmt.Errorf("pod client has no metadata present")
}
annotations := podClient.podMetadata.Annotations
if len(annotations) == 0 {
annotations = map[string]string{}
}
if !podClient.podMetadata.DeletionTimestamp.IsZero() {
return fmt.Errorf("pod is marked for deletion, cannot update annotations")
@ -264,13 +259,24 @@ func (podClient *kubernetesClient) updateAnnotationsOnPod(annotationChanges map[
return true
}, func() error {
annotations := podClient.podMetadata.Annotations
if len(annotations) == 0 {
annotations = map[string]string{}
currentAnnotations := podClient.podMetadata.Annotations
if len(currentAnnotations) == 0 {
currentAnnotations = map[string]string{}
}
var hasChanges bool
for key, val := range annotationChanges {
annotations[key] = val
currentValue, present := currentAnnotations[key]
if !present || currentValue != val {
podClient.Logger.Info("update annotation with new value", "annotation", key, "currentValue", currentValue, "newValue", val, "present", present)
currentAnnotations[key] = val
hasChanges = true
}
}
// If no changes are present, we can skip the patch.
if !hasChanges {
return nil
}
return podClient.Patch(context.Background(), &corev1.Pod{
@ -281,7 +287,7 @@ func (podClient *kubernetesClient) updateAnnotationsOnPod(annotationChanges map[
ObjectMeta: metav1.ObjectMeta{
Namespace: podClient.podMetadata.Namespace,
Name: podClient.podMetadata.Name,
Annotations: annotations,
Annotations: currentAnnotations,
},
}, client.Apply, client.FieldOwner("fdb-kubernetes-monitor"), client.ForceOwnership)
})

View File

@ -283,6 +283,12 @@ func (monitor *monitor) loadConfiguration() {
}
monitor.acceptConfiguration(configuration, configurationBytes)
// Always update the annotations if needed to handle cases where the fdb-kubernetes-monitor
// has loaded the new configuration but was not able to update the annotations.
err := monitor.podClient.updateAnnotations(monitor)
if err != nil {
monitor.logger.Error(err, "Error updating pod annotations")
}
}
// checkOwnerExecutable validates that a path is a file that exists and is
@ -332,11 +338,6 @@ func (monitor *monitor) acceptConfiguration(configuration *api.ProcessConfigurat
if hasRunningProcesses && !monitor.activeConfiguration.ShouldRunServers() {
monitor.sendSignalToProcesses(syscall.SIGTERM)
}
err := monitor.podClient.updateAnnotations(monitor)
if err != nil {
monitor.logger.Error(err, "Error updating pod annotations")
}
}
// getBackoffDuration returns the backoff duration. The backoff time will increase exponential with a maximum of 60 seconds.