Skip to content

Commit fd20530

Browse files
authored
[OADP-452 , OADP-490] cherrypick 1.0 #668 (#688)
* Squashed commit from #668 of the following: commit e57aade Author: Tiger Kaovilai <[email protected]> Date: Tue May 10 17:12:39 2022 -0400 return err from Delete commit ed53505 Author: Tiger Kaovilai <[email protected]> Date: Tue May 10 17:04:38 2022 -0400 mitigate immutable selector update with recreate commit 32d3faa Author: Tiger Kaovilai <[email protected]> Date: Tue May 10 15:49:19 2022 -0400 pod -> pods commit 2870858 Author: Tiger Kaovilai <[email protected]> Date: Mon May 9 15:34:24 2022 -0400 add catalog-build-replaces commit a0ecefd Author: Tiger Kaovilai <[email protected]> Date: Fri May 6 09:46:16 2022 -0400 go fmt from make test commit c0dc1a6 Author: Tiger Kaovilai <[email protected]> Date: Fri May 6 09:43:40 2022 -0400 clean up restic config nil condition commit d2724b8 Author: Tiger Kaovilai <[email protected]> Date: Fri May 6 09:26:54 2022 -0400 return from customizeResticDaemonset early if config is nil commit 04cdb7c Author: Tiger Kaovilai <[email protected]> Date: Fri May 6 09:26:15 2022 -0400 remove cmp from velero_test commit c8744cd Author: Tiger Kaovilai <[email protected]> Date: Fri May 6 01:46:21 2022 -0400 add unit test for proxy commit 4350a91 Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 23:13:03 2022 -0400 go mod tidy commit 6cc6dd2 Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 23:12:56 2022 -0400 don't override matchlabels commit 0a34837 Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 22:27:30 2022 -0400 move comment commit c2b6754 Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 22:18:32 2022 -0400 go fmt commit 3ef486a Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 22:18:23 2022 -0400 label struct comment, make bundle, deepcopy commit 4936b44 Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 22:09:08 2022 -0400 Add labels to podConfig of restic and velero commit b7f04ad Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 21:26:54 2022 -0400 err update commit da84c27 Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 21:22:53 2022 -0400 AppendUniqueKeyStringOfStringMaps handle nil sandwich commit f026cb5 Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 21:13:58 2022 -0400 deploy pod label is back commit 4b237d6 Author: Tiger Kaovilai <[email protected]> Date: Thu May 5 20:53:38 2022 -0400 bring back `deploy: velero` label to velero pods * go mod tidy
1 parent e9a2deb commit fd20530

14 files changed

+894
-50
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ bundle-build: ## Build the bundle image.
275275
bundle-push: ## Push the bundle image.
276276
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
277277

