From 251a25aef268f99cb62e00ff14fa82bc5166d5e5 Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Wed, 24 Jan 2024 14:49:44 +0100 Subject: [PATCH] Add cakephp-mysql-persistent helm chart Signed-off-by: Petr "Stone" Hracek --- charts/redhat/cakephp-mysql-persistent/OWNERS | 9 ++ .../cakephp-mysql-persistent/src/Chart.yaml | 15 +++ .../src/templates/buildconfig.yaml | 39 +++++++ .../src/templates/deployment.yaml | 98 +++++++++++++++++ .../src/templates/deployment_mysql.yaml | 78 ++++++++++++++ .../src/templates/imagestream.yaml | 9 ++ .../src/templates/persistentvolumeclaim.yaml | 13 +++ .../src/templates/route.yaml | 14 +++ .../src/templates/secret.yaml | 12 +++ .../src/templates/service.yaml | 35 ++++++ .../src/values.schema.json | 102 ++++++++++++++++++ .../cakephp-mysql-persistent/src/values.yaml | 21 ++++ .../src/values.schema.json | 5 + tests/test_php_cakephp_mysql_template.py | 58 ++++++++++ 14 files changed, 508 insertions(+) create mode 100644 charts/redhat/cakephp-mysql-persistent/OWNERS create mode 100644 charts/redhat/cakephp-mysql-persistent/src/Chart.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/templates/buildconfig.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/templates/deployment.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/templates/deployment_mysql.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/templates/imagestream.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/templates/persistentvolumeclaim.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/templates/route.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/templates/secret.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/templates/service.yaml create mode 100644 charts/redhat/cakephp-mysql-persistent/src/values.schema.json create mode 100644 charts/redhat/cakephp-mysql-persistent/src/values.yaml create mode 100644 tests/test_php_cakephp_mysql_template.py diff --git a/charts/redhat/cakephp-mysql-persistent/OWNERS b/charts/redhat/cakephp-mysql-persistent/OWNERS new file mode 100644 index 0000000..145c592 --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/OWNERS @@ -0,0 +1,9 @@ +chart: + name: cakephp-mysql-persistent + description: This is the Red Hat quickstart CakePHP application with MySQL database helm chart +publicPgpKey: null +users: + - githubUsername: phracek +vendor: + label: redhat + name: Red Hat diff --git a/charts/redhat/cakephp-mysql-persistent/src/Chart.yaml b/charts/redhat/cakephp-mysql-persistent/src/Chart.yaml new file mode 100644 index 0000000..9bda6bd --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/Chart.yaml @@ -0,0 +1,15 @@ +description: This content is experimental, do not use it in production. An example CakePHP application with a MySQL database. For more information + about using this template, including OpenShift considerations, see https://github.com/sclorg/cakephp-ex/blob/master/README.md. +name: cakephp-mysql-persistent +tags: quickstart,php,cakephp +version: 0.0.1 + +kubeVersion: '>=1.20.0' +annotations: + charts.openshift.io/name: Red Hat Apache CakePHP application with MySQL database (experimental) + charts.openshift.io/provider: Red Hat + charts.openshift.io/providerType: redhat +apiVersion: v2 +appVersion: 0.0.1 +sources: + - https://github.com/sclorg/helm-charts diff --git a/charts/redhat/cakephp-mysql-persistent/src/templates/buildconfig.yaml b/charts/redhat/cakephp-mysql-persistent/src/templates/buildconfig.yaml new file mode 100644 index 0000000..e06fdc5 --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/templates/buildconfig.yaml @@ -0,0 +1,39 @@ +apiVersion: build.openshift.io/v1 +kind: BuildConfig +metadata: + annotations: + description: Defines how to build the application + template.alpha.openshift.io/wait-for-ready: "true" + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.name }} +spec: + output: + to: + kind: ImageStreamTag + name: {{ .Values.name }}:latest + postCommit: + script: ./vendor/bin/phpunit + source: + contextDir: {{ .Values.context_dir }} + git: + ref: {{ .Values.source_repository_ref }} + uri: {{ .Values.source_repository_url }} + type: Git + strategy: + sourceStrategy: + env: + - name: COMPOSER_MIRROR + value: {{ .Values.composer_mirror }} + from: + kind: ImageStreamTag + name: php:{{ .Values.php_version }} + namespace: {{ .Values.namespace }} + type: Source + triggers: + - type: ImageChange + - type: ConfigChange + - github: + secret: {{ .Values.github_webhook_secret }} + type: GitHub diff --git a/charts/redhat/cakephp-mysql-persistent/src/templates/deployment.yaml b/charts/redhat/cakephp-mysql-persistent/src/templates/deployment.yaml new file mode 100644 index 0000000..add2120 --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/templates/deployment.yaml @@ -0,0 +1,98 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + description: Defines how to deploy the application server + image.openshift.io/triggers: |- + [ + { + "from": { + "kind": "ImageStreamTag", + "name": "{{ .Values.name }}:latest" + }, + "fieldPath": "spec.template.spec.containers[0].image" + } + ] + template.alpha.openshift.io/wait-for-ready: "true" + service.alpha.openshift.io/dependencies: |- + [ + { + "name": "{{ .Values.database_service_name }}", + "kind": "Deployment" + } + ] + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.name }} +spec: + replicas: 1 + selector: + matchLabels: + name: {{ .Values.name }} + strategy: + recreateParams: + pre: + execNewPod: + command: + - ./migrate-database.sh + containerName: cakephp-mysql + failurePolicy: Retry + type: Recreate + template: + metadata: + labels: + name: {{ .Values.name }} + name: {{ .Values.name }} + spec: + containers: + - env: + - name: DATABASE_SERVICE_NAME + value: "{{ .Values.database_service_name }}" + - name: PHP_CLEAR_ENV + value: "OFF" + - name: DATABASE_NAME + value: "{{ .Values.database_name }}" + - name: DATABASE_USER + valueFrom: + secretKeyRef: + key: database-user + name: {{ .Values.name }} + - name: DATABASE_PASSWORD + valueFrom: + secretKeyRef: + key: database-password + name: {{ .Values.name }} + - name: CAKEPHP_SECRET_TOKEN + valueFrom: + secretKeyRef: + key: cakephp-secret-token + name: {{ .Values.name }} + - name: CAKEPHP_SECURITY_SALT + valueFrom: + secretKeyRef: + key: cakephp-security-salt + name: {{ .Values.name }} + - name: OPCACHE_REVALIDATE_FREQ + value: "{{ .Values.opcache_revalidate_freq }}" + image: " " + livenessProbe: + httpGet: + path: /health.php + port: 8080 + initialDelaySeconds: 30 + periodSeconds: 60 + timeoutSeconds: 3 + name: cakephp-mysql + ports: + - containerPort: 8080 + readinessProbe: + httpGet: + path: /health.php + port: 8080 + initialDelaySeconds: 3 + periodSeconds: 60 + timeoutSeconds: 3 + resources: + limits: + memory: {{ .Values.memory_limit }} diff --git a/charts/redhat/cakephp-mysql-persistent/src/templates/deployment_mysql.yaml b/charts/redhat/cakephp-mysql-persistent/src/templates/deployment_mysql.yaml new file mode 100644 index 0000000..00812be --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/templates/deployment_mysql.yaml @@ -0,0 +1,78 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + description: Defines how to deploy the database + image.openshift.io/triggers: |- + [ + { + "from": { + "kind": "ImageStreamTag", + "name": "mysql:{{ .Values.mysql_version }}" + }, + "fieldPath": "spec.template.spec.containers[0].image" + } + ] + template.alpha.openshift.io/wait-for-ready: "true" + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.database_service_name }} +spec: + replicas: 1 + selector: + matchLabels: + name: {{ .Values.database_service_name }} + strategy: + type: Recreate + template: + metadata: + labels: + name: {{ .Values.database_service_name }} + name: {{ .Values.database_service_name }} + spec: + containers: + - env: + - name: MYSQL_USER + valueFrom: + secretKeyRef: + key: database-user + name: {{ .Values.name }} + - name: MYSQL_PASSWORD + valueFrom: + secretKeyRef: + key: database-password + name: {{ .Values.name }} + - name: MYSQL_DATABASE + value: "{{ .Values.database_name }}" + - name: MYSQL_DEFAULT_AUTHENTICATION_PLUGIN + value: "{{ .Values.mysql_default_authentication_plugin }}" + image: " " + livenessProbe: + initialDelaySeconds: 30 + tcpSocket: + port: 3306 + timeoutSeconds: 1 + name: mysql + ports: + - containerPort: 3306 + readinessProbe: + exec: + command: + - /bin/sh + - -i + - -c + - MYSQL_PWD='{{ .Values.database_password }}' mysql -h 127.0.0.1 -u {{ .Values.database_user }} + -D {{ .Values.database_name }} -e 'SELECT 1' + initialDelaySeconds: 5 + timeoutSeconds: 1 + resources: + limits: + memory: {{ .Values.memory_mysql_limit }} + volumeMounts: + - mountPath: /var/lib/mysql/data + name: {{ .Values.database_service_name }}-data + volumes: + - name: {{ .Values.database_service_name }}-data + persistentVolumeClaim: + claimName: {{ .Values.database_service_name }} diff --git a/charts/redhat/cakephp-mysql-persistent/src/templates/imagestream.yaml b/charts/redhat/cakephp-mysql-persistent/src/templates/imagestream.yaml new file mode 100644 index 0000000..3b0c5e4 --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/templates/imagestream.yaml @@ -0,0 +1,9 @@ +apiVersion: image.openshift.io/v1 +kind: ImageStream +metadata: + annotations: + description: Keeps track of changes in the application image + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.name }} diff --git a/charts/redhat/cakephp-mysql-persistent/src/templates/persistentvolumeclaim.yaml b/charts/redhat/cakephp-mysql-persistent/src/templates/persistentvolumeclaim.yaml new file mode 100644 index 0000000..e7619d6 --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/templates/persistentvolumeclaim.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.database_service_name }} +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.volume_capacity }} diff --git a/charts/redhat/cakephp-mysql-persistent/src/templates/route.yaml b/charts/redhat/cakephp-mysql-persistent/src/templates/route.yaml new file mode 100644 index 0000000..7c2a845 --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/templates/route.yaml @@ -0,0 +1,14 @@ +apiVersion: route.openshift.io/v1 +kind: Route +metadata: + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.name }} +spec: + host: {{ .Values.application_domain }} + tls: + termination: Edge + to: + kind: Service + name: {{ .Values.name }} diff --git a/charts/redhat/cakephp-mysql-persistent/src/templates/secret.yaml b/charts/redhat/cakephp-mysql-persistent/src/templates/secret.yaml new file mode 100644 index 0000000..a5b2e6a --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/templates/secret.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Secret +metadata: + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.name }} +stringData: + cakephp-secret-token: {{ .Values.cakephp_secret_token }} + cakephp-security-salt: {{ .Values.cakephp_security_salt }} + database-password: {{ .Values.database_password }} + database-user: {{ .Values.database_user }} diff --git a/charts/redhat/cakephp-mysql-persistent/src/templates/service.yaml b/charts/redhat/cakephp-mysql-persistent/src/templates/service.yaml new file mode 100644 index 0000000..8fa7e90 --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/templates/service.yaml @@ -0,0 +1,35 @@ +apiVersion: v1 +kind: Service +metadata: + annotations: + description: Exposes and load balances the application pods + service.alpha.openshift.io/dependencies: '[{"name": "{{ .Values.database_service_name }}", + "kind": "Service"}]' + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.name }} +spec: + ports: + - name: web + port: 8080 + targetPort: 8080 + selector: + name: {{ .Values.name }} +--- +apiVersion: v1 +kind: Service +metadata: + annotations: + description: Exposes the database server + labels: + app: cakephp-mysql + template: cakephp-mysql + name: {{ .Values.database_service_name }} +spec: + ports: + - name: mysql + port: 3306 + targetPort: 3306 + selector: + name: {{ .Values.database_service_name }} diff --git a/charts/redhat/cakephp-mysql-persistent/src/values.schema.json b/charts/redhat/cakephp-mysql-persistent/src/values.schema.json new file mode 100644 index 0000000..88faefb --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/values.schema.json @@ -0,0 +1,102 @@ +{ + "$schema": "http://json-schema.org/schema#", + "type": "object", + "properties": { + "name": { + "type": "string", + "pattern": "^[a-z0-9-_]+$" + }, + "namespace": { + "type": "string", + "title": "The URL of the repository with your application source code." + }, + "php_version": { + "type": "string", + "description": "Version of PHP image to be used (8.1-ubi9 by default).", + "enum": [ "latest", "8.0-ubi8", "8.1-ubi9", "8.0-ubi9" ] + }, + "mysql_version": { + "type": "string", + "description": "Specify MySQL imagestream tag", + "enum": [ "latest", "8.0-el8", "8.0-el9" ] + }, + "memory_mysql_limit": { + "type": "string", + "title": "Memory limit", + "form": true, + "render": "slider", + "sliderMin": 512, + "sliderMax": 65536, + "sliderUnit": "Mi" + }, + "memory_limit": { + "type": "string", + "title": "Maximum amount of memory the container can use.", + "form": true, + "render": "slider", + "sliderMin": 512, + "sliderMax": 65536, + "sliderUnit": "Mi" + }, + "source_repository_url": { + "type": "string" + }, + "source_repository_ref": { + "type": "string" + }, + "context_dir": { + "type": "string", + "description": "Set this to the relative path to your project if it is not in the root of your repository." + }, + "application_domain": { + "type": "string", + "description": "The exposed hostname that will route to the httpd service, if left blank a value will be defaulted." + }, + "github_webhook_secret": { + "type": "string", + "description": "Github trigger secret. A difficult to guess string encoded as part of the webhook URL. Not encrypted." + }, + "cakephp_secret_token": { + "type": "string", + "description": "Set this to a long random string." + }, + "cakephp_security_salt": { + "type": "string", + "description": "Security salt for session hash." + }, + "composer_mirror": { + "type": "string", + "description": "The custom Composer mirror URL." + }, + "opcache_revalidate_freq": { + "type": "string", + "description": "How often to check script timestamps for updates, in seconds. 0 will result in OPcache checking for updates on every request." + }, + "volume_capacity": { + "type": "string", + "title": "Persistent Volume Size", + "form": true, + "render": "slider", + "sliderMin": 1, + "sliderMax": 100, + "sliderUnit": "Gi" + }, + "mysql_default_authentication_plugin": { + "type": "string", + "description": "The custom MySQL default authentication plugin (default: mysql_native_password), might be changed to caching_sha2_password once clients support it." + }, + "database_service_name": { + "type": "string", + "pattern": "^[a-z0-9-_]+$" + }, + "database_name": { + "type": "string" + }, + "database_password": { + "type": "string" + }, + "database_user": { + "type": "string" + } + } +} diff --git a/charts/redhat/cakephp-mysql-persistent/src/values.yaml b/charts/redhat/cakephp-mysql-persistent/src/values.yaml new file mode 100644 index 0000000..9baf114 --- /dev/null +++ b/charts/redhat/cakephp-mysql-persistent/src/values.yaml @@ -0,0 +1,21 @@ +application_domain: "" # TODO: must define a default value for .application_domain +cakephp_secret_token: "" # TODO: must define a default value for .cakephp_secret_token +cakephp_security_salt: "" # TODO: must define a default value for .cakephp_security_salt +composer_mirror: "" # TODO: must define a default value for .composer_mirror +context_dir: "" # TODO: must define a default value for .context_dir +database_name: default +database_password: cakephp # TODO: must define a default value for .database_password +database_service_name: mysql +database_user: cakephp +github_webhook_secret: "SOMETHING" # TODO: must define a default value for .github_webhook_secret +memory_limit: 512Mi +memory_mysql_limit: 512Mi +mysql_default_authentication_plugin: mysql_native_password +mysql_version: 8.0-el8 +name: cakephp-mysql +namespace: openshift +opcache_revalidate_freq: "2" +php_version: 8.1-ubi9 +source_repository_ref: 4.X # TODO: must define a default value for .source_repository_ref +source_repository_url: https://github.com/sclorg/cakephp-ex.git +volume_capacity: 1Gi diff --git a/charts/redhat/perl-dancer-mysql-persistent/src/values.schema.json b/charts/redhat/perl-dancer-mysql-persistent/src/values.schema.json index 9fc2ca0..ba1ab68 100644 --- a/charts/redhat/perl-dancer-mysql-persistent/src/values.schema.json +++ b/charts/redhat/perl-dancer-mysql-persistent/src/values.schema.json @@ -45,6 +45,11 @@ "description": "Specify PERL imagestream tag", "enum": [ "latest", "5.30-el7", "5.26-ubi8", "5.30-ubi8", "5.30-ubi9", "5.32-ubi8", "5.32-ubi9" ] }, + "mysql_version": { + "type": "string", + "description": "Specify MySQL imagestream tag", + "enum": [ "latest", "8.0-el8", "8.0-el9" ] + }, "mysql_default_authentication_plugin": { "type": "string", "description": "The custom MySQL default authentication plugin (default: mysql_native_password), might be changed to caching_sha2_password once clients support it." diff --git a/tests/test_php_cakephp_mysql_template.py b/tests/test_php_cakephp_mysql_template.py new file mode 100644 index 0000000..05b95ba --- /dev/null +++ b/tests/test_php_cakephp_mysql_template.py @@ -0,0 +1,58 @@ +import os + +import pytest +from pathlib import Path + +from container_ci_suite.helm import HelmChartsAPI + +test_dir = Path(os.path.abspath(os.path.dirname(__file__))) + + +class TestHelmCakePHPMySQLTemplate: + + def setup_method(self): + package_name = "cakephp-mysql-persistent" + path = test_dir / "../charts/redhat" + self.hc_api = HelmChartsAPI(path=path, package_name=package_name, tarball_dir=test_dir) + + def teardown_method(self): + self.hc_api.delete_project() + + def test_curl_connection(self): + self.hc_api.package_name = "php-imagestreams" + self.hc_api.helm_package() + assert self.hc_api.helm_installation() + self.hc_api.package_name = "mysql-imagestreams" + self.hc_api.helm_package() + assert self.hc_api.helm_installation() + self.hc_api.package_name = "cakephp-mysql-persistent" + assert self.hc_api.helm_package() + assert self.hc_api.helm_installation( + values={ + "php_version": "8.0-ubi8", + "namespace": self.hc_api.namespace + } + ) + assert self.hc_api.is_s2i_pod_running(pod_name_prefix="cakephp-mysql") + assert self.hc_api.test_helm_curl_output( + route_name="cakephp-mysql", + expected_str="Welcome to CakePHP 4.5" + ) + # + # def test_by_helm_test(self): + # self.hc_api.package_name = "php-imagestreams" + # self.hc_api.helm_package() + # assert self.hc_api.helm_installation() + # self.hc_api.package_name = "mysql-imagestreams" + # self.hc_api.helm_package() + # assert self.hc_api.helm_installation() + # self.hc_api.package_name = "cakephp-mysql-persistent" + # assert self.hc_api.helm_package() + # assert self.hc_api.helm_installation( + # values={ + # "php_version": "8.0-ubi8", + # "namespace": self.hc_api.namespace + # } + # ) + # assert self.hc_api.is_s2i_pod_running(pod_name_prefix="cakephp-mysql") + # assert self.hc_api.test_helm_chart(expected_str=["Welcome to CakePHP 4.5"])