Skip to content

Commit

Permalink
Add decorator to remove namespace from ClusterRole and ClusterRoleBin…
Browse files Browse the repository at this point in the history
…ding
  • Loading branch information
mcruzdev committed Sep 29, 2024
1 parent 2811eec commit dc093a8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ private static Collection<DecoratorBuildItem> createRbacDecorators(String name,
List<KubernetesEffectiveServiceAccountBuildItem> effectiveServiceAccounts,
List<KubernetesRoleBindingBuildItem> roleBindingsFromExtensions) {
List<DecoratorBuildItem> result = new ArrayList<>();

// Cluster resources does not have namespace
result.add(new DecoratorBuildItem(target, new RemoveNamespaceFromClusterRoleBindingDecorator()));
result.add(new DecoratorBuildItem(target, new RemoveNamespaceFromClusterRoleDecorator()));

boolean kubernetesClientRequiresRbacGeneration = kubernetesClientConfiguration
.map(KubernetesClientCapabilityBuildItem::isGenerateRbac).orElse(false);
Set<String> roles = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.kubernetes.deployment;

import io.dekorate.kubernetes.decorator.Decorator;
import io.dekorate.kubernetes.decorator.NamedResourceDecorator;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder;

/**
* Decorator responsible for remove namespace from ClusterRoleBinding resource.
*
* This decorator executes after {@link AddNamespaceDecorator}.
*/
public class RemoveNamespaceFromClusterRoleBindingDecorator extends NamedResourceDecorator<ClusterRoleBindingBuilder> {

@Override
public void andThenVisit(ClusterRoleBindingBuilder clusterRoleBindingBuilder, ObjectMeta objectMeta) {
clusterRoleBindingBuilder
.withNewMetadata()
.withNamespace(null)
.withName(objectMeta.getName())
.endMetadata();
}

@Override
public Class<? extends Decorator>[] after() {
return new Class[] { AddNamespaceDecorator.class };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.kubernetes.deployment;

import io.dekorate.kubernetes.decorator.Decorator;
import io.dekorate.kubernetes.decorator.NamedResourceDecorator;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBuilder;

/**
* Decorator responsible for remove namespace from ClusterRole resource.
*
* This decorator executes after {@link AddNamespaceDecorator}.
*/
public class RemoveNamespaceFromClusterRoleDecorator extends NamedResourceDecorator<ClusterRoleBuilder> {

@Override
public void andThenVisit(ClusterRoleBuilder clusterRoleBuilder, ObjectMeta objectMeta) {
clusterRoleBuilder
.withNewMetadata()
.withNamespace(null)
.withName(objectMeta.getName())
.endMetadata();
}

@Override
public Class<? extends Decorator>[] after() {
return new Class[] { AddNamespaceDecorator.class };
}
}
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()).isEqualTo(null);
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()).isEqualTo(null);
}

private int lastIndexOfKind(String content, String... kinds) {
Expand Down

0 comments on commit dc093a8

Please sign in to comment.