diff --git a/.github/workflows/operator-build.yaml b/.github/workflows/operator-build.yaml index 7bb81d8..a4b4516 100644 --- a/.github/workflows/operator-build.yaml +++ b/.github/workflows/operator-build.yaml @@ -43,6 +43,7 @@ jobs: with: context: operator file: operator/Dockerfile + platforms: linux/amd64,linux/arm64 push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/README-CN.md b/README-CN.md index e5acaf6..3225e4d 100644 --- a/README-CN.md +++ b/README-CN.md @@ -143,7 +143,7 @@ mysql-gf2vd 1/1 Running 0 111m ``` ## 执行数据库初始化语句 -数据库初始化语句位置 +数据库初始化语句位置 @@ -155,6 +155,7 @@ mysql-gf2vd 1/1 Running 0 111m ```yaml data: + mysql.host: "数据库地址" mysql.db.name: "数据库名称" mysql.port: "端口" mysql.user: "用户名" @@ -281,6 +282,7 @@ for i in 0 1 2; do echo nacos-$i; kubectl exec nacos-$i curl GET "http://localho | mysql.port | N | 端口 | | mysql.user | Y | 用户名 | | mysql.password | Y | 密码 | +| SPRING_DATASOURCE_PLATFORM | Y | 数据库类型,默认embedded嵌入式数据库,参数只支持mysql或embedded | | NACOS_REPLICAS | N | 确定执行Nacos启动节点数量,如果不适用动态扩容插件,就必须配置这个属性,否则使用扩容插件后不会生效 | | NACOS_SERVER_PORT | N | Nacos 端口 为peer_finder插件提供端口 | | NACOS_APPLICATION_PORT | N | Nacos 端口 | diff --git a/README.md b/README.md index 5f28167..8743886 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ chmod +x quick-startup.sh ## Tips If you use a custom database, please initialize the database script yourself first. - + > In advanced use, the cluster is automatically scaled and data is persisted, but [PersistentVolumeClaims](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims) must be deployed. In this example, NFS is used. @@ -165,6 +165,7 @@ mysql-gf2vd 1/1 Running 0 111m ```yaml data: + mysql.host: "db host" mysql.db.name: "db name" mysql.port: " db port" mysql.user: " db username" @@ -284,6 +285,7 @@ You can find that the new node has joined the cluster | mysql.port | N | database port | | mysql.user | Y | database username | | mysql.password | Y | database password | +| SPRING_DATASOURCE_PLATFORM | Y | Database type,The default is embedded database,parameters only support mysql or embedded | | NACOS_REPLICAS | Y | The number of clusters must be consistent with the value of the replicas attribute | | NACOS_SERVER_PORT | N | Nacos port,default:8848 for Peer-finder plugin | | NACOS_APPLICATION_PORT | N | Nacos port, default:8848 | diff --git a/deploy/nacos/nacos-no-pvc-ingress.yaml b/deploy/nacos/nacos-no-pvc-ingress.yaml index 5310c6a..6633907 100644 --- a/deploy/nacos/nacos-no-pvc-ingress.yaml +++ b/deploy/nacos/nacos-no-pvc-ingress.yaml @@ -31,11 +31,11 @@ kind: ConfigMap metadata: name: nacos-cm data: - mysql.host: "10.127.1.12" + mysql.host: "mysql" mysql.db.name: "nacos_devtest" mysql.port: "3306" mysql.user: "nacos" - mysql.password: "passwd" + mysql.password: "nacos" --- apiVersion: apps/v1 kind: StatefulSet @@ -62,7 +62,7 @@ spec: - nacos topologyKey: "kubernetes.io/hostname" containers: - - name: k8snacos + - name: nacos imagePullPolicy: Always image: nacos/nacos-server:latest resources: @@ -106,14 +106,14 @@ spec: configMapKeyRef: name: nacos-cm key: mysql.password + - name: SPRING_DATASOURCE_PLATFORM + value: "mysql" - name: MODE value: "cluster" - name: NACOS_SERVER_PORT value: "8848" - name: PREFER_HOST_MODE value: "hostname" - - name: SPRING_DATASOURCE_PLATFORM - value: "mysql" - name: NACOS_SERVERS value: "nacos-0.nacos-headless.default.svc.cluster.local:8848 nacos-1.nacos-headless.default.svc.cluster.local:8848 nacos-2.nacos-headless.default.svc.cluster.local:8848" selector: @@ -125,18 +125,19 @@ spec: --- # ------------------- App Ingress ------------------- # -apiVersion: extensions/v1beta1 +apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: nacos-headless - namespace: default - spec: rules: - host: nacos-web.nacos-demo.com http: paths: - - path: / + - path: /nacos + pathType: Prefix backend: - serviceName: nacos-headless - servicePort: server + service: + name: nacos-headless + port: + name: server diff --git a/deploy/nacos/nacos-pvc-ceph.yaml b/deploy/nacos/nacos-pvc-ceph.yaml index 338d285..b73527c 100644 --- a/deploy/nacos/nacos-pvc-ceph.yaml +++ b/deploy/nacos/nacos-pvc-ceph.yaml @@ -5,9 +5,8 @@ metadata: name: nacos-headless labels: app: nacos - annotations: - service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" spec: + publishNotReadyAddresses: true ports: - port: 8848 name: server @@ -31,6 +30,7 @@ kind: ConfigMap metadata: name: nacos-cm data: + mysql.host: "mysql" mysql.db.name: "nacos_devtest" mysql.port: "3306" mysql.user: "nacos" @@ -41,6 +41,7 @@ kind: StatefulSet metadata: name: nacos spec: + podManagementPolicy: Parallel serviceName: nacos-headless replicas: 3 template: @@ -98,6 +99,11 @@ spec: fieldRef: apiVersion: v1 fieldPath: metadata.namespace + - name: MYSQL_SERVICE_HOST + valueFrom: + configMapKeyRef: + name: nacos-cm + key: mysql.host - name: MYSQL_SERVICE_DB_NAME valueFrom: configMapKeyRef: @@ -118,12 +124,12 @@ spec: configMapKeyRef: name: nacos-cm key: mysql.password + - name: SPRING_DATASOURCE_PLATFORM + value: "mysql" - name: NACOS_SERVER_PORT value: "8848" - name: NACOS_APPLICATION_PORT value: "8848" - - name: SPRING_DATASOURCE_PLATFORM - value: "mysql" - name: PREFER_HOST_MODE value: "hostname" volumeMounts: diff --git a/deploy/nacos/nacos-pvc-nfs.yaml b/deploy/nacos/nacos-pvc-nfs.yaml index 9347530..f316ac4 100644 --- a/deploy/nacos/nacos-pvc-nfs.yaml +++ b/deploy/nacos/nacos-pvc-nfs.yaml @@ -1,3 +1,5 @@ +# 请阅读Wiki文章 +# https://github.com/nacos-group/nacos-k8s/wiki/%E4%BD%BF%E7%94%A8peerfinder%E6%89%A9%E5%AE%B9%E6%8F%92%E4%BB%B6 --- apiVersion: v1 kind: Service @@ -5,9 +7,8 @@ metadata: name: nacos-headless labels: app: nacos - annotations: - service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" spec: + publishNotReadyAddresses: true ports: - port: 8848 name: server @@ -31,6 +32,7 @@ kind: ConfigMap metadata: name: nacos-cm data: + mysql.host: "mysql" mysql.db.name: "nacos_devtest" mysql.port: "3306" mysql.user: "nacos" @@ -41,6 +43,7 @@ kind: StatefulSet metadata: name: nacos spec: + podManagementPolicy: Parallel serviceName: nacos-headless replicas: 3 template: @@ -98,6 +101,11 @@ spec: fieldRef: apiVersion: v1 fieldPath: metadata.namespace + - name: MYSQL_SERVICE_HOST + valueFrom: + configMapKeyRef: + name: nacos-cm + key: mysql.host - name: MYSQL_SERVICE_DB_NAME valueFrom: configMapKeyRef: @@ -118,14 +126,14 @@ spec: configMapKeyRef: name: nacos-cm key: mysql.password + - name: SPRING_DATASOURCE_PLATFORM + value: "mysql" - name: NACOS_SERVER_PORT value: "8848" - name: NACOS_APPLICATION_PORT value: "8848" - name: PREFER_HOST_MODE value: "hostname" - - name: SPRING_DATASOURCE_PLATFORM - value: "mysql" volumeMounts: - name: data mountPath: /home/nacos/plugins/peer-finder diff --git a/deploy/nacos/nacos-quick-start.yaml b/deploy/nacos/nacos-quick-start.yaml index 6fde23d..64899de 100644 --- a/deploy/nacos/nacos-quick-start.yaml +++ b/deploy/nacos/nacos-quick-start.yaml @@ -30,6 +30,7 @@ kind: ConfigMap metadata: name: nacos-cm data: + mysql.host: "mysql" mysql.db.name: "nacos_devtest" mysql.port: "3306" mysql.user: "nacos" @@ -60,7 +61,7 @@ spec: - nacos topologyKey: "kubernetes.io/hostname" containers: - - name: k8snacos + - name: nacos imagePullPolicy: Always image: nacos/nacos-server:latest resources: @@ -79,6 +80,11 @@ spec: env: - name: NACOS_REPLICAS value: "3" + - name: MYSQL_SERVICE_HOST + valueFrom: + configMapKeyRef: + name: nacos-cm + key: mysql.host - name: MYSQL_SERVICE_DB_NAME valueFrom: configMapKeyRef: @@ -99,14 +105,14 @@ spec: configMapKeyRef: name: nacos-cm key: mysql.password + - name: SPRING_DATASOURCE_PLATFORM + value: "mysql" - name: NACOS_SERVER_PORT value: "8848" - name: NACOS_APPLICATION_PORT value: "8848" - name: PREFER_HOST_MODE value: "hostname" - - name: SPRING_DATASOURCE_PLATFORM - value: "mysql" - name: NACOS_SERVERS value: "nacos-0.nacos-headless.default.svc.cluster.local:8848 nacos-1.nacos-headless.default.svc.cluster.local:8848 nacos-2.nacos-headless.default.svc.cluster.local:8848" selector: diff --git a/helm/templates/ingress.yaml b/helm/templates/ingress.yaml index 7e93972..8445aa0 100644 --- a/helm/templates/ingress.yaml +++ b/helm/templates/ingress.yaml @@ -22,6 +22,9 @@ spec: secretName: {{ .secretName }} {{- end }} {{- end }} + {{- if .Values.ingress.ingressClassName }} + ingressClassName: {{ .Values.ingress.ingressClassName }} + {{- end }} {{- if eq .Values.ingress.apiVersion "networking.k8s.io/v1" }} rules: {{- range .Values.ingress.hosts }} diff --git a/helm/templates/statefulset.yaml b/helm/templates/statefulset.yaml index 89211e4..854ad2a 100644 --- a/helm/templates/statefulset.yaml +++ b/helm/templates/statefulset.yaml @@ -43,7 +43,7 @@ spec: initContainers: - name: peer-finder-plugin-install image: {{.Values.nacos.plugin.image.repository}}:{{.Values.nacos.plugin.image.tag}} - imagePullPolicy: Always + imagePullPolicy: {{ .Values.nacos.plugin.image.pullPolicy }} volumeMounts: - mountPath: /home/nacos/plugins/peer-finder name: data diff --git a/helm/values.yaml b/helm/values.yaml index 49cb41b..08f8a20 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -18,6 +18,7 @@ nacos: image: repository: nacos/nacos-peer-finder-plugin tag: 1.1 + pullPolicy: IfNotPresent replicaCount: 1 podManagementPolicy: Parallel domainName: cluster.local @@ -56,11 +57,15 @@ service: ingress: enabled: false - #apiVersion: networking.k8s.io/v1 - apiVersion: extensions/v1beta1 + # apiVersion: extensions/v1beta1 + apiVersion: networking.k8s.io/v1 annotations: { } # kubernetes.io/ingress.class: nginx - # kubernetes.io/tls-acme: "true" + # kubernetes.io/tls-acme: "true" + # For Kubernetes >= 1.18 you should specify the ingress-controller via the field ingressClassName + # See https://kubernetes.io/blog/2020/04/02/improvements-to-the-ingress-api-in-kubernetes-1.18/#specifying-the-class-of-an-ingress + # ingressClassName: nginx + ingressClassName: "nginx" hosts: - host: nacos.example.com #paths: [ ] diff --git a/operator/Dockerfile b/operator/Dockerfile index 6102743..0530af6 100644 --- a/operator/Dockerfile +++ b/operator/Dockerfile @@ -1,5 +1,7 @@ # Build the manager binary + FROM golang:1.16 AS builder +ARG TARGETARCH WORKDIR /workspace # Copy the Go Modules manifests @@ -18,7 +20,7 @@ COPY pkg/ pkg/ ADD https://raw.githubusercontent.com/alibaba/nacos/develop/distribution/conf/mysql-schema.sql config/sql/nacos-mysql.sql # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -v -o manager main.go +RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on go build -a -v -o manager main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/operator/Makefile b/operator/Makefile index f6508c3..1d350ef 100644 --- a/operator/Makefile +++ b/operator/Makefile @@ -92,7 +92,7 @@ ifeq (, $(shell which controller-gen)) CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\ cd $$CONTROLLER_GEN_TMP_DIR ;\ go mod init tmp ;\ - go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\ + go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.6.2 ;\ rm -rf $$CONTROLLER_GEN_TMP_DIR ;\ } CONTROLLER_GEN=$(GOBIN)/controller-gen @@ -107,7 +107,7 @@ ifeq (, $(shell which kustomize)) KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\ cd $$KUSTOMIZE_GEN_TMP_DIR ;\ go mod init tmp ;\ - go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\ + go get sigs.k8s.io/kustomize/kustomize/v4@v4.1.2 ;\ rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\ } KUSTOMIZE=$(GOBIN)/kustomize diff --git a/operator/api/v1alpha1/nacos_types.go b/operator/api/v1alpha1/nacos_types.go index c825657..911df58 100644 --- a/operator/api/v1alpha1/nacos_types.go +++ b/operator/api/v1alpha1/nacos_types.go @@ -45,15 +45,25 @@ type NacosSpec struct { // 自定义配置 // 部署模式 - Type string `json:"type,omitempty"` - Database Database `json:"database,omitempty"` - Volume Storage `json:"volume,omitempty"` + Type string `json:"type,omitempty"` + FunctionMode string `json:"function_mode,omitempty"` + Database Database `json:"database,omitempty"` + Volume Storage `json:"volume,omitempty"` // 配置文件 Config string `json:"config,omitempty"` + // 开启认证 + Certification Certification `json:"certification,omitempty"` // 通用k8s配置包装器 K8sWrapper K8sWrapper `json:"k8sWrapper,omitempty"` } +type Certification struct { + Enabled bool `json:"enabled,omitempty"` + Token string `json:"token,omitempty"` + TokenExpireSeconds string `json:"token_expire_seconds,omitempty"` + CacheEnabled bool `json:"cache_enabled,omitempty"` +} + type K8sWrapper struct { PodSpec PodSpecWrapper `json:"PodSpec,omitempty"` } diff --git a/operator/api/v1alpha1/zz_generated.deepcopy.go b/operator/api/v1alpha1/zz_generated.deepcopy.go index f8e8308..b9d2d40 100644 --- a/operator/api/v1alpha1/zz_generated.deepcopy.go +++ b/operator/api/v1alpha1/zz_generated.deepcopy.go @@ -26,6 +26,21 @@ import ( runtime "k8s.io/apimachinery/pkg/runtime" ) +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Certification) DeepCopyInto(out *Certification) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Certification. +func (in *Certification) DeepCopy() *Certification { + if in == nil { + return nil + } + out := new(Certification) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Condition) DeepCopyInto(out *Condition) { *out = *in @@ -200,6 +215,7 @@ func (in *NacosSpec) DeepCopyInto(out *NacosSpec) { } out.Database = in.Database in.Volume.DeepCopyInto(&out.Volume) + out.Certification = in.Certification in.K8sWrapper.DeepCopyInto(&out.K8sWrapper) } diff --git a/operator/chart/nacos-operator/nacos-operator-all.yaml b/operator/chart/nacos-operator/nacos-operator-all.yaml index 2224b04..f4a15a8 100644 --- a/operator/chart/nacos-operator/nacos-operator-all.yaml +++ b/operator/chart/nacos-operator/nacos-operator-all.yaml @@ -1281,6 +1281,15 @@ rules: - patch - list - watch + - apiGroups: + - coordination.k8s.io + resources: + - leases + verbs: + - create + - get + - list + - update --- # Source: nacos-operator/templates/serviceaccount.yaml apiVersion: rbac.authorization.k8s.io/v1 diff --git a/operator/chart/nacos-operator/templates/serviceaccount.yaml b/operator/chart/nacos-operator/templates/serviceaccount.yaml index ac018f3..661732b 100644 --- a/operator/chart/nacos-operator/templates/serviceaccount.yaml +++ b/operator/chart/nacos-operator/templates/serviceaccount.yaml @@ -66,5 +66,16 @@ rules: - patch - list - watch + - apiGroups: + - coordination.k8s.io + resources: + - leases + verbs: + - get + - create + - update + - patch + - list + - watch {{- end }} diff --git a/operator/config/crd/bases/nacos.io_nacos.yaml b/operator/config/crd/bases/nacos.io_nacos.yaml index fb590df..85d205c 100644 --- a/operator/config/crd/bases/nacos.io_nacos.yaml +++ b/operator/config/crd/bases/nacos.io_nacos.yaml @@ -1,4 +1,3 @@ - --- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition @@ -880,6 +879,18 @@ spec: type: array type: object type: object + certification: + description: 开启认证 + properties: + cache_enabled: + type: boolean + enabled: + type: boolean + token: + type: string + token_expire_seconds: + type: string + type: object config: description: 配置文件 type: string @@ -1000,6 +1011,8 @@ spec: - name type: object type: array + function_mode: + type: string image: description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster Important: Run "make" to regenerate code after modifying this file diff --git a/operator/pkg/service/k8s/statefulset.go b/operator/pkg/service/k8s/statefulset.go index c8fedac..361153d 100644 --- a/operator/pkg/service/k8s/statefulset.go +++ b/operator/pkg/service/k8s/statefulset.go @@ -121,12 +121,13 @@ func (s *StatefulSetService) CreateOrUpdateStatefulSet(namespace string, statefu // namespace is our spec(https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#concurrency-control-and-consistency), // we will replace the current namespace state. - dAtA, _ := json.Marshal(storedStatefulSet.Spec.Template.Spec.Containers[0].Resources) - dAtB, _ := json.Marshal(statefulSet.Spec.Template.Spec.Containers[0].Resources) - if !bytes.Equal(dAtA, dAtB) || - *statefulSet.Spec.Replicas != *storedStatefulSet.Spec.Replicas { + switch checkSts(storedStatefulSet, statefulSet) { + case Update: statefulSet.ResourceVersion = storedStatefulSet.ResourceVersion return s.UpdateStatefulSet(namespace, statefulSet) + //updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden + case Delete: + return s.DeleteStatefulSet(namespace, storedStatefulSet.Name) } return nil } @@ -141,3 +142,55 @@ func (s *StatefulSetService) DeleteStatefulSet(namespace, name string) error { func (s *StatefulSetService) ListStatefulSets(namespace string) (*appsv1.StatefulSetList, error) { return s.kubeClient.AppsV1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{}) } + +type operator int + +const ( + None operator = iota + Update + Delete +) + +// check whether delete sts +func checkSts(old *appsv1.StatefulSet, new *appsv1.StatefulSet) operator { + + rsA, _ := json.Marshal(old.Spec.Template.Spec.Containers[0].Resources) + rsB, _ := json.Marshal(new.Spec.Template.Spec.Containers[0].Resources) + + envA, _ := json.Marshal(old.Spec.Template.Spec.Containers[0].Env) + envB, _ := json.Marshal(new.Spec.Template.Spec.Containers[0].Env) + + if checkVolumeClaimTemplates(old, new) { + return Delete + } + + if !bytes.Equal(rsA, rsB) || *old.Spec.Replicas != *new.Spec.Replicas || !bytes.Equal(envA, envB) { + return Update + } + + return None +} + +// check whether delete sts +func checkVolumeClaimTemplates(old *appsv1.StatefulSet, new *appsv1.StatefulSet) bool { + ov := old.Spec.VolumeClaimTemplates + nv := new.Spec.VolumeClaimTemplates + if len(ov) == 0 && len(nv) == 0 { + return false + } + + if len(ov) != len(nv) { + return true + } + + if len(ov) > 0 && len(nv) > 0 { + vmA, _ := json.Marshal(old.Spec.VolumeClaimTemplates[0].Spec.Resources) + vmB, _ := json.Marshal(new.Spec.VolumeClaimTemplates[0].Spec.Resources) + + oscn := old.Spec.VolumeClaimTemplates[0].Spec.StorageClassName + nscn := new.Spec.VolumeClaimTemplates[0].Spec.StorageClassName + return !bytes.Equal(vmA, vmB) || !strings.EqualFold(*oscn, *nscn) + } + + return false +} diff --git a/operator/pkg/service/nacos/cluster.go b/operator/pkg/service/nacos/cluster.go index 2630d92..97dc234 100644 --- a/operator/pkg/service/nacos/cluster.go +++ b/operator/pkg/service/nacos/cluster.go @@ -3,7 +3,7 @@ package nacosClient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" "strings" @@ -57,7 +57,7 @@ func (c *NacosClient) GetClusterNodes(ip string) (ServersInfo, error) { return servers, err } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return servers, err } diff --git a/operator/pkg/service/operator/Kind.go b/operator/pkg/service/operator/Kind.go index a4b452f..e7edc99 100644 --- a/operator/pkg/service/operator/Kind.go +++ b/operator/pkg/service/operator/Kind.go @@ -2,7 +2,6 @@ package operator import ( "fmt" - "io/ioutil" batchv1 "k8s.io/api/batch/v1" "k8s.io/apimachinery/pkg/runtime" "nacos.io/nacos-operator/pkg/util/merge" @@ -112,10 +111,37 @@ func (e *KindClient) generateClientSvcName(nacos *nacosgroupv1alpha1.Nacos) stri // CR格式验证 func (e *KindClient) ValidationField(nacos *nacosgroupv1alpha1.Nacos) { + setDefaultValue := []func(nacos *nacosgroupv1alpha1.Nacos){ + setDefaultNacosType, + setDefaultMysql, + setDefaultCertification, + } + + for _, f := range setDefaultValue { + f(nacos) + } +} + +func setDefaultNacosType(nacos *nacosgroupv1alpha1.Nacos) { + // 默认设置单节点 if nacos.Spec.Type == "" { nacos.Spec.Type = "standalone" } +} + +func setDefaultCertification(nacos *nacosgroupv1alpha1.Nacos) { + // 默认设置认证参数 + if nacos.Spec.Certification.Enabled { + if nacos.Spec.Certification.Token == "" { + nacos.Spec.Certification.Token = "SecretKey012345678901234567890123456789012345678901234567890123456789" + } + if nacos.Spec.Certification.TokenExpireSeconds == "" { + nacos.Spec.Certification.TokenExpireSeconds = "18000" + } + } +} +func setDefaultMysql(nacos *nacosgroupv1alpha1.Nacos) { // 默认设置内置数据库 if nacos.Spec.Database.TypeDatabase == "" { nacos.Spec.Database.TypeDatabase = "embedded" @@ -355,7 +381,7 @@ func (e *KindClient) buildJob(nacos *nacosgroupv1alpha1.Nacos) *batchv1.Job { func readSql(sqlFileName string) string { // abspath:项目的根路径 abspath, _ := filepath.Abs("") - bytes, err := ioutil.ReadFile(abspath + "/config/sql/" + sqlFileName) + bytes, err := os.ReadFile(abspath + "/config/sql/" + sqlFileName) if err != nil { fmt.Printf("read sql file failed, err: %s", err.Error()) return "" @@ -424,6 +450,11 @@ func (e *KindClient) buildClientService(nacos *nacosgroupv1alpha1.Nacos) *v1.Ser Port: NACOS_PORT, Protocol: "TCP", }, + { + Name: "rpc", + Port: 9848, + Protocol: "TCP", + }, }, Selector: labels, }, @@ -451,6 +482,42 @@ func (e *KindClient) buildStatefulset(nacos *nacosgroupv1alpha1.Nacos) *appv1.St Value: "hostname", }) + switch nacos.Spec.FunctionMode { + case "naming": + env = append(nacos.Spec.Env, v1.EnvVar{ + Name: "FUNCTION_MODE", + Value: "naming", + }) + case "config": + env = append(nacos.Spec.Env, v1.EnvVar{ + Name: "FUNCTION_MODE", + Value: "config", + }) + } + + // 设置认证环境变量 + if nacos.Spec.Certification.Enabled { + env = append(env, v1.EnvVar{ + Name: "NACOS_AUTH_ENABLE", + Value: strconv.FormatBool(nacos.Spec.Certification.Enabled), + }) + + env = append(env, v1.EnvVar{ + Name: "NACOS_AUTH_TOKEN_EXPIRE_SECONDS", + Value: nacos.Spec.Certification.TokenExpireSeconds, + }) + + env = append(env, v1.EnvVar{ + Name: "NACOS_AUTH_TOKEN", + Value: nacos.Spec.Certification.Token, + }) + + env = append(env, v1.EnvVar{ + Name: "NACOS_AUTH_CACHE_ENABLE", + Value: strconv.FormatBool(nacos.Spec.Certification.CacheEnabled), + }) + } + // 数据库设置 if nacos.Spec.Database.TypeDatabase == "embedded" { env = append(env, v1.EnvVar{ @@ -527,6 +594,17 @@ func (e *KindClient) buildStatefulset(nacos *nacosgroupv1alpha1.Nacos) *appv1.St { Name: nacos.Name, Image: nacos.Spec.Image, + Lifecycle: &v1.Lifecycle{ + PreStop: &v1.Handler{ + Exec: &v1.ExecAction{ + Command: []string{ + "/bin/sh", + "-c", + "rm -rf /home/nacos/data/protocol/raft", + }, + }, + }, + }, Ports: []v1.ContainerPort{ { Name: "client",