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

Fix invalid condition status for errors in GardenerCluster #70

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions api/v1/gardenercluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func (cluster *GardenerCluster) UpdateConditionForReadyState(conditionType Condi
meta.SetStatusCondition(&cluster.Status.Conditions, condition)
}

func (cluster *GardenerCluster) UpdateConditionForErrorState(conditionType ConditionType, reason ConditionReason, conditionStatus metav1.ConditionStatus, error error) {
func (cluster *GardenerCluster) UpdateConditionForErrorState(conditionType ConditionType, reason ConditionReason, error error) {
cluster.Status.State = ErrorState

condition := metav1.Condition{
Type: string(conditionType),
Status: conditionStatus,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: string(reason),
Message: fmt.Sprintf("%s Error: %s", getMessage(reason), error.Error()),
Expand Down
44 changes: 14 additions & 30 deletions internal/controller/gardener_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req
err := controller.Get(ctx, req.NamespacedName, &cluster)
if err != nil {
if k8serrors.IsNotFound(err) {
err = controller.deleteKubeconfigSecret(req.Name)
err = controller.deleteKubeconfigSecret(ctx, req.Name)
}

if err == nil {
Expand Down Expand Up @@ -117,10 +117,8 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req
}

if kubeconfigStatus == ksCreated || kubeconfigStatus == ksModified {
err = controller.persistStatusChange(ctx, &cluster)
if err != nil {
return controller.resultWithoutRequeue(), err
}
_ = controller.persistStatusChange(ctx, &cluster)
return controller.resultWithoutRequeue(), nil
}

return controller.resultWithRequeue(), nil
Expand All @@ -146,34 +144,20 @@ func (controller *GardenerClusterController) resultWithoutRequeue() ctrl.Result
}

func (controller *GardenerClusterController) persistStatusChange(ctx context.Context, cluster *imv1.GardenerCluster) error {
key := types.NamespacedName{
Name: cluster.Name,
Namespace: cluster.Namespace,
}
var clusterToUpdate imv1.GardenerCluster

err := controller.Get(ctx, key, &clusterToUpdate)
err := controller.Client.Status().Update(ctx, cluster)
if err != nil {
return err
controller.log.Error(err, "status update failed")
}

clusterToUpdate.Status = cluster.Status

statusErr := controller.Client.Status().Update(ctx, &clusterToUpdate)
if statusErr != nil {
controller.log.Error(statusErr, "Failed to set state for GardenerCluster")
}

return statusErr
return err
}

func (controller *GardenerClusterController) deleteKubeconfigSecret(clusterCRName string) error {
func (controller *GardenerClusterController) deleteKubeconfigSecret(ctx context.Context, clusterCRName string) error {
selector := client.MatchingLabels(map[string]string{
clusterCRNameLabel: clusterCRName,
})

var secretList corev1.SecretList
err := controller.Client.List(context.TODO(), &secretList, selector)
err := controller.Client.List(ctx, &secretList, selector)
if err != nil {
return err
}
Expand All @@ -182,7 +166,7 @@ func (controller *GardenerClusterController) deleteKubeconfigSecret(clusterCRNam
return errors.Errorf("unexpected numer of secrets found for cluster CR `%s`", clusterCRName)
}

return controller.Client.Delete(context.TODO(), &secretList.Items[0])
return controller.Client.Delete(ctx, &secretList.Items[0])
}

func (controller *GardenerClusterController) getSecret(shootName string) (*corev1.Secret, error) {
Expand Down Expand Up @@ -222,13 +206,13 @@ const (
func (controller *GardenerClusterController) handleKubeconfig(ctx context.Context, cluster *imv1.GardenerCluster, lastSyncTime time.Time) (kubeconfigStatus, error) {
existingSecret, err := controller.getSecret(cluster.Spec.Shoot.Name)
if err != nil && !k8serrors.IsNotFound(err) {
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToGetSecret, metav1.ConditionTrue, err)
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToGetSecret, err)
return ksZero, err
}

kubeconfig, err := controller.KubeconfigProvider.Fetch(ctx, cluster.Spec.Shoot.Name)
if err != nil {
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToGetKubeconfig, metav1.ConditionTrue, err)
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToGetKubeconfig, err)
return ksZero, err
}

Expand All @@ -238,7 +222,7 @@ func (controller *GardenerClusterController) handleKubeconfig(ctx context.Contex

// delete secret containing kubeconfig to be rotated
if err := controller.removeKubeconfig(ctx, cluster, existingSecret); err != nil {
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToDeleteSecret, metav1.ConditionTrue, err)
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToDeleteSecret, err)
return ksZero, err
}

Expand Down Expand Up @@ -302,7 +286,7 @@ func (controller *GardenerClusterController) createNewSecret(ctx context.Context
newSecret := controller.newSecret(*cluster, kubeconfig, lastSyncTime)
err := controller.Create(ctx, &newSecret)
if err != nil {
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToCreateSecret, metav1.ConditionTrue, err)
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToCreateSecret, err)
return err
}

Expand Down Expand Up @@ -343,7 +327,7 @@ func (controller *GardenerClusterController) updateExistingSecret(ctx context.Co

err := controller.Update(ctx, existingSecret)
if err != nil {
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToUpdateSecret, metav1.ConditionTrue, err)
cluster.UpdateConditionForErrorState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonFailedToUpdateSecret, err)

return err
}
Expand Down