-
Notifications
You must be signed in to change notification settings - Fork 203
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
Conversation
internal/..
to pkg/common
package.
v2/pkg/common/constants.go
Outdated
const ReconcilePolicyAnnotation = "serviceoperator.azure.com/reconcile-policy" | ||
|
||
const ( | ||
// ReconcilePolicyManage instructs the operator to manage the resource in question. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 Report
@@ 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
|
v2/cmd/controller/app/setup.go
Outdated
@@ -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" |
There was a problem hiding this comment.
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} |
There was a problem hiding this comment.
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.
rg.Annotations = map[string]string{common.PerResourceSecretAnnotation: perResourceCredentialName.Name} | |
rg.Annotations = map[string]string{ | |
common.PerResourceSecretAnnotation: perResourceCredentialName.Name, | |
} |
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) | ||
) | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @theunrepentantgeek
v2/pkg/common/constants.go
Outdated
// #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" |
There was a problem hiding this comment.
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.
v2/pkg/common/constants.go
Outdated
const ReconcilePolicyAnnotation = "serviceoperator.azure.com/reconcile-policy" | ||
|
||
const ( | ||
// ReconcilePolicyManage instructs the operator to manage the resource in question. |
There was a problem hiding this comment.
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).
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) | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @theunrepentantgeek
v2/pkg/common/constants.go
Outdated
|
||
const ( | ||
// #nosec | ||
ClientSecretVar = "AZURE_CLIENT_SECRET" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
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" |
There was a problem hiding this comment.
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.
const ReconcilePolicyAnnotation = "serviceoperator.azure.com/reconcile-policy" | |
const ReconcilePolicy = "serviceoperator.azure.com/reconcile-policy" |
There was a problem hiding this comment.
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 ( |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
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") |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
Closes #3149
What this PR does / why we need it:
Refactoring constants from
internal/..
topkg/common
package.