Skip to content

Commit

Permalink
bugfix: make sure ns exsit when create velero secret
Browse files Browse the repository at this point in the history
Signed-off-by: Xieql <xieqianglong@huawei.com>
  • Loading branch information
Xieql authored and kurator-bot committed Oct 30, 2023
1 parent 06be3a9 commit 459a2dc
Showing 1 changed file with 24 additions and 3 deletions.
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{})
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) {
return err
return fmt.Errorf("failed to create secret in namespace %s: %w", namespace, err)
}

return nil
Expand Down

0 comments on commit 459a2dc

Please sign in to comment.