Skip to content

Commit

Permalink
Observe logic only when the feature is enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Tyr <jiri.tyr@gmail.com>
  • Loading branch information
jtyr committed Mar 30, 2024
1 parent 1b50d4b commit 93567c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/release/observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func generateObservation(in *release.Release) v1beta1.ReleaseObservation {
}

// isUpToDate checks whether desired spec up to date with the observed state for a given release
func isUpToDate(ctx context.Context, kube client.Client, spec *v1beta1.ReleaseSpec, observed *release.Release, s v1beta1.ReleaseStatus) (bool, error) { // nolint:gocyclo
func isUpToDate(ctx context.Context, kube client.Client, managementPoliciesEnabled bool, spec *v1beta1.ReleaseSpec, observed *release.Release, s v1beta1.ReleaseStatus) (bool, error) { // nolint:gocyclo
if observed.Info == nil {
return false, errors.New(errReleaseInfoNilInObservedRelease)
}
Expand All @@ -88,7 +88,7 @@ func isUpToDate(ctx context.Context, kube client.Client, spec *v1beta1.ReleaseSp

mp := sets.New[xpv1.ManagementAction](spec.ManagementPolicies...)

if len(mp) != 0 && !mp.HasAny(xpv1.ManagementActionUpdate, xpv1.ManagementActionAll) {
if managementPoliciesEnabled && len(mp) != 0 && !mp.HasAny(xpv1.ManagementActionUpdate, xpv1.ManagementActionAll) {
// Treated as up-to-date as we don't update or create the resource
return true, nil
}
Expand Down
54 changes: 29 additions & 25 deletions pkg/controller/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,18 @@ func Setup(mgr ctrl.Manager, o controller.Options, timeout time.Duration) error

reconcilerOptions := []managed.ReconcilerOption{
managed.WithExternalConnecter(&connector{
client: mgr.GetClient(),
logger: o.Logger,
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &helmv1beta1.ProviderConfigUsage{}),
kcfgExtractorFn: resource.CommonCredentialExtractor,
gcpExtractorFn: resource.CommonCredentialExtractor,
gcpInjectorFn: gke.WrapRESTConfig,
azureExtractorFn: resource.CommonCredentialExtractor,
azureInjectorFn: azure.WrapRESTConfig,
newRestConfigFn: clients.NewRESTConfig,
newKubeClientFn: clients.NewKubeClient,
newHelmClientFn: helmClient.NewClient,
client: mgr.GetClient(),
logger: o.Logger,
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &helmv1beta1.ProviderConfigUsage{}),
managementPoliciesEnabled: o.Features.Enabled(feature.EnableBetaManagementPolicies),
kcfgExtractorFn: resource.CommonCredentialExtractor,
gcpExtractorFn: resource.CommonCredentialExtractor,
gcpInjectorFn: gke.WrapRESTConfig,
azureExtractorFn: resource.CommonCredentialExtractor,
azureInjectorFn: azure.WrapRESTConfig,
newRestConfigFn: clients.NewRESTConfig,
newKubeClientFn: clients.NewKubeClient,
newHelmClientFn: helmClient.NewClient,
}),
managed.WithPollInterval(o.PollInterval),
managed.WithLogger(o.Logger.WithValues("controller", name)),
Expand All @@ -130,9 +131,10 @@ func Setup(mgr ctrl.Manager, o controller.Options, timeout time.Duration) error
}

type connector struct {
logger logging.Logger
client client.Client
usage resource.Tracker
logger logging.Logger
client client.Client
usage resource.Tracker
managementPoliciesEnabled bool

kcfgExtractorFn func(ctx context.Context, src xpv1.CredentialsSource, c client.Client, ccs xpv1.CommonCredentialSelectors) ([]byte, error)
gcpExtractorFn func(ctx context.Context, src xpv1.CredentialsSource, c client.Client, ccs xpv1.CommonCredentialSelectors) ([]byte, error)
Expand Down Expand Up @@ -251,20 +253,22 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E
}

return &helmExternal{
logger: l,
localKube: c.client,
kube: k,
helm: h,
patch: newPatcher(),
logger: l,
localKube: c.client,
kube: k,
helm: h,
patch: newPatcher(),
managementPoliciesEnabled: c.managementPoliciesEnabled,
}, nil
}

type helmExternal struct {
logger logging.Logger
localKube client.Client
kube client.Client
helm helmClient.Client
patch Patcher
logger logging.Logger
localKube client.Client
kube client.Client
helm helmClient.Client
patch Patcher
managementPoliciesEnabled bool
}

func (e *helmExternal) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {
Expand Down Expand Up @@ -301,7 +305,7 @@ func (e *helmExternal) Observe(ctx context.Context, mg resource.Managed) (manage
return managed.ExternalObservation{ResourceExists: true}, nil
}

s, err := isUpToDate(ctx, e.localKube, &cr.Spec, rel, cr.Status)
s, err := isUpToDate(ctx, e.localKube, e.managementPoliciesEnabled, &cr.Spec, rel, cr.Status)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errFailedToCheckIfUpToDate)
}
Expand Down

0 comments on commit 93567c4

Please sign in to comment.