278+
GIT_REV:=$(shell git rev-parse --short HEAD)
278279
## Build current branch operator image, bundle image, push and install via OLM
279-
deploy-olm: GIT_REV=$(shell git rev-parse --short HEAD)
280+
.PHONY: deploy-olm
280281
deploy-olm: THIS_OPERATOR_IMAGE?=ttl.sh/oadp-operator-$(GIT_REV):1h # Set target specific variable
281282
deploy-olm: THIS_BUNDLE_IMAGE?=ttl.sh/oadp-operator-bundle-$(GIT_REV):1h # Set target specific variable
282283
deploy-olm:
@@ -322,6 +323,11 @@ endif
322323
catalog-build: opm ## Build a catalog image.
323324
$(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
324325

326+
# For testing oeprator upgrade
327+
# opm upgrade
328+
catalog-build-replaces: opm ## Build a catalog image using replace mode
329+
$(OPM) index add --container-tool docker --mode replaces --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
330+
325331
# Push the catalog image.
326332
.PHONY: catalog-push
327333
catalog-push: ## Push a catalog image.

api/v1alpha1/oadp_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ type VeleroConfig struct {
8282

8383
// PodConfig defines the pod configuration options
8484
type PodConfig struct {
85+
// Labels to add to pods
86+
// +optional
87+
Labels map[string]string `json:"labels,omitempty"`
8588
// NodeSelector defines the nodeSelector to be supplied to Restic podSpec
8689
// +optional
8790
NodeSelector map[string]string `json:"nodeSelector,omitempty"`

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/oadp-operator.clusterserviceversion.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,15 @@ spec:
430430
- security.openshift.io
431431
resourceNames:
432432
- privileged
433-
- velero-privileged
434433
resources:
435434
- securitycontextconstraints
436435
verbs:
437436
- use
438437
serviceAccountName: velero
439438
deployments:
440-
- name: openshift-adp-controller-manager
439+
- label:
440+
control-plane: controller-manager
441+
name: openshift-adp-controller-manager
441442
spec:
442443
replicas: 1
443444
selector:

bundle/manifests/oadp.openshift.io_dataprotectionapplications.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ spec:
192192
podConfig:
193193
description: Pod specific configuration
194194
properties:
195+
labels:
196+
additionalProperties:
197+
type: string
198+
description: Labels to add to pods
199+
type: object
195200
nodeSelector:
196201
additionalProperties:
197202
type: string
@@ -329,6 +334,11 @@ spec:
329334
podConfig:
330335
description: Pod specific configuration
331336
properties:
337+
labels:
338+
additionalProperties:
339+
type: string
340+
description: Labels to add to pods
341+
type: object
332342
nodeSelector:
333343
additionalProperties:
334344
type: string

config/crd/bases/oadp.openshift.io_dataprotectionapplications.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ spec:
194194
podConfig:
195195
description: Pod specific configuration
196196
properties:
197+
labels:
198+
additionalProperties:
199+
type: string
200+
description: Labels to add to pods
201+
type: object
197202
nodeSelector:
198203
additionalProperties:
199204
type: string
@@ -331,6 +336,11 @@ spec:
331336
podConfig:
332337
description: Pod specific configuration
333338
properties:
339+
labels:
340+
additionalProperties:
341+
type: string
342+
description: Labels to add to pods
343+
type: object
334344
nodeSelector:
335345
additionalProperties:
336346
type: string

controllers/restic.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ func (r *DPAReconciler) ReconcileResticDaemonset(log logr.Logger) (bool, error)
101101
// Deployment selector is immutable so we set this value only if
102102
// a new object is going to be created
103103
if ds.ObjectMeta.CreationTimestamp.IsZero() {
104-
ds.Spec.Selector = resticLabelSelector
104+
if ds.Spec.Selector == nil {
105+
ds.Spec.Selector = &metav1.LabelSelector{}
106+
}
107+
var err error
108+
if ds.Spec.Selector == nil {
109+
ds.Spec.Selector = &metav1.LabelSelector{
110+
MatchLabels: make(map[string]string),
111+
}
112+
}
113+
if ds.Spec.Selector.MatchLabels == nil {
114+
ds.Spec.Selector.MatchLabels = make(map[string]string)
115+
}
116+
ds.Spec.Selector.MatchLabels, err = common.AppendUniqueLabels(ds.Spec.Selector.MatchLabels, resticLabelSelector.MatchLabels)
117+
if err != nil {
118+
return fmt.Errorf("failed to append labels to selector: %s", err)
119+
}
105120
}
106121

107122
if err := controllerutil.SetControllerReference(&dpa, ds, r.Scheme); err != nil {
@@ -114,6 +129,19 @@ func (r *DPAReconciler) ReconcileResticDaemonset(log logr.Logger) (bool, error)
114129
})
115130

116131
if err != nil {
132+
if errors.IsInvalid(err) {
133+
cause, isStatusCause := errors.StatusCause(err, metav1.CauseTypeFieldValueInvalid)
134+
if isStatusCause && cause.Field == "spec.selector" {
135+
// recreate deployment
136+
// TODO: check for in-progress backup/restore to wait for it to finish
137+
log.Info("Found immutable selector from previous daemonset, recreating restic daemonset")
138+
err := r.Delete(r.Context, ds)
139+
if err != nil {
140+
return false, err
141+
}
142+
return r.ReconcileResticDaemonset(log)
143+
}
144+
}
117145
return false, err
118146
}
119147

@@ -149,16 +177,29 @@ func (r *DPAReconciler) buildResticDaemonset(dpa *oadpv1alpha1.DataProtectionApp
149177
install.WithAnnotations(dpa.Spec.PodAnnotations),
150178
install.WithSecret(false))
151179
// Update Items in ObjectMeta
180+
dsName := ds.Name
152181
ds.TypeMeta = installDs.TypeMeta
153182
// Update Spec
154183
ds.Spec = installDs.Spec
155-
ds.Labels = installDs.Labels
184+
ds.ObjectMeta = installDs.ObjectMeta
185+
ds.Name = dsName
156186

157187
return r.customizeResticDaemonset(dpa, ds)
158188
}
159189

160190
func (r *DPAReconciler) customizeResticDaemonset(dpa *oadpv1alpha1.DataProtectionApplication, ds *appsv1.DaemonSet) (*appsv1.DaemonSet, error) {
161-
191+
if dpa.Spec.Configuration.Restic == nil {
192+
// if restic is not configured, therefore not enabled, return early.
193+
return nil, nil
194+
}
195+
// add custom pod labels
196+
if dpa.Spec.Configuration.Restic.PodConfig != nil && dpa.Spec.Configuration.Restic.PodConfig.Labels != nil {
197+
var err error
198+
ds.Spec.Template.Labels, err = common.AppendUniqueLabels(ds.Spec.Template.Labels, dpa.Spec.Configuration.Restic.PodConfig.Labels)
199+
if err != nil {
200+
return nil, fmt.Errorf("restic daemonset template custom label: %s", err)
201+
}
202+
}
162203
// customize specs
163204
ds.Spec.Selector = resticLabelSelector
164205
ds.Spec.UpdateStrategy = appsv1.DaemonSetUpdateStrategy{

controllers/restic_test.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,172 @@ func TestDPAReconciler_buildResticDaemonset(t *testing.T) {
241241
},
242242
},
243243
},
244+
{
245+
name: "podConfig label for velero and restic",
246+
args: args{
247+
&oadpv1alpha1.DataProtectionApplication{
248+
Spec: oadpv1alpha1.DataProtectionApplicationSpec{
249+
Configuration: &oadpv1alpha1.ApplicationConfig{
250+
Restic: &oadpv1alpha1.ResticConfig{
251+
PodConfig: &oadpv1alpha1.PodConfig{
252+
Labels: map[string]string{
253+
"resticLabel": "this is a label",
254+
},
255+
},
256+
},
257+
Velero: &oadpv1alpha1.VeleroConfig{
258+
PodConfig: &oadpv1alpha1.PodConfig{
259+
Labels: map[string]string{
260+
"veleroLabel": "this is a label",
261+
},
262+
},
263+
},
264+
},
265+
},
266+
}, &appsv1.DaemonSet{
267+
ObjectMeta: getResticObjectMeta(r),
268+
},
269+
},
270+
wantErr: false,
271+
want: &appsv1.DaemonSet{
272+
ObjectMeta: getResticObjectMeta(r),
273+
TypeMeta: metav1.TypeMeta{
274+
Kind: "DaemonSet",
275+
APIVersion: appsv1.SchemeGroupVersion.String(),
276+
},
277+
Spec: appsv1.DaemonSetSpec{
278+
UpdateStrategy: appsv1.DaemonSetUpdateStrategy{
279+
Type: appsv1.RollingUpdateDaemonSetStrategyType,
280+
},
281+
Selector: resticLabelSelector,
282+
Template: v1.PodTemplateSpec{
283+
ObjectMeta: metav1.ObjectMeta{
284+
Labels: map[string]string{
285+
"component": common.Velero,
286+
"name": common.Restic,
287+
"resticLabel": "this is a label",
288+
},
289+
},
290+
Spec: v1.PodSpec{
291+
NodeSelector: dpa.Spec.Configuration.Restic.PodConfig.NodeSelector,
292+
ServiceAccountName: common.Velero,
293+
SecurityContext: &v1.PodSecurityContext{
294+
RunAsUser: pointer.Int64(0),
295+
SupplementalGroups: dpa.Spec.Configuration.Restic.SupplementalGroups,
296+
},
297+
Volumes: []v1.Volume{
298+
// Cloud Provider volumes are dynamically added in the for loop below
299+
{
300+
Name: HostPods,
301+
VolumeSource: v1.VolumeSource{
302+
HostPath: &v1.HostPathVolumeSource{
303+
Path: resticPvHostPath,
304+
},
305+
},
306+
},
307+
{
308+
Name: "scratch",
309+
VolumeSource: v1.VolumeSource{
310+
EmptyDir: &v1.EmptyDirVolumeSource{},
311+
},
312+
},
313+
{
314+
Name: "certs",
315+
VolumeSource: v1.VolumeSource{
316+
EmptyDir: &v1.EmptyDirVolumeSource{},
317+
},
318+
},
319+
},
320+
Tolerations: dpa.Spec.Configuration.Restic.PodConfig.Tolerations,
321+
Containers: []v1.Container{
322+
{
323+
Name: common.Restic,
324+
SecurityContext: &v1.SecurityContext{
325+
Privileged: pointer.Bool(true),
326+
},
327+
Image: getVeleroImage(&dpa),
328+
ImagePullPolicy: v1.PullAlways,
329+
Resources: r.getResticResourceReqs(&dpa), //setting default.
330+
Command: []string{
331+
"/velero",
332+
},
333+
Args: []string{
334+
"restic",
335+
"server",
336+
},
337+
VolumeMounts: []v1.VolumeMount{
338+
{
339+
Name: "host-pods",
340+
MountPath: "/host_pods",
341+
MountPropagation: &mountPropagationToHostContainer,
342+
},
343+
{
344+
Name: "scratch",
345+
MountPath: "/scratch",
346+
},
347+
{
348+
Name: "certs",
349+
MountPath: "/etc/ssl/certs",
350+
},
351+
},
352+
Env: []v1.EnvVar{
353+
{
354+
Name: "NODE_NAME",
355+
ValueFrom: &v1.EnvVarSource{
356+
FieldRef: &v1.ObjectFieldSelector{
357+
FieldPath: "spec.nodeName",
358+
},
359+
},
360+
},
361+
{
362+
Name: "VELERO_NAMESPACE",
363+
ValueFrom: &v1.EnvVarSource{
364+
FieldRef: &v1.ObjectFieldSelector{
365+
FieldPath: "metadata.namespace",
366+
},
367+
},
368+
},
369+
{
370+
Name: "VELERO_SCRATCH_DIR",
371+
Value: "/scratch",
372+
},
373+
},
374+
},
375+
},
376+
},
377+
},
378+
},
379+
},
380+
},
381+
{
382+
name: "Invalid podConfig label for velero and restic",
383+
args: args{
384+
&oadpv1alpha1.DataProtectionApplication{
385+
Spec: oadpv1alpha1.DataProtectionApplicationSpec{
386+
Configuration: &oadpv1alpha1.ApplicationConfig{
387+
Restic: &oadpv1alpha1.ResticConfig{
388+
PodConfig: &oadpv1alpha1.PodConfig{
389+
Labels: map[string]string{
390+
"name": "not-restic", // this label is already defined by https://github.com/openshift/velero/blob/198ea57407d5271dc4ae00068123754ecff306ea/pkg/install/daemonset.go#L72
391+
},
392+
},
393+
},
394+
Velero: &oadpv1alpha1.VeleroConfig{
395+
PodConfig: &oadpv1alpha1.PodConfig{
396+
Labels: map[string]string{
397+
"veleroLabel": "this is a label",
398+
},
399+
},
400+
},
401+
},
402+
},
403+
}, &appsv1.DaemonSet{
404+
ObjectMeta: getResticObjectMeta(r),
405+
},
406+
},
407+
wantErr: true,
408+
want: nil,
409+
},
244410
{
245411
name: "test restic nodeselector customization via dpa",
246412
args: args{

0 commit comments

Comments
 (0)