Skip to content

Commit

Permalink
restic node-agent refactor label
Browse files Browse the repository at this point in the history
  • Loading branch information
kaovilai committed Jan 19, 2023
1 parent 1200dc8 commit 0d35ee8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
22 changes: 10 additions & 12 deletions controllers/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

const (
Restic = "restic"
ResticRestoreHelperCM = "restic-restore-action-config"
HostPods = "host-pods"
)
Expand All @@ -37,11 +36,12 @@ var (
// we need to declare mountPropagationToHostContainer so that we have an address to point to
// for ds.Spec.Template.Spec.Volumes[].Containers[].VolumeMounts[].MountPropagation
mountPropagationToHostContainer = corev1.MountPropagationHostToContainer
resticLabelSelector = &metav1.LabelSelector{
MatchLabels: map[string]string{
nodeAgentMatchLabels = map[string]string{
"component": common.Velero,
"name": common.NodeAgent,
},
}
nodeAgentLabelSelector = &metav1.LabelSelector{
MatchLabels: nodeAgentMatchLabels,
}
)

Expand All @@ -53,13 +53,11 @@ func getResticPvHostPath() string {
return env
}

func getResticObjectMeta(r *DPAReconciler) metav1.ObjectMeta {
func getNodeAgentObjectMeta(r *DPAReconciler) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: Restic,
Name: common.NodeAgent,
Namespace: r.NamespacedName.Namespace,
Labels: map[string]string{
"component": "velero",
},
Labels: nodeAgentMatchLabels,
}
}

Expand All @@ -71,7 +69,7 @@ func (r *DPAReconciler) ReconcileResticDaemonset(log logr.Logger) (bool, error)

// Define "static" portion of daemonset
ds := &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
}
if dpa.Spec.Configuration.Restic == nil || dpa.Spec.Configuration.Restic != nil && (dpa.Spec.Configuration.Restic.Enable == nil || !*dpa.Spec.Configuration.Restic.Enable) {
deleteContext := context.Background()
Expand Down Expand Up @@ -114,7 +112,7 @@ func (r *DPAReconciler) ReconcileResticDaemonset(log logr.Logger) (bool, error)
if ds.Spec.Selector.MatchLabels == nil {
ds.Spec.Selector.MatchLabels = make(map[string]string)
}
ds.Spec.Selector.MatchLabels, err = common.AppendUniqueLabels(ds.Spec.Selector.MatchLabels, resticLabelSelector.MatchLabels)
ds.Spec.Selector.MatchLabels, err = common.AppendUniqueLabels(ds.Spec.Selector.MatchLabels, nodeAgentLabelSelector.MatchLabels)
if err != nil {
return fmt.Errorf("failed to append labels to selector: %s", err)
}
Expand Down Expand Up @@ -206,7 +204,7 @@ func (r *DPAReconciler) customizeResticDaemonset(dpa *oadpv1alpha1.DataProtectio
}
}
// customize specs
ds.Spec.Selector = resticLabelSelector
ds.Spec.Selector = nodeAgentLabelSelector
ds.Spec.UpdateStrategy = appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
}
Expand Down
74 changes: 37 additions & 37 deletions controllers/restic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -144,7 +144,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -271,12 +271,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -285,7 +285,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: true,
Expand Down Expand Up @@ -441,12 +441,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -455,7 +455,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -609,12 +609,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -623,7 +623,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -776,12 +776,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -790,7 +790,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -942,12 +942,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -956,7 +956,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1105,12 +1105,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -1119,7 +1119,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1271,12 +1271,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -1285,7 +1285,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1434,12 +1434,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -1448,7 +1448,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1592,12 +1592,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -1606,7 +1606,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1766,12 +1766,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -1780,7 +1780,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1938,12 +1938,12 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
},
},
}, &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
},
},
wantErr: false,
want: &appsv1.DaemonSet{
ObjectMeta: getResticObjectMeta(r),
ObjectMeta: getNodeAgentObjectMeta(r),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
Expand All @@ -1952,7 +1952,7 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
Type: appsv1.RollingUpdateDaemonSetStrategyType,
},
Selector: resticLabelSelector,
Selector: nodeAgentLabelSelector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down

0 comments on commit 0d35ee8

Please sign in to comment.