Skip to content

Commit 4e7484d

Browse files
committed
Fix all the goconst errors
1 parent 1207fab commit 4e7484d

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,
@@ -509,10 +543,10 @@ func createOrUpdateConsoleLink(client dynamic.Interface, argoName, argoNamespace
509543

510544
consoleLinkObj := &unstructured.Unstructured{
511545
Object: map[string]any{
512-
"apiVersion": ConsoleLinkGroup + "/" + ConsoleLinkVersion,
513-
"kind": "ConsoleLink",
514-
"metadata": map[string]any{
515-
"name": linkName,
546+
FieldAPIVersion: ConsoleLinkGroup + "/" + ConsoleLinkVersion,
547+
FieldKind: "ConsoleLink",
548+
FieldMetadata: map[string]any{
549+
FieldName: linkName,
516550
},
517551
"spec": map[string]any{
518552
"applicationMenu": map[string]any{
@@ -614,28 +648,28 @@ func newApplicationParameters(p *api.Pattern) []argoapi.HelmParameter {
614648
Value: strconv.FormatBool(p.Spec.GitConfig.TokenSecret != ""),
615649
},
616650
{
617-
Name: "global.multiSourceSupport",
651+
Name: ParamMultiSourceSupport,
618652
Value: strconv.FormatBool(*p.Spec.MultiSourceConfig.Enabled),
619653
},
620654
{
621-
Name: "global.multiSourceRepoUrl",
655+
Name: ParamMultiSourceRepoUrl,
622656
Value: p.Spec.MultiSourceConfig.HelmRepoUrl,
623657
},
624658

625659
{
626-
Name: "global.experimentalCapabilities",
660+
Name: ParamExperimentalCapabilities,
627661
Value: p.Spec.ExperimentalCapabilities,
628662
},
629663
}
630664
_, gitOpsSubNamespace := DetectGitOpsSubscription()
631665
parameters = append(parameters, argoapi.HelmParameter{
632-
Name: "global.gitOpsSubNamespace",
666+
Name: ParamGitOpsSubNamespace,
633667
Value: gitOpsSubNamespace,
634668
}, argoapi.HelmParameter{
635-
Name: "global.vpArgoNamespace",
669+
Name: ParamVpArgoNamespace,
636670
Value: getClusterWideArgoNamespace(),
637671
}, argoapi.HelmParameter{
638-
Name: "global.multiSourceTargetRevision",
672+
Name: ParamMultiSourceTargetRevision,
639673
Value: getClusterGroupChartVersion(p),
640674
})
641675
for _, extra := range p.Spec.ExtraParameters {
@@ -662,7 +696,7 @@ func newApplicationParameters(p *api.Pattern) []argoapi.HelmParameter {
662696
deletePatternValue = "DeleteChildApps"
663697
}
664698
parameters = append(parameters, argoapi.HelmParameter{
665-
Name: "global.deletePattern",
699+
Name: ParamDeletePattern,
666700
Value: string(deletePatternValue),
667701
ForceString: true,
668702
})
@@ -807,12 +841,12 @@ func commonSyncPolicy(p *api.Pattern) *argoapi.SyncPolicy {
807841
func commonApplicationSpec(p *api.Pattern, sources []argoapi.ApplicationSource) *argoapi.ApplicationSpec {
808842
spec := &argoapi.ApplicationSpec{
809843
Destination: argoapi.ApplicationDestination{
810-
Name: "in-cluster",
844+
Name: InClusterDestination,
811845
Namespace: p.Namespace,
812846
},
813847
// Project is a reference to the project this application belongs to.
814848
// The empty string means that application belongs to the 'default' project.
815-
Project: "default",
849+
Project: DefaultProject,
816850

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

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

13031337
app.Operation = &argoapi.Operation{
13041338
Sync: &argoapi.SyncOperation{
13051339
Prune: withPrune,
1306-
SyncOptions: []string{"Force=true"},
1340+
SyncOptions: []string{SyncOptionForce},
13071341
},
13081342
}
13091343

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)