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

Refactor constants from internal/.. to pkg/common package. #3171

Merged
merged 12 commits into from
Aug 7, 2023

Conversation

super-harsh
Copy link
Collaborator

Closes #3149

What this PR does / why we need it:

Refactoring constants from internal/.. to pkg/common package.

@super-harsh super-harsh changed the title Refactor constants Refactor constants from internal/.. to pkg/common package. Jul 31, 2023
const ReconcilePolicyAnnotation = "serviceoperator.azure.com/reconcile-policy"

const (
// ReconcilePolicyManage instructs the operator to manage the resource in question.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should we keep these comments here, in reconcile_policy.go or both places?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Have to do it this way to avoid cyclic imports. Also, would be good to expose these values to external users with comments to have an idea of what the value does.

Copy link
Member

Choose a reason for hiding this comment

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

I think we move the ReconcilePolicy type here too, retaining type safety for clients who want it (and the others can cast it if they need to).

@codecov-commenter
Copy link

codecov-commenter commented Jul 31, 2023

Codecov Report

Merging #3171 (f5354e6) into main (11f4803) will decrease coverage by 0.01%.
Report is 2 commits behind head on main.
The diff coverage is 66.00%.

@@            Coverage Diff             @@
##             main    #3171      +/-   ##
==========================================
- Coverage   54.38%   54.37%   -0.01%     
==========================================
  Files        1419     1420       +1     
  Lines      604182   604172      -10     
==========================================
- Hits       328557   328537      -20     
- Misses     221948   221954       +6     
- Partials    53677    53681       +4     
Files Changed Coverage Δ
v2/internal/testcommon/creds.go 0.00% <0.00%> (ø)
v2/internal/identity/credential_provider.go 54.72% <38.46%> (ø)
v2/internal/config/vars.go 68.42% <78.57%> (ø)
v2/internal/reconcilers/reconcile_policy.go 89.47% <80.00%> (-1.84%) ⬇️
...internal/reconcilers/generic/generic_reconciler.go 83.89% <100.00%> (ø)
v2/internal/reconcilers/predicates.go 100.00% <100.00%> (ø)
v2/pkg/common/reconciler/reconcile_policy.go 100.00% <100.00%> (ø)

... and 36 files with indirect coverage changes

@@ -37,6 +37,7 @@ import (
. "github.com/Azure/azure-service-operator/v2/internal/logging"
asometrics "github.com/Azure/azure-service-operator/v2/internal/metrics"
armreconciler "github.com/Azure/azure-service-operator/v2/internal/reconcilers/arm"
"github.com/Azure/azure-service-operator/v2/pkg/common"
Copy link
Member

Choose a reason for hiding this comment

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

I'll beat @matthchr to this (because he always catches me out) - move this into the import group for local packages, just below.

@@ -141,7 +141,7 @@ func Test_ARMClientCache_ReturnsPerResourceScopedClientOverNamespacedClient(t *t
g.Expect(err).ToNot(HaveOccurred())

rg := newResourceGroup("test-namespace")
rg.Annotations = map[string]string{reconcilers.PerResourceSecretAnnotation: perResourceCredentialName.Name}
rg.Annotations = map[string]string{common.PerResourceSecretAnnotation: perResourceCredentialName.Name}
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer if we spread these literal maps across multiple lines, as we already typically do elsewhere.

Suggested change
rg.Annotations = map[string]string{common.PerResourceSecretAnnotation: perResourceCredentialName.Name}
rg.Annotations = map[string]string{
common.PerResourceSecretAnnotation: perResourceCredentialName.Name,
}

Comment on lines 17 to 33
type ReconcilePolicy string

const (
// ReconcilePolicyManage instructs the operator to manage the resource in question.
// This includes issuing PUTs to update it and DELETE's to delete it from Azure if deleted in Kuberentes.
// This includes issuing PUTs to update it and DELETE's to delete it from Azure if deleted in Kubernetes.
// This is the default policy when no policy is specified.
ReconcilePolicyManage = ReconcilePolicy("manage")
ReconcilePolicyManage = ReconcilePolicy(common.ReconcilePolicyManage)

// ReconcilePolicySkip instructs the operator to skip all reconciliation actions. This includes creating
// the resource.
ReconcilePolicySkip = ReconcilePolicy("skip")
ReconcilePolicySkip = ReconcilePolicy(common.ReconcilePolicySkip)

// ReconcilePolicyDetachOnDelete instructs the operator to skip deletion of resources in Azure. This allows
// deletion of the resource in Kubernetes to go through but does not delete the underlying Azure resource.
ReconcilePolicyDetachOnDelete = ReconcilePolicy("detach-on-delete")
ReconcilePolicyDetachOnDelete = ReconcilePolicy(common.ReconcilePolicyDetachOnDelete)
)

Copy link
Member

Choose a reason for hiding this comment

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

I suggest moving all of this into common - and get rid of the string typed constants. Consumers can then use the strongly typed ReconcilePolicyManage and cast it to a string if they must.

Copy link
Member

Choose a reason for hiding this comment

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

This also gets rid of the need to document them in two places.

Copy link
Member

Choose a reason for hiding this comment

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

Agree with @theunrepentantgeek

Comment on lines 9 to 23
// #nosec
ClientSecretVar = "AZURE_CLIENT_SECRET"
SubscriptionIDVar = "AZURE_SUBSCRIPTION_ID"
TenantIDVar = "AZURE_TENANT_ID"
ClientIDVar = "AZURE_CLIENT_ID"
ClientCertificateVar = "AZURE_CLIENT_CERTIFICATE"
// #nosec
ClientCertificatePasswordVar = "AZURE_CLIENT_CERTIFICATE_PASSWORD"
TargetNamespacesVar = "AZURE_TARGET_NAMESPACES"
OperatorModeVar = "AZURE_OPERATOR_MODE"
SyncPeriodVar = "AZURE_SYNC_PERIOD"
ResourceManagerEndpointVar = "AZURE_RESOURCE_MANAGER_ENDPOINT"
ResourceManagerAudienceVar = "AZURE_RESOURCE_MANAGER_AUDIENCE"
AzureAuthorityHostVar = "AZURE_AUTHORITY_HOST"
PodNamespaceVar = "POD_NAMESPACE"
Copy link
Member

Choose a reason for hiding this comment

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

We should remove the Var suffix from all of these, it's kinda implied.

Might pay to put a line or two of documentation on each one, maybe even a link to our docs.

const ReconcilePolicyAnnotation = "serviceoperator.azure.com/reconcile-policy"

const (
// ReconcilePolicyManage instructs the operator to manage the resource in question.
Copy link
Member

Choose a reason for hiding this comment

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

I think we move the ReconcilePolicy type here too, retaining type safety for clients who want it (and the others can cast it if they need to).

Comment on lines 17 to 33
type ReconcilePolicy string

const (
// ReconcilePolicyManage instructs the operator to manage the resource in question.
// This includes issuing PUTs to update it and DELETE's to delete it from Azure if deleted in Kuberentes.
// This includes issuing PUTs to update it and DELETE's to delete it from Azure if deleted in Kubernetes.
// This is the default policy when no policy is specified.
ReconcilePolicyManage = ReconcilePolicy("manage")
ReconcilePolicyManage = ReconcilePolicy(common.ReconcilePolicyManage)

// ReconcilePolicySkip instructs the operator to skip all reconciliation actions. This includes creating
// the resource.
ReconcilePolicySkip = ReconcilePolicy("skip")
ReconcilePolicySkip = ReconcilePolicy(common.ReconcilePolicySkip)

// ReconcilePolicyDetachOnDelete instructs the operator to skip deletion of resources in Azure. This allows
// deletion of the resource in Kubernetes to go through but does not delete the underlying Azure resource.
ReconcilePolicyDetachOnDelete = ReconcilePolicy("detach-on-delete")
ReconcilePolicyDetachOnDelete = ReconcilePolicy(common.ReconcilePolicyDetachOnDelete)
)

Copy link
Member

Choose a reason for hiding this comment

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

Agree with @theunrepentantgeek


const (
// #nosec
ClientSecretVar = "AZURE_CLIENT_SECRET"
Copy link
Member

Choose a reason for hiding this comment

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

I think we should make some effort to differentiate what these constants are for. Previous, when they were in their own separate packages, it was "obvious" which ones were for the Configuration secret and which were used elsewhere.

Now, it's not so obvious. It will be even less obvious if we remove Var suffix from them (not that I disagree with the plan to remove that suffix).

I wonder if we should either put the config specific constants into a common subpackage, like common/config, so that it clearly explains what goes into the configuration secret, or else we could give them a Config suffix (I like that less). We could also at least put them into their own file, although that won't really help clarify for users who are using them as they don't see our file structure just our package structure.

All in all, I think I tend towards common/config

Copy link
Member

Choose a reason for hiding this comment

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

We could also have a common/annotations although I'm less certain that's a good idea.

Copy link
Member

Choose a reason for hiding this comment

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

I'm still not used to the way Go projects sometimes deliberately have very small packages purely for naming reasons - but I think both common/config and common/annotations have merit.

@@ -95,7 +98,7 @@ func NewCredentialProvider(
// If no matching credential can be found, an error is returned.
func (c *credentialProvider) GetCredential(ctx context.Context, obj genruntime.MetaObject) (*Credential, error) {
// Resource annotation
cred, err := c.getCredentialFromAnnotation(ctx, obj, common.PerResourceSecretAnnotation)
cred, err := c.getCredentialFromAnnotation(ctx, obj, annotations.PerResourceSecretAnnotation)
Copy link
Member

Choose a reason for hiding this comment

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

Given the namespace package it's now in, can remove the suffix

Suggested change
cred, err := c.getCredentialFromAnnotation(ctx, obj, annotations.PerResourceSecretAnnotation)
cred, err := c.getCredentialFromAnnotation(ctx, obj, annotations.PerResourceSecret)

// A reconcile policy describes what action (if any) the operator is allowed to take when
// reconciling the resource.
// If no reconcile policy is specified, the default is "run"
const ReconcilePolicyAnnotation = "serviceoperator.azure.com/reconcile-policy"
Copy link
Member

Choose a reason for hiding this comment

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

Suffix mirrors the package name and can be removed.

Suggested change
const ReconcilePolicyAnnotation = "serviceoperator.azure.com/reconcile-policy"
const ReconcilePolicy = "serviceoperator.azure.com/reconcile-policy"

Copy link
Member

Choose a reason for hiding this comment

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

Hmm actually, one interesting thing here is key vs value. This is the reconcile policy annotation key, but there's also a reconcile policy annotation value (which is currently in reconciler but IMO should be here.

We should adapt a common naming pattern for both of these.

annotations.ReconcilePolicy (for the annotation) and annotations.ReconcilePolicyValue (the enum type), and annotations.ReconcilePolicySkip or annotations.ReconcilePolicyManage for the actual values?


package config

const (
Copy link
Member

Choose a reason for hiding this comment

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

These read really nicely now.

// ReconcilePolicyManage instructs the operator to manage the resource in question.
// This includes issuing PUTs to update it and DELETE's to delete it from Azure if deleted in Kubernetes.
// This is the default policy when no policy is specified.
ReconcilePolicyManage = ReconcilePolicy("manage")
Copy link
Member

Choose a reason for hiding this comment

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

The common prefix here mirrors the name of the package, so can probably be removed for the type and all values.

Suggested change
ReconcilePolicyManage = ReconcilePolicy("manage")
PolicyManage = Policy("manage")

// ReconcilePolicyManage instructs the operator to manage the resource in question.
// This includes issuing PUTs to update it and DELETE's to delete it from Azure if deleted in Kubernetes.
// This is the default policy when no policy is specified.
ReconcilePolicyManage = ReconcilePolicy("manage")
Copy link
Member

Choose a reason for hiding this comment

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

This is an annotation value, so shouldn't it just go in annotations?

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package annotations
Copy link
Member

Choose a reason for hiding this comment

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

minor: Consider putting each annotation + value pair into its own file. So this could be called reconcile_policy.go

@super-harsh super-harsh enabled auto-merge (squash) August 7, 2023 22:12
@super-harsh super-harsh merged commit 6bd3b0d into main Aug 7, 2023
7 of 8 checks passed
@super-harsh super-harsh deleted the refactor/constants branch August 7, 2023 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Expose user-facing constants
4 participants