Skip to content

Commit ded04e9

Browse files
committed
Address code review comments
- use caching client for secrets - use TTLSecondsAfterFinished to remove old Jobs - update logger Signed-off-by: Ales Raszka <[email protected]>
1 parent 6af5fb8 commit ded04e9

File tree

11 files changed

+52
-22
lines changed

11 files changed

+52
-22
lines changed

apis/controller/v1alpha1/devworkspaceoperatorconfig_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ type RegistryConfig struct {
8383
// If secret is not found in the workspace namespace, the operator will look for the secret
8484
// in the namespace where the operator is running in.
8585
// as the DevWorkspaceOperatorCongfig.
86+
// The secret must contain "controller.devfile.io/watch-secret=true" label so that it can be
87+
// recognized by the operator.
8688
// +kubebuilder:validation:Optional
8789
AuthSecret string `json:"authSecret,omitempty"`
8890
}

controllers/backupcronjob/backupcronjob_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (r *BackupCronJobReconciler) executeBackupSync(ctx context.Context, dwOpera
256256
}
257257
dwOperatorConfig.Status.LastBackupTime = &metav1.Time{Time: metav1.Now().Time}
258258

259-
err = r.NonCachingClient.Status().Patch(ctx, dwOperatorConfig, origConfig)
259+
err = r.Status().Patch(ctx, dwOperatorConfig, origConfig)
260260
if err != nil {
261261
log.Error(err, "Failed to update DevWorkspaceOperatorConfig status with last backup time")
262262
return err
@@ -340,6 +340,7 @@ func (r *BackupCronJobReconciler) createBackupJob(
340340
},
341341
},
342342
Spec: batchv1.JobSpec{
343+
TTLSecondsAfterFinished: ptr.To[int32](120),
343344
Template: corev1.PodTemplateSpec{
344345
ObjectMeta: metav1.ObjectMeta{
345346
Annotations: map[string]string{
@@ -473,7 +474,7 @@ func (r *BackupCronJobReconciler) handleRegistryAuthSecret(ctx context.Context,
473474

474475
// First check the workspace namespace for the secret
475476
registryAuthSecret := &corev1.Secret{}
476-
err := r.NonCachingClient.Get(ctx, client.ObjectKey{
477+
err := r.Get(ctx, client.ObjectKey{
477478
Name: secretName,
478479
Namespace: workspace.Namespace}, registryAuthSecret)
479480
if err == nil {
@@ -487,7 +488,7 @@ func (r *BackupCronJobReconciler) handleRegistryAuthSecret(ctx context.Context,
487488
log.Info("Registry auth secret not found in workspace namespace, checking operator namespace", "secretName", secretName)
488489

489490
// If the secret is not found in the workspace namespace, check the operator namespace as fallback
490-
err = r.NonCachingClient.Get(ctx, client.ObjectKey{
491+
err = r.Get(ctx, client.ObjectKey{
491492
Name: secretName,
492493
Namespace: dwOperatorConfig.Namespace}, registryAuthSecret)
493494
if err != nil {
@@ -501,7 +502,7 @@ func (r *BackupCronJobReconciler) handleRegistryAuthSecret(ctx context.Context,
501502
// copySecret copies the given secret from the operator namespace to the workspace namespace.
502503
func (r *BackupCronJobReconciler) copySecret(ctx context.Context, workspace *dw.DevWorkspace, sourceSecret *corev1.Secret, log logr.Logger) (namespaceSecret *corev1.Secret, err error) {
503504
existingNamespaceSecret := &corev1.Secret{}
504-
err = r.NonCachingClient.Get(ctx, client.ObjectKey{
505+
err = r.Get(ctx, client.ObjectKey{
505506
Name: constants.DevWorkspaceBackupAuthSecretName,
506507
Namespace: workspace.Namespace}, existingNamespaceSecret)
507508
if client.IgnoreNotFound(err) != nil {

controllers/backupcronjob/rbac.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2323
"github.com/devfile/devworkspace-operator/pkg/constants"
2424
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
25+
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
2526
corev1 "k8s.io/api/core/v1"
2627
rbacv1 "k8s.io/api/rbac/v1"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -38,8 +39,7 @@ func (r *BackupCronJobReconciler) ensureJobRunnerRBAC(ctx context.Context, works
3839
saName := JobRunnerSAName + "-" + workspace.Status.DevWorkspaceId
3940
sa := &corev1.ServiceAccount{
4041
ObjectMeta: metav1.ObjectMeta{Name: saName, Namespace: workspace.Namespace, Labels: map[string]string{
41-
constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId,
42-
constants.DevWorkspaceWatchSecretLabel: "true",
42+
constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId,
4343
}},
4444
}
4545

@@ -48,17 +48,27 @@ func (r *BackupCronJobReconciler) ensureJobRunnerRBAC(ctx context.Context, works
4848
return err
4949
}
5050

51-
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, sa, func() error { return nil }); err != nil {
52-
return fmt.Errorf("ensuring ServiceAccount: %w", err)
51+
clusterAPI := sync.ClusterAPI{
52+
Client: r.Client,
53+
Scheme: r.Scheme,
54+
Logger: r.Log,
55+
Ctx: ctx,
56+
}
57+
58+
_, err := sync.SyncObjectWithCluster(sa, clusterAPI)
59+
if err != nil {
60+
if _, ok := err.(*sync.NotInSyncError); !ok {
61+
return fmt.Errorf("synchronizing ServiceAccount: %w", err)
62+
}
5363
}
5464

5565
if infrastructure.IsOpenShift() {
5666
// Create ClusterRoleBinding for image push role
57-
if err := r.ensureImagePushRoleBinding(ctx, saName, workspace); err != nil {
67+
if err := r.ensureImagePushRoleBinding(ctx, saName, workspace, clusterAPI); err != nil {
5868
return fmt.Errorf("ensuring image push ClusterRoleBinding: %w", err)
5969
}
6070
// Create ImageStream for backup images
61-
if err := r.ensureImageStreamForBackup(ctx, workspace); err != nil {
71+
if err := r.ensureImageStreamForBackup(ctx, workspace, clusterAPI); err != nil {
6272
return fmt.Errorf("ensuring ImageStream for backup: %w", err)
6373
}
6474
}
@@ -69,11 +79,12 @@ func (r *BackupCronJobReconciler) ensureJobRunnerRBAC(ctx context.Context, works
6979

7080
// ensureImagePushRoleBinding creates a ClusterRoleBinding to allow the given ServiceAccount to push images
7181
// to the OpenShift internal registry.
72-
func (r *BackupCronJobReconciler) ensureImagePushRoleBinding(ctx context.Context, saName string, workspace *dw.DevWorkspace) error {
82+
func (r *BackupCronJobReconciler) ensureImagePushRoleBinding(ctx context.Context, saName string, workspace *dw.DevWorkspace, clusterAPI sync.ClusterAPI) error {
7383
// Create ClusterRoleBinding for system:image-builder role
74-
clusterRoleBinding := &rbacv1.ClusterRoleBinding{
84+
clusterRoleBinding := &rbacv1.RoleBinding{
7585
ObjectMeta: metav1.ObjectMeta{
76-
Name: "devworkspace-image-builder-" + workspace.Status.DevWorkspaceId,
86+
Name: "devworkspace-image-builder-" + workspace.Status.DevWorkspaceId,
87+
Namespace: workspace.Namespace,
7788
Labels: map[string]string{
7889
constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId,
7990
},
@@ -92,15 +103,19 @@ func (r *BackupCronJobReconciler) ensureImagePushRoleBinding(ctx context.Context
92103
},
93104
}
94105

95-
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, clusterRoleBinding, func() error { return nil }); err != nil {
96-
return fmt.Errorf("ensuring ClusterRoleBinding: %w", err)
106+
_, err := sync.SyncObjectWithCluster(clusterRoleBinding, clusterAPI)
107+
if err != nil {
108+
if _, ok := err.(*sync.NotInSyncError); !ok {
109+
return fmt.Errorf("ensuring ClusterRoleBinding: %w", err)
110+
}
97111
}
112+
98113
return nil
99114
}
100115

101116
// ensureImageStreamForBackup creates an ImageStream for the backup images in OpenShift in case user
102117
// selects to use the internal registry. Push to non-existing ImageStream fails, so we need to create it first.
103-
func (r *BackupCronJobReconciler) ensureImageStreamForBackup(ctx context.Context, workspace *dw.DevWorkspace) error {
118+
func (r *BackupCronJobReconciler) ensureImageStreamForBackup(ctx context.Context, workspace *dw.DevWorkspace, clusterAPI sync.ClusterAPI) error {
104119
// Create ImageStream for backup images using unstructured to avoid scheme conflicts
105120
imageStream := &unstructured.Unstructured{
106121
Object: map[string]interface{}{
@@ -120,18 +135,19 @@ func (r *BackupCronJobReconciler) ensureImageStreamForBackup(ctx context.Context
120135
},
121136
},
122137
}
138+
if err := controllerutil.SetControllerReference(workspace, imageStream, r.Scheme); err != nil {
139+
return err
140+
}
141+
123142
imageStream.SetGroupVersionKind(schema.GroupVersionKind{
124143
Group: "image.openshift.io",
125144
Version: "v1",
126145
Kind: "ImageStream",
127146
})
128147

129-
if err := controllerutil.SetControllerReference(workspace, imageStream, r.Scheme); err != nil {
130-
return err
131-
}
132-
133148
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, imageStream, func() error { return nil }); err != nil {
134149
return fmt.Errorf("ensuring ImageStream: %w", err)
135150
}
151+
136152
return nil
137153
}

deploy/bundle/manifests/controller.devfile.io_devworkspaceoperatorconfigs.yaml

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

deploy/deployment/kubernetes/combined.yaml

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

deploy/deployment/kubernetes/objects/devworkspaceoperatorconfigs.controller.devfile.io.CustomResourceDefinition.yaml

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

deploy/deployment/openshift/combined.yaml

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

deploy/deployment/openshift/objects/devworkspaceoperatorconfigs.controller.devfile.io.CustomResourceDefinition.yaml

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

deploy/templates/crd/bases/controller.devfile.io_devworkspaceoperatorconfigs.yaml

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

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func main() {
192192
if err = (&backupCronJobController.BackupCronJobReconciler{
193193
Client: mgr.GetClient(),
194194
NonCachingClient: nonCachingClient,
195-
Log: ctrl.Log.WithName("controllers").WithName("BackupCronJob"),
195+
Log: ctrl.Log.WithName("controllers").WithName("BackupCronJob").V(1),
196196
Scheme: mgr.GetScheme(),
197197
}).SetupWithManager(mgr); err != nil {
198198
setupLog.Error(err, "unable to create controller", "controller", "BackupCronJob")

0 commit comments

Comments
 (0)