Skip to content
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

Add namespace override to community-operator #284

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions charts/community-operator/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{{/*
Construct the default namespace name.
*/}}
{{- define "community-operator.namespace" -}}
{{- default .Release.Namespace .Values.operator.namespace.name | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Expand the name of the chart.
*/}}
{{- define "community-operator.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "community-operator.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "community-operator.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "community-operator.labels" -}}
helm.sh/chart: {{ include "community-operator.chart" . }}
{{ include "community-operator.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "community-operator.selectorLabels" -}}
app.kubernetes.io/name: {{ include "community-operator.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "community-operator.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "community-operator.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
13 changes: 9 additions & 4 deletions charts/community-operator/templates/database_roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .Values.database.name }}
namespace: {{ if .Values.database.namespace }} {{ .Values.database.namespace }} {{ else }} {{ .Release.Namespace }} {{ end }}

namespace: {{ if .Values.database.namespace }} {{ .Values.database.namespace }} {{ else }} {{ include "community-operator.namespace" . }} {{ end }}
labels:
{{- include "community-operator.labels" . | nindent 4 }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ .Values.database.name }}
namespace: {{ if .Values.database.namespace }} {{ .Values.database.namespace }} {{ else }} {{ .Release.Namespace }} {{ end }}
namespace: {{ if .Values.database.namespace }} {{ .Values.database.namespace }} {{ else }} {{ include "community-operator.namespace" . }} {{ end }}
labels:
{{- include "community-operator.labels" . | nindent 4 }}
rules:
- apiGroups:
- ""
Expand All @@ -32,7 +35,9 @@ kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ .Values.database.name }}
namespace: {{ if .Values.database.namespace }} {{ .Values.database.namespace }} {{ else }} {{ .Release.Namespace }} {{ end }}
namespace: {{ if .Values.database.namespace }} {{ .Values.database.namespace }} {{ else }} {{ include "community-operator.namespace" . }} {{ end }}
labels:
{{- include "community-operator.labels" . | nindent 4 }}
subjects:
- kind: ServiceAccount
name: {{ .Values.database.name }}
Expand Down
4 changes: 3 additions & 1 deletion charts/community-operator/templates/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ metadata:
email: [email protected]
labels:
owner: mongodb
{{- include "community-operator.labels" . | nindent 4 }}
name: {{ .Values.operator.name }}
namespace: {{ .Release.Namespace }}
namespace: {{ include "community-operator.namespace" . }}
spec:
replicas: {{ .Values.operator.replicas }}
selector:
Expand All @@ -21,6 +22,7 @@ spec:
metadata:
labels:
name: {{ .Values.operator.name }}
{{- include "community-operator.labels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
Expand Down
8 changes: 8 additions & 0 deletions charts/community-operator/templates/operator_namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- if .Values.operator.namespace.create -}}
apiVersion: v1
kind: Namespace
metadata:
name: {{ include "community-operator.namespace" . }}
labels:
{{- include "community-operator.labels" . | nindent 4 }}
{{- end }}
15 changes: 10 additions & 5 deletions charts/community-operator/templates/operator_roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .Values.operator.name }}
namespace: {{ .Release.Namespace }}

namespace: {{ include "community-operator.namespace" . }}
labels:
{{- include "community-operator.labels" . | nindent 4 }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: {{ if eq (.Values.operator.watchNamespace | default "") "*" }} ClusterRole {{ else }} Role {{ end }}
Expand All @@ -13,8 +14,10 @@ metadata:
{{- if not (eq (.Values.operator.watchNamespace | default "*") "*") }}
namespace: {{ .Values.operator.watchNamespace }}
{{- else }}
namespace: {{ .Release.Namespace }}
namespace: {{ include "community-operator.namespace" . }}
{{- end }}
labels:
{{- include "community-operator.labels" . | nindent 4 }}
rules:
- apiGroups:
- ""
Expand Down Expand Up @@ -65,12 +68,14 @@ metadata:
{{- if ne (.Values.operator.watchNamespace | default "*") "*" }}
namespace: {{ .Values.operator.watchNamespace }}
{{- else }}
namespace: {{ .Release.Namespace }}
namespace: {{ include "community-operator.namespace" . }}
{{- end }}
labels:
{{- include "community-operator.labels" . | nindent 4 }}
subjects:
- kind: ServiceAccount
name: {{ .Values.operator.name }}
namespace: {{ .Release.Namespace }}
namespace: {{ include "community-operator.namespace" . }}
roleRef:
kind: {{ if eq (.Values.operator.watchNamespace | default "") "*" }} ClusterRole {{ else }} Role {{ end }}
name: {{ .Values.operator.name }}
Expand Down
8 changes: 8 additions & 0 deletions charts/community-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ operator:
# Deployment, ServiceAccount, Role etc.
name: mongodb-kubernetes-operator

# Controls the namespace that will be used or created. This is useful
# if the operator is going to be included as a subchart.
namespace:
# create set to true will cause the namespace to be created
create: false
# name set to the operator namespace. This only used if creating the namespace
name:

# Name of the operator image
operatorImageName: mongodb-kubernetes-operator

Expand Down