diff --git a/internal/cmd/agent/deployer/deployer.go b/internal/cmd/agent/deployer/deployer.go index 4cf181f655..d77919ebe5 100644 --- a/internal/cmd/agent/deployer/deployer.go +++ b/internal/cmd/agent/deployer/deployer.go @@ -16,6 +16,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" ) @@ -161,20 +162,15 @@ func (d *Deployer) updateNamespace(ctx context.Context, ns *corev1.Namespace) er } // fetchNamespace gets the namespace matching the release ID. Returns an error if none is found. +// releaseID is composed of release.Namespace/release.Name/release.Version func (d *Deployer) fetchNamespace(ctx context.Context, releaseID string) (*corev1.Namespace, error) { - // releaseID is composed of release.Namespace/release.Name/release.Version namespace := strings.Split(releaseID, "/")[0] - list := &corev1.NamespaceList{} - err := d.client.List(ctx, list, client.MatchingLabels{ - corev1.LabelMetadataName: namespace, - }) + ns := &corev1.Namespace{} + err := d.client.Get(ctx, types.NamespacedName{Name: namespace}, ns) if err != nil { return nil, err } - if len(list.Items) == 0 { - return nil, fmt.Errorf("namespace %s not found", namespace) - } - return &list.Items[0], nil + return ns, nil } // 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. diff --git a/internal/cmd/agent/deployer/deployer_test.go b/internal/cmd/agent/deployer/deployer_test.go index 4e82b9d256..c12c8fc76a 100644 --- a/internal/cmd/agent/deployer/deployer_test.go +++ b/internal/cmd/agent/deployer/deployer_test.go @@ -2,12 +2,12 @@ package deployer import ( "context" - "errors" "testing" 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/runtime" "k8s.io/apimachinery/pkg/types" @@ -32,13 +32,14 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) { }}, ns: corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ - Name: "namespace1234", + Name: "namespace", Labels: map[string]string{"kubernetes.io/metadata.name": "namespace"}, }, }, release: "namespace/foo/bar", expectedNs: corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ + Name: "namespace", Labels: map[string]string{"kubernetes.io/metadata.name": "namespace", "optLabel1": "optValue1", "optLabel2": "optValue2"}, Annotations: map[string]string{"optAnn1": "optValue1"}, }, @@ -54,7 +55,7 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) { }}, ns: corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ - Name: "namespace1234", + Name: "namespace", Labels: map[string]string{"nsLabel": "nsValue", "kubernetes.io/metadata.name": "namespace"}, Annotations: map[string]string{"nsAnn": "nsValue"}, }, @@ -62,6 +63,7 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) { release: "namespace/foo/bar", expectedNs: corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ + Name: "namespace", Labels: map[string]string{"optLabel": "optValue", "kubernetes.io/metadata.name": "namespace"}, Annotations: map[string]string{}, }, @@ -77,7 +79,7 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) { }}, ns: corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ - Name: "namespace1234", + Name: "namespace", Labels: map[string]string{"bdLabel": "nsValue", "kubernetes.io/metadata.name": "namespace"}, Annotations: map[string]string{"bdAnn": "nsValue"}, }, @@ -85,6 +87,7 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) { release: "namespace/foo/bar", expectedNs: corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ + Name: "namespace", Labels: map[string]string{"bdLabel": "labelUpdated", "kubernetes.io/metadata.name": "namespace"}, Annotations: map[string]string{"bdAnn": "annUpdated"}, }, @@ -132,7 +135,6 @@ func TestSetNamespaceLabelsAndAnnotationsError(t *testing.T) { }, }} release := "test/foo/bar" - expectedErr := errors.New("namespace test not found") scheme := runtime.NewScheme() utilruntime.Must(clientgoscheme.AddToScheme(scheme)) @@ -143,7 +145,7 @@ func TestSetNamespaceLabelsAndAnnotationsError(t *testing.T) { err := h.setNamespaceLabelsAndAnnotations(context.Background(), 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) } }