-
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
Support generation of ClusterRoleBinding resources #32605
Merged
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
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; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...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,70 @@ | ||
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 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(CLUSTER_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
36 changes: 36 additions & 0 deletions
36
...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,36 @@ | ||
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 ClusterRole resource to use by the RoleRef element in the generated ClusterRoleBinding resource. | ||
*/ | ||
@ConfigItem | ||
public String roleName; | ||
|
||
/** | ||
* 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.
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.
I am wondering if it makes sense to update an existing role binding with matching name.
Not sure, just wondering.