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

[release-2.9] 🤖 Sync from open-cluster-management-io/governance-policy-propagator: #149 #505

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
9 changes: 8 additions & 1 deletion controllers/propagator/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ func (r *Propagator) getEncryptionKey(ctx context.Context, clusterName string) (
}

err = r.Create(ctx, encryptionSecret)
if err != nil {
if k8serrors.IsAlreadyExists(err) {
// Some kind of race condition occurred (e.g. cache not updated in time), so just refetch the encryption
// secret.
err := r.Get(ctx, objectKey, encryptionSecret)
if err != nil {
return nil, fmt.Errorf("failed to get the Secret %s/%s: %w", clusterName, EncryptionKeySecret, err)
}
} else if err != nil {
return nil, fmt.Errorf("failed to create the Secret %s/%s: %w", clusterName, EncryptionKeySecret, err)
}
} else if err != nil {
Expand Down
17 changes: 15 additions & 2 deletions controllers/propagator/propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package propagator

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -34,6 +35,8 @@ const (
TriggerUpdateAnnotation = "policy.open-cluster-management.io/trigger-update"
)

var ErrRetryable = errors.New("")

type Propagator struct {
client.Client
Scheme *runtime.Scheme
Expand Down Expand Up @@ -297,7 +300,7 @@ func (r *ReplicatedPolicyReconciler) processTemplates(
if err != nil {
log.Error(err, "Failed to get/generate the policy encryption key")

return err
return fmt.Errorf("%w%w", ErrRetryable, err)
}

// Get/generate the initialization vector
Expand Down Expand Up @@ -366,6 +369,16 @@ func (r *ReplicatedPolicyReconciler) processTemplates(
}
}

// If the failure was due to a Kubernetes API error that could be recoverable, let's retry it.
// Missing objects are handled by the templating library sending reconcile requests when they get created.
if errors.Is(tplErr, templates.ErrMissingAPIResource) ||
k8serrors.IsInternalError(tplErr) ||
k8serrors.IsServiceUnavailable(tplErr) ||
k8serrors.IsTimeout(tplErr) ||
k8serrors.IsTooManyRequests(tplErr) {
tplErr = fmt.Errorf("%w%w", ErrRetryable, tplErr)
}

return tplErr
}

Expand Down Expand Up @@ -405,7 +418,7 @@ func (r *ReplicatedPolicyReconciler) processTemplates(
if templateResult.CacheCleanUp != nil {
err := templateResult.CacheCleanUp()
if err != nil {
return err
return fmt.Errorf("%w%w", ErrRetryable, err)
}
}

Expand Down
13 changes: 8 additions & 5 deletions controllers/propagator/replicatedpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package propagator

import (
"context"
"errors"
"fmt"
"strings"
"sync"
Expand Down Expand Up @@ -236,11 +237,13 @@ func (r *ReplicatedPolicyReconciler) Reconcile(ctx context.Context, request ctrl
}
}

// resolve hubTemplate before replicating
// #nosec G104 -- any errors are logged and recorded in the processTemplates method,
// but the ignored status will be handled appropriately by the policy controllers on
// the managed cluster(s).
_ = r.processTemplates(ctx, desiredReplicatedPolicy, decision.Cluster, rootPolicy)
// Any errors to expose to the user are logged and recorded in the processTemplates method. Only retry
// the request if it's determined to be a retryable error (i.e. don't retry syntax errors).
err := r.processTemplates(ctx, desiredReplicatedPolicy, decision.Cluster, rootPolicy)
if errors.Is(err, ErrRetryable) {
// Return the error if it's retryable, which will utilize controller-runtime's exponential backoff.
return reconcile.Result{}, err
}
} else {
watcherErr := r.TemplateResolver.UncacheWatcher(instanceObjID)
if watcherErr != nil {
Expand Down