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

Ensure RoleBinding resource is generated after Role/ClusterRole/ServiceAccount resources #32208

Merged
merged 1 commit into from
Mar 31, 2023
Merged
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
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