CopySecret copies the given secret from the operator namespace to the workspace namespace. It NEVER overwrites an existing secret: if a secret already exists in the workspace namespace, it returns the existing secret without modification.
(ctx context.Context, c client.Client, workspace *dw.DevWorkspace, sourceSecret *corev1.Secret, scheme *runtime.Scheme, log logr.Logger)
| 102 | // It NEVER overwrites an existing secret: if a secret already exists in the workspace namespace, |
| 103 | // it returns the existing secret without modification. |
| 104 | func CopySecret(ctx context.Context, c client.Client, workspace *dw.DevWorkspace, sourceSecret *corev1.Secret, scheme *runtime.Scheme, log logr.Logger) (namespaceSecret *corev1.Secret, err error) { |
| 105 | desiredSecret := &corev1.Secret{ |
| 106 | ObjectMeta: metav1.ObjectMeta{ |
| 107 | Name: constants.DevWorkspaceBackupAuthSecretName, |
| 108 | Namespace: workspace.Namespace, |
| 109 | Labels: map[string]string{ |
| 110 | constants.DevWorkspaceWatchSecretLabel: "true", |
| 111 | }, |
| 112 | }, |
| 113 | Data: sourceSecret.Data, |
| 114 | Type: sourceSecret.Type, |
| 115 | } |
| 116 | |
| 117 | err = c.Create(ctx, desiredSecret) |
| 118 | if err != nil { |
| 119 | if k8sErrors.IsAlreadyExists(err) { |
| 120 | // Race condition - secret was created between Get and Create |
| 121 | // Fetch and return it (respect what's there) |
| 122 | if err := c.Get(ctx, client.ObjectKey{ |
| 123 | Name: constants.DevWorkspaceBackupAuthSecretName, |
| 124 | Namespace: workspace.Namespace, |
| 125 | }, sourceSecret); err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | log.Info("Registry auth secret was created concurrently, using existing secret", |
| 129 | "secretName", constants.DevWorkspaceBackupAuthSecretName) |
| 130 | return sourceSecret, nil |
| 131 | } |
| 132 | return nil, err |
| 133 | } |
| 134 | |
| 135 | log.Info("Successfully copied registry auth secret to workspace namespace", |
| 136 | "name", desiredSecret.Name, "namespace", workspace.Namespace) |
| 137 | return desiredSecret, nil |
| 138 | } |
no test coverage detected