Skip to content

Commit

Permalink
filter by kubernetes.io/metadata.name label
Browse files Browse the repository at this point in the history
use the label kubernetes.io/metadata.name to filter namespaces

Signed-off-by: raul <[email protected]>
  • Loading branch information
raulcabello authored and weyfonk committed Feb 1, 2024
1 parent 0158a4d commit 1c86b73
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/agent/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (d *Deployer) fetchNamespace(ctx context.Context, releaseID string) (*corev
namespace := strings.Split(releaseID, "/")[0]
list := &corev1.NamespaceList{}
err := d.client.List(ctx, list, client.MatchingLabels{
"name": namespace,
corev1.LabelMetadataName: namespace,
})
if err != nil {
return nil, err
Expand All @@ -184,9 +184,9 @@ func addLabelsFromOptions(nsLabels map[string]string, optLabels map[string]strin
}

// Delete labels not defined in the options.
// Keep the name label as it is added by helm when creating the namespace.
// Keep the corev1.LabelMetadataName 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)
}
}
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/agent/deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
ns: corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "namespace1234",
Labels: map[string]string{"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": "namespace", "optLabel1": "optValue1", "optLabel2": "optValue2"},
Labels: map[string]string{"kubernetes.io/metadata.name": "namespace", "optLabel1": "optValue1", "optLabel2": "optValue2"},
Annotations: map[string]string{"optAnn1": "optValue1"},
},
},
Expand All @@ -55,14 +55,14 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
ns: corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "namespace1234",
Labels: map[string]string{"nsLabel": "nsValue", "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": "namespace"},
Labels: map[string]string{"optLabel": "optValue", "kubernetes.io/metadata.name": "namespace"},
Annotations: map[string]string{},
},
},
Expand All @@ -78,14 +78,14 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
ns: corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "namespace1234",
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"},
},
},
Expand Down

0 comments on commit 1c86b73

Please sign in to comment.