Skip to content

Commit

Permalink
[Squash me!] Addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
a13x5 committed Oct 18, 2024
1 parent a83a24f commit 51889f3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
2 changes: 0 additions & 2 deletions api/v1alpha1/managedcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ type ManagedClusterStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
// ObservedGeneration is the last observed generation.
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
// CCMSecretReconciled signify if CCM secret was reconciled on managed cluster
CCMSecretReconciled bool `json:"ccmSecretReconciled,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
35 changes: 14 additions & 21 deletions internal/controller/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ func (r *ManagedClusterReconciler) Update(ctx context.Context, managedCluster *h
return ctrl.Result{RequeueAfter: DefaultRequeueInterval}, nil
}

if !managedCluster.Status.CCMSecretReconciled {
return r.reconcileCredentialPropagation(ctx, l, managedCluster)
result, err := r.reconcileCredentialPropagation(ctx, managedCluster)
if err != nil {
return result, err
}

return r.updateServices(ctx, managedCluster)
Expand Down Expand Up @@ -705,16 +706,12 @@ func (r *ManagedClusterReconciler) machinesAvailable(ctx context.Context, namesp
return len(itemsList.Items) != 0, nil
}

func (r *ManagedClusterReconciler) reconcileCredentialPropagation(ctx context.Context, l logr.Logger, managedCluster *hmc.ManagedCluster) (ctrl.Result, error) {
func (r *ManagedClusterReconciler) reconcileCredentialPropagation(ctx context.Context, managedCluster *hmc.ManagedCluster) (ctrl.Result, error) {
l := ctrl.LoggerFrom(ctx)
l.Info("Reconciling CCM credentials propagation")
var err error
defer func() {
err = errors.Join(err, r.updateStatus(ctx, managedCluster))
}()

providers, err := r.getInfraProviders(ctx, managedCluster.Namespace, managedCluster.Spec.Template)
if err != nil {
managedCluster.Status.CCMSecretReconciled = false
return ctrl.Result{},
fmt.Errorf("failed to get cluster providers for cluster %s/%s: %s", managedCluster.Namespace, managedCluster.Name, err)
}
Expand All @@ -724,21 +721,19 @@ func (r *ManagedClusterReconciler) reconcileCredentialPropagation(ctx context.Co
Name: fmt.Sprintf("%s-kubeconfig", managedCluster.Name),
Namespace: managedCluster.Namespace,
}, kubeconfSecret); err != nil {
managedCluster.Status.CCMSecretReconciled = false
return ctrl.Result{},
fmt.Errorf("failed to get kubeconfig secret for cluster %s/%s: %s", managedCluster.Namespace, managedCluster.Name, err)
}

for _, provider := range providers {
switch provider {
switch provider.Name {
case "aws":
l.Info("Skipping creds propagation for AWS")
continue
case "azure":
l.Info("Azure creds propagation start")
err := r.propagateAzureSecrets(ctx, managedCluster, kubeconfSecret)
if err != nil {
managedCluster.Status.CCMSecretReconciled = false
errMsg := fmt.Sprintf("failed to create Azure CCM credentials: %s", err)
apimeta.SetStatusCondition(managedCluster.GetConditions(), metav1.Condition{
Type: hmc.CredentialsPropagatedCondition,
Expand All @@ -759,7 +754,6 @@ func (r *ManagedClusterReconciler) reconcileCredentialPropagation(ctx context.Co
l.Info("vSphere creds propagation start")
err := r.propagateVSphereSecrets(ctx, managedCluster, kubeconfSecret)
if err != nil {
managedCluster.Status.CCMSecretReconciled = false
errMsg := fmt.Sprintf("failed to create vSphere CCM credentials: %s", err)
apimeta.SetStatusCondition(managedCluster.GetConditions(), metav1.Condition{
Type: hmc.CredentialsPropagatedCondition,
Expand All @@ -777,18 +771,16 @@ func (r *ManagedClusterReconciler) reconcileCredentialPropagation(ctx context.Co
})
continue
default:
managedCluster.Status.CCMSecretReconciled = true
errMsg := fmt.Sprintf("unsupported infrastructure provider %s", provider)
apimeta.SetStatusCondition(managedCluster.GetConditions(), metav1.Condition{
Type: hmc.CredentialsPropagatedCondition,
Status: metav1.ConditionFalse,
Reason: hmc.FailedReason,
Message: errMsg,
})
return ctrl.Result{}, errors.New(errMsg)
continue
}
}
managedCluster.Status.CCMSecretReconciled = true
l.Info("CCM credentials reconcile finished")
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -860,7 +852,7 @@ func generateAzureCCMSecret(azureCluster *capz.AzureCluster, azureClIdty *capz.A
"cloud-config": azureJSON,
}

return makeSecret("azure-cloud-provider", "kube-system", secretData), nil
return makeSecret("azure-cloud-provider", metav1.NamespaceSystem, secretData), nil
}

func (r *ManagedClusterReconciler) propagateVSphereSecrets(ctx context.Context, managedCluster *hmc.ManagedCluster, kubeconfSecret *corev1.Secret) error {
Expand Down Expand Up @@ -918,7 +910,6 @@ func (r *ManagedClusterReconciler) propagateVSphereSecrets(ctx context.Context,
}

func generateVSphereCCMConfigs(vCl *capv.VSphereCluster, vScrt *corev1.Secret, vMa *capv.VSphereMachine) (*corev1.Secret, *corev1.ConfigMap, error) {
ns := "kube-system"
secretName := "vsphere-cloud-secret"
secretData := map[string][]byte{
fmt.Sprintf("%s.username", vCl.Spec.Server): vScrt.Data["username"],
Expand All @@ -929,7 +920,7 @@ func generateVSphereCCMConfigs(vCl *capv.VSphereCluster, vScrt *corev1.Secret, v
"port": 443,
"insecureFlag": true,
"secretName": secretName,
"secretNamespace": ns,
"secretNamespace": metav1.NamespaceSystem,
},
"vcenter": map[string]any{
vCl.Spec.Server: map[string]any{
Expand All @@ -944,15 +935,17 @@ func generateVSphereCCMConfigs(vCl *capv.VSphereCluster, vScrt *corev1.Secret, v
"zone": "k8s-zone",
},
}

ccmCfgYaml, err := yaml.Marshal(ccmCfg)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal CCM config: %s", err)
}

cmData := map[string]string{
"vsphere.conf": string(ccmCfgYaml),
}
return makeSecret(secretName, ns, secretData),
makeConfigMap("cloud-config", ns, cmData),
return makeSecret(secretName, metav1.NamespaceSystem, secretData),
makeConfigMap("cloud-config", metav1.NamespaceSystem, cmData),
nil
}

Expand Down Expand Up @@ -993,7 +986,7 @@ datacenters = "{{ .Datacenter }}"
"csi-vsphere.conf": buf.Bytes(),
}

return makeSecret("vcenter-config-secret", "kube-system", secretData), nil
return makeSecret("vcenter-config-secret", metav1.NamespaceSystem, secretData), nil
}

func applyCCMConfigs(ctx context.Context, kubeconfSecret *corev1.Secret, objects ...client.Object) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ spec:
status:
description: ManagedClusterStatus defines the observed state of ManagedCluster
properties:
ccmSecretReconciled:
description: CCMSecretReconciled signify if CCM secret was reconciled
on managed cluster
type: boolean
conditions:
description: Conditions contains details for the current state of
the ManagedCluster
Expand Down

0 comments on commit 51889f3

Please sign in to comment.