From 106f313d65e08a034ad4f832c119312b0d40bca6 Mon Sep 17 00:00:00 2001 From: Yeray Borges Date: Fri, 17 Jun 2022 12:02:33 +0100 Subject: [PATCH 1/2] [226] Use patch instead of update to change pod labels fixes #226 --- .../wildflyserver/transaction_recovery.go | 4 +++- pkg/resources/actions.go | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/controller/wildflyserver/transaction_recovery.go b/pkg/controller/wildflyserver/transaction_recovery.go index 88e4eb9b..a17e19ea 100644 --- a/pkg/controller/wildflyserver/transaction_recovery.go +++ b/pkg/controller/wildflyserver/transaction_recovery.go @@ -14,6 +14,7 @@ import ( wfly "github.com/wildfly/wildfly-operator/pkg/controller/util" "github.com/wildfly/wildfly-operator/pkg/resources" corev1 "k8s.io/api/core/v1" + "sigs.k8s.io/controller-runtime/pkg/client" ) var ( @@ -222,8 +223,9 @@ func (r *ReconcileWildFlyServer) setupRecoveryPropertiesAndRestart(reqLogger log func (r *ReconcileWildFlyServer) updatePodLabel(w *wildflyv1alpha1.WildFlyServer, scaleDownPod *corev1.Pod, labelName, labelValue string) (bool, error) { updated := false if scaleDownPod.ObjectMeta.Labels[labelName] != labelValue { + patch := client.MergeFrom(scaleDownPod.DeepCopyObject()) scaleDownPod.ObjectMeta.Labels[labelName] = labelValue - if err := resources.Update(w, r.client, scaleDownPod); err != nil { + if err := resources.Patch(w, r.client, scaleDownPod, patch); err != nil { return false, fmt.Errorf("Failed to update pod labels for pod %v with label [%s=%s], error: %v", scaleDownPod.ObjectMeta.Name, labelName, labelValue, err) } diff --git a/pkg/resources/actions.go b/pkg/resources/actions.go index 1fba0235..1c9e4554 100644 --- a/pkg/resources/actions.go +++ b/pkg/resources/actions.go @@ -80,6 +80,25 @@ func Update(w *wildflyv1alpha1.WildFlyServer, client client.Client, objectDefini return nil } +func Patch(w *wildflyv1alpha1.WildFlyServer, client client.Client, objectDefinition runtime.Object, patch client.Patch) error { + logger := logWithValues(w, objectDefinition) + logger.Info("Patching Resource") + + meta := objectDefinition.(metav1.Object) + // mark the object with the current wildflyserver generation unless it is the wildflyserver itself + if objectDefinition != w { + MarkServerGeneration(w, meta) + } + + if err := client.Patch(context.TODO(), objectDefinition, patch); err != nil { + logger.Error(err, "Failed to patch resource") + return err + } + + logger.Info("Patched resource") + return nil +} + // UpdateStatus updates status of the resource specified by the objectDefinition. func UpdateStatus(w *wildflyv1alpha1.WildFlyServer, client client.Client, objectDefinition runtime.Object) error { logger := logWithValues(w, objectDefinition) From 9b51ed7b54cac8d4832b516d3c157121802d1bc0 Mon Sep 17 00:00:00 2001 From: Yeray Borges Date: Fri, 17 Jun 2022 14:36:17 +0100 Subject: [PATCH 2/2] [226] Use debug loglevel for the trace that confirms the action (create/read/update/pacth/delete) taken on a resource --- pkg/resources/actions.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/resources/actions.go b/pkg/resources/actions.go index 1c9e4554..7c865fde 100644 --- a/pkg/resources/actions.go +++ b/pkg/resources/actions.go @@ -40,7 +40,7 @@ func Create(w *wildflyv1alpha1.WildFlyServer, client client.Client, scheme *runt return err } - logger.Info("Created resource") + logger.V(1).Info("Created resource") return nil } @@ -56,7 +56,7 @@ func Get(w *wildflyv1alpha1.WildFlyServer, namespacedName types.NamespacedName, return err } - logger.Info("Got resource") + logger.V(1).Info("Got resource") return nil } @@ -76,7 +76,7 @@ func Update(w *wildflyv1alpha1.WildFlyServer, client client.Client, objectDefini return err } - logger.Info("Updated resource") + logger.V(1).Info("Updated resource") return nil } @@ -95,7 +95,7 @@ func Patch(w *wildflyv1alpha1.WildFlyServer, client client.Client, objectDefinit return err } - logger.Info("Patched resource") + logger.V(1).Info("Patched resource") return nil } @@ -109,7 +109,7 @@ func UpdateStatus(w *wildflyv1alpha1.WildFlyServer, client client.Client, object return err } - logger.Info("Updated status of resource") + logger.V(1).Info("Updated status of resource") return nil } @@ -123,7 +123,7 @@ func UpdateWildFlyServerStatus(w *wildflyv1alpha1.WildFlyServer, client client.C return err } - logger.Info("Updated status of WildFlyServer") + logger.V(1).Info("Updated status of WildFlyServer") return nil } @@ -137,7 +137,7 @@ func Delete(w *wildflyv1alpha1.WildFlyServer, client client.Client, objectDefini return err } - logger.Info("Deleted resource") + logger.V(1).Info("Deleted resource") return nil }