From 53ab1e55658d8d0db83d80fac41c886f666d4f5b Mon Sep 17 00:00:00 2001 From: redstone Date: Sat, 20 Jan 2024 08:11:28 +0000 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=85=8D?= =?UTF-8?q?=E7=BD=AEenableIPv6=EF=BC=8C=E9=BB=98=E8=AE=A4=E4=B8=BAfalse?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E5=9C=A8=E4=B8=8D=E6=94=AF=E6=8C=81?= =?UTF-8?q?IPv6=E7=9A=84=E7=8E=AF=E5=A2=83=E4=B8=8B=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos-operator/templates/deployment.yaml | 3 + operator/chart/nacos-operator/values.yaml | 2 + operator/pkg/service/operator/Kind.go | 56 +++++++++++-------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/operator/chart/nacos-operator/templates/deployment.yaml b/operator/chart/nacos-operator/templates/deployment.yaml index f6bc1a0..274ceba 100644 --- a/operator/chart/nacos-operator/templates/deployment.yaml +++ b/operator/chart/nacos-operator/templates/deployment.yaml @@ -35,6 +35,9 @@ spec: command: ["/manager"] args: ["--enable-leader-election"] imagePullPolicy: {{ .Values.image.pullPolicy }} + env: + - name: ENABLE_IPV6 + value: "{{ .Values.enableIPv6 | default false }}" resources: {{- toYaml .Values.resources | nindent 12 }} {{- with .Values.nodeSelector }} diff --git a/operator/chart/nacos-operator/values.yaml b/operator/chart/nacos-operator/values.yaml index b79ae18..7e9fe0b 100644 --- a/operator/chart/nacos-operator/values.yaml +++ b/operator/chart/nacos-operator/values.yaml @@ -4,6 +4,8 @@ replicaCount: 1 +enableIPv6: false + image: repository: nacos/nacos-operator pullPolicy: Always diff --git a/operator/pkg/service/operator/Kind.go b/operator/pkg/service/operator/Kind.go index e7edc99..b72dc06 100644 --- a/operator/pkg/service/operator/Kind.go +++ b/operator/pkg/service/operator/Kind.go @@ -2,11 +2,12 @@ package operator import ( "fmt" + "path/filepath" + "strconv" + batchv1 "k8s.io/api/batch/v1" "k8s.io/apimachinery/pkg/runtime" "nacos.io/nacos-operator/pkg/util/merge" - "path/filepath" - "strconv" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -21,12 +22,14 @@ import ( "nacos.io/nacos-operator/pkg/service/k8s" ) -const TYPE_STAND_ALONE = "standalone" -const TYPE_CLUSTER = "cluster" -const NACOS = "nacos" -const NACOS_PORT = 8848 -const RAFT_PORT = 7848 -const NEW_RAFT_PORT = 9848 +const ( + TYPE_STAND_ALONE = "standalone" + TYPE_CLUSTER = "cluster" + NACOS = "nacos" + NACOS_PORT = 8848 + RAFT_PORT = 7848 + NEW_RAFT_PORT = 9848 +) // 导入的sql文件名称 const SQL_FILE_NAME = "nacos-mysql.sql" @@ -104,13 +107,13 @@ func (e *KindClient) generateName(nacos *nacosgroupv1alpha1.Nacos) string { func (e *KindClient) generateHeadlessSvcName(nacos *nacosgroupv1alpha1.Nacos) string { return fmt.Sprintf("%s-headless", nacos.Name) } + func (e *KindClient) generateClientSvcName(nacos *nacosgroupv1alpha1.Nacos) string { return fmt.Sprintf("%s-client", nacos.Name) } // CR格式验证 func (e *KindClient) ValidationField(nacos *nacosgroupv1alpha1.Nacos) { - setDefaultValue := []func(nacos *nacosgroupv1alpha1.Nacos){ setDefaultNacosType, setDefaultMysql, @@ -357,7 +360,7 @@ func (e *KindClient) buildJob(nacos *nacosgroupv1alpha1.Nacos) *batchv1.Job { Key: "SQL_SCRIPT", }, }, - //Value: readSql(SQL_FILE_NAME), + // Value: readSql(SQL_FILE_NAME), }, }, // 导入nacos-mysql.sql中的数据 @@ -459,17 +462,27 @@ func (e *KindClient) buildClientService(nacos *nacosgroupv1alpha1.Nacos) *v1.Ser Selector: labels, }, } - //client-service提供双栈 - var ipf = make([]v1.IPFamily, 0) + // client-service提供双栈 + ipf := make([]v1.IPFamily, 0) ipf = append(ipf, v1.IPv4Protocol) - ipf = append(ipf, v1.IPv6Protocol) + if isIPv6Enabled() { + ipf = append(ipf, v1.IPv6Protocol) + } svc.Spec.IPFamilies = ipf - var ipPli = v1.IPFamilyPolicyPreferDualStack + ipPli := v1.IPFamilyPolicyPreferDualStack svc.Spec.IPFamilyPolicy = &ipPli myErrors.EnsureNormal(controllerutil.SetControllerReference(nacos, svc, e.scheme)) return svc } +func isIPv6Enabled() bool { + enableIPv6, err := strconv.ParseBool(os.Getevn("ENABLE_IPV6")) + if err != nil { + return false + } + return enableIPv6 +} + func (e *KindClient) buildStatefulset(nacos *nacosgroupv1alpha1.Nacos) *appv1.StatefulSet { // 生成label labels := e.generateLabels(nacos.Name, NACOS) @@ -570,7 +583,7 @@ func (e *KindClient) buildStatefulset(nacos *nacosgroupv1alpha1.Nacos) *appv1.St }) } - var ss = &appv1.StatefulSet{ + ss := &appv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ Name: e.generateName(nacos), Namespace: nacos.Namespace, @@ -638,7 +651,7 @@ func (e *KindClient) buildStatefulset(nacos *nacosgroupv1alpha1.Nacos) *appv1.St if nacos.Spec.Volume.Enabled { ss.Spec.VolumeClaimTemplates = append(ss.Spec.VolumeClaimTemplates, v1.PersistentVolumeClaim{ Spec: v1.PersistentVolumeClaimSpec{ - //VolumeName: "db", + // VolumeName: "db", StorageClassName: nacos.Spec.Volume.StorageClass, AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}, Resources: v1.ResourceRequirements{ @@ -712,7 +725,6 @@ func (e *KindClient) buildStatefulset(nacos *nacosgroupv1alpha1.Nacos) *appv1.St func (e *KindClient) AddCheckDatabase(nacos *nacosgroupv1alpha1.Nacos, sts *appv1.StatefulSet) *appv1.StatefulSet { container := v1.Container{ - Name: "mysql-check-database", Image: nacos.Spec.MysqlInitImage, Env: []v1.EnvVar{ @@ -740,7 +752,8 @@ func (e *KindClient) AddCheckDatabase(nacos *nacosgroupv1alpha1.Nacos, sts *appv Command: []string{ "/bin/sh", "-c", - "while ! mysqlcheck --host=\"${MYSQL_HOST}\" --port=\"${MYSQL_PORT}\" --user=\"${MYSQL_USER}\" --password=\"${MYSQL_PASS}\" --databases \"${MYSQL_DB}\" ; do sleep 1; done"}, + "while ! mysqlcheck --host=\"${MYSQL_HOST}\" --port=\"${MYSQL_PORT}\" --user=\"${MYSQL_USER}\" --password=\"${MYSQL_PASS}\" --databases \"${MYSQL_DB}\" ; do sleep 1; done", + }, } sts.Spec.Template.Spec.InitContainers = append(sts.Spec.Template.Spec.InitContainers, container) return sts @@ -833,7 +846,6 @@ func (e *KindClient) buildDefaultConfigMap(nacos *nacosgroupv1alpha1.Nacos) *v1. } func (e *KindClient) buildStatefulsetCluster(nacos *nacosgroupv1alpha1.Nacos, ss *appv1.StatefulSet) *appv1.StatefulSet { - domain := "cluster.local" // 从环境变量中获取domain for _, env := range nacos.Spec.Env { @@ -864,11 +876,11 @@ func (e *KindClient) buildStatefulsetCluster(nacos *nacosgroupv1alpha1.Nacos, ss func (e *KindClient) buildHeadlessServiceCluster(svc *v1.Service, nacos *nacosgroupv1alpha1.Nacos) *v1.Service { svc.Spec.ClusterIP = "None" svc.Name = e.generateHeadlessSvcName(nacos) - //nacos pod间raft 探测交互走ipv4 - var ipf = make([]v1.IPFamily, 0) + // nacos pod间raft 探测交互走ipv4 + ipf := make([]v1.IPFamily, 0) ipf = append(ipf, v1.IPv4Protocol) svc.Spec.IPFamilies = ipf - var ipPli = v1.IPFamilyPolicySingleStack + ipPli := v1.IPFamilyPolicySingleStack svc.Spec.IPFamilyPolicy = &ipPli return svc } From 643f0ee490282fc2dfbde0274048bca050cdafaf Mon Sep 17 00:00:00 2001 From: redstone Date: Tue, 23 Jan 2024 02:07:37 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E7=BC=BA=E5=B0=91?= =?UTF-8?q?=E7=9A=84import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- operator/pkg/service/operator/Kind.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/operator/pkg/service/operator/Kind.go b/operator/pkg/service/operator/Kind.go index b72dc06..4e4664b 100644 --- a/operator/pkg/service/operator/Kind.go +++ b/operator/pkg/service/operator/Kind.go @@ -2,6 +2,7 @@ package operator import ( "fmt" + "os" "path/filepath" "strconv" @@ -476,7 +477,7 @@ func (e *KindClient) buildClientService(nacos *nacosgroupv1alpha1.Nacos) *v1.Ser } func isIPv6Enabled() bool { - enableIPv6, err := strconv.ParseBool(os.Getevn("ENABLE_IPV6")) + enableIPv6, err := strconv.ParseBool(os.Getenv("ENABLE_IPV6")) if err != nil { return false }