Skip to content

Commit

Permalink
renamed vars for readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
guettli committed May 23, 2023
1 parent 98cb232 commit 76b91a8
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions controllers/hivelocitycluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,18 @@ func reconcileTargetSecret(ctx context.Context, clusterScope *scope.ClusterScope
return fmt.Errorf("failed to get rest config: %w", err)
}

clientSet, err := kubernetes.NewForConfig(restConfig)
targetClientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("failed to get client set: %w", err)
}

if _, err := clientSet.CoreV1().Secrets(metav1.NamespaceSystem).Get(
secretName := clusterScope.HivelocityCluster.Spec.HivelocitySecret.Name
targetNS := metav1.NamespaceSystem
sourceNS := clusterScope.HivelocityCluster.Namespace

if _, err := targetClientSet.CoreV1().Secrets(targetNS).Get(
ctx,
clusterScope.HivelocityCluster.Spec.HivelocitySecret.Name,
secretName,
metav1.GetOptions{},
); err == nil {
// Secret exists. Nothing to do.
Expand All @@ -376,28 +380,30 @@ func reconcileTargetSecret(ctx context.Context, clusterScope *scope.ClusterScope
}

apiKeySecretName := types.NamespacedName{
Namespace: clusterScope.HivelocityCluster.Namespace,
Name: clusterScope.HivelocityCluster.Spec.HivelocitySecret.Name,
Namespace: sourceNS,
Name: secretName,
}
secretManager := secretutil.NewSecretManager(clusterScope.Logger, clusterScope.Client, clusterScope.APIReader)
apiKeySecret, err := secretManager.AcquireSecret(ctx, apiKeySecretName, clusterScope.HivelocityCluster, false, clusterScope.HivelocityCluster.DeletionTimestamp.IsZero())
if err != nil {
return fmt.Errorf("failed to acquire secret: %w", err)
}

apiKey, keyExists := apiKeySecret.Data[clusterScope.HivelocityCluster.Spec.HivelocitySecret.Key]
key := clusterScope.HivelocityCluster.Spec.HivelocitySecret.Key

apiKey, keyExists := apiKeySecret.Data[key]
if !keyExists {
return fmt.Errorf(
"error key %s does not exist in secret/%s: %w",
clusterScope.HivelocityCluster.Spec.HivelocitySecret.Key,
key,
apiKeySecretName,
err,
)
}

var immutable bool
data := make(map[string][]byte)
data[clusterScope.HivelocityCluster.Spec.HivelocitySecret.Key] = apiKey
data[key] = apiKey

// Save api server information
data["apiserver-host"] = []byte(clusterScope.HivelocityCluster.Spec.ControlPlaneEndpoint.Host)
Expand All @@ -408,13 +414,13 @@ func reconcileTargetSecret(ctx context.Context, clusterScope *scope.ClusterScope
Data: data,
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: clusterScope.HivelocityCluster.Spec.HivelocitySecret.Name,
Namespace: metav1.NamespaceSystem,
Name: secretName,
Namespace: targetNS,
},
}

// create secret in cluster
if _, err := clientSet.CoreV1().Secrets(metav1.NamespaceSystem).Create(ctx, &newSecret, metav1.CreateOptions{}); err != nil {
if _, err := targetClientSet.CoreV1().Secrets(targetNS).Create(ctx, &newSecret, metav1.CreateOptions{}); err != nil {
return fmt.Errorf("failed to create secret: %w", err)
}

Expand Down

0 comments on commit 76b91a8

Please sign in to comment.