Skip to content
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

🌱 Move internal.labels to format package for use by providers #9002

Merged
merged 1 commit into from
Jul 17, 2023
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
6 changes: 3 additions & 3 deletions api/v1beta1/machineset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"sigs.k8s.io/cluster-api/feature"
capilabels "sigs.k8s.io/cluster-api/internal/labels"
"sigs.k8s.io/cluster-api/util/labels/format"
"sigs.k8s.io/cluster-api/util/version"
)

Expand Down Expand Up @@ -70,8 +70,8 @@ func (m *MachineSet) Default() {

if len(m.Spec.Selector.MatchLabels) == 0 && len(m.Spec.Selector.MatchExpressions) == 0 {
// Note: MustFormatValue is used here as the value of this label will be a hash if the MachineSet name is longer than 63 characters.
m.Spec.Selector.MatchLabels[MachineSetNameLabel] = capilabels.MustFormatValue(m.Name)
m.Spec.Template.Labels[MachineSetNameLabel] = capilabels.MustFormatValue(m.Name)
m.Spec.Selector.MatchLabels[MachineSetNameLabel] = format.MustFormatValue(m.Name)
m.Spec.Template.Labels[MachineSetNameLabel] = format.MustFormatValue(m.Name)
}

if m.Spec.Template.Spec.Version != nil && !strings.HasPrefix(*m.Spec.Template.Spec.Version, "v") {
Expand Down
4 changes: 2 additions & 2 deletions controlplane/kubeadm/internal/cluster_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package internal
import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
capilabels "sigs.k8s.io/cluster-api/internal/labels"
"sigs.k8s.io/cluster-api/util/labels/format"
)

// ControlPlaneMachineLabelsForCluster returns a set of labels to add to a control plane machine for this specific cluster.
Expand All @@ -36,6 +36,6 @@ func ControlPlaneMachineLabelsForCluster(kcp *controlplanev1.KubeadmControlPlane
labels[clusterv1.ClusterNameLabel] = clusterName
labels[clusterv1.MachineControlPlaneLabel] = ""
// Note: MustFormatValue is used here as the label value can be a hash if the control plane name is longer than 63 characters.
labels[clusterv1.MachineControlPlaneNameLabel] = capilabels.MustFormatValue(kcp.Name)
labels[clusterv1.MachineControlPlaneNameLabel] = format.MustFormatValue(kcp.Name)
return labels
}
8 changes: 4 additions & 4 deletions exp/internal/controllers/machinepool_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
capierrors "sigs.k8s.io/cluster-api/errors"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
capilabels "sigs.k8s.io/cluster-api/internal/labels"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/cluster-api/util/labels/format"
"sigs.k8s.io/cluster-api/util/patch"
)

Expand Down Expand Up @@ -342,7 +342,7 @@ func (r *MachinePoolReconciler) reconcileMachines(ctx context.Context, mp *expv1

infraMachineSelector := metav1.LabelSelector{
MatchLabels: map[string]string{
clusterv1.MachinePoolNameLabel: capilabels.MustFormatValue(mp.Name),
clusterv1.MachinePoolNameLabel: format.MustFormatValue(mp.Name),
clusterv1.ClusterNameLabel: mp.Spec.ClusterName,
},
}
Expand Down Expand Up @@ -503,7 +503,7 @@ func getNewMachine(mp *expv1.MachinePool, infraMachine *unstructured.Unstructure
}

// Enforce that the MachinePoolNameLabel and ClusterNameLabel are present on the Machine.
machine.Labels[clusterv1.MachinePoolNameLabel] = capilabels.MustFormatValue(mp.Name)
machine.Labels[clusterv1.MachinePoolNameLabel] = format.MustFormatValue(mp.Name)
machine.Labels[clusterv1.ClusterNameLabel] = mp.Spec.ClusterName

return machine
Expand All @@ -528,7 +528,7 @@ func (r *MachinePoolReconciler) infraMachineToMachinePoolMapper(ctx context.Cont
}

for _, mp := range machinePoolList.Items {
if capilabels.MustFormatValue(mp.Name) == poolNameHash {
if format.MustFormatValue(mp.Name) == poolNameHash {
return []ctrl.Request{
{
NamespacedName: client.ObjectKey{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
"sigs.k8s.io/cluster-api/controllers/remote"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
capilabels "sigs.k8s.io/cluster-api/internal/labels"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/labels/format"
)

const (
Expand Down Expand Up @@ -1398,7 +1398,7 @@ func TestInfraMachineToMachinePoolMapper(t *testing.T) {
"namespace": metav1.NamespaceDefault,
"labels": map[string]interface{}{
clusterv1.ClusterNameLabel: clusterName,
clusterv1.MachinePoolNameLabel: capilabels.MustFormatValue(machinePool1.Name),
clusterv1.MachinePoolNameLabel: format.MustFormatValue(machinePool1.Name),
},
},
},
Expand All @@ -1413,7 +1413,7 @@ func TestInfraMachineToMachinePoolMapper(t *testing.T) {
"namespace": metav1.NamespaceDefault,
"labels": map[string]interface{}{
clusterv1.ClusterNameLabel: "other-cluster",
clusterv1.MachinePoolNameLabel: capilabels.MustFormatValue(machinePoolLongName.Name),
clusterv1.MachinePoolNameLabel: format.MustFormatValue(machinePoolLongName.Name),
},
},
},
Expand All @@ -1428,7 +1428,7 @@ func TestInfraMachineToMachinePoolMapper(t *testing.T) {
"namespace": metav1.NamespaceDefault,
"labels": map[string]interface{}{
clusterv1.ClusterNameLabel: "other-cluster",
clusterv1.MachinePoolNameLabel: capilabels.MustFormatValue("missing-machinepool"),
clusterv1.MachinePoolNameLabel: format.MustFormatValue("missing-machinepool"),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/controllers/machineset/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ import (
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/internal/contract"
"sigs.k8s.io/cluster-api/internal/controllers/machine"
capilabels "sigs.k8s.io/cluster-api/internal/labels"
"sigs.k8s.io/cluster-api/internal/util/ssa"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/conditions"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/cluster-api/util/labels/format"
clog "sigs.k8s.io/cluster-api/util/log"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
Expand Down Expand Up @@ -684,7 +684,7 @@ func machineLabelsFromMachineSet(machineSet *clusterv1.MachineSet) map[string]st
// Note: If a client tries to create a MachineSet without a selector, the MachineSet webhook
// will add this label automatically. But we want this label to always be present even if the MachineSet
// has a selector which doesn't include it. Therefore, we have to set it here explicitly.
machineLabels[clusterv1.MachineSetNameLabel] = capilabels.MustFormatValue(machineSet.Name)
machineLabels[clusterv1.MachineSetNameLabel] = format.MustFormatValue(machineSet.Name)
// Propagate the MachineDeploymentNameLabel from MachineSet to Machine if it exists.
if mdName, ok := machineSet.Labels[clusterv1.MachineDeploymentNameLabel]; ok {
machineLabels[clusterv1.MachineDeploymentNameLabel] = mdName
Expand Down
4 changes: 2 additions & 2 deletions internal/labels/helpers.go → util/labels/format/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package labels contains functions to validate and compare values used in Kubernetes labels.
package labels
// Package format contains functions to format and compare formatted values used in Kubernetes labels.
package format

import (
"encoding/base64"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package labels
package format

import (
"testing"
Expand Down
Loading