Skip to content

Commit

Permalink
Merge pull request #105 from mshitrit/add-no-remediate-label-on-maint…
Browse files Browse the repository at this point in the history
…nacne

NMO handling the new Exclude From remediation label on the node
  • Loading branch information
openshift-merge-bot[bot] authored Dec 5, 2023
2 parents c5459af + dc2d436 commit 137cff3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
38 changes: 36 additions & 2 deletions controllers/nodemaintenance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/medik8s/common/pkg/labels"
"github.com/medik8s/common/pkg/lease"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -175,8 +176,13 @@ func (r *NodeMaintenanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
if instance.Status.Phase != nodemaintenancev1beta1.MaintenanceRunning || instance.Status.ErrorOnLeaseCount != 0 {
instance.Status.Phase = nodemaintenancev1beta1.MaintenanceRunning
instance.Status.ErrorOnLeaseCount = 0

}
}
//Add exclude from remediation label
if err := r.addExcludeRemediationLabel(ctx, node); err != nil {
return r.onReconcileError(instance, err)
}

// Cordon node
err = AddOrRemoveTaint(r.drainer.Client, node, true)
Expand Down Expand Up @@ -313,6 +319,35 @@ func (r *NodeMaintenanceReconciler) obtainLease(node *corev1.Node) (bool, error)

return false, nil
}

func (r *NodeMaintenanceReconciler) addExcludeRemediationLabel(ctx context.Context, node *corev1.Node) error {
if node.Labels[labels.ExcludeFromRemediation] != "true" {
patch := client.MergeFrom(node.DeepCopy())
if node.Labels == nil {
node.Labels = map[string]string{labels.ExcludeFromRemediation: "true"}
} else if node.Labels[labels.ExcludeFromRemediation] != "true" {
node.Labels[labels.ExcludeFromRemediation] = "true"
}
if err := r.Client.Patch(ctx, node, patch); err != nil {
r.logger.Error(err, "Failed to add exclude from remediation label from the node", "node name", node.Name)
return err
}
}
return nil
}

func (r *NodeMaintenanceReconciler) removeExcludeRemediationLabel(ctx context.Context, node *corev1.Node) error {
if node.Labels[labels.ExcludeFromRemediation] == "true" {
patch := client.MergeFrom(node.DeepCopy())
delete(node.Labels, labels.ExcludeFromRemediation)
if err := r.Client.Patch(ctx, node, patch); err != nil {
r.logger.Error(err, "Failed to remove exclude from remediation label from the node", "node name", node.Name)
return err
}
}
return nil
}

func (r *NodeMaintenanceReconciler) stopNodeMaintenanceImp(ctx context.Context, node *corev1.Node) error {
// Uncordon the node
err := AddOrRemoveTaint(r.drainer.Client, node, false)
Expand All @@ -327,8 +362,7 @@ func (r *NodeMaintenanceReconciler) stopNodeMaintenanceImp(ctx context.Context,
if err := r.LeaseManager.InvalidateLease(ctx, node); err != nil {
return err
}

return nil
return r.removeExcludeRemediationLabel(ctx, node)
}

func (r *NodeMaintenanceReconciler) stopNodeMaintenanceOnDeletion(ctx context.Context, nodeName string) error {
Expand Down
18 changes: 18 additions & 0 deletions controllers/nodemaintenance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"time"

"github.com/medik8s/common/pkg/labels"
"github.com/medik8s/common/pkg/lease"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -235,6 +236,23 @@ var _ = Describe("Node Maintenance", func() {
checkFailedReconcile()
})

It("add/remove Exclude remediation label", func() {
reconcileMaintenance(nm)
checkSuccesfulReconcile()
node := &corev1.Node{}
err := k8sClient.Get(context.TODO(), client.ObjectKey{Name: nm.Spec.NodeName}, node)
Expect(err).NotTo(HaveOccurred())
//Label added on CR creation
Expect(node.Labels[labels.ExcludeFromRemediation]).To(Equal("true"))

Expect(k8sClient.Delete(context.Background(), nm)).To(Succeed())
reconcileMaintenance(nm)
//Re-fetch node after reconcile
Expect(k8sClient.Get(context.TODO(), client.ObjectKey{Name: nm.Spec.NodeName}, node)).NotTo(HaveOccurred())
_, exist := node.Labels[labels.ExcludeFromRemediation]
Expect(exist).To(BeFalse())
})

})
})

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/go-logr/logr v1.2.4
github.com/medik8s/common v1.2.0
github.com/medik8s/common v1.11.0
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.8
github.com/sirupsen/logrus v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2 h1:hAHbPm5IJGijwng3PWk09JkG9WeqChjprR5s9bBZ+OM=
github.com/matttproud/golang_protobuf_extensions v1.0.2/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/medik8s/common v1.2.0 h1:xgQOijD3rEn+PfCd4lJuV3WFdt5QA6SIaqF01rRp2as=
github.com/medik8s/common v1.2.0/go.mod h1:ZT/3hfMXJLmZEcqmxRWB5LGC8Wl+qKGGQ4zM8hOE7PY=
github.com/medik8s/common v1.11.0 h1:uw00Ej2aeRXG81zjfw9V69KRTyF47vBlSiV6eu8JipM=
github.com/medik8s/common v1.11.0/go.mod h1:ZT/3hfMXJLmZEcqmxRWB5LGC8Wl+qKGGQ4zM8hOE7PY=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
Expand Down
4 changes: 4 additions & 0 deletions vendor/github.com/medik8s/common/pkg/labels/labels.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions vendor/github.com/medik8s/common/pkg/lease/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ github.com/mailru/easyjson/jwriter
# github.com/matttproud/golang_protobuf_extensions v1.0.2
## explicit; go 1.9
github.com/matttproud/golang_protobuf_extensions/pbutil
# github.com/medik8s/common v1.2.0
# github.com/medik8s/common v1.11.0
## explicit; go 1.20
github.com/medik8s/common/pkg/labels
github.com/medik8s/common/pkg/lease
Expand Down

0 comments on commit 137cff3

Please sign in to comment.