Skip to content

Commit b0717c5

Browse files
committed
Fix all the goconst errors
1 parent 74f40be commit b0717c5

9 files changed

Lines changed: 177 additions & 103 deletions

File tree

internal/controller/acm.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,35 @@ import (
2828
"k8s.io/apimachinery/pkg/runtime/schema"
2929
)
3030

31+
const (
32+
apiVersionV1 = "v1"
33+
acmGroup = "cluster.open-cluster-management.io"
34+
acmManagedClusterRes = "managedclusters"
35+
acmLocalCluster = "local-cluster"
36+
)
37+
3138
func haveACMHub(r *PatternReconciler) bool {
32-
gvrMCH := schema.GroupVersionResource{Group: "operator.open-cluster-management.io", Version: "v1", Resource: "multiclusterhubs"}
39+
gvrMCH := schema.GroupVersionResource{Group: "operator.open-cluster-management.io", Version: apiVersionV1, Resource: "multiclusterhubs"}
3340

34-
mch, err := r.dynamicClient.Resource(gvrMCH).Namespace("open-cluster-management").Get(context.Background(), "multiclusterhub", metav1.GetOptions{})
41+
mch, err := r.dynamicClient.Resource(gvrMCH).Namespace(acmNamespace).Get(context.Background(), "multiclusterhub", metav1.GetOptions{})
3542
if err != nil {
3643
log.Printf("Error obtaining hub: %s\n", err)
3744
return false
3845
}
3946

4047
return strings.EqualFold(
4148
mch.GetAnnotations()["patterns.gitops.validatedpatterns.io/managed"],
42-
"true",
49+
boolTrue,
4350
)
4451
}
4552

