From 660012035d09669eb7e8c591d910aa51adce78df Mon Sep 17 00:00:00 2001 From: Matheus Cruz Date: Sat, 5 Oct 2024 20:09:26 -0300 Subject: [PATCH] Remove namespace from clustered resources --- .../deployment/AddNamespaceDecorator.java | 23 +++++++++++-------- .../KubernetesWithRbacFullTest.java | 2 ++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/AddNamespaceDecorator.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/AddNamespaceDecorator.java index 3e04125891e8ec..083b2227f89d60 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/AddNamespaceDecorator.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/AddNamespaceDecorator.java @@ -1,15 +1,16 @@ package io.quarkus.kubernetes.deployment; +import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; -import io.dekorate.kubernetes.decorator.AddSidecarDecorator; import io.dekorate.kubernetes.decorator.Decorator; -import io.dekorate.kubernetes.decorator.NamedResourceDecorator; import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator; -import io.fabric8.kubernetes.api.model.ObjectMeta; -import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.KubernetesListBuilder; +import io.fabric8.kubernetes.api.model.Namespaced; -public class AddNamespaceDecorator extends NamedResourceDecorator { +public class AddNamespaceDecorator extends Decorator { private final String namespace; @@ -18,10 +19,13 @@ public AddNamespaceDecorator(String namespace) { } @Override - public void andThenVisit(ObjectMetaBuilder builder, ObjectMeta resourceMeta) { - if (!builder.hasNamespace()) { - builder.withNamespace(namespace); - } + public void visit(KubernetesListBuilder list) { + List buildItems = list.buildItems() + .stream() + .filter(Namespaced.class::isInstance) + .peek(o -> o.setMetadata(o.getMetadata().edit().withNamespace(namespace).build())).collect(Collectors.toList()); + + list.withItems(buildItems); } @Override @@ -43,4 +47,5 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(namespace); } + } diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithRbacFullTest.java b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithRbacFullTest.java index 83be5e5411d865..88da309588f08c 100644 --- a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithRbacFullTest.java +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithRbacFullTest.java @@ -84,6 +84,7 @@ public void assertGeneratedResources() throws IOException { // secret-reader assertions ClusterRole secretReaderRole = getClusterRoleByName(kubernetesList, "secret-reader"); + assertThat(secretReaderRole.getMetadata().getNamespace()).isNull(); assertThat(secretReaderRole.getRules()).satisfiesOnlyOnce(r -> { assertThat(r.getApiGroups()).containsExactly(""); assertThat(r.getResources()).containsExactly("secrets"); @@ -111,6 +112,7 @@ public void assertGeneratedResources() throws IOException { assertEquals("Group", clusterSubject.getKind()); assertEquals("manager", clusterSubject.getName()); assertEquals("rbac.authorization.k8s.io", clusterSubject.getApiGroup()); + assertThat(clusterRoleBinding.getMetadata().getNamespace()).isNull(); } private int lastIndexOfKind(String content, String... kinds) {