diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 4147fb8..19c75ec 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -3,12 +3,16 @@ name: build on: pull_request: branches: - - "*" + - "*" + paths-ignore: + - config/helm-chart/** push: branches: - - main + - main tags: - - "v*" + - "v*" + paths-ignore: + - config/helm-chart/** jobs: build: diff --git a/.github/workflows/chart-release.yaml b/.github/workflows/chart-release.yaml new file mode 100644 index 0000000..21a87f5 --- /dev/null +++ b/.github/workflows/chart-release.yaml @@ -0,0 +1,41 @@ +name: "Publish charts to repo" +on: + push: + branches: + - main + tags: + - "chart-*" + paths: + - "config/helm-chart/**" + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + pages: write + contents: write + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Configure Git + run: | + git config user.name "$GITHUB_ACTOR" + git config user.email "$GITHUB_ACTOR@users.noreply.github.com" + + - name: Install Helm + uses: azure/setup-helm@v3.5 + + - name: add repos + run: | + helm repo add bitnami https://charts.bitnami.com/bitnami + helm repo add bitnami-pre2022 https://raw.githubusercontent.com/bitnami/charts/eb5f9a9513d987b519f0ecd732e7031241c50328/bitnami + + - name: Run chart-releaser + uses: helm/chart-releaser-action@v1.5.0 + with: + charts_dir: charts + env: + CR_TOKEN: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/chart-test.yaml b/.github/workflows/chart-test.yaml new file mode 100644 index 0000000..67f759d --- /dev/null +++ b/.github/workflows/chart-test.yaml @@ -0,0 +1,59 @@ +name: "Test using kind and chart-testing tool" +on: + pull_request: + paths: + - "config/helm-chart/**" + +jobs: + test: + strategy: + fail-fast: false + matrix: + kubernetesVersion: [ "v1.24.15", "v1.28.0" ] + runs-on: ubuntu-latest + if: github.ref != 'refs/heads/master' + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Shellcheck + uses: ludeeus/action-shellcheck@2.0.0 + + - name: Set up Helm + uses: azure/setup-helm@v3.5 + + - uses: actions/setup-python@v4 + with: + python-version: 3.7 + + - name: Set up chart-testing + uses: helm/chart-testing-action@v2.4.0 + + - name: Run chart-testing (list-changed) + id: list-changed + run: | + changed=$(ct list-changed --config ct-config.yaml) + if [[ -n "$changed" ]]; then + echo "::set-output name=changed::true" + fi + + - name: Run chart-testing (lint) + run: ct lint --config ct-config.yaml + + - name: Install kind + if: steps.list-changed.outputs.changed == 'true' + uses: helm/kind-action@v1.8.0 + with: + node_image: "kindest/node:${{ matrix.kubernetesVersion }}" + config: kind-config.yaml + - name: Verify kind + if: steps.list-changed.outputs.changed == 'true' + run: | + kubectl cluster-info + kubectl get nodes -o wide + kubectl get pods -n kube-system + + - name: Run chart-testing (install) + run: ct install --config ct-config.yaml \ No newline at end of file diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yaml similarity index 100% rename from .github/workflows/codeql.yml rename to .github/workflows/codeql.yaml diff --git a/config/helm-chart/.gitignore b/config/helm-chart/.gitignore new file mode 100644 index 0000000..ebf1d3d --- /dev/null +++ b/config/helm-chart/.gitignore @@ -0,0 +1 @@ +charts diff --git a/config/helm-chart/flyway-operator/.helmignore b/config/helm-chart/flyway-operator/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/config/helm-chart/flyway-operator/.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/config/helm-chart/flyway-operator/Chart.lock b/config/helm-chart/flyway-operator/Chart.lock new file mode 100644 index 0000000..792dfd2 --- /dev/null +++ b/config/helm-chart/flyway-operator/Chart.lock @@ -0,0 +1,6 @@ +dependencies: +- name: common + repository: oci://registry-1.docker.io/bitnamicharts + version: 2.11.1 +digest: sha256:ead8f26c76a9ec082f23629a358e8efd8f88d87aaed734bf41febcb8a7bc5d4c +generated: "2023-09-17T21:09:19.356982+02:00" diff --git a/config/helm-chart/flyway-operator/Chart.yaml b/config/helm-chart/flyway-operator/Chart.yaml new file mode 100644 index 0000000..668b01c --- /dev/null +++ b/config/helm-chart/flyway-operator/Chart.yaml @@ -0,0 +1,16 @@ +apiVersion: v2 +name: flyway-operator +description: Flyway Operator - an operator to do declarative database schema migration. +type: application +icon: https://pbs.twimg.com/profile_images/1339525060999192576/9ltj0fpE_400x400.png +version: 0.1.0 +appVersion: 0.1.6 +maintainers: +- name: davidkarlsen + email: david@davidkarlsen.com +dependencies: +- name: common + repository: oci://registry-1.docker.io/bitnamicharts + tags: + - bitnami-common + version: 2.x.x diff --git a/config/helm-chart/flyway-operator/crds/flyway.davidkarlsen.com_migrations.yaml b/config/helm-chart/flyway-operator/crds/flyway.davidkarlsen.com_migrations.yaml new file mode 100644 index 0000000..901d346 --- /dev/null +++ b/config/helm-chart/flyway-operator/crds/flyway.davidkarlsen.com_migrations.yaml @@ -0,0 +1,197 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.13.0 + name: migrations.flyway.davidkarlsen.com +spec: + group: flyway.davidkarlsen.com + names: + kind: Migration + listKind: MigrationList + plural: migrations + singular: migration + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: Migration is the Schema for the migrations 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: MigrationSpec defines the desired state of Migration + properties: + database: + description: settings for database connection + properties: + credentials: + description: reference to a secret containing the password for + connecting to database + properties: + key: + description: The key of the secret to select from. Must be + a valid secret key. + type: string + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, uid?' + type: string + optional: + description: Specify whether the Secret or its key must be + defined + type: boolean + required: + - key + type: object + x-kubernetes-map-type: atomic + jdbcUrl: + description: the jdbcUrl to connect to database + pattern: ^jdbc:.* + type: string + username: + description: username for connecting to database + type: string + required: + - credentials + - jdbcUrl + - username + type: object + migrationSource: + description: settings defining the SQL migrations + properties: + ImagePullSecret: + description: Optional. Image-pull secret to pull the migration + source + items: + description: LocalObjectReference contains enough information + to let you locate the referenced object inside the same namespace. + properties: + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, uid?' + type: string + type: object + x-kubernetes-map-type: atomic + type: array + encoding: + default: UTF-8 + description: The encoding of the SQL-files. + type: string + flywayImage: + description: Reference to the flyway image to use. + type: string + imageRef: + description: Reference to the image holding the SQLs to migrate + type: string + path: + default: /sql + description: Path within the image to the SQLs for flyway + type: string + placeholders: + additionalProperties: + type: string + description: 'Flyway placeholders, see: https://documentation.red-gate.com/fd/placeholders-configuration-184127475.html + These will be injected as env-vars with the required prefix.' + type: object + required: + - encoding + - imageRef + - path + type: object + required: + - database + - migrationSource + type: object + status: + description: MigrationStatus defines the observed state of Migration + properties: + conditions: + items: + description: "Condition contains details for one aspect of the current + state of this API Resource. --- This struct is intended for direct + use as an array at the field path .status.conditions. For example, + \n type FooStatus struct{ // Represents the observations of a + foo's current state. // Known .status.conditions.type are: \"Available\", + \"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge + // +listType=map // +listMapKey=type Conditions []metav1.Condition + `json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\" + protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }" + properties: + lastTransitionTime: + description: lastTransitionTime is the last time the condition + transitioned from one status to another. This should be when + the underlying condition changed. If that is not known, then + using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: message is a human readable message indicating + details about the transition. This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: observedGeneration represents the .metadata.generation + that the condition was set based upon. For instance, if .metadata.generation + is currently 12, but the .status.conditions[x].observedGeneration + is 9, the condition is out of date with respect to the current + state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: reason contains a programmatic identifier indicating + the reason for the condition's last transition. Producers + of specific condition types may define expected values and + meanings for this field, and whether the values are considered + a guaranteed API. The value should be a CamelCase string. + This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. + --- Many .condition.type values are consistent across resources + like Available, but because arbitrary conditions can be useful + (see .node.status.conditions), the ability to deconflict is + important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt) + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + x-kubernetes-list-map-keys: + - type + x-kubernetes-list-type: map + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/config/helm-chart/flyway-operator/templates/NOTES.txt b/config/helm-chart/flyway-operator/templates/NOTES.txt new file mode 100644 index 0000000..e69de29 diff --git a/config/helm-chart/flyway-operator/templates/_helpers.tpl b/config/helm-chart/flyway-operator/templates/_helpers.tpl new file mode 100644 index 0000000..9fe7741 --- /dev/null +++ b/config/helm-chart/flyway-operator/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "flyway-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 "flyway-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 "flyway-operator.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "flyway-operator.labels" -}} +helm.sh/chart: {{ include "flyway-operator.chart" . }} +{{ include "flyway-operator.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "flyway-operator.selectorLabels" -}} +app.kubernetes.io/name: {{ include "flyway-operator.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "flyway-operator.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "flyway-operator.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/config/helm-chart/flyway-operator/templates/deployment.yaml b/config/helm-chart/flyway-operator/templates/deployment.yaml new file mode 100644 index 0000000..239414a --- /dev/null +++ b/config/helm-chart/flyway-operator/templates/deployment.yaml @@ -0,0 +1,56 @@ +apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }} +kind: Deployment +metadata: + name: {{ include "common.names.fullname" . }} + labels: {{- include "common.labels.standard" . | nindent 4 }} +spec: + replicas: 1 + selector: + matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: {{- include "common.labels.matchLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "flyway-operator.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: {{ include "common.images.image" ( dict "imageRoot" .Values.image "global" .Values.global) }} + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 8081 + protocol: TCP + livenessProbe: + httpGet: + path: /healthz + port: http + readinessProbe: + httpGet: + path: /readyz + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- 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/config/helm-chart/flyway-operator/templates/migration_editor_role.yaml b/config/helm-chart/flyway-operator/templates/migration_editor_role.yaml new file mode 100644 index 0000000..add3f5a --- /dev/null +++ b/config/helm-chart/flyway-operator/templates/migration_editor_role.yaml @@ -0,0 +1,25 @@ +# permissions for end users to edit migrations. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: {{- include "common.labels.standard" . | nindent 4 }} + name: migration-editor-role +rules: +- apiGroups: + - flyway.davidkarlsen.com + resources: + - migrations + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - flyway.davidkarlsen.com + resources: + - migrations/status + verbs: + - get diff --git a/config/helm-chart/flyway-operator/templates/migration_viewer_role.yaml b/config/helm-chart/flyway-operator/templates/migration_viewer_role.yaml new file mode 100644 index 0000000..74eb4b7 --- /dev/null +++ b/config/helm-chart/flyway-operator/templates/migration_viewer_role.yaml @@ -0,0 +1,21 @@ +# permissions for end users to view migrations. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: {{- include "common.labels.standard" . | nindent 4 }} + name: migration-viewer-role +rules: +- apiGroups: + - flyway.davidkarlsen.com + resources: + - migrations + verbs: + - get + - list + - watch +- apiGroups: + - flyway.davidkarlsen.com + resources: + - migrations/status + verbs: + - get diff --git a/config/helm-chart/flyway-operator/templates/role.yaml b/config/helm-chart/flyway-operator/templates/role.yaml new file mode 100644 index 0000000..d1bf1fb --- /dev/null +++ b/config/helm-chart/flyway-operator/templates/role.yaml @@ -0,0 +1,51 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: {{ include "common.names.fullname" . }} + labels: {{- include "common.labels.standard" . | nindent 4 }} +rules: +- apiGroups: + - batch + resources: + - jobs + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - "" + resources: + - events + verbs: + - create + - patch +- apiGroups: + - flyway.davidkarlsen.com + resources: + - migrations + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - flyway.davidkarlsen.com + resources: + - migrations/finalizers + verbs: + - update +- apiGroups: + - flyway.davidkarlsen.com + resources: + - migrations/status + verbs: + - get + - patch + - update diff --git a/config/helm-chart/flyway-operator/templates/role_binding.yaml b/config/helm-chart/flyway-operator/templates/role_binding.yaml new file mode 100644 index 0000000..426aa92 --- /dev/null +++ b/config/helm-chart/flyway-operator/templates/role_binding.yaml @@ -0,0 +1,13 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: {{ include "common.names.fullname" . }} + labels: {{- include "common.labels.standard" . | nindent 4 }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: {{ include "common.names.fullname" . }} +subjects: +- kind: ServiceAccount + name: {{ include "common.names.fullname" . }} + namespace: {{ .Release.Namespace }} diff --git a/config/helm-chart/flyway-operator/templates/service.yaml b/config/helm-chart/flyway-operator/templates/service.yaml new file mode 100644 index 0000000..55eafe7 --- /dev/null +++ b/config/helm-chart/flyway-operator/templates/service.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "common.names.fullname" . }} + labels: {{- include "common.labels.standard" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "flyway-operator.selectorLabels" . | nindent 4 }} diff --git a/config/helm-chart/flyway-operator/templates/serviceaccount.yaml b/config/helm-chart/flyway-operator/templates/serviceaccount.yaml new file mode 100644 index 0000000..284f7c6 --- /dev/null +++ b/config/helm-chart/flyway-operator/templates/serviceaccount.yaml @@ -0,0 +1,11 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "common.names.fullname" . }} + labels: {{- include "common.labels.standard" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/config/helm-chart/flyway-operator/values.yaml b/config/helm-chart/flyway-operator/values.yaml new file mode 100644 index 0000000..cf29a5b --- /dev/null +++ b/config/helm-chart/flyway-operator/values.yaml @@ -0,0 +1,55 @@ +# Default values for flyway-operator. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + registry: ghcr.io + repository: davidkarlsen/flyway-operator + pullPolicy: IfNotPresent + tag: 0.1.6 + +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: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +resources: + limits: + cpu: 500m + memory: 128Mi + requests: + cpu: 10m + memory: 64Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/ct-config.yaml b/ct-config.yaml new file mode 100644 index 0000000..9cb70e6 --- /dev/null +++ b/ct-config.yaml @@ -0,0 +1,7 @@ +remote: origin +target-branch: main +chart-dirs: +- config/helm-chart +chart-repos: +- bitnami=https://charts.bitnami.com/bitnami +- bitnami-pre2022=https://raw.githubusercontent.com/bitnami/charts/eb5f9a9513d987b519f0ecd732e7031241c50328/bitnami diff --git a/kind-config.yaml b/kind-config.yaml new file mode 100644 index 0000000..0fe29e7 --- /dev/null +++ b/kind-config.yaml @@ -0,0 +1,5 @@ +kind: Cluster +apiVersion: kind.x-k8s.io/v1alpha4 +nodes: +- role: control-plane +- role: worker \ No newline at end of file