forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support generation of ClusterRoleBinding resources
Fix quarkusio#32572
- Loading branch information
Showing
12 changed files
with
238 additions
and
67 deletions.
There are no files selected for viewing
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
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
19 changes: 19 additions & 0 deletions
19
extensions/kubernetes/spi/src/main/java/io/quarkus/kubernetes/spi/RoleRef.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,19 @@ | ||
package io.quarkus.kubernetes.spi; | ||
|
||
public class RoleRef { | ||
private final boolean clusterWide; | ||
private final String name; | ||
|
||
public RoleRef(String name, boolean clusterWide) { | ||
this.name = name; | ||
this.clusterWide = clusterWide; | ||
} | ||
|
||
public boolean isClusterWide() { | ||
return clusterWide; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
extensions/kubernetes/spi/src/main/java/io/quarkus/kubernetes/spi/Subject.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,31 @@ | ||
package io.quarkus.kubernetes.spi; | ||
|
||
public class Subject { | ||
private final String apiGroup; | ||
private final String kind; | ||
private final String name; | ||
private final String namespace; | ||
|
||
public Subject(String apiGroup, String kind, String name, String namespace) { | ||
this.apiGroup = apiGroup; | ||
this.kind = kind; | ||
this.name = name; | ||
this.namespace = namespace; | ||
} | ||
|
||
public String getApiGroup() { | ||
return apiGroup; | ||
} | ||
|
||
public String getKind() { | ||
return kind; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getNamespace() { | ||
return namespace; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...rc/main/java/io/quarkus/kubernetes/deployment/AddClusterRoleBindingResourceDecorator.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,71 @@ | ||
package io.quarkus.kubernetes.deployment; | ||
|
||
import static io.quarkus.kubernetes.deployment.Constants.CLUSTER_ROLE; | ||
import static io.quarkus.kubernetes.deployment.Constants.CLUSTER_ROLE_BINDING; | ||
import static io.quarkus.kubernetes.deployment.Constants.RBAC_API_GROUP; | ||
import static io.quarkus.kubernetes.deployment.Constants.RBAC_API_VERSION; | ||
import static io.quarkus.kubernetes.deployment.Constants.ROLE; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
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.ClusterRoleBindingBuilder; | ||
import io.quarkus.kubernetes.spi.RoleRef; | ||
import io.quarkus.kubernetes.spi.Subject; | ||
|
||
public class AddClusterRoleBindingResourceDecorator extends ResourceProvidingDecorator<KubernetesListBuilder> { | ||
|
||
private final String deploymentName; | ||
private final String name; | ||
private final Map<String, String> labels; | ||
private final RoleRef roleRef; | ||
private final Subject[] subjects; | ||
|
||
public AddClusterRoleBindingResourceDecorator(String deploymentName, String name, Map<String, String> labels, | ||
RoleRef roleRef, | ||
Subject... subjects) { | ||
this.deploymentName = deploymentName; | ||
this.name = name; | ||
this.labels = labels; | ||
this.roleRef = roleRef; | ||
this.subjects = subjects; | ||
} | ||
|
||
public void visit(KubernetesListBuilder list) { | ||
if (contains(list, RBAC_API_VERSION, CLUSTER_ROLE_BINDING, name)) { | ||
return; | ||
} | ||
|
||
Map<String, String> clusterRoleBindingLabels = new HashMap<>(); | ||
clusterRoleBindingLabels.putAll(labels); | ||
getDeploymentMetadata(list, deploymentName) | ||
.map(ObjectMeta::getLabels) | ||
.ifPresent(clusterRoleBindingLabels::putAll); | ||
|
||
ClusterRoleBindingBuilder builder = new ClusterRoleBindingBuilder() | ||
.withNewMetadata() | ||
.withName(name) | ||
.withLabels(clusterRoleBindingLabels) | ||
.endMetadata() | ||
.withNewRoleRef() | ||
.withKind(roleRef.isClusterWide() ? CLUSTER_ROLE : ROLE) | ||
.withName(roleRef.getName()) | ||
.withApiGroup(RBAC_API_GROUP) | ||
.endRoleRef(); | ||
|
||
for (Subject subject : subjects) { | ||
builder.addNewSubject() | ||
.withApiGroup(subject.getApiGroup()) | ||
.withKind(subject.getKind()) | ||
.withName(Strings.defaultIfEmpty(subject.getName(), deploymentName)) | ||
.withNamespace(subject.getNamespace()) | ||
.endSubject(); | ||
} | ||
|
||
list.addToItems(builder.build()); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...a/deployment/src/main/java/io/quarkus/kubernetes/deployment/ClusterRoleBindingConfig.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,42 @@ | ||
package io.quarkus.kubernetes.deployment; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
@ConfigGroup | ||
public class ClusterRoleBindingConfig { | ||
|
||
/** | ||
* Name of the ClusterRoleBinding resource to be generated. If not provided, it will use the application name plus the role | ||
* ref name. | ||
*/ | ||
@ConfigItem | ||
public Optional<String> name; | ||
|
||
/** | ||
* Labels to add into the RoleBinding resource. | ||
*/ | ||
@ConfigItem | ||
public Map<String, String> labels; | ||
|
||
/** | ||
* The name of the Role resource to use by the RoleRef element in the generated ClusterRoleBinding resource. | ||
*/ | ||
@ConfigItem | ||
public String roleName; | ||
|
||
/** | ||
* If the Role sets in the `role-name` property is cluster wide or not. | ||
*/ | ||
@ConfigItem(defaultValue = "true") | ||
public Boolean clusterWide; | ||
|
||
/** | ||
* List of subjects elements to use in the generated ClusterRoleBinding resource. | ||
*/ | ||
@ConfigItem | ||
public Map<String, SubjectConfig> subjects; | ||
} |
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
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
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
Oops, something went wrong.