diff --git a/.gitignore b/.gitignore index 23789ac3..39e6026c 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,9 @@ bin *~ # control-plane kubeconfig file -*kubeconfig \ No newline at end of file +*kubeconfig + +# helm files +charts/postgreslet/Chart.lock +charts/postgreslet/charts/* + diff --git a/Makefile b/Makefile index a5523c37..d1436a74 100644 --- a/Makefile +++ b/Makefile @@ -145,3 +145,12 @@ create-postgres: delete-postgres: kubectl --kubeconfig kubeconfig delete -f config/samples/database_v1_postgres.yaml + +helm-clean: + rm -f charts/postgreslet/Chart.lock + rm -f charts/postgreslet/charts/* + +helm: + helm package charts/postgreslet-support/ + helm dependency build charts/postgreslet/ + helm package charts/postgreslet/ diff --git a/README.md b/README.md index f31defda..5379a1d0 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,37 @@ Delete _postgres_ on control-cluster and all the local corresponding resources. ```sh make delete-postgres ``` + +## Local Helm Development + +Delete and recreate all existing kind clusters (optional) + +```sh +kind delete cluster --name ctrl +kind delete cluster +kind create cluster --name ctrl --kubeconfig ./kubeconfig --config ctrl-cluster-config +kind create cluster +``` + +Build the charts + +```sh +make helm-clean +make helm +``` + +Prepare the control cluster + +```sh +helm --kubeconfig kubeconfig upgrade --install postgreslet-support postgreslet-support-0.1.0.tgz +kubectl --kubeconfig kubeconfig get postgres -A +``` + +Install the Postgreslet to the service cluster + +```sh +make kind-load-image +kubectl create namespace postgreslet-system +helm upgrade --install postgreslet postgreslet-0.1.0.tgz --namespace postgreslet-system --set-file controlplaneKubeconfig=kubeconfig --set image.tag=latest +kubectl get po -A --watch +``` diff --git a/charts/postgreslet-support/.helmignore b/charts/postgreslet-support/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/charts/postgreslet-support/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/charts/postgreslet-support/Chart.yaml b/charts/postgreslet-support/Chart.yaml new file mode 100644 index 00000000..b6499f72 --- /dev/null +++ b/charts/postgreslet-support/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: postgreslet-support +description: A Helm chart for installing support for postgres resources, which can be used by the Postgreslet + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "v0" diff --git a/charts/postgreslet-support/crds/postgres.yaml b/charts/postgreslet-support/crds/postgres.yaml new file mode 100644 index 00000000..e4722b1e --- /dev/null +++ b/charts/postgreslet-support/crds/postgres.yaml @@ -0,0 +1,136 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.2.5 + name: postgres.database.fits.cloud +spec: + group: database.fits.cloud + names: + kind: Postgres + listKind: PostgresList + plural: postgres + singular: postgres + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .spec.version + name: Version + type: string + - jsonPath: .status.description + name: Status + type: string + name: v1 + schema: + openAPIV3Schema: + description: Postgres is the Schema for the postgres API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: PostgresSpec defines the desired state of Postgres + properties: + accessList: + description: AccessList defines access restrictions + properties: + sourceRanges: + description: SourceRanges defines a list of prefixes in CIDR Notation e.g. 1.2.3.0/24 FIXME implement validation if source is a parsable CIDR + items: + type: string + type: array + type: object + backup: + description: 'todo: add default Backup parametes of the database backup' + properties: + retention: + description: Retention defines how many days a backup will persist + format: int32 + type: integer + s3BucketURL: + description: S3BucketURL defines the URL of the S3 bucket for backup + type: string + schedule: + description: Schedule defines how often a backup should be made, in cron format + type: string + type: object + description: + description: Description + type: string + maintenance: + description: 'todo: add default Maintenance defines automatic maintenance of the database' + properties: + timeWindow: + description: TimeWindow defines when the maintenance should happen + properties: + end: + format: date-time + type: string + start: + format: date-time + type: string + type: object + weekday: + description: Weekday defines when the operator is allowed to do maintenance + type: integer + type: object + numberOfInstances: + default: 1 + description: NumberOfInstances number of replicas + format: int32 + minimum: 1 + type: integer + partitionID: + description: PartitionID the partition where the database is created + type: string + projectID: + description: ProjectID metal project ID + type: string + size: + description: Size of the database + properties: + cpu: + description: CPU is in the format as pod.spec.resource.request.cpu + type: string + sharedBuffer: + description: SharedBuffer of the database + type: string + storageSize: + default: 1Gi + description: StorageSize the amount of Storage this database will get + pattern: ^[1-9][0-9]*Gi + type: string + type: object + tenant: + description: Tenant metal tenant + type: string + version: + default: "12" + description: Version is the version of Postgre-as-a-Service + enum: + - "12" + type: string + type: object + status: + description: PostgresStatus defines the observed state of Postgres + properties: + description: + description: 'INSERT ADDITIONAL STATUS FIELD - define observed state of cluster Important: Run "make" to regenerate code after modifying this file' + type: string + type: object + type: object + served: true + storage: true + subresources: + status: {} +status: + acceptedNames: + kind: "" + plural: "" + conditions: [] + storedVersions: [] diff --git a/charts/postgreslet/.helmignore b/charts/postgreslet/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/charts/postgreslet/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/charts/postgreslet/Chart.yaml b/charts/postgreslet/Chart.yaml new file mode 100644 index 00000000..7c2762d0 --- /dev/null +++ b/charts/postgreslet/Chart.yaml @@ -0,0 +1,30 @@ +apiVersion: v2 +name: postgreslet +description: A Helm chart for deploying the Postgres Controller aka Postgreslet + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "0.0.1" + +dependencies: + - name: postgreslet-support + version: 0.1.0 + # TODO define repo + repository: file://../postgreslet-support diff --git a/charts/postgreslet/templates/_helpers.tpl b/charts/postgreslet/templates/_helpers.tpl new file mode 100644 index 00000000..362e8b03 --- /dev/null +++ b/charts/postgreslet/templates/_helpers.tpl @@ -0,0 +1,73 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "postgreslet.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 "postgreslet.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 "postgreslet.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "postgreslet.labels" -}} +helm.sh/chart: {{ include "postgreslet.chart" . }} +{{ include "postgreslet.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "postgreslet.selectorLabels" -}} +app.kubernetes.io/name: {{ include "postgreslet.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the prefix to use in (cluster)role names and bindings +*/}} +{{- define "postgreslet.roleNamePrefix" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "postgreslet.fullname" .) .Values.serviceAccount.roleNamePrefix }} +{{- else }} +{{- default "postgreslet" .Values.serviceAccount.roleNamePrefix }} +{{- end }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "postgreslet.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "postgreslet.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/charts/postgreslet/templates/clusterrole.yaml b/charts/postgreslet/templates/clusterrole.yaml new file mode 100644 index 00000000..1099889e --- /dev/null +++ b/charts/postgreslet/templates/clusterrole.yaml @@ -0,0 +1,45 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: {{ include "postgreslet.roleNamePrefix" . }}-manager-role +rules: +- apiGroups: + - acid.zalan.do + resources: + - postgresqls + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - acid.zalan.do + resources: + - postgresqls/status + verbs: + - get + - list + - watch +- apiGroups: + - database.fits.cloud + resources: + - postgres + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - database.fits.cloud + resources: + - postgres/status + verbs: + - get + - patch + - update diff --git a/charts/postgreslet/templates/clusterrolebinding.yaml b/charts/postgreslet/templates/clusterrolebinding.yaml new file mode 100644 index 00000000..f81957ea --- /dev/null +++ b/charts/postgreslet/templates/clusterrolebinding.yaml @@ -0,0 +1,25 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: {{ include "postgreslet.roleNamePrefix" . }}-cluster-admin-rolebinding +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: cluster-admin +subjects: +- kind: ServiceAccount + name: {{ include "postgreslet.serviceAccountName" . }} + namespace: {{ .Release.Namespace }} +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: {{ include "postgreslet.roleNamePrefix" . }}-manager-rolebinding +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: {{ include "postgreslet.roleNamePrefix" . }}-manager-role +subjects: +- kind: ServiceAccount + name: {{ include "postgreslet.serviceAccountName" . }} + namespace: {{ .Release.Namespace }} diff --git a/charts/postgreslet/templates/configmap.yaml b/charts/postgreslet/templates/configmap.yaml new file mode 100644 index 00000000..26ca552a --- /dev/null +++ b/charts/postgreslet/templates/configmap.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +data: + ENABLE_LEADER_ELECTION: {{ .Values.postgreslet.enableLeaderElection | quote }} + METRICS_ADDR_CTRL_MGR: "0" + METRICS_ADDR_SVC_MGR: {{ .Values.postgreslet.metricsAddr | quote }} + PARTITION_ID: {{ .Values.postgreslet.partitionId | quote }} + TENANT: {{ .Values.postgreslet.tenant | quote }} +kind: ConfigMap +metadata: + name: {{ include "postgreslet.fullname" . }} +{{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 4 }} +{{- end }} +{{- with .Values.podAnnotations }} + {{- toYaml . | nindent 4 }} +{{- end }} + labels: + {{- include "postgreslet.labels" . | nindent 4 }} diff --git a/charts/postgreslet/templates/deployment.yaml b/charts/postgreslet/templates/deployment.yaml new file mode 100644 index 00000000..4b61d86c --- /dev/null +++ b/charts/postgreslet/templates/deployment.yaml @@ -0,0 +1,75 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + control-plane: controller-manager + {{- include "postgreslet.labels" . | nindent 4 }} + name: {{ include "postgreslet.fullname" . }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + control-plane: controller-manager + {{- include "postgreslet.selectorLabels" . | nindent 6 }} + template: + metadata: + annotations: + checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }} + checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }} + {{- with .Values.podAnnotations }} + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + control-plane: controller-manager + {{- include "postgreslet.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "postgreslet.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + command: + - /manager + args: + - --metrics-addr-svc-mgr=$(METRICS_ADDR_SVC_MGR) + - --metrics-addr-ctrl-mgr=$(METRICS_ADDR_CTRL_MGR) + - --enable-leader-election=$(ENABLE_LEADER_ELECTION) + - --partition-id=$(PARTITION_ID) + - --tenant=$(TENANT) + envFrom: + - configMapRef: + name: {{ include "postgreslet.fullname" . }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - mountPath: /var/run/secrets/postgreslet + name: controlplane-kubeconfig + readOnly: true + terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} + volumes: + - name: controlplane-kubeconfig + secret: + items: + - key: controlplane-kubeconfig + path: kube/config + secretName: {{ include "postgreslet.fullname" . }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/charts/postgreslet/templates/role.yaml b/charts/postgreslet/templates/role.yaml new file mode 100644 index 00000000..21a36497 --- /dev/null +++ b/charts/postgreslet/templates/role.yaml @@ -0,0 +1,31 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: {{ include "postgreslet.roleNamePrefix" . }}-leader-election-role +rules: +- apiGroups: + - "" + resources: + - configmaps + verbs: + - get + - list + - watch + - create + - update + - patch + - delete +- apiGroups: + - "" + resources: + - configmaps/status + verbs: + - get + - update + - patch +- apiGroups: + - "" + resources: + - events + verbs: + - create diff --git a/charts/postgreslet/templates/rolebinding.yaml b/charts/postgreslet/templates/rolebinding.yaml new file mode 100644 index 00000000..8468b09e --- /dev/null +++ b/charts/postgreslet/templates/rolebinding.yaml @@ -0,0 +1,12 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: {{ include "postgreslet.roleNamePrefix" . }}-leader-election-rolebinding +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: {{ include "postgreslet.roleNamePrefix" . }}-leader-election-role +subjects: +- kind: ServiceAccount + name: {{ include "postgreslet.serviceAccountName" . }} + namespace: {{ .Release.Namespace }} \ No newline at end of file diff --git a/charts/postgreslet/templates/secret.yaml b/charts/postgreslet/templates/secret.yaml new file mode 100644 index 00000000..26680a4f --- /dev/null +++ b/charts/postgreslet/templates/secret.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +data: + controlplane-kubeconfig: {{ b64enc .Values.controlplaneKubeconfig }} +kind: Secret +metadata: + name: {{ include "postgreslet.fullname" . }} +{{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 4 }} +{{- end }} + labels: + {{- include "postgreslet.labels" . | nindent 4 }} diff --git a/charts/postgreslet/templates/serviceaccount.yaml b/charts/postgreslet/templates/serviceaccount.yaml new file mode 100644 index 00000000..1b05e670 --- /dev/null +++ b/charts/postgreslet/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "postgreslet.serviceAccountName" . }} + labels: + {{- include "postgreslet.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/charts/postgreslet/values.yaml b/charts/postgreslet/values.yaml new file mode 100644 index 00000000..4b26b3de --- /dev/null +++ b/charts/postgreslet/values.yaml @@ -0,0 +1,68 @@ +# Default values for postgreslet. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: r.metal-stack.io/extensions/postgreslet + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + # A prefix to use in all the (Cluster)Roles that are being created for this service account. + # If not set and create is true, a name is generated using the fullname template + roleNamePrefix: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +resources: + limits: + cpu: 100m + memory: 30Mi + requests: + cpu: 100m + memory: 20Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +controlplaneKubeconfig: "" + +terminationGracePeriodSeconds: 10 + +postgreslet: + # enableLeaderElection specifies weather leader election should be performed + enableLeaderElection: false + # partitionId specifies which partition this postgreslet is responsable for, postgres resources from other partitions will be ignored + partitionId: sample-partition + # tenant specifies which tenant this postgreslet is responsable for, postgres resources from other tenants will be ignored + tenant: sample-tenant + # metricsAddr defines the listen address of the metrics endpoint + metricsAddr: ":8080" \ No newline at end of file