Skip to content

Commit

Permalink
Merge pull request #32208 from Sgitario/32193
Browse files Browse the repository at this point in the history
Ensure RoleBinding resource is generated after Role/ClusterRole/ServiceAccount resources
  • Loading branch information
iocanel authored Mar 31, 2023
2 parents 68a26b1 + f3d8aaf commit c8cacd0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;

import io.dekorate.kubernetes.decorator.Decorator;
import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
Expand Down Expand Up @@ -45,4 +46,9 @@ public void visit(KubernetesListBuilder list) {
.endMetadata()
.withRules(rules));
}

@Override
public Class<? extends Decorator>[] before() {
return new Class[] { AddRoleBindingResourceDecorator.class };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;

import io.dekorate.kubernetes.decorator.Decorator;
import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
Expand Down Expand Up @@ -48,4 +49,9 @@ public void visit(KubernetesListBuilder list) {
.endMetadata()
.withRules(rules));
}

@Override
public Class<? extends Decorator>[] before() {
return new Class[] { AddRoleBindingResourceDecorator.class };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.Map;

import io.dekorate.kubernetes.decorator.Decorator;
import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
Expand Down Expand Up @@ -43,4 +44,9 @@ public void visit(KubernetesListBuilder list) {
.endMetadata()
.endServiceAccountItem();
}

@Override
public Class<? extends Decorator>[] before() {
return new Class[] { AddRoleBindingResourceDecorator.class };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -40,9 +43,16 @@ public class KubernetesWithRbacFullTest {

@Test
public void assertGeneratedResources() throws IOException {
final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
List<HasMetadata> kubernetesList = DeserializationUtil
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
final Path kubernetesFile = prodModeTestResults.getBuildDir().resolve("kubernetes").resolve("kubernetes.yml");

// ensure rbac resources are generated in order: having the RoleBinding resource at the end:
String kubernetesFileContent = Files.readString(kubernetesFile);
int lastIndexOfRoleRefKind = lastIndexOfKind(kubernetesFileContent, "Role", "ClusterRole", "ServiceAccount");
int firstIndexOfRoleBinding = kubernetesFileContent.indexOf("kind: RoleBinding");
assertTrue(lastIndexOfRoleRefKind < firstIndexOfRoleBinding, "RoleBinding resource is created before "
+ "the Role/ClusterRole/ServiceAccount resource!");

List<HasMetadata> kubernetesList = DeserializationUtil.deserializeAsList(kubernetesFile);

Deployment deployment = getDeploymentByName(kubernetesList, APP_NAME);
assertEquals(APP_NAMESPACE, deployment.getMetadata().getNamespace());
Expand Down Expand Up @@ -84,6 +94,21 @@ public void assertGeneratedResources() throws IOException {
assertEquals("projectc", subject.getNamespace());
}

private int lastIndexOfKind(String content, String... kinds) {
int index = Integer.MIN_VALUE;
for (String kind : kinds) {
Matcher matcher = Pattern.compile("(?m)^kind: " + kind).matcher(content);
if (matcher.find()) {
int lastIndexOfKind = matcher.end();
if (lastIndexOfKind > index) {
index = lastIndexOfKind;
}
}
}

return index;
}

private Deployment getDeploymentByName(List<HasMetadata> kubernetesList, String name) {
return getResourceByName(kubernetesList, Deployment.class, name);
}
Expand Down

0 comments on commit c8cacd0

Please sign in to comment.