Repository: rancher/fleet
Pull request #2206 (2024-03-04 13:24:53)

Bump kubernetes go module to 1.28.6


Patch:
diff --git a/go.mod b/go.mod
index e60ccf21..0f5c40da 100644
--- a/go.mod
+++ b/go.mod
@@ -71,7 +71,7 @@ require (
 	k8s.io/client-go v12.0.0+incompatible
 	k8s.io/klog/v2 v2.100.1
 	k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9
-	k8s.io/kubernetes v1.28.0
+	k8s.io/kubernetes v1.28.6
 	k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
 	sigs.k8s.io/cli-utils v0.34.0
 	sigs.k8s.io/controller-runtime v0.13.0
diff --git a/go.sum b/go.sum
index d275016b..135e29d2 100644
--- a/go.sum
+++ b/go.sum
@@ -1597,8 +1597,8 @@ k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5Ohx
 k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
 k8s.io/kubectl v0.28.6 h1:46O3gGJYlpqy7wtwYlggieemyIcuZqmflnQVDci3MgY=
 k8s.io/kubectl v0.28.6/go.mod h1:FS5ugZhi3kywpMQSCnp8MN+gctdFHJACzC6mH3fZ6lc=
-k8s.io/kubernetes v1.28.0 h1:p8qq/VoNHnBWinLEi5LO2IvCfzFouN7Jhdz8+L++V+U=
-k8s.io/kubernetes v1.28.0/go.mod h1:rBQpjGYlLBV0KuOLw8EG45N5EBCskWiPpi0xy5liHMI=
+k8s.io/kubernetes v1.28.6 h1:Nn2fXNjaqmKyXeFq6verbdqzlj1aqUpBcyeC1OhRFa4=
+k8s.io/kubernetes v1.28.6/go.mod h1:0qpyGJTR3blkbQOmZA3Z0u1VDZJNxJM8ifLUVNJN0X8=
 k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
 k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
 oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY=
diff --git a/internal/cmd/agent/controllers/bundledeployment/controller.go b/internal/cmd/agent/controllers/bundledeployment/controller.go
index 92d3768b..9c44b154 100644
--- a/internal/cmd/agent/controllers/bundledeployment/controller.go
+++ b/internal/cmd/agent/controllers/bundledeployment/controller.go
@@ -198,18 +198,13 @@ func (h *handler) updateNamespace(ns *corev1.Namespace) error {
 func (h *handler) fetchNamespace(releaseID string) (*corev1.Namespace, error) {
 	// releaseID is composed of release.Namespace/release.Name/release.Version
 	namespace := strings.Split(releaseID, "/")[0]
-	list, err := h.dynamic.Resource(nsResource).List(h.ctx, metav1.ListOptions{
-		LabelSelector: "name=" + namespace,
-	})
+	res, err := h.dynamic.Resource(nsResource).Get(h.ctx, namespace, metav1.GetOptions{})
 	if err != nil {
 		return nil, err
 	}
-	if len(list.Items) == 0 {
-		return nil, fmt.Errorf("namespace %s not found", namespace)
-	}
 	var ns corev1.Namespace
 	err = runtime.DefaultUnstructuredConverter.
-		FromUnstructured(list.Items[0].Object, &ns)
+		FromUnstructured(res.Object, &ns)
 	if err != nil {
 		return nil, err
 	}
@@ -217,16 +212,18 @@ func (h *handler) fetchNamespace(releaseID string) (*corev1.Namespace, error) {
 	return &ns, nil
 }
 
-// addLabelsFromOptions updates nsLabels so that it only contains all labels specified in optLabels, plus the `name` labels added by Helm when creating the namespace.
+// addLabelsFromOptions updates nsLabels so that it only contains all labels
+// specified in optLabels, plus the `kubernetes.io/metadata.name` labels added
+// by kubernetes when creating the namespace.
 func addLabelsFromOptions(nsLabels map[string]string, optLabels map[string]string) {
 	for k, v := range optLabels {
 		nsLabels[k] = v
 	}
 
 	// Delete labels not defined in the options.
-	// Keep the name label as it is added by helm when creating the namespace.
+	// Keep the `kubernetes.io/metadata.name` label as it is added by kubernetes when creating the namespace.
 	for k := range nsLabels {
-		if _, ok := optLabels[k]; k != "name" && !ok {
+		if _, ok := optLabels[k]; k != corev1.LabelMetadataName && !ok {
 			delete(nsLabels, k)
 		}
 	}


Test patch:
diff --git a/internal/cmd/agent/controllers/bundledeployment/controller_test.go b/internal/cmd/agent/controllers/bundledeployment/controller_test.go
index fee311ab..b7c45a71 100644
--- a/internal/cmd/agent/controllers/bundledeployment/controller_test.go
+++ b/internal/cmd/agent/controllers/bundledeployment/controller_test.go
@@ -3,7 +3,6 @@ package bundledeployment
 //go:generate mockgen --build_flags=--mod=mod -destination=../../../controller/mocks/dynamic_mock.go -package mocks k8s.io/client-go/dynamic Interface,NamespaceableResourceInterface
 
 import (
-	"errors"
 	"testing"
 
 	"github.com/golang/mock/gomock"
@@ -12,6 +11,7 @@ import (
 	fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
 
 	corev1 "k8s.io/api/core/v1"
+	apierrors "k8s.io/apimachinery/pkg/api/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 	"k8s.io/apimachinery/pkg/runtime"
@@ -33,13 +33,15 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
 			}},
 			ns: corev1.Namespace{
 				ObjectMeta: metav1.ObjectMeta{
-					Labels: map[string]string{"name": "test"},
+					Name:   "namespace",
+					Labels: map[string]string{"kubernetes.io/metadata.name": "namespace"},
 				},
 			},
 			release: "namespace/foo/bar",
 			expectedNs: corev1.Namespace{
 				ObjectMeta: metav1.ObjectMeta{
-					Labels:      map[string]string{"name": "test", "optLabel1": "optValue1", "optLabel2": "optValue2"},
+					Name:        "namespace",
+					Labels:      map[string]string{"kubernetes.io/metadata.name": "namespace", "optLabel1": "optValue1", "optLabel2": "optValue2"},
 					Annotations: map[string]string{"optAnn1": "optValue1"},
 				},
 			},
@@ -54,14 +56,16 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
 			}},
 			ns: corev1.Namespace{
 				ObjectMeta: metav1.ObjectMeta{
-					Labels:      map[string]string{"nsLabel": "nsValue", "name": "test"},
+					Name:        "namespace",
+					Labels:      map[string]string{"nsLabel": "nsValue", "kubernetes.io/metadata.name": "namespace"},
 					Annotations: map[string]string{"nsAnn": "nsValue"},
 				},
 			},
 			release: "namespace/foo/bar",
 			expectedNs: corev1.Namespace{
 				ObjectMeta: metav1.ObjectMeta{
-					Labels:      map[string]string{"optLabel": "optValue", "name": "test"},
+					Name:        "namespace",
+					Labels:      map[string]string{"optLabel": "optValue", "kubernetes.io/metadata.name": "namespace"},
 					Annotations: map[string]string{},
 				},
 			},
@@ -76,14 +80,16 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
 			}},
 			ns: corev1.Namespace{
 				ObjectMeta: metav1.ObjectMeta{
-					Labels:      map[string]string{"bdLabel": "nsValue"},
+					Name:        "namespace",
+					Labels:      map[string]string{"bdLabel": "nsValue", "kubernetes.io/metadata.name": "namespace"},
 					Annotations: map[string]string{"bdAnn": "nsValue"},
 				},
 			},
 			release: "namespace/foo/bar",
 			expectedNs: corev1.Namespace{
 				ObjectMeta: metav1.ObjectMeta{
-					Labels:      map[string]string{"bdLabel": "labelUpdated"},
+					Name:        "namespace",
+					Labels:      map[string]string{"bdLabel": "labelUpdated", "kubernetes.io/metadata.name": "namespace"},
 					Annotations: map[string]string{"bdAnn": "annUpdated"},
 				},
 			},
@@ -98,13 +104,10 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
 			mockDynamic := mocks.NewMockInterface(ctrl)
 			mockNamespaceableResourceInterface := mocks.NewMockNamespaceableResourceInterface(ctrl)
 			u, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(&test.ns)
-			// Resource will be called twice, one time for UPDATE and another time for LIST
+			// Resource will be called twice, one time for UPDATE and another time for GET
 			mockDynamic.EXPECT().Resource(gomock.Any()).Return(mockNamespaceableResourceInterface).Times(2)
-			mockNamespaceableResourceInterface.EXPECT().List(gomock.Any(), metav1.ListOptions{
-				LabelSelector: "name=namespace",
-			}).Return(&unstructured.UnstructuredList{
-				Items: []unstructured.Unstructured{{Object: u}},
-			}, nil).Times(1)
+			mockNamespaceableResourceInterface.EXPECT().Get(gomock.Any(), "namespace", metav1.GetOptions{}).
+				Return(&unstructured.Unstructured{Object: u}, nil).Times(1)
 			uns, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(&test.expectedNs)
 			mockNamespaceableResourceInterface.EXPECT().Update(gomock.Any(), &unstructured.Unstructured{Object: uns}, gomock.Any()).Times(1)
 
@@ -128,24 +131,20 @@ func TestSetNamespaceLabelsAndAnnotationsError(t *testing.T) {
 		},
 	}}
 	release := "test/foo/bar"
