Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[226] Use patch instead of update to change pod labels #227

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/controller/wildflyserver/transaction_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/resources/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yersan should this be logged at Debug level?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it can be a debug level instead, I just followed the same pattern we had for the other actions, but it is very verbose indeed.

Copy link
Collaborator Author

@yersan yersan Jun 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmesnil I've changed the message to debug level for the trace that confirms the action taken. There is no need to show it as info, if the operation fails an error is logged, otherwise, we can assume it was executed successfully, no need to log such a confirmation

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)
Expand Down