Skip to content

Commit b82fd46

Browse files
committed
api: add mysqltemplate webhook check #722
1 parent b07ebe6 commit b82fd46

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

api/v1alpha1/mysqlcluster_webhook.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"net"
2223
"strings"
2324

2425
apierrors "k8s.io/apimachinery/pkg/api/errors"
2526
"k8s.io/apimachinery/pkg/api/resource"
27+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628
"k8s.io/apimachinery/pkg/runtime"
2729
"k8s.io/apimachinery/pkg/runtime/schema"
30+
"k8s.io/client-go/kubernetes"
31+
"k8s.io/client-go/rest"
2832
ctrl "sigs.k8s.io/controller-runtime"
2933
logf "sigs.k8s.io/controller-runtime/pkg/log"
3034
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -57,6 +61,10 @@ func (r *MysqlCluster) ValidateCreate() error {
5761
if err := r.validateMysqlVersionAndImage(); err != nil {
5862
return err
5963
}
64+
65+
if err := r.ValidMySQLTemplate(); err != nil {
66+
return err
67+
}
6068
return nil
6169
}
6270

@@ -77,12 +85,42 @@ func (r *MysqlCluster) ValidateUpdate(old runtime.Object) error {
7785
if err := r.validateMysqlVersionAndImage(); err != nil {
7886
return err
7987
}
80-
if err := r.validateNFSServerAddress(oldCluster); err != nil {
88+
89+
if err := r.ValidMySQLTemplate(); err != nil {
8190
return err
8291
}
8392
return nil
8493
}
8594

95+
func (r *MysqlCluster) ValidMySQLTemplate() error {
96+
97+
tmplName := r.Spec.MysqlOpts.MysqlConfTemplate
98+
if len(tmplName) != 0 {
99+
// check whether the template is exist.
100+
if ok, err := getCofigMap(tmplName, r.Namespace); !ok {
101+
return apierrors.NewForbidden(schema.GroupResource{}, "",
102+
fmt.Errorf("configmap is not exist! %s", err.Error()))
103+
}
104+
}
105+
return nil
106+
}
107+
108+
func getCofigMap(name string, namespace string) (bool, error) {
109+
config, err := rest.InClusterConfig()
110+
if err != nil {
111+
return false, err
112+
}
113+
// creates the clientset
114+
clientset, err := kubernetes.NewForConfig(config)
115+
if err != nil {
116+
return false, err
117+
}
118+
if _, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), name, v1.GetOptions{}); err != nil {
119+
return false, err
120+
}
121+
return true, nil
122+
}
123+
86124
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
87125
func (r *MysqlCluster) ValidateDelete() error {
88126
mysqlclusterlog.Info("validate delete", "name", r.Name)

config/samples/mysql_v1alpha1_mysqlcluster_backup_schedule_demo.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ spec:
1111

1212
# if you want create mysqlcluster from S3, uncomment and fill the directory in S3 bucket below:
1313
# restoreFrom:
14-
BackupSchedule: "0 50 * * * *"
14+
backupSchedule: "0 50 * * * *"
1515
mysqlOpts:
1616
image: percona/percona-server:5.7.34
17-
rootPassword: "RadonDB@123"
18-
rootHost: localhost
1917
user: radondb_usr
2018
password: RadonDB@123
2119
database: radondb

0 commit comments

Comments
 (0)