From e7a7c52e119cba8a8ac81f569e75a216ec6ab471 Mon Sep 17 00:00:00 2001 From: acekingke Date: Wed, 19 Oct 2022 16:30:07 +0800 Subject: [PATCH] *: support the x.x.x.x:/dbbackup nfs address. #716 --- api/v1alpha1/mysqlcluster_webhook.go | 11 ++++++++--- backup/syncer/job.go | 6 ++++-- config/samples/nfs_server.yaml | 2 +- mysqlcluster/mysqlcluster.go | 5 +++-- utils/common.go | 9 +++++++++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/api/v1alpha1/mysqlcluster_webhook.go b/api/v1alpha1/mysqlcluster_webhook.go index 69b08c57..61234792 100644 --- a/api/v1alpha1/mysqlcluster_webhook.go +++ b/api/v1alpha1/mysqlcluster_webhook.go @@ -91,11 +91,16 @@ func (r *MysqlCluster) ValidateDelete() error { return nil } -// TODO: Add NFSServerAddress webhook & backup schedule. +// Add NFSServerAddress webhook & backup schedule. func (r *MysqlCluster) validateNFSServerAddress(oldCluster *MysqlCluster) error { - isIP := net.ParseIP(r.Spec.NFSServerAddress) != nil + //nfsaddress format is x.x.x.x:/yyy such as 10.233.55.172:/backup + ipStr := strings.Split(r.Spec.NFSServerAddress, ":") + if len(r.Spec.NFSServerAddress) != 0 && len(ipStr) == 0 { + return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("nfsServerAddress should be set as IP:/PATH or IP")) + } + isIP := net.ParseIP(ipStr[0]) != nil if len(r.Spec.NFSServerAddress) != 0 && !isIP { - return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("nfsServerAddress should be set as IP")) + return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("nfsServerAddress should be set as IP:/PATH or IP")) } if len(r.Spec.BackupSchedule) != 0 && len(r.Spec.BackupSecretName) == 0 && !isIP { return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("backupSchedule is set without any backupSecretName or nfsServerAddress")) diff --git a/backup/syncer/job.go b/backup/syncer/job.go index dde56efc..be418b56 100644 --- a/backup/syncer/job.go +++ b/backup/syncer/job.go @@ -135,14 +135,16 @@ func (s *jobSyncer) ensurePodSpec(in corev1.PodSpec) corev1.PodSpec { in.Containers[0].Image = fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), s.backup.Spec.Image) in.ServiceAccountName = s.backup.Spec.ClusterName if len(s.backup.Spec.NFSServerAddress) != 0 { + //parse NFSServerAddress to IP:/Path + ip, path := utils.ParseIPAndPath(s.backup.Spec.NFSServerAddress) // add volumn about pvc in.Volumes = []corev1.Volume{ { Name: utils.XtrabackupPV, VolumeSource: corev1.VolumeSource{ NFS: &corev1.NFSVolumeSource{ - Server: s.backup.Spec.NFSServerAddress, - Path: "/", + Server: ip, + Path: path, }, }, }, diff --git a/config/samples/nfs_server.yaml b/config/samples/nfs_server.yaml index dfcbcc3d..3bc58006 100644 --- a/config/samples/nfs_server.yaml +++ b/config/samples/nfs_server.yaml @@ -14,7 +14,7 @@ spec: containers: - name: nfs-server ## TODO: radondb/volume-nfs:0.8 - image: k8s.gcr.io/volume-nfs:0.8 + image: gcr.azk8s.cn/google_containers/volume-nfs:0.8 ports: - name: nfs containerPort: 2049 diff --git a/mysqlcluster/mysqlcluster.go b/mysqlcluster/mysqlcluster.go index 7e213727..98c3e5cf 100644 --- a/mysqlcluster/mysqlcluster.go +++ b/mysqlcluster/mysqlcluster.go @@ -249,12 +249,13 @@ func (c *MysqlCluster) EnsureVolumes() []corev1.Volume { ) // add the nfs volumn mount if len(c.Spec.NFSServerAddress) != 0 { + ip, path := utils.ParseIPAndPath(c.Spec.NFSServerAddress) volumes = append(volumes, corev1.Volume{ Name: utils.XtrabackupPV, VolumeSource: corev1.VolumeSource{ NFS: &corev1.NFSVolumeSource{ - Server: c.Spec.NFSServerAddress, - Path: "/", + Server: ip, + Path: path, }, }, }) diff --git a/utils/common.go b/utils/common.go index fbcfec0b..0efd9c45 100644 --- a/utils/common.go +++ b/utils/common.go @@ -150,3 +150,12 @@ func ParseImageName(image string) (string, string, string, error) { imageTag := imageNameArry[1] return imagePrefix, imageName, imageTag, nil } + +func ParseIPAndPath(nfsaddr string) (string, string) { + res := strings.Split(nfsaddr, ":") + if len(res) == 2 { + return res[0], res[1] + } else { + return res[0], "/" + } +}