Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #401。增加配置enableIPv6,默认为false,避免在不支持IPv6的环境下无法正常使用 #442

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions operator/chart/nacos-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 2 additions & 0 deletions operator/chart/nacos-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

replicaCount: 1

enableIPv6: false

image:
repository: nacos/nacos-operator
pullPolicy: Always
Expand Down
55 changes: 35 additions & 20 deletions operator/pkg/service/operator/Kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package operator

import (
"fmt"
"os"
"path/filepath"
"strconv"

batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime"
"nacos.io/nacos-operator/pkg/util/merge"
Expand All @@ -22,12 +26,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"
Expand Down Expand Up @@ -105,13 +111,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,
Expand Down Expand Up @@ -358,7 +364,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中的数据
Expand Down Expand Up @@ -460,17 +466,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.Getenv("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)
Expand Down Expand Up @@ -571,7 +587,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,
Expand Down Expand Up @@ -639,7 +655,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{
Expand Down Expand Up @@ -713,7 +729,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{
Expand Down Expand Up @@ -741,7 +756,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
Expand Down Expand Up @@ -834,7 +850,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 {
Expand Down Expand Up @@ -865,11 +880,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
}