Skip to content
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
16 changes: 12 additions & 4 deletions internal/controller/finalizer_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controller

import (
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -53,15 +55,21 @@ func hasSAFinalizer(sa *corev1.ServiceAccount) bool {

// nolint:unparam // conditionType is currently always Ready, but may vary in the future
func updateCondition(conditions []metav1.Condition, conditionType, status, reason, message string) []metav1.Condition {
now := metav1.NewTime(time.Now())

condition := metav1.Condition{
Type: conditionType,
Status: metav1.ConditionStatus(status),
Reason: reason,
Message: message,
Type: conditionType,
Status: metav1.ConditionStatus(status),
Reason: reason,
Message: message,
LastTransitionTime: now,
}

for i := range conditions {
if conditions[i].Type == conditionType {
if conditions[i].Status == condition.Status {
condition.LastTransitionTime = conditions[i].LastTransitionTime
}
conditions[i] = condition
return conditions
}
Expand Down
57 changes: 37 additions & 20 deletions internal/controller/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
agentoctopuscomv1beta1 "github.com/octopusdeploy/octopus-permissions-controller/api/v1beta1"
"github.com/octopusdeploy/octopus-permissions-controller/internal/metrics"
"github.com/octopusdeploy/octopus-permissions-controller/internal/rules"
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -138,17 +139,25 @@ func updateStatusOnFailure[T WSAResource, S WSAStatus](
) {
log := logf.FromContext(ctx)

switch s := any(status).(type) {
case *agentoctopuscomv1beta1.WorkloadServiceAccountStatus:
s.Conditions = updateCondition(s.Conditions, ConditionTypeReady,
"False", ReasonReconcileFailed, err.Error())
case *agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus:
s.Conditions = updateCondition(s.Conditions, ConditionTypeReady,
"False", ReasonReconcileFailed, err.Error())
}
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
if getErr := c.Get(ctx, client.ObjectKeyFromObject(resource), resource); getErr != nil {
return getErr
}

switch s := any(status).(type) {
case *agentoctopuscomv1beta1.WorkloadServiceAccountStatus:
s.Conditions = updateCondition(s.Conditions, ConditionTypeReady,
"False", ReasonReconcileFailed, err.Error())
case *agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be simplified to:

switch any(status).(type) {
case *agentoctopuscomv1beta1.WorkloadServiceAccountStatus,
     *agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus:
    status.Conditions = updateCondition(status.Conditions, ConditionTypeReady,
        "False", ReasonReconcileFailed, err.Error())
}

?

Maybe this is code style preference though for readability

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a blocker though

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried to do this, Go couldn't be certain on the type that I was asserting, so Conditions didn't exist 😞

s.Conditions = updateCondition(s.Conditions, ConditionTypeReady,
"False", ReasonReconcileFailed, err.Error())
}

return c.Status().Update(ctx, resource)
})

if statusErr := c.Status().Update(ctx, resource); statusErr != nil {
log.Error(statusErr, "failed to update status after reconciliation failure")
if retryErr != nil {
log.Error(retryErr, "failed to update status after reconciliation failure")
}
}

Expand All @@ -161,17 +170,25 @@ func updateStatusOnSuccess[T WSAResource, S WSAStatus](
) {
log := logf.FromContext(ctx)

switch s := any(status).(type) {
case *agentoctopuscomv1beta1.WorkloadServiceAccountStatus:
s.Conditions = updateCondition(s.Conditions, ConditionTypeReady,
"True", ReasonReconcileSuccess, successMessage)
case *agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus:
s.Conditions = updateCondition(s.Conditions, ConditionTypeReady,
"True", ReasonReconcileSuccess, successMessage)
}
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
if getErr := c.Get(ctx, client.ObjectKeyFromObject(resource), resource); getErr != nil {
return getErr
}

switch s := any(status).(type) {
case *agentoctopuscomv1beta1.WorkloadServiceAccountStatus:
s.Conditions = updateCondition(s.Conditions, ConditionTypeReady,
"True", ReasonReconcileSuccess, successMessage)
case *agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus:
s.Conditions = updateCondition(s.Conditions, ConditionTypeReady,
"True", ReasonReconcileSuccess, successMessage)
}

return c.Status().Update(ctx, resource)
})

if err := c.Status().Update(ctx, resource); err != nil {
log.Error(err, "failed to update status after successful reconciliation")
if retryErr != nil {
log.Error(retryErr, "failed to update status after successful reconciliation")
}
}

Expand Down