From 737a3bea513c386eeee8486fa59aff69f39aaaf1 Mon Sep 17 00:00:00 2001 From: acekingke Date: Wed, 7 Jun 2023 10:58:27 +0800 Subject: [PATCH] *: v3.0.0 backup convert do not work when use helm install it. #793 --- Makefile | 5 +- PROJECT | 3 ++ api/v1beta1/mysqlcluster_conversion.go | 47 +++++++++++++++++++ api/v1beta1/zz_generated.conversion.go | 5 -- charts/mysql-operator/templates/NOTES.txt | 4 +- .../mysql.radondb.com_backups.yaml | 22 ++++++++- .../mysql.radondb.com_mysqlclusters.yaml | 14 ++++-- config/crd/kustomization.yaml | 2 +- config/crd/patches/webhook_in_backups.yaml | 16 +++++++ 9 files changed, 102 insertions(+), 16 deletions(-) rename charts/mysql-operator/{crds => templates}/mysql.radondb.com_backups.yaml (95%) rename charts/mysql-operator/{crds => templates}/mysql.radondb.com_mysqlclusters.yaml (99%) create mode 100644 config/crd/patches/webhook_in_backups.yaml diff --git a/Makefile b/Makefile index eced009d..f95a9fe2 100644 --- a/Makefile +++ b/Makefile @@ -57,8 +57,9 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases update-crd: ## Synchronize the generated YAML files to operator Chart after make manifests. - make manifests - cp config/crd/bases/* charts/mysql-operator/crds/ +##make manifests + @echo "should modify by manaual for mysqlclster and mysqlbackup" + cp config/crd/bases/mysql.radondb.com_mysqlusers.yaml charts/mysql-operator/crds/ generate: controller-gen generate-go-conversions ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." diff --git a/PROJECT b/PROJECT index 059dd1da..6d5e6a38 100644 --- a/PROJECT +++ b/PROJECT @@ -60,4 +60,7 @@ resources: kind: Backup path: github.com/radondb/radondb-mysql-kubernetes/api/v1beta1 version: v1beta1 + webhooks: + conversion: true + webhookVersion: v1 version: "3" diff --git a/api/v1beta1/mysqlcluster_conversion.go b/api/v1beta1/mysqlcluster_conversion.go index 40f72be9..8b1563bb 100644 --- a/api/v1beta1/mysqlcluster_conversion.go +++ b/api/v1beta1/mysqlcluster_conversion.go @@ -19,11 +19,13 @@ package v1beta1 import ( "fmt" "strings" + "time" "unsafe" "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" apiconversion "k8s.io/apimachinery/pkg/conversion" "sigs.k8s.io/controller-runtime/pkg/conversion" ) @@ -183,6 +185,31 @@ func Convert_v1beta1_BackupStatus_To_v1alpha1_BackupStatus(in *BackupStatus, out if err := autoConvert_v1beta1_BackupStatus_To_v1alpha1_BackupStatus(in, out, s); err != nil { return err } + out.BackupDate = in.CompletionTime.Format("2006-01-02 15:04:05") + switch in.State { + case BackupFailed: + out.Conditions = append(out.Conditions, v1alpha1.BackupCondition{ + Type: v1alpha1.BackupStart, + Status: corev1.ConditionTrue, + Reason: "", + }, v1alpha1.BackupCondition{ + Type: v1alpha1.BackupFailed, + Status: corev1.ConditionTrue, + Reason: "", + }) + out.Completed = true + case BackupSucceeded: + out.Conditions = append(out.Conditions, v1alpha1.BackupCondition{ + Type: v1alpha1.BackupStart, + Status: corev1.ConditionTrue, + Reason: "", + }, v1alpha1.BackupCondition{ + Type: v1alpha1.BackupComplete, + Status: corev1.ConditionTrue, + Reason: "", + }) + out.Completed = true + } return nil } @@ -190,6 +217,26 @@ func Convert_v1alpha1_BackupStatus_To_v1beta1_BackupStatus(in *v1alpha1.BackupSt if err := autoConvert_v1alpha1_BackupStatus_To_v1beta1_BackupStatus(in, out, s); err != nil { return err } + if t, err := time.Parse("2006-01-02 15:04:05", in.BackupDate); err != nil { + return err + } else { + completeTime := metav1.NewTime(t) + out.CompletionTime = &completeTime + } + if in.Completed { + out.State = BackupFailed + for _, cond := range in.Conditions { + + if cond.Type == v1alpha1.BackupStart && cond.Status == corev1.ConditionTrue && + out.State != BackupSucceeded { + out.State = BackupStart + } + if cond.Type == v1alpha1.BackupComplete && cond.Status == corev1.ConditionTrue { + out.State = BackupSucceeded + break + } + } + } return nil } diff --git a/api/v1beta1/zz_generated.conversion.go b/api/v1beta1/zz_generated.conversion.go index c6f7b8fb..ba01de49 100644 --- a/api/v1beta1/zz_generated.conversion.go +++ b/api/v1beta1/zz_generated.conversion.go @@ -289,11 +289,6 @@ func autoConvert_v1beta1_BackupStatus_To_v1alpha1_BackupStatus(in *BackupStatus, // WARNING: in.StartTime requires manual conversion: does not exist in peer-type // WARNING: in.CompletionTime requires manual conversion: does not exist in peer-type // WARNING: in.State requires manual conversion: does not exist in peer-type - if in.State == BackupFailed || in.State == BackupSucceeded{ - out.Completed = true; - }else { - out.Completed = false; - } // WARNING: in.ManualBackup requires manual conversion: does not exist in peer-type // WARNING: in.ScheduledBackups requires manual conversion: does not exist in peer-type return nil diff --git a/charts/mysql-operator/templates/NOTES.txt b/charts/mysql-operator/templates/NOTES.txt index fcdf3254..9bc43557 100644 --- a/charts/mysql-operator/templates/NOTES.txt +++ b/charts/mysql-operator/templates/NOTES.txt @@ -8,11 +8,11 @@ Create MySQLCluster: > Create MySQLCluster: {{- end }} -kubectl apply -f https://github.com/radondb/radondb-mysql-kubernetes/releases/latest/download/mysql_v1alpha1_mysqlcluster.yaml +kubectl apply -f https://github.com/radondb/radondb-mysql-kubernetes/releases/latest/download/mysql_v1beta1_mysqlcluster.yaml Create Users: -kubectl apply -f https://github.com/radondb/radondb-mysql-kubernetes/releases/latest/download/mysql_v1alpha1_mysqluser.yaml +kubectl apply -f https://github.com/radondb/radondb-mysql-kubernetes/releases/latest/download/mysql_v1beta1_mysqluser.yaml {{- if .Values.mysqlcluster.install }} diff --git a/charts/mysql-operator/crds/mysql.radondb.com_backups.yaml b/charts/mysql-operator/templates/mysql.radondb.com_backups.yaml similarity index 95% rename from charts/mysql-operator/crds/mysql.radondb.com_backups.yaml rename to charts/mysql-operator/templates/mysql.radondb.com_backups.yaml index 8de57375..dab74df9 100644 --- a/charts/mysql-operator/crds/mysql.radondb.com_backups.yaml +++ b/charts/mysql-operator/templates/mysql.radondb.com_backups.yaml @@ -1,13 +1,31 @@ - ---- +{{- $certManagerEnabled := .Values.webhook.certManager.enabled -}} +{{- $caCertPEM := include "webhook.caBundleCertPEM" . -}} +{{- $tlsCertPEM := include "webhook.certPEM" . -}} +{{- $tlsKeyPEM := include "webhook.keyPEM" . -}} apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: controller-gen.kubebuilder.io/version: v0.4.1 + {{- if $certManagerEnabled }} + cert-manager.io/inject-ca-from: "{{ .Release.Namespace }}/{{ template "certificate.name" . }}" + {{- end }} creationTimestamp: null name: backups.mysql.radondb.com spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + {{- if not $certManagerEnabled }} + caBundle: {{ ternary (b64enc $caCertPEM) (b64enc (trim $tlsCertPEM)) (empty $tlsKeyPEM) }} + {{- end }} + service: + name: radondb-mysql-webhook + namespace: default + path: /convert + conversionReviewVersions: + - v1 group: mysql.radondb.com names: kind: Backup diff --git a/charts/mysql-operator/crds/mysql.radondb.com_mysqlclusters.yaml b/charts/mysql-operator/templates/mysql.radondb.com_mysqlclusters.yaml similarity index 99% rename from charts/mysql-operator/crds/mysql.radondb.com_mysqlclusters.yaml rename to charts/mysql-operator/templates/mysql.radondb.com_mysqlclusters.yaml index bbc68b20..033b902f 100644 --- a/charts/mysql-operator/crds/mysql.radondb.com_mysqlclusters.yaml +++ b/charts/mysql-operator/templates/mysql.radondb.com_mysqlclusters.yaml @@ -1,11 +1,15 @@ - ---- +{{- $certManagerEnabled := .Values.webhook.certManager.enabled -}} +{{- $caCertPEM := include "webhook.caBundleCertPEM" . -}} +{{- $tlsCertPEM := include "webhook.certPEM" . -}} +{{- $tlsKeyPEM := include "webhook.keyPEM" . -}} apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: controller-gen.kubebuilder.io/version: v0.4.1 - cert-manager.io/inject-ca-from: default/radondb-mysql-certificate +{{- if $certManagerEnabled }} + cert-manager.io/inject-ca-from: "{{ .Release.Namespace }}/{{ template "certificate.name" . }}" +{{- end }} creationTimestamp: null name: mysqlclusters.mysql.radondb.com spec: @@ -13,7 +17,9 @@ spec: strategy: Webhook webhook: clientConfig: - caBundle: Cg== + {{- if not $certManagerEnabled }} + caBundle: {{ ternary (b64enc $caCertPEM) (b64enc (trim $tlsCertPEM)) (empty $tlsKeyPEM) }} + {{- end }} service: name: radondb-mysql-webhook namespace: default diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index f8105b47..6d489b17 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -12,7 +12,7 @@ patchesStrategicMerge: # patches here are for enabling the conversion webhook for each CRD - patches/webhook_in_mysqlclusters.yaml #- patches/webhook_in_mysqlclusters.yaml -#- patches/webhook_in_backups.yaml +- patches/webhook_in_backups.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix. diff --git a/config/crd/patches/webhook_in_backups.yaml b/config/crd/patches/webhook_in_backups.yaml new file mode 100644 index 00000000..56a37e0c --- /dev/null +++ b/config/crd/patches/webhook_in_backups.yaml @@ -0,0 +1,16 @@ +# The following patch enables a conversion webhook for the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: backups.mysql.radondb.com +spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + namespace: system + name: radondb-mysql-webhook + path: /convert + conversionReviewVersions: + - v1