4653
// listManagedClusters lists all ManagedCluster resources (excluding local-cluster)
4754
// Returns a list of cluster names and an error
4855
func (r *PatternReconciler) listManagedClusters(ctx context.Context) ([]string, error) {
4956
gvrMC := schema.GroupVersionResource{
50-
Group: "cluster.open-cluster-management.io",
51-
Version: "v1",
52-
Resource: "managedclusters",
57+
Group: acmGroup,
58+
Version: apiVersionV1,
59+
Resource: acmManagedClusterRes,
5360
}
5461

5562
// ManagedCluster is a cluster-scoped resource, so no namespace needed
@@ -62,7 +69,7 @@ func (r *PatternReconciler) listManagedClusters(ctx context.Context) ([]string,
6269
for _, item := range mcList.Items {
6370
name := item.GetName()
6471
// Exclude local-cluster (hub cluster)
65-
if name != "local-cluster" {
72+
if name != acmLocalCluster {
6673
clusterNames = append(clusterNames, name)
6774
}
6875
}
@@ -74,9 +81,9 @@ func (r *PatternReconciler) listManagedClusters(ctx context.Context) ([]string,
7481
// Returns the number of clusters deleted and an error
7582
func (r *PatternReconciler) deleteManagedClusters(ctx context.Context) (int, error) {
7683
gvrMC := schema.GroupVersionResource{
77-
Group: "cluster.open-cluster-management.io",
78-
Version: "v1",
79-
Resource: "managedclusters",
84+
Group: acmGroup,
85+
Version: apiVersionV1,
86+
Resource: acmManagedClusterRes,
8087
}
8188

8289
// ManagedCluster is a cluster-scoped resource, so no namespace needed
@@ -89,7 +96,7 @@ func (r *PatternReconciler) deleteManagedClusters(ctx context.Context) (int, err
8996
for _, item := range mcList.Items {
9097
name := item.GetName()
9198
// Exclude local-cluster (hub cluster)
92-
if name == "local-cluster" {
99+
if name == acmLocalCluster {
93100
continue
94101
}
95102

internal/controller/analytics.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"github.com/hybrid-cloud-patterns/patterns-operator/version"
1818
)
1919

20+
const analyticsPattern = "Pattern"
21+
2022
type VpAnalyticsInterface interface {
2123
SendPatternInstallationInfo(p *api.Pattern) bool
2224
SendPatternStartEventInfo(p *api.Pattern) bool
@@ -98,7 +100,7 @@ func getAnalyticsContext(p *api.Pattern) *analytics.Context {
98100

99101
ctx := &analytics.Context{
100102
Extra: map[string]any{
101-
"Pattern": p.Name,
103+
analyticsPattern: p.Name,
102104
"Domain": getSimpleDomain(p),
103105
"OperatorVersion": version.Version,
104106
"RepoBaseName": getBaseGitRepo(p),

internal/controller/argo.go

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,43 @@ import (
4444

4545
// Which ArgoCD objects we're creating
4646
const (
47-
ArgoCDGroup = "argoproj.io"
48-
ArgoCDVersion = "v1beta1"
49-
ArgoCDResource = "argocds"
47+
ArgoCDGroup = "argoproj.io"
48+
ArgoCDVersion = "v1beta1"
49+
ArgoCDResource = "argocds"
50+
ArgoCDKind = "ArgoCD"
51+
ArgoCDAPIVersion = ArgoCDGroup + "/" + ArgoCDVersion
52+
53+
ApplicationKind = "Application"
54+
55+
DefaultProject = "default"
56+
InClusterDestination = "in-cluster"
57+
PatternRef = "patternref"
58+
SyncOptionForce = "Force=true"
59+
)
60+
61+
// Unstructured map keys
62+
const (
63+
FieldAPIVersion = "apiVersion"
64+
FieldKind = "kind"
65+
FieldMetadata = "metadata"
66+
FieldName = "name"
67+
)
68+
69+
// Volume/ConfigMap names for CA bundle handling
70+
const (
71+
KubeRootCACM = "kube-root-ca.crt"
72+
CaBundlesVol = "ca-bundles"
73+
)
74+
75+
// Helm parameter names
76+
const (
77+
ParamMultiSourceSupport = "global.multiSourceSupport"
78+
ParamMultiSourceRepoUrl = "global.multiSourceRepoUrl"
79+
ParamExperimentalCapabilities = "global.experimentalCapabilities"
80+
ParamGitOpsSubNamespace = "global.gitOpsSubNamespace"
81+
ParamVpArgoNamespace = "global.vpArgoNamespace"
82+
ParamMultiSourceTargetRevision = "global.multiSourceTargetRevision"
83+
ParamDeletePattern = "global.deletePattern"
5084
)
5185

5286
// ConsoleLink constants
@@ -62,7 +96,7 @@ func newArgoCD(name, namespace string, patternsOperatorConfig PatternsOperatorCo
6296
"g, cluster-admins, role:admin",
6397
"g, admin, role:admin",
6498
}
65-
for argoAdmin := range strings.SplitSeq(patternsOperatorConfig.getStringValue("gitops.additionalArgoAdmins"), ",") {
99+
for argoAdmin := range strings.SplitSeq(patternsOperatorConfig.getStringValue(configKeyAdditionalAdmins), ",") {
66100
argoAdmin = strings.TrimSpace(argoAdmin)
67101
if argoAdmin != "" {
68102
argoPolicies = append(argoPolicies, "g, "+argoAdmin+", role:admin")
@@ -160,12 +194,12 @@ health_status.message = "An install plan for a subscription is pending installat
160194
return health_status`,
161195
},
162196
}
163-
if patternsOperatorConfig.getBoolValue("gitops.applicationHealthCheckEnabled") {
197+
if patternsOperatorConfig.getBoolValue(configKeyHealthCheck) {
164198
// As of ArgoCD 1.8 the Application health check was dropped (see https://github.com/argoproj/argo-cd/issues/3781),
165199
// but in app-of-apps pattern this is needed in order to implement children apps dependencies via sync-waves
166200
resourceHealthChecks = append(resourceHealthChecks, argooperator.ResourceHealthCheck{
167-
Group: "argoproj.io",
168-
Kind: "Application",
201+
Group: ArgoCDGroup,
202+
Kind: ApplicationKind,
169203
Check: `local health_status = {}
170204
health_status.status = "Progressing"
171205
health_status.message = ""
@@ -199,32 +233,32 @@ return health_status`,
199233
VolumeSource: v1.VolumeSource{
200234
ConfigMap: &v1.ConfigMapVolumeSource{
201235
LocalObjectReference: v1.LocalObjectReference{
202-
Name: "kube-root-ca.crt",
236+
Name: KubeRootCACM,
203237
},
204238
},
205239
},
206240
},
207241
{
208-
Name: "trusted-ca-bundle",
242+
Name: trustedBundleCM,
209243
VolumeSource: v1.VolumeSource{
210244
ConfigMap: &v1.ConfigMapVolumeSource{
211245
LocalObjectReference: v1.LocalObjectReference{
212-
Name: "trusted-ca-bundle",
246+
Name: trustedBundleCM,
213247
},
214248
Optional: &trueBool,
215249
},
216250
},
217251
},
218252
{
219-
Name: "ca-bundles",
253+
Name: CaBundlesVol,
220254
VolumeSource: v1.VolumeSource{
221255
EmptyDir: &v1.EmptyDirVolumeSource{},
222256
},
223257
},
224258
}
225259
initVolumeMounts := []v1.VolumeMount{
226260
{
227-
Name: "ca-bundles",
261+
Name: CaBundlesVol,
228262
MountPath: "/etc/pki/tls/certs",
229263
},
230264
}
@@ -239,11 +273,11 @@ return health_status`,
239273
MountPath: "/var/run/kube-root-ca", // ca.crt field
240274
},
241275
{
242-
Name: "trusted-ca-bundle",
276+
Name: trustedBundleCM,
243277
MountPath: "/var/run/trusted-ca", // ca-bundle.crt field
244278
},
245279
{
246-
Name: "ca-bundles",
280+
Name: CaBundlesVol,
247281
MountPath: "/tmp/ca-bundles",
248282
},
249283
},
@@ -257,8 +291,8 @@ return health_status`,
257291

258292
s := argooperator.ArgoCD{
259293
TypeMeta: metav1.TypeMeta{
260-
Kind: "ArgoCD",
261-
APIVersion: "argoproj.io/v1beta1",
294+
Kind: ArgoCDKind,
295+
APIVersion: ArgoCDAPIVersion,
262296
},
263297
ObjectMeta: metav1.ObjectMeta{
264298
Name: name,
@@ -512,10 +546,10 @@ func createOrUpdateConsoleLink(client dynamic.Interface, argoName, argoNamespace
512546

513547
consoleLinkObj := &unstructured.Unstructured{
514548
Object: map[string]any{
515-
"apiVersion": ConsoleLinkGroup + "/" + ConsoleLinkVersion,
516-
"kind": "ConsoleLink",
517-
"metadata": map[string]any{
518-
"name": linkName,
549+
FieldAPIVersion: ConsoleLinkGroup + "/" + ConsoleLinkVersion,
550+
FieldKind: "ConsoleLink",
551+
FieldMetadata: map[string]any{
552+
FieldName: linkName,
519553
},
520554
"spec": map[string]any{
521555
"applicationMenu": map[string]any{
@@ -617,28 +651,28 @@ func newApplicationParameters(p *api.Pattern) []argoapi.HelmParameter {
617651
Value: strconv.FormatBool(p.Spec.GitConfig.TokenSecret != ""),
618652
},
619653
{
620-
Name: "global.multiSourceSupport",
654+
Name: ParamMultiSourceSupport,
621655
Value: strconv.FormatBool(*p.Spec.MultiSourceConfig.Enabled),
622656
},
623657
{
624-
Name: "global.multiSourceRepoUrl",
658+
Name: ParamMultiSourceRepoUrl,
625659
Value: p.Spec.MultiSourceConfig.HelmRepoUrl,
626660
},
627661

628662
{
629-
Name: "global.experimentalCapabilities",
663+
Name: ParamExperimentalCapabilities,
630664
Value: p.Spec.ExperimentalCapabilities,
631665
},
632666
}
633667
_, gitOpsSubNamespace := DetectGitOpsSubscription()
634668
parameters = append(parameters, argoapi.HelmParameter{
635-
Name: "global.gitOpsSubNamespace",
669+
Name: ParamGitOpsSubNamespace,
636670
Value: gitOpsSubNamespace,
637671
}, argoapi.HelmParameter{
638-
Name: "global.vpArgoNamespace",
672+
Name: ParamVpArgoNamespace,
639673
Value: getClusterWideArgoNamespace(),
640674
}, argoapi.HelmParameter{
641-
Name: "global.multiSourceTargetRevision",
675+
Name: ParamMultiSourceTargetRevision,
642676
Value: getClusterGroupChartVersion(p),
643677
})
644678
for _, extra := range p.Spec.ExtraParameters {
@@ -665,7 +699,7 @@ func newApplicationParameters(p *api.Pattern) []argoapi.HelmParameter {
665699
deletePatternValue = "DeleteChildApps"
666700
}
667701
parameters = append(parameters, argoapi.HelmParameter{
668-
Name: "global.deletePattern",
702+
Name: ParamDeletePattern,
669703
Value: string(deletePatternValue),
670704
ForceString: true,
671705
})
@@ -810,12 +844,12 @@ func commonSyncPolicy(p *api.Pattern) *argoapi.SyncPolicy {
810844
func commonApplicationSpec(p *api.Pattern, sources []argoapi.ApplicationSource) *argoapi.ApplicationSpec {
811845
spec := &argoapi.ApplicationSpec{
812846
Destination: argoapi.ApplicationDestination{
813-
Name: "in-cluster",
847+
Name: InClusterDestination,
814848
Namespace: p.Namespace,
815849
},
816850
// Project is a reference to the project this application belongs to.
817851
// The empty string means that application belongs to the 'default' project.
818-
Project: "default",
852+
Project: DefaultProject,
819853

820854
// IgnoreDifferences is a list of resources and their fields which should be ignored during comparison
821855
// IgnoreDifferences []ResourceIgnoreDifferences `json:"ignoreDifferences,omitempty" protobuf:"bytes,5,name=ignoreDifferences"`
@@ -912,7 +946,7 @@ func newMultiSourceApplication(p *api.Pattern) *argoapi.Application {
912946
valuesSource := &argoapi.ApplicationSource{
913947
RepoURL: p.Spec.GitConfig.TargetRepo,
914948
TargetRevision: p.Spec.GitConfig.TargetRevision,
915-
Ref: "patternref",
949+
Ref: PatternRef,
916950
}
917951
sources = append(sources, *valuesSource)
918952

@@ -987,10 +1021,10 @@ func newArgoGiteaApplication(p *api.Pattern, patternsOperatorConfig PatternsOper
9871021
}
9881022
spec := &argoapi.ApplicationSpec{
9891023
Destination: argoapi.ApplicationDestination{
990-
Name: "in-cluster",
1024+
Name: InClusterDestination,
9911025
Namespace: GiteaNamespace,
9921026
},
993-
Project: "default",
1027+
Project: DefaultProject,
9941028
Source: &argoapi.ApplicationSource{
9951029
RepoURL: patternsOperatorConfig.getStringValue("gitea.helmRepoUrl"),
9961030
TargetRevision: patternsOperatorConfig.getStringValue("gitea.chartVersion"),
@@ -1299,14 +1333,14 @@ func updateHelmParameter(goal api.PatternParameter, actual []argoapi.HelmParamet
12991333
// syncApplication syncs the application with prune and force options if such a sync is not already in progress.
13001334
// Returns nil if a sync is already in progress, error otherwise
13011335
func syncApplication(client argoclient.Interface, app *argoapi.Application, withPrune bool) error {
1302-
if app.Operation != nil && app.Operation.Sync != nil && app.Operation.Sync.Prune == withPrune && slices.Contains(app.Operation.Sync.SyncOptions, "Force=true") {
1336+
if app.Operation != nil && app.Operation.Sync != nil && app.Operation.Sync.Prune == withPrune && slices.Contains(app.Operation.Sync.SyncOptions, SyncOptionForce) {
13031337
return nil
13041338
}
13051339

13061340
app.Operation = &argoapi.Operation{
13071341
Sync: &argoapi.SyncOperation{
13081342
Prune: withPrune,
1309-
SyncOptions: []string{"Force=true"},
1343+
SyncOptions: []string{SyncOptionForce},
13101344
},
13111345
}
13121346

internal/controller/checkout.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151
GitAuthGitHubApp GitAuthenticationBackend = 3
5252
)
5353

54+
const gitRemoteOrigin = "origin"
5455
const ContextTimeout = 15 * time.Second
5556
const GitCustomCAFile = "/tmp/vp-git-cas.pem"
5657
const GitHEAD = "HEAD"
@@ -162,7 +163,7 @@ func getCommitFromTarget(repo *git.Repository, name string) (plumbing.Hash, erro
162163
if h, err := getHashFromReference(repo, plumbing.NewRemoteHEADReferenceName(name)); err == nil {
163164
return h, nil
164165
}
165-
if h, err := getHashFromReference(repo, plumbing.NewRemoteReferenceName("origin", name)); err == nil {
166+
if h, err := getHashFromReference(repo, plumbing.NewRemoteReferenceName(gitRemoteOrigin, name)); err == nil {
166167
return h, nil
167168
}
168169

@@ -267,7 +268,7 @@ func cloneRepo(fullClient kubernetes.Interface, gitOps GitOperations, url, direc
267268

268269
func getFetchOptions(fullClient kubernetes.Interface, url string, secret map[string][]byte) (*git.FetchOptions, error) {
269270
var foptions = &git.FetchOptions{
270-
RemoteName: "origin",
271+
RemoteName: gitRemoteOrigin,
271272
Force: true,
272273
InsecureSkipTLS: true,
273274
Tags: git.AllTags,
@@ -297,7 +298,7 @@ func getCloneOptions(fullClient kubernetes.Interface, url string, secret map[str
297298
// Clone the given repository to the given directory
298299
var options = &git.CloneOptions{
299300
URL: url,
300-
RemoteName: "origin",
301+
RemoteName: gitRemoteOrigin,
301302
Progress: os.Stdout,
302303
Depth: 0,
303304
SingleBranch: false,
@@ -330,8 +331,8 @@ func getHttpAuth(secret map[string][]byte) *http.BasicAuth {
330331
// because access tokens can easily be revoked.
331332
// https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
332333
auth := &http.BasicAuth{
333-
Username: string(getField(secret, "username")),
334-
Password: string(getField(secret, "password")),
334+
Username: string(getField(secret, secretFieldUsername)),
335+
Password: string(getField(secret, secretFieldPassword)),
335336
}
336337

337338
return auth
@@ -444,8 +445,8 @@ func detectGitAuthType(secret map[string][]byte) GitAuthenticationBackend {
444445
}
445446

446447
// Username + Password
447-
_, hasUser := secret["username"]
448-
_, hasPassword := secret["password"]
448+
_, hasUser := secret[secretFieldUsername]
449+
_, hasPassword := secret[secretFieldPassword]
449450
if hasUser && hasPassword {
450451
return GitAuthPassword
451452
}

0 commit comments

Comments
 (0)