-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enable role binding * Update fiaas_deploy_daemon/config.py Co-authored-by: Eloy Maillo <[email protected]> * Update fiaas_deploy_daemon/specs/models.py Co-authored-by: Eloy Maillo <[email protected]> * Change role binding deployer implementation * Ack review comments * Rename to check_if_matches * Add tests when role binding exists * Fix codestyle * Update fiaas_deploy_daemon/deployer/kubernetes/role_binding.py Co-authored-by: Øyvind Ingebrigtsen Øvergaard <[email protected]> * Add test for clean * Fix style * Update helm/fiaas-deploy-daemon/templates/custom_rolebinding.yaml Co-authored-by: Øyvind Ingebrigtsen Øvergaard <[email protected]> * Improve operator guide and add empty lists in the values example * Update operator_guide.md --------- Co-authored-by: Eloy Maillo <[email protected]> Co-authored-by: herodes1991 <[email protected]> Co-authored-by: Øyvind Ingebrigtsen Øvergaard <[email protected]>
- Loading branch information
1 parent
016aaea
commit a793765
Showing
25 changed files
with
468 additions
and
12 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
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
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
100 changes: 100 additions & 0 deletions
100
fiaas_deploy_daemon/deployer/kubernetes/role_binding.py
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,100 @@ | ||
import logging | ||
|
||
from k8s.models.common import ObjectMeta | ||
from k8s.models.role_binding import RoleBinding, RoleRef, Subject | ||
from fiaas_deploy_daemon.specs.models import AppSpec | ||
from fiaas_deploy_daemon.deployer.kubernetes.owner_references import OwnerReferences | ||
from fiaas_deploy_daemon.tools import merge_dicts | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class RoleBindingDeployer: | ||
def __init__(self, config, owner_references): | ||
self._owner_references: OwnerReferences = owner_references | ||
self._list_of_roles = config.list_of_roles | ||
self._list_of_cluster_roles = config.list_of_cluster_roles | ||
|
||
def deploy(self, app_spec: AppSpec, labels): | ||
custom_annotations = {} | ||
custom_labels = labels | ||
custom_labels = merge_dicts(app_spec.labels.role_binding, custom_labels) | ||
custom_annotations = merge_dicts(app_spec.annotations.role_binding, custom_annotations) | ||
# Getting list of rolebindings with the label app=app_name | ||
role_bindings = RoleBinding.find(name=app_spec.name, namespace=app_spec.namespace) | ||
self._clean_not_needed_role_bindings(role_bindings) | ||
self._update_or_create_role_bindings(app_spec, self._list_of_roles, "Role", custom_annotations, custom_labels, role_bindings) | ||
self._update_or_create_role_bindings(app_spec, self._list_of_cluster_roles, "ClusterRole", custom_annotations, custom_labels, | ||
role_bindings) | ||
|
||
def _update_or_create_role_bindings(self, app_spec: AppSpec, roles_list, role_kind, custom_annotations, custom_labels, | ||
role_bindings): | ||
namespace = app_spec.namespace | ||
service_account_name = app_spec.name | ||
for role_name in roles_list: | ||
role_binding = self._find_role_in_role_bindings(role_kind, role_name, role_bindings) | ||
if role_binding: | ||
if self._owned_by_fiaas(role_binding): | ||
LOG.info("Updating RoleBinding %s", role_binding.metadata.name) | ||
generate = False | ||
role_binding_name = role_binding.metadata.name | ||
else: | ||
LOG.info( | ||
"Aborting the creation of a roleBinding for Application: %s with %s: %s, role is already bound by %s", | ||
app_spec.name, | ||
role_kind, | ||
role_name, | ||
role_binding.metadata.name | ||
) | ||
continue | ||
else: | ||
role_binding = RoleBinding() | ||
role_binding_name = f"{app_spec.name}-" | ||
LOG.info("Creating a new rolebinding for %s, the name will be generated by K8s with the prefix %s", | ||
app_spec.name, role_binding_name) | ||
generate = True | ||
|
||
self._deploy_role_binding(app_spec, role_kind, custom_annotations, custom_labels, namespace, | ||
service_account_name, role_name, role_binding, role_binding_name, generate) | ||
|
||
def _deploy_role_binding(self, app_spec, role_kind, custom_annotations, custom_labels, namespace, | ||
service_account_name, role_name, role_binding, role_binding_name, generate): | ||
if generate: | ||
role_binding.metadata = ObjectMeta(generateName=role_binding_name, namespace=namespace, | ||
labels=custom_labels, annotations=custom_annotations) | ||
else: | ||
role_binding.metadata = ObjectMeta(name=role_binding_name, namespace=namespace, labels=custom_labels, | ||
annotations=custom_annotations) | ||
|
||
role_ref = RoleRef(kind=role_kind, apiGroup="rbac.authorization.k8s.io", name=role_name) | ||
subject = Subject(kind="ServiceAccount", name=service_account_name, namespace=namespace) | ||
role_binding.roleRef = role_ref | ||
role_binding.subjects = [subject] | ||
self._owner_references.apply(role_binding, app_spec) | ||
role_binding.save() | ||
|
||
def _find_role_in_role_bindings(self, role_kind, role_name, role_bindings: list[RoleBinding]): | ||
for role_binding in role_bindings: | ||
if role_binding.roleRef.kind == role_kind and role_binding.roleRef.name == role_name: | ||
return role_binding | ||
return None | ||
|
||
def _clean_not_needed_role_bindings(self, role_bindings: list[RoleBinding]): | ||
role_bindings_aux = role_bindings.copy() | ||
for role_binding in role_bindings_aux: | ||
if not self._should_bind(role_binding): | ||
if self._owned_by_fiaas(role_binding): | ||
LOG.info("Deleting RoleBinding %s", role_binding.metadata.name) | ||
RoleBinding.delete(role_binding.metadata.name, role_binding.metadata.namespace) | ||
role_bindings.remove(role_binding) | ||
|
||
def _owned_by_fiaas(self, role_binding): | ||
return any( | ||
ref.apiVersion == "fiaas.schibsted.io/v1" and ref.kind == "Application" | ||
for ref in role_binding.metadata.ownerReferences | ||
) | ||
|
||
# Check if matches the role_binding with any role or clusterRole in the list_of_roles or list_of_cluster_roles | ||
def _should_bind(self, role_binding): | ||
return (role_binding.roleRef.kind == "Role" and role_binding.roleRef.name in self._list_of_roles) \ | ||
or (role_binding.roleRef.kind == "ClusterRole" and role_binding.roleRef.name in self._list_of_cluster_roles) |
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 |
---|---|---|
|
@@ -110,6 +110,7 @@ def version(self): | |
"pod", | ||
"status", | ||
"pod_disruption_budget", | ||
"role_binding", | ||
], | ||
) | ||
|
||
|
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
95 changes: 95 additions & 0 deletions
95
helm/fiaas-deploy-daemon/templates/custom_rolebinding.yaml
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,95 @@ | ||
{{- if .Values.rbac.roleBinding.roles -}} | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: Role | ||
metadata: | ||
name: {{ .Values.name }}-role-grantor | ||
labels: | ||
{{ include "fiaas-deploy-daemon.labels" . | indent 4 }} | ||
{{ include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.rbac.roleBinding.labels | indent 4 }} | ||
{{- if or .Values.annotations.global .Values.rbac.roleBinding.annotations }} | ||
annotations: | ||
{{- include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.annotations.global | indent 4 }} | ||
{{- include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.rbac.roleBinding.annotations | indent 4 }} | ||
{{- end }} | ||
rules: | ||
- apiGroups: | ||
- rbac.authorization.k8s.io | ||
resources: | ||
- roles | ||
verbs: | ||
- bind | ||
resourceNames: | ||
{{- range $role := .Values.rbac.roleBinding.roles }} | ||
- {{ $role }} | ||
{{- end}} | ||
|
||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: RoleBinding | ||
metadata: | ||
name: {{ .Values.name }}-rb-role-grantor | ||
labels: | ||
{{ include "fiaas-deploy-daemon.labels" . | indent 4 }} | ||
{{ include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.rbac.roleBinding.labels | indent 4 }} | ||
{{- if or .Values.annotations.global .Values.rbac.roleBinding.annotations }} | ||
annotations: | ||
{{- include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.annotations.global | indent 4 }} | ||
{{- include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.rbac.roleBinding.annotations | indent 4 }} | ||
{{- end }} | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: Role | ||
name: {{ .Values.name }}-role-grantor | ||
subjects: | ||
- kind: ServiceAccount | ||
name: {{ .Values.name }} | ||
namespace: {{ .Release.Namespace }} | ||
{{- end }} | ||
|
||
{{- if .Values.rbac.roleBinding.clusterRoles }} | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: Role | ||
metadata: | ||
name: {{ .Values.name }}-clusterrole-grantor | ||
labels: | ||
{{ include "fiaas-deploy-daemon.labels" . | indent 4 }} | ||
{{ include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.rbac.roleBinding.labels | indent 4 }} | ||
{{- if or .Values.annotations.global .Values.rbac.roleBinding.annotations }} | ||
annotations: | ||
{{- include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.annotations.global | indent 4 }} | ||
{{- include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.rbac.roleBinding.annotations | indent 4 }} | ||
{{- end }} | ||
rules: | ||
- apiGroups: | ||
- rbac.authorization.k8s.io | ||
resources: | ||
- clusterroles | ||
verbs: | ||
- bind | ||
resourceNames: | ||
{{- range $clusterRole := .Values.rbac.roleBinding.clusterRoles }} | ||
- {{ $clusterRole }} | ||
{{- end}} | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: RoleBinding | ||
metadata: | ||
name: {{ .Values.name }}-rb-clusterrole-grantor | ||
labels: | ||
{{ include "fiaas-deploy-daemon.labels" . | indent 4 }} | ||
{{ include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.rbac.roleBinding.labels | indent 4 }} | ||
{{- if or .Values.annotations.global .Values.rbac.roleBinding.annotations }} | ||
annotations: | ||
{{- include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.annotations.global | indent 4 }} | ||
{{- include "fiaas-deploy-daemon.labelsOrAnnotations" .Values.rbac.roleBinding.annotations | indent 4 }} | ||
{{- end }} | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: Role | ||
name: {{ .Values.name }}-clusterrole-grantor | ||
subjects: | ||
- kind: ServiceAccount | ||
name: {{ .Values.name }} | ||
namespace: {{ .Release.Namespace }} | ||
{{- end }} |
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.