Skip to content

Commit

Permalink
api: add mysqltemplate webhook check radondb#722
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Oct 28, 2022
1 parent e7a7c52 commit 5503628
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion api/v1alpha1/mysqlcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ limitations under the License.
package v1alpha1

import (
"context"
"fmt"
"net"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -57,6 +61,10 @@ func (r *MysqlCluster) ValidateCreate() error {
if err := r.validateMysqlVersionAndImage(); err != nil {
return err
}

if err := r.ValidMySQLTemplate(); err != nil {
return err
}
return nil
}

Expand All @@ -77,12 +85,42 @@ func (r *MysqlCluster) ValidateUpdate(old runtime.Object) error {
if err := r.validateMysqlVersionAndImage(); err != nil {
return err
}
if err := r.validateNFSServerAddress(oldCluster); err != nil {

if err := r.ValidMySQLTemplate(); err != nil {
return err
}
return nil
}

func (r *MysqlCluster) ValidMySQLTemplate() error {

tmplName := r.Spec.MysqlOpts.MysqlConfTemplate
if len(tmplName) != 0 {
// check whether the template is exist.
if ok, err := getCofigMap(tmplName, r.Namespace); !ok {
return apierrors.NewForbidden(schema.GroupResource{}, "",
fmt.Errorf("configmap is not exist! %s", err.Error()))
}
}
return nil
}

func getCofigMap(name string, namespace string) (bool, error) {
config, err := rest.InClusterConfig()
if err != nil {
return false, err
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return false, err
}
if _, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), name, v1.GetOptions{}); err != nil {
return false, err
}
return true, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *MysqlCluster) ValidateDelete() error {
mysqlclusterlog.Info("validate delete", "name", r.Name)
Expand Down

0 comments on commit 5503628

Please sign in to comment.