diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddRoleBindingResourceDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddRoleBindingResourceDecorator.java index 69a4290f8..5fd8964fa 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/AddRoleBindingResourceDecorator.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddRoleBindingResourceDecorator.java @@ -15,6 +15,8 @@ */ package io.dekorate.kubernetes.decorator; +import java.util.Collections; + import io.dekorate.doc.Description; import io.dekorate.utils.Strings; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; @@ -54,18 +56,20 @@ public AddRoleBindingResourceDecorator(String name, String serviceAccount, Strin } public void visit(KubernetesListBuilder list) { - ObjectMeta meta = getMandatoryDeploymentMetadata(list, this.name); - String name = Strings.isNotNullOrEmpty(this.name) ? this.name : meta.getName() + "-" + this.role; - String serviceAccount = Strings.isNotNullOrEmpty(this.serviceAccount) ? this.serviceAccount : meta.getName(); + // If name is null, it will get the first deployment resource found. + ObjectMeta meta = getMandatoryDeploymentMetadata(list, name); + String actualName = Strings.isNotNullOrEmpty(name) ? name : meta.getName(); + String roleBindingName = actualName + "-" + this.role; + String serviceAccount = Strings.isNotNullOrEmpty(this.serviceAccount) ? this.serviceAccount : actualName; - if (contains(list, "rbac.authorization.k8s.io/v1", "RoleBinding", name)) { + if (contains(list, "rbac.authorization.k8s.io/v1", "RoleBinding", roleBindingName)) { return; } list.addToItems(new RoleBindingBuilder() .withNewMetadata() - .withName(name) - .withLabels(meta.getLabels()) + .withName(roleBindingName) + .withLabels(Strings.isNotNullOrEmpty(name) ? meta.getLabels() : Collections.emptyMap()) .endMetadata() .withNewRoleRef() .withKind(kind.name()) diff --git a/tests/issue-987-role-binding-name/pom.xml b/tests/issue-987-role-binding-name/pom.xml new file mode 100644 index 000000000..eb12c0679 --- /dev/null +++ b/tests/issue-987-role-binding-name/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + + dekorate-tests + io.dekorate + 2.9-SNAPSHOT + ../ + + + io.dekorate + issue-987-role-binding-name + Dekorate :: Tests :: Annotations :: Role Binding Name #987 + + + + + io.dekorate + kubernetes-annotations + ${project.version} + + + io.dekorate + dekorate-spring-boot + ${project.version} + + + org.springframework.boot + spring-boot-starter-web + ${version.spring-boot} + + + org.springframework.cloud + spring-cloud-kubernetes-discovery + 1.0.1.RELEASE + + + + + org.junit.jupiter + junit-jupiter-api + ${version.junit-jupiter} + test + + + org.junit.jupiter + junit-jupiter-engine + ${version.junit-jupiter} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + true + + false + + + + org.springframework.boot + spring-boot-maven-plugin + ${version.spring-boot} + + + + diff --git a/tests/issue-987-role-binding-name/src/main/java/io/dekorate/example/DemoApplication.java b/tests/issue-987-role-binding-name/src/main/java/io/dekorate/example/DemoApplication.java new file mode 100644 index 000000000..90fbe9518 --- /dev/null +++ b/tests/issue-987-role-binding-name/src/main/java/io/dekorate/example/DemoApplication.java @@ -0,0 +1,28 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.dekorate.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/tests/issue-987-role-binding-name/src/main/java/io/dekorate/example/HelloController.java b/tests/issue-987-role-binding-name/src/main/java/io/dekorate/example/HelloController.java new file mode 100644 index 000000000..7e28b773f --- /dev/null +++ b/tests/issue-987-role-binding-name/src/main/java/io/dekorate/example/HelloController.java @@ -0,0 +1,32 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +**/ + +package io.dekorate.example; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + + private static final String HELLO = "hello world!"; + + @RequestMapping("/") + public String hello() { + return HELLO; + } +} diff --git a/tests/issue-987-role-binding-name/src/main/resources/application.properties b/tests/issue-987-role-binding-name/src/main/resources/application.properties new file mode 100644 index 000000000..9f96389cd --- /dev/null +++ b/tests/issue-987-role-binding-name/src/main/resources/application.properties @@ -0,0 +1 @@ +dekorate.options.input-path=kubernetes diff --git a/tests/issue-987-role-binding-name/src/main/resources/kubernetes/common.yml b/tests/issue-987-role-binding-name/src/main/resources/kubernetes/common.yml new file mode 100644 index 000000000..187ce053e --- /dev/null +++ b/tests/issue-987-role-binding-name/src/main/resources/kubernetes/common.yml @@ -0,0 +1,14 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-service +spec: + replicas: 1 + selector: + matchLabels: + name: my-service + template: + metadata: + labels: + name: my-service diff --git a/tests/issue-987-role-binding-name/src/test/java/io/dekorate/example/Issue987RoleBindingNameTest.java b/tests/issue-987-role-binding-name/src/test/java/io/dekorate/example/Issue987RoleBindingNameTest.java new file mode 100644 index 000000000..d238d1ee2 --- /dev/null +++ b/tests/issue-987-role-binding-name/src/test/java/io/dekorate/example/Issue987RoleBindingNameTest.java @@ -0,0 +1,50 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +**/ + +package io.dekorate.example; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.util.Optional; + +import org.junit.jupiter.api.Test; + +import io.dekorate.utils.Serialization; +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.KubernetesList; +import io.fabric8.kubernetes.api.model.rbac.RoleBinding; + +public class Issue987RoleBindingNameTest { + + @Test + public void shouldContainRoleBindingWithCorrectName() { + KubernetesList list = Serialization + .unmarshalAsList(getClass().getClassLoader().getResourceAsStream("META-INF/dekorate/kubernetes.yml")); + assertNotNull(list); + RoleBinding s = findFirst(list, RoleBinding.class).orElseThrow(() -> new IllegalStateException()); + assertEquals("issue-987-role-binding-name-view", s.getMetadata().getName()); + assertNull(s.getMetadata().getLabels()); + } + + Optional findFirst(KubernetesList list, Class t) { + return (Optional) list.getItems().stream() + .filter(i -> t.isInstance(i)) + .findFirst(); + } +} diff --git a/tests/pom.xml b/tests/pom.xml index 914c08b5e..7a5454652 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -79,6 +79,7 @@ issue-963-docker-system-properties feat-967-ingress-tls issue-983-tests-inject-in-super-class + issue-987-role-binding-name