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

Match labels #133

Merged
merged 12 commits into from
Aug 3, 2023
9 changes: 6 additions & 3 deletions controllers/selfnoderemediation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,9 @@ func verifyNodeIsUnschedulable() *v1.Node {
func verifySelfNodeRemediationPodExist() {
podList := &v1.PodList{}
selector := labels.NewSelector()
requirement, _ := labels.NewRequirement("app", selection.Equals, []string{"self-node-remediation-agent"})
selector = selector.Add(*requirement)
nameRequirement, _ := labels.NewRequirement("app.kubernetes.io/name", selection.Equals, []string{"self-node-remediation"})
componentRequirement, _ := labels.NewRequirement("app.kubernetes.io/component", selection.Equals, []string{"agent"})
selector = selector.Add(*nameRequirement, *componentRequirement)

EventuallyWithOffset(1, func() (int, error) {
err := k8sClient.Client.List(context.Background(), podList, &client.ListOptions{LabelSelector: selector})
Expand All @@ -567,7 +568,9 @@ func createSNR(strategy v1alpha1.RemediationStrategyType) {
func createSelfNodeRemediationPod() {
pod := &v1.Pod{}
pod.Spec.NodeName = unhealthyNodeName
pod.Labels = map[string]string{"app": "self-node-remediation-agent"}
pod.Labels = map[string]string{"app.kubernetes.io/name": "self-node-remediation",
"app.kubernetes.io/component": "agent"}

pod.Name = "self-node-remediation"
pod.Namespace = namespace
container := v1.Container{
Expand Down
41 changes: 41 additions & 0 deletions controllers/selfnoderemediationconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
"os"

"github.com/go-logr/logr"
pkgerrors "github.com/pkg/errors"

v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -38,6 +40,12 @@ import (
"github.com/medik8s/self-node-remediation/pkg/render"
)

const (
dsName = "self-node-remediation-ds"
lastChangedAnnotationKey = "dsLastChangeVersion"
lastChangeAnnotationVal = "rhwa-23.3"
mshitrit marked this conversation as resolved.
Show resolved Hide resolved
)

// SelfNodeRemediationConfigReconciler reconciles a SelfNodeRemediationConfig object
type SelfNodeRemediationConfigReconciler struct {
client.Client
Expand Down Expand Up @@ -103,6 +111,10 @@ func (r *SelfNodeRemediationConfigReconciler) syncConfigDaemonSet(ctx context.Co
logger := r.Log.WithName("syncConfigDaemonset")
logger.Info("Start to sync config daemonset")

if err := r.removeOldDsOnUpdateOperator(ctx); err != nil {
mshitrit marked this conversation as resolved.
Show resolved Hide resolved
return err
}

data := render.MakeRenderData()
data.Data["Image"] = os.Getenv("SELF_NODE_REMEDIATION_IMAGE")
data.Data["Namespace"] = snrConfig.Namespace
Expand Down Expand Up @@ -243,3 +255,32 @@ func (r *SelfNodeRemediationConfigReconciler) convertTolerationsToUnstructed(tol
}
return convertedTolerations, nil
}

func (r *SelfNodeRemediationConfigReconciler) removeOldDsOnUpdateOperator(ctx context.Context) error {
ds := &v1.DaemonSet{}
key := types.NamespacedName{
Namespace: r.Namespace,
Name: dsName,
}

if err := r.Client.Get(ctx, key, ds); err == nil {
if lastChangedActualVal, _ := ds.Annotations[lastChangedAnnotationKey]; lastChangedActualVal == lastChangeAnnotationVal {
mshitrit marked this conversation as resolved.
Show resolved Hide resolved
//ds is up-to-date this is not an update scenario
return nil
}

if err = r.Client.Delete(ctx, ds); err != nil {
r.Log.Error(err, "snr update failed could not delete old daemonset")
return pkgerrors.Wrap(err, "unable to delete old daemon set")
}
r.Log.Info("snr update old daemonset deleted")
return nil

} else if !errors.IsNotFound(err) {
r.Log.Error(err, "snr install/update failed error when trying to fetch old daemonset")
return pkgerrors.Wrap(err, "unable to fetch daemon set")
}

r.Log.Info("snr didn't find old daemonset to be deleted")
return nil
}
9 changes: 6 additions & 3 deletions install/self-node-remediation-deamonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ metadata:
namespace: {{.Namespace}}
labels:
k8s-app: self-node-remediation
annotations:
dsLastChangeVersion: rhwa-23.3
mshitrit marked this conversation as resolved.
Show resolved Hide resolved
spec:
selector:
matchLabels:
control-plane: controller-manager
app.kubernetes.io/name: self-node-remediation
app.kubernetes.io/component: agent
template:
metadata:
creationTimestamp: null
labels:
control-plane: controller-manager
app: self-node-remediation-agent
app.kubernetes.io/name: self-node-remediation
app.kubernetes.io/component: agent
spec:
volumes:
- name: devices
Expand Down
5 changes: 3 additions & 2 deletions pkg/utils/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ func GetSelfNodeRemediationAgentPod(nodeName string, r client.Reader) (*v1.Pod,
podList := &v1.PodList{}

selector := labels.NewSelector()
requirement, _ := labels.NewRequirement("app", selection.Equals, []string{"self-node-remediation-agent"})
selector = selector.Add(*requirement)
nameRequirement, _ := labels.NewRequirement("app.kubernetes.io/name", selection.Equals, []string{"self-node-remediation"})
componentRequirement, _ := labels.NewRequirement("app.kubernetes.io/component", selection.Equals, []string{"agent"})
selector = selector.Add(*nameRequirement, *componentRequirement)

err := r.List(context.Background(), podList, &client.ListOptions{LabelSelector: selector})
if err != nil {
Expand Down