Skip to content

Commit 1bde0bf

Browse files
authored
refactor: move label helper functions to utils package (#626)
**What problem does this PR solve?**: Moving the utility functions that creates labels to `common/utils` package. I like to use these function in another mutator. **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> Existing unit tests **Special notes for your reviewer:**
1 parent e0ed2c0 commit 1bde0bf

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

common/pkg/capi/utils/labels.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package utils
5+
6+
import (
7+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
8+
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
9+
)
10+
11+
type LabelFn func(labels map[string]string)
12+
13+
func NewLabels(fs ...LabelFn) map[string]string {
14+
labels := map[string]string{}
15+
for _, f := range fs {
16+
f(labels)
17+
}
18+
return labels
19+
}
20+
21+
func WithClusterName(clusterName string) LabelFn {
22+
return func(labels map[string]string) {
23+
labels[clusterv1.ClusterNameLabel] = clusterName
24+
}
25+
}
26+
27+
func WithMove() LabelFn {
28+
return func(labels map[string]string) {
29+
labels[clusterctlv1.ClusterctlMoveLabel] = ""
30+
}
31+
}

pkg/handlers/generic/mutation/imageregistries/credentials/credentials_secret.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313

1414
corev1 "k8s.io/api/core/v1"
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1716
cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
18-
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
17+
18+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils"
1919
)
2020

2121
const (
@@ -76,7 +76,7 @@ func generateCredentialsSecret(
7676
ObjectMeta: metav1.ObjectMeta{
7777
Name: credentialSecretName(clusterName),
7878
Namespace: namespace,
79-
Labels: newLabels(withMove(), withClusterName(clusterName)),
79+
Labels: utils.NewLabels(utils.WithMove(), utils.WithClusterName(clusterName)),
8080
},
8181
StringData: secretData,
8282
Type: corev1.SecretTypeOpaque,
@@ -116,25 +116,3 @@ func kubeletStaticCredentialProviderSecretContents(configs []providerConfig) (st
116116
func credentialSecretName(clusterName string) string {
117117
return fmt.Sprintf("%s-registry-creds", clusterName)
118118
}
119-
120-
type labelFn func(labels map[string]string)
121-
122-
func newLabels(fs ...labelFn) map[string]string {
123-
labels := map[string]string{}
124-
for _, f := range fs {
125-
f(labels)
126-
}
127-
return labels
128-
}
129-
130-
func withClusterName(clusterName string) labelFn {
131-
return func(labels map[string]string) {
132-
labels[clusterv1.ClusterNameLabel] = clusterName
133-
}
134-
}
135-
136-
func withMove() labelFn {
137-
return func(labels map[string]string) {
138-
labels[clusterctlv1.ClusterctlMoveLabel] = ""
139-
}
140-
}

0 commit comments

Comments
 (0)