Skip to content
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

Add decorator to remove namespace from ClusterRole and ClusterRoleBin… #43579

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;

public class AddNamespaceDecorator extends NamedResourceDecorator<ObjectMetaBuilder> {
public class AddNamespaceDecorator extends Decorator<KubernetesListBuilder> {

private final String namespace;

Expand All @@ -18,10 +20,19 @@ public AddNamespaceDecorator(String namespace) {
}

@Override
public void andThenVisit(ObjectMetaBuilder builder, ObjectMeta resourceMeta) {
if (!builder.hasNamespace()) {
builder.withNamespace(namespace);
}
public void visit(KubernetesListBuilder list) {
List<HasMetadata> buildItems = list.buildItems()
.stream()
.peek(obj -> {
if (obj instanceof Namespaced) {
final ObjectMeta metadata = obj.getMetadata();
if (metadata.getNamespace() == null) {
mcruzdev marked this conversation as resolved.
Show resolved Hide resolved
metadata.setNamespace(namespace);
obj.setMetadata(metadata);
}
}
}).collect(Collectors.toList());
list.withItems(buildItems);
}

@Override
Expand All @@ -43,4 +54,5 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(namespace);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down