Skip to content

✨clusterctl: fix labels #2152

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
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
5 changes: 5 additions & 0 deletions api/v1alpha3/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const (
// external objects(bootstrap and infrastructure providers)
ClusterLabelName = "cluster.x-k8s.io/cluster-name"

// ProviderLabelName is the label set on components in the provider manifest.
// This label allows to easily identify all the components belonging to a provider; the clusterctl
// tool uses this label for implementing provider's lifecycle operations.
ProviderLabelName = "cluster.x-k8s.io/provider"

// PausedAnnotation is an annotation that can be applied to any Cluster API
// object to prevent a controller from processing a resource.
//
Expand Down
7 changes: 5 additions & 2 deletions cmd/clusterctl/api/v1alpha3/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package v1alpha3

const (
ClusterctlLabelName = "clusterctl.cluster.x-k8s.io"
ClusterctlProviderLabelName = "clusterctl.cluster.x-k8s.io/provider"
// ClusterctlLabelName defines the label that is applied to all the components managed by clusterctl.
ClusterctlLabelName = "clusterctl.cluster.x-k8s.io"

// ClusterctlCoreLabelName defines the label that is applied to all the core objects managed by clusterctl.
ClusterctlCoreLabelName = "clusterctl.cluster.x-k8s.io/core"
)
9 changes: 9 additions & 0 deletions cmd/clusterctl/pkg/client/cluster/cert_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/util"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -91,6 +92,14 @@ func (cm *certMangerClient) EnsureWebHook() error {
objs = sortResourcesForCreate(objs)
for _, o := range objs {
klog.V(3).Infof("Creating: %s, %s/%s", o.GroupVersionKind(), o.GetNamespace(), o.GetName())

labels := o.GetLabels()
if labels == nil {
labels = map[string]string{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
labels = map[string]string{}
labels = make(map[string]string)

}
labels[clusterctlv1.ClusterctlCoreLabelName] = "cert-manager"
o.SetLabels(labels)

if err = c.Create(ctx, &o); err != nil { //nolint
if apierrors.IsAlreadyExists(err) {
continue
Expand Down
6 changes: 4 additions & 2 deletions cmd/clusterctl/pkg/client/cluster/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/repository"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -104,7 +105,8 @@ func (p *providerComponents) Create(components repository.Components) error {
func (p *providerComponents) Delete(options DeleteOptions) error {
// Fetch all the components belonging to a provider.
labels := map[string]string{
clusterctlv1.ClusterctlProviderLabelName: options.Provider.Name,
clusterctlv1.ClusterctlLabelName: "",
clusterv1.ProviderLabelName: options.Provider.Name,
}
resources, err := p.proxy.ListResources(options.Provider.Namespace, labels)
if err != nil {
Expand All @@ -121,7 +123,7 @@ func (p *providerComponents) Delete(options DeleteOptions) error {
}

// If the Namespace should NOT be deleted, skip it, otherwise keep track of the namespaces we are deleting;
// NB. Skipping Namespaces deletion ensures that also the objects hosted in the namespace but without the "clusterctl.cluster.x-k8s.io/provider" label are not deleted.
// NB. Skipping Namespaces deletion ensures that also the objects hosted in the namespace but without the "clusterctl.cluster.x-k8s.io" and the "cluster.x-k8s.io/provider" label are not deleted.
if obj.GroupVersionKind().Kind == "Namespace" {
if !options.ForceDeleteNamespace {
continue
Expand Down
3 changes: 2 additions & 1 deletion cmd/clusterctl/pkg/client/cluster/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/test"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func Test_providerComponents_Delete(t *testing.T) {
labels := map[string]string{
clusterctlv1.ClusterctlProviderLabelName: "aws",
clusterv1.ProviderLabelName: "aws",
}

crd := unstructured.Unstructured{}
Expand Down
8 changes: 8 additions & 0 deletions cmd/clusterctl/pkg/client/cluster/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ func (p *inventoryClient) EnsureCustomResourceDefinitions() error {
// Install the CRDs.
for _, o := range objs {
klog.V(3).Infof("Creating: %s, %s/%s", o.GroupVersionKind(), o.GetNamespace(), o.GetName())

labels := o.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels[clusterctlv1.ClusterctlCoreLabelName] = "inventory"
o.SetLabels(labels)

if err := c.Create(ctx, o.DeepCopy()); err != nil {
if apierrors.IsAlreadyExists(err) {
continue
Expand Down
10 changes: 7 additions & 3 deletions cmd/clusterctl/pkg/client/repository/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/scheme"
Expand Down Expand Up @@ -107,6 +108,9 @@ func (c *components) WatchingNamespace() string {
}

func (c *components) Metadata() clusterctlv1.Provider {
labels := getLabels(c.Name())
labels[clusterctlv1.ClusterctlCoreLabelName] = "inventory"

return clusterctlv1.Provider{
TypeMeta: metav1.TypeMeta{
APIVersion: clusterctlv1.GroupVersion.String(),
Expand All @@ -115,7 +119,7 @@ func (c *components) Metadata() clusterctlv1.Provider {
ObjectMeta: metav1.ObjectMeta{
Namespace: c.targetNamespace,
Name: c.Name(),
Labels: getLabels(c.Name()),
Labels: labels,
},
Type: string(c.Type()),
Version: c.version,
Expand Down Expand Up @@ -536,7 +540,7 @@ func addLabels(objs []unstructured.Unstructured, name string) []unstructured.Uns

func getLabels(name string) map[string]string {
return map[string]string{
clusterctlv1.ClusterctlLabelName: "",
clusterctlv1.ClusterctlProviderLabelName: name,
clusterctlv1.ClusterctlLabelName: "",
clusterv1.ProviderLabelName: name,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"testing"

clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/test"
Expand Down Expand Up @@ -283,7 +284,7 @@ func Test_componentsClient_Get(t *testing.T) {
}

for _, o := range got.Objs() {
for _, v := range []string{clusterctlv1.ClusterctlLabelName, clusterctlv1.ClusterctlProviderLabelName} {
for _, v := range []string{clusterctlv1.ClusterctlLabelName, clusterv1.ProviderLabelName} {
if _, ok := o.GetLabels()[v]; !ok {
t.Errorf("Get().Objs() object %s does not contains %s label", o.GetName(), v)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/clusterctl/pkg/client/repository/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/test"
Expand Down Expand Up @@ -764,8 +765,8 @@ func Test_addLabels(t *testing.T) {
"kind": "ClusterRole",
"metadata": map[string]interface{}{
"labels": map[string]interface{}{
clusterctlv1.ClusterctlLabelName: "",
clusterctlv1.ClusterctlProviderLabelName: "provider",
clusterctlv1.ClusterctlLabelName: "",
clusterv1.ProviderLabelName: "provider",
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions cmd/clusterctl/pkg/internal/test/fake_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ func (f *FakeProxy) WithProviderInventory(name string, providerType clusterctlv1
Namespace: targetNamespace,
Name: name,
Labels: map[string]string{
clusterctlv1.ClusterctlLabelName: "",
clusterctlv1.ClusterctlProviderLabelName: name,
clusterctlv1.ClusterctlLabelName: "",
clusterv1.ProviderLabelName: name,
clusterctlv1.ClusterctlCoreLabelName: "inventory",
},
},
Type: string(providerType),
Expand Down
4 changes: 2 additions & 2 deletions docs/book/src/clusterctl/commands/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ subsequent moments of the provider's lifecycle, e.g. upgrades.
```bash
labels:
- clusterctl.cluster.x-k8s.io: ""
- clusterctl.cluster.x-k8s.io/provider: "<provider-name>"
- cluster.x-k8s.io/provider: "<provider-name>"
```

* An additional `Provider` object is created in the target namespace where the provider is installed.
Expand All @@ -227,7 +227,7 @@ for the inventory of the providers currently installed in the management cluster

<h1>Warning</h1>

The `clusterctl.cluster.x-k8s.io` labels and the `Provider` objects MUST NOT altered.
The `clusterctl.cluster.x-k8s.io` labels, the `cluster.x-k8s.io/provider` labels and the `Provider` objects MUST NOT altered.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
The `clusterctl.cluster.x-k8s.io` labels, the `cluster.x-k8s.io/provider` labels and the `Provider` objects MUST NOT altered.
The `clusterctl.cluster.x-k8s.io` labels, the `cluster.x-k8s.io/provider` labels and the `Provider` objects MUST NOT be altered.

If this happens, there are no guarantees about the proper functioning of `clusterctl`.

</aside>