-	expectedErr := errors.New("namespace test not found")
 
 	ctrl := gomock.NewController(t)
 	defer ctrl.Finish()
 	mockDynamic := mocks.NewMockInterface(ctrl)
 	mockNamespaceableResourceInterface := mocks.NewMockNamespaceableResourceInterface(ctrl)
 	mockDynamic.EXPECT().Resource(gomock.Any()).Return(mockNamespaceableResourceInterface).Times(1)
-	mockNamespaceableResourceInterface.EXPECT().List(gomock.Any(), metav1.ListOptions{
-		LabelSelector: "name=test",
-	}).Return(&unstructured.UnstructuredList{
-		Items: []unstructured.Unstructured{},
-	}, nil).Times(1)
+	mockNamespaceableResourceInterface.EXPECT().Get(gomock.Any(), "test", metav1.GetOptions{}).
+		Return(nil, apierrors.NewNotFound(corev1.Resource("namespace"), "test")).Times(1)
 	h := handler{
 		dynamic: mockDynamic,
 	}
 	err := h.setNamespaceLabelsAndAnnotations(bd, release)
 
-	if err.Error() != expectedErr.Error() {
-		t.Errorf("expected error %v: got %v", expectedErr, err)
+	if !apierrors.IsNotFound(err) {
+		t.Errorf("expected not found error: got %v", err)
 	}
 }