-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Kubernetes Client: Use the correct service account name #25750
Merged
+164
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...yment/src/main/java/io/quarkus/kubernetes/deployment/AddRoleBindingResourceDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.quarkus.kubernetes.deployment; | ||
|
||
import java.util.Collections; | ||
|
||
import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator; | ||
import io.dekorate.utils.Strings; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder; | ||
|
||
/** | ||
* Workaround for: https://github.com/dekorateio/dekorate/issues/987 | ||
* Once the issue is fixed in upstream, and we bump the Dekorate version, we should delete this decorator. | ||
*/ | ||
public class AddRoleBindingResourceDecorator extends ResourceProvidingDecorator<KubernetesListBuilder> { | ||
|
||
private static final String DEFAULT_RBAC_API_GROUP = "rbac.authorization.k8s.io"; | ||
|
||
public static enum RoleKind { | ||
Role, | ||
ClusterRole | ||
} | ||
|
||
private final String serviceAccount; | ||
private final String name; | ||
private final String role; | ||
private final RoleKind kind; | ||
|
||
public AddRoleBindingResourceDecorator(String name, String serviceAccount, String role, RoleKind kind) { | ||
this.name = name; | ||
this.serviceAccount = serviceAccount; | ||
this.role = role; | ||
this.kind = kind; | ||
} | ||
|
||
public void visit(KubernetesListBuilder list) { | ||
// If name is null, it will get the first deployment resource. | ||
ObjectMeta meta = getMandatoryDeploymentMetadata(list, name); | ||
String roleBindingName = meta.getName() + "-" + this.role; | ||
String serviceAccount = Strings.isNotNullOrEmpty(this.serviceAccount) ? this.serviceAccount : meta.getName(); | ||
|
||
if (contains(list, "rbac.authorization.k8s.io/v1", "RoleBinding", roleBindingName)) { | ||
return; | ||
} | ||
|
||
list.addToItems(new RoleBindingBuilder() | ||
.withNewMetadata() | ||
.withName(roleBindingName) | ||
.withLabels(Strings.isNotNullOrEmpty(name) ? getMandatoryDeploymentMetadata(list, name).getLabels() | ||
: Collections.emptyMap()) | ||
.endMetadata() | ||
.withNewRoleRef() | ||
.withKind(kind.name()) | ||
.withName(role) | ||
.withApiGroup(DEFAULT_RBAC_API_GROUP) | ||
.endRoleRef() | ||
.addNewSubject() | ||
.withKind("ServiceAccount") | ||
.withName(serviceAccount) | ||
.endSubject()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../src/test/java/io/quarkus/it/kubernetes/WithKubernetesClientAndExistingResourcesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.quarkus.it.kubernetes; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.quarkus.bootstrap.model.AppArtifact; | ||
import io.quarkus.builder.Version; | ||
import io.quarkus.kubernetes.spi.CustomProjectRootBuildItem; | ||
import io.quarkus.test.ProdBuildResults; | ||
import io.quarkus.test.ProdModeTestResults; | ||
import io.quarkus.test.QuarkusProdModeTest; | ||
|
||
public class WithKubernetesClientAndExistingResourcesTest { | ||
private static final String APPLICATION_NAME = "client-existing-resources"; | ||
|
||
@RegisterExtension | ||
static final QuarkusProdModeTest config = new QuarkusProdModeTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class)) | ||
.setApplicationName(APPLICATION_NAME) | ||
.setApplicationVersion("0.1-SNAPSHOT") | ||
.withConfigurationResource("kubernetes-with-" + APPLICATION_NAME + ".properties") | ||
.addCustomResourceEntry(Path.of("src", "main", "kubernetes", "kubernetes.yml"), | ||
"manifests/kubernetes-with-" + APPLICATION_NAME + "/kubernetes.yml") | ||
.setForcedDependencies(Collections.singletonList( | ||
new AppArtifact("io.quarkus", "quarkus-kubernetes-client", Version.getVersion()))) | ||
.addBuildChainCustomizerEntries( | ||
new QuarkusProdModeTest.BuildChainCustomizerEntry( | ||
KubernetesWithCustomResourcesTest.CustomProjectRootBuildItemProducerProdMode.class, | ||
Collections.singletonList(CustomProjectRootBuildItem.class), Collections.emptyList())); | ||
|
||
@ProdBuildResults | ||
private ProdModeTestResults prodModeTestResults; | ||
|
||
@Test | ||
public void assertGeneratedResources() throws IOException { | ||
final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes"); | ||
assertThat(kubernetesDir) | ||
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json")) | ||
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml")); | ||
List<HasMetadata> kubernetesList = DeserializationUtil | ||
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml")); | ||
|
||
assertThat(kubernetesList).filteredOn(h -> "Deployment".equals(h.getKind())).allSatisfy(h -> { | ||
Deployment deployment = (Deployment) h; | ||
assertThat(deployment.getSpec().getTemplate().getSpec().getServiceAccountName()).isEqualTo(APPLICATION_NAME); | ||
}); | ||
|
||
assertThat(kubernetesList).filteredOn(h -> "ServiceAccount".equals(h.getKind())).singleElement().satisfies(h -> { | ||
assertThat(h.getMetadata().getName()).isEqualTo(APPLICATION_NAME); | ||
}); | ||
|
||
assertThat(kubernetesList).filteredOn(h -> "RoleBinding".equals(h.getKind())).singleElement().satisfies(h -> { | ||
assertThat(h.getMetadata().getName()).isEqualTo(APPLICATION_NAME + "-view"); | ||
}); | ||
} | ||
} |
Empty file.
33 changes: 33 additions & 0 deletions
33
...way/src/test/resources/manifests/kubernetes-with-client-existing-resources/kubernetes.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: my-service | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
name: my-service | ||
template: | ||
metadata: | ||
labels: | ||
name: my-service | ||
spec: | ||
containers: | ||
- image: bitnami/mongodb:5.0 | ||
name: my-service | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
labels: | ||
name: my-service | ||
name: my-service | ||
spec: | ||
ports: | ||
- port: 27017 | ||
protocol: TCP | ||
targetPort: 27017 | ||
selector: | ||
name: my-service | ||
type: ClusterIP |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reported issue on Dekorate side: dekorateio/dekorate#987
I've also provided the fix there.