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

bugfix: make sure ns exsit when create velero secret #426

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions pkg/fleet-manager/fleet_plugin_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (f *FleetManager) reconcileBackupPlugin(ctx context.Context, fleet *v1alpha
// newSecret is a variable used to store the newly created secret object which contains the necessary credentials for the object storage provider. The specific structure and content of the secret vary depending on the provider.
newSecret, err := f.buildNewSecret(ctx, veleroCfg.Storage.SecretName, objStoreProvider, fleetNN)
if err != nil {
log.Error(err, "failed to builder new object store secret")
err = fmt.Errorf("error building new secret for objStoreProvider %s: %w", objStoreProvider, err)
return nil, ctrl.Result{}, err
}

Expand All @@ -87,11 +87,13 @@ func (f *FleetManager) reconcileBackupPlugin(ctx context.Context, fleet *v1alpha
SecretKey: cluster.SecretKey,
}, veleroCfg, newSecret.Name)
if err != nil {
err = fmt.Errorf("error rendering Velero for fleet cluster %s: %w", key.Name, err)
return nil, ctrl.Result{}, err
}

// create a new secret in the current fleet cluster before initializing the backup plugin.
if err := createNewSecretInFleetCluster(cluster, newSecret); err != nil {
err = fmt.Errorf("error creating new secret in fleet cluster %s: %w", key.Name, err)
return nil, ctrl.Result{}, err
}

Expand All @@ -117,7 +119,7 @@ func (f *FleetManager) reconcileBackupPlugin(ctx context.Context, fleet *v1alpha
// preventing orphaned resources and maintaining the cleanliness of the cluster.
for key, cluster := range fleetClusters {
if err := f.updateNewSecretOwnerReference(ctx, key.Name, cluster, newSecret); err != nil {
log.Error(err, "failed to update object store owner reference", "cluster", key.Name)
err = fmt.Errorf("error updating owner reference for secret in cluster %s: %w", key.Name, err)
return nil, ctrl.Result{}, err
}
}
Expand Down Expand Up @@ -210,9 +212,28 @@ func createNewSecretInFleetCluster(cluster *fleetCluster, newSecret *corev1.Secr
// Get the namespace of the secret
namespace := newSecret.Namespace

// Check if namespace exists
_, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: reuse karmadautil.EnsureNamespaceExist

if err != nil {
if apierrors.IsNotFound(err) {
// Namespace does not exist, create it
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}
_, err := kubeClient.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create namespace %s: %w", namespace, err)
}
} else {
return fmt.Errorf("failed to check for namespace %s: %w", namespace, err)
}
}

// Create the new secret
if _, err := kubeClient.CoreV1().Secrets(namespace).Create(context.TODO(), newSecret, metav1.CreateOptions{}); err != nil && !apierrors.IsAlreadyExists(err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate creating secret, though you have checked alkready exists

return err
return fmt.Errorf("failed to create secret in namespace %s: %w", namespace, err)
}

return nil
Expand Down