Skip to content

refactor: move label helper functions to utils package #626

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

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions common/pkg/capi/utils/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package utils

import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
)

type LabelFn func(labels map[string]string)

func NewLabels(fs ...LabelFn) map[string]string {
labels := map[string]string{}
for _, f := range fs {
f(labels)
}
return labels
}

func WithClusterName(clusterName string) LabelFn {
return func(labels map[string]string) {
labels[clusterv1.ClusterNameLabel] = clusterName
}
}

func WithMove() LabelFn {
return func(labels map[string]string) {
labels[clusterctlv1.ClusterctlMoveLabel] = ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils"
)

const (
Expand Down Expand Up @@ -76,7 +76,7 @@ func generateCredentialsSecret(
ObjectMeta: metav1.ObjectMeta{
Name: credentialSecretName(clusterName),
Namespace: namespace,
Labels: newLabels(withMove(), withClusterName(clusterName)),
Labels: utils.NewLabels(utils.WithMove(), utils.WithClusterName(clusterName)),
},
StringData: secretData,
Type: corev1.SecretTypeOpaque,
Expand Down Expand Up @@ -116,25 +116,3 @@ func kubeletStaticCredentialProviderSecretContents(configs []providerConfig) (st
func credentialSecretName(clusterName string) string {
return fmt.Sprintf("%s-registry-creds", clusterName)
}

type labelFn func(labels map[string]string)

func newLabels(fs ...labelFn) map[string]string {
labels := map[string]string{}
for _, f := range fs {
f(labels)
}
return labels
}

func withClusterName(clusterName string) labelFn {
return func(labels map[string]string) {
labels[clusterv1.ClusterNameLabel] = clusterName
}
}

func withMove() labelFn {
return func(labels map[string]string) {
labels[clusterctlv1.ClusterctlMoveLabel] = ""
}
}
Loading