Skip to content

Commit

Permalink
[squash me!] Remove context from the PropagationCfg struct
Browse files Browse the repository at this point in the history
  • Loading branch information
a13x5 committed Oct 29, 2024
1 parent dd74267 commit d375778
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
23 changes: 9 additions & 14 deletions internal/controller/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,19 +651,20 @@ func (r *ManagedClusterReconciler) reconcileCredentialPropagation(ctx context.Co
return fmt.Errorf("failed to get kubeconfig secret for cluster %s/%s: %w", managedCluster.Namespace, managedCluster.Name, err)
}

propnCfg := &credspropagation.PropagationCfg{
Client: r.Client,
ManagedCluster: managedCluster,
KubeconfSecret: kubeconfSecret,
SystemNamespace: r.SystemNamespace,
}

for _, provider := range providers {
switch provider {
case "aws":
l.Info("Skipping creds propagation for AWS")
case "azure":
l.Info("Azure creds propagation start")
if err := credspropagation.PropagateAzureSecrets(credspropagation.PropagationCfg{
Ctx: ctx,
Client: r.Client,
ManagedCluster: managedCluster,
KubeconfSecret: kubeconfSecret,
SystemNamespace: r.SystemNamespace,
}); err != nil {
if err := credspropagation.PropagateAzureSecrets(ctx, propnCfg); err != nil {
errMsg := fmt.Sprintf("failed to create Azure CCM credentials: %s", err)
apimeta.SetStatusCondition(managedCluster.GetConditions(), metav1.Condition{
Type: hmc.CredentialsPropagatedCondition,
Expand All @@ -683,13 +684,7 @@ func (r *ManagedClusterReconciler) reconcileCredentialPropagation(ctx context.Co
})
case "vsphere":
l.Info("vSphere creds propagation start")
if err := credspropagation.PropagateVSphereSecrets(credspropagation.PropagationCfg{
Ctx: ctx,
Client: r.Client,
ManagedCluster: managedCluster,
KubeconfSecret: kubeconfSecret,
SystemNamespace: r.SystemNamespace,
}); err != nil {
if err := credspropagation.PropagateVSphereSecrets(ctx, propnCfg); err != nil {
errMsg := fmt.Sprintf("failed to create vSphere CCM credentials: %s", err)
apimeta.SetStatusCondition(managedCluster.GetConditions(), metav1.Condition{
Type: hmc.CredentialsPropagatedCondition,
Expand Down
11 changes: 6 additions & 5 deletions internal/credspropagation/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package credspropagation

import (
"context"
"encoding/json"
"fmt"

Expand All @@ -24,25 +25,25 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func PropagateAzureSecrets(cfg PropagationCfg) error {
func PropagateAzureSecrets(ctx context.Context, cfg *PropagationCfg) error {
azureCluster := &capz.AzureCluster{}
if err := cfg.Client.Get(cfg.Ctx, client.ObjectKey{
if err := cfg.Client.Get(ctx, client.ObjectKey{
Name: cfg.ManagedCluster.Name,
Namespace: cfg.ManagedCluster.Namespace,
}, azureCluster); err != nil {
return fmt.Errorf("failed to get AzureCluster %s: %w", cfg.ManagedCluster.Name, err)
}

azureClIdty := &capz.AzureClusterIdentity{}
if err := cfg.Client.Get(cfg.Ctx, client.ObjectKey{
if err := cfg.Client.Get(ctx, client.ObjectKey{
Name: azureCluster.Spec.IdentityRef.Name,
Namespace: azureCluster.Spec.IdentityRef.Namespace,
}, azureClIdty); err != nil {
return fmt.Errorf("failed to get AzureClusterIdentity %s: %w", azureCluster.Spec.IdentityRef.Name, err)
}

azureSecret := &corev1.Secret{}
if err := cfg.Client.Get(cfg.Ctx, client.ObjectKey{
if err := cfg.Client.Get(ctx, client.ObjectKey{
Name: azureClIdty.Spec.ClientSecret.Name,
Namespace: azureClIdty.Spec.ClientSecret.Namespace,
}, azureSecret); err != nil {
Expand All @@ -54,7 +55,7 @@ func PropagateAzureSecrets(cfg PropagationCfg) error {
return fmt.Errorf("failed to generate Azure CCM secret: %s", err)
}

if err := applyCCMConfigs(cfg.Ctx, cfg.KubeconfSecret, ccmSecret); err != nil {
if err := applyCCMConfigs(ctx, cfg.KubeconfSecret, ccmSecret); err != nil {
return fmt.Errorf("failed to apply Azure CCM secret: %s", err)
}

Expand Down
1 change: 0 additions & 1 deletion internal/credspropagation/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
)

type PropagationCfg struct {
Ctx context.Context
Client client.Client
ManagedCluster *hmc.ManagedCluster
KubeconfSecret *corev1.Secret
Expand Down
13 changes: 7 additions & 6 deletions internal/credspropagation/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package credspropagation

import (
"bytes"
"context"
"fmt"
texttemplate "text/template"

Expand All @@ -29,24 +30,24 @@ import (
hmc "github.com/Mirantis/hmc/api/v1alpha1"
)

func PropagateVSphereSecrets(cfg PropagationCfg) error {
func PropagateVSphereSecrets(ctx context.Context, cfg *PropagationCfg) error {
vsphereCluster := &capv.VSphereCluster{}
if err := cfg.Client.Get(cfg.Ctx, client.ObjectKey{
if err := cfg.Client.Get(ctx, client.ObjectKey{
Name: cfg.ManagedCluster.Name,
Namespace: cfg.ManagedCluster.Namespace,
}, vsphereCluster); err != nil {
return fmt.Errorf("failed to get VSphereCluster %s: %w", cfg.ManagedCluster.Name, err)
}

vsphereClIdty := &capv.VSphereClusterIdentity{}
if err := cfg.Client.Get(cfg.Ctx, client.ObjectKey{
if err := cfg.Client.Get(ctx, client.ObjectKey{
Name: vsphereCluster.Spec.IdentityRef.Name,
}, vsphereClIdty); err != nil {
return fmt.Errorf("failed to get VSphereClusterIdentity %s: %w", vsphereCluster.Spec.IdentityRef.Name, err)
}

vsphereSecret := &corev1.Secret{}
if err := cfg.Client.Get(cfg.Ctx, client.ObjectKey{
if err := cfg.Client.Get(ctx, client.ObjectKey{
Name: vsphereClIdty.Spec.SecretName,
Namespace: cfg.SystemNamespace,
}, vsphereSecret); err != nil {
Expand All @@ -55,7 +56,7 @@ func PropagateVSphereSecrets(cfg PropagationCfg) error {

vsphereMachines := &capv.VSphereMachineList{}
if err := cfg.Client.List(
cfg.Ctx,
ctx,
vsphereMachines,
&client.ListOptions{
Namespace: cfg.ManagedCluster.Namespace,
Expand All @@ -76,7 +77,7 @@ func PropagateVSphereSecrets(cfg PropagationCfg) error {
return fmt.Errorf("failed to generate VSphere CSI secret: %s", err)
}

if err := applyCCMConfigs(cfg.Ctx, cfg.KubeconfSecret, ccmSecret, ccmConfig, csiSecret); err != nil {
if err := applyCCMConfigs(ctx, cfg.KubeconfSecret, ccmSecret, ccmConfig, csiSecret); err != nil {
return fmt.Errorf("failed to apply VSphere CCM/CSI secrets: %s", err)
}

Expand Down

0 comments on commit d375778

Please sign in to comment.