From 8b3969ca37e232668037216956eb91fc4a172781 Mon Sep 17 00:00:00 2001 From: hualongzhong Date: Wed, 2 Nov 2022 18:22:25 +0800 Subject: [PATCH] api: Add webhook check for sidecar image (#731) --- api/v1alpha1/mysqlcluster_webhook.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/api/v1alpha1/mysqlcluster_webhook.go b/api/v1alpha1/mysqlcluster_webhook.go index ff67071e..249971bc 100644 --- a/api/v1alpha1/mysqlcluster_webhook.go +++ b/api/v1alpha1/mysqlcluster_webhook.go @@ -65,6 +65,9 @@ func (r *MysqlCluster) ValidateCreate() error { if err := r.ValidMySQLTemplate(); err != nil { return err } + if err := r.validateMysqlVersion(); err != nil { + return err + } return nil } @@ -89,6 +92,12 @@ func (r *MysqlCluster) ValidateUpdate(old runtime.Object) error { if err := r.ValidMySQLTemplate(); err != nil { return err } + if err := r.validateMysqlVersion(); err != nil { + return err + } + if err := r.validateNFSServerAddress(oldCluster); err != nil { + return err + } return nil } @@ -183,3 +192,21 @@ func (r *MysqlCluster) validateMysqlVersionAndImage() error { } return nil } + +// Validate MySQL version and related image. +func (r *MysqlCluster) validateMysqlVersion() error { + switch r.Spec.MysqlVersion { + case "5.7": + if !strings.Contains(r.Spec.PodPolicy.SidecarImage, "57") { + return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("spec.MysqlVersion is 5.7, but spec.PodPolicy.SidecarImage is not 5.7")) + } + case "8.0": + if !strings.Contains(r.Spec.PodPolicy.SidecarImage, "80") { + return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("spec.MysqlVersion is 8.0, but spec.PodPolicy.SidecarImage is not 8.0")) + } + default: + return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("spec.MysqlVersion is not provided")) + + } + return nil +}