Skip to content

Commit

Permalink
Init nacos-k8s-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecialYang committed Jul 20, 2021
1 parent ce7cc80 commit 75fa9af
Show file tree
Hide file tree
Showing 30 changed files with 2,158 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/

23 changes: 23 additions & 0 deletions .helm/nacos-k8s-sync/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions .helm/nacos-k8s-sync/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: nacos-k8s-sync
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.0.3"
62 changes: 62 additions & 0 deletions .helm/nacos-k8s-sync/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "nacos-k8s-sync.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "nacos-k8s-sync.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "nacos-k8s-sync.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "nacos-k8s-sync.labels" -}}
helm.sh/chart: {{ include "nacos-k8s-sync.chart" . }}
{{ include "nacos-k8s-sync.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "nacos-k8s-sync.selectorLabels" -}}
app.kubernetes.io/name: {{ include "nacos-k8s-sync.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "nacos-k8s-sync.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "nacos-k8s-sync.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
56 changes: 56 additions & 0 deletions .helm/nacos-k8s-sync/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "nacos-k8s-sync.fullname" . }}
namespace: {{ .Values.global.namespace }}
labels:
{{- include "nacos-k8s-sync.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "nacos-k8s-sync.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "nacos-k8s-sync.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: nacos-k8s-sync-sa
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command:
- "/main"
args:
- --serversIP={{ .Values.global.mseAddr }}
- --serverPort={{ .Values.global.msePort }}
- --appNamespace={{ .Values.global.namespace }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
9 changes: 9 additions & 0 deletions .helm/nacos-k8s-sync/templates/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: nacos-k8s-sync-{{ .Values.global.namespace }}
namespace: {{ .Values.global.namespace }}
rules:
- apiGroups: [""]
resources: ["services", "endpoints"]
verbs: ["get", "watch", "list"]
13 changes: 13 additions & 0 deletions .helm/nacos-k8s-sync/templates/rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: nacos-k8s-sync-{{ .Values.global.namespace }}
namespace: {{ .Values.global.namespace }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: nacos-k8s-sync-{{ .Values.global.namespace }}
subjects:
- kind: ServiceAccount
name: nacos-k8s-sync-sa
namespace: {{ .Values.global.namespace }}
11 changes: 11 additions & 0 deletions .helm/nacos-k8s-sync/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: nacos-k8s-sync-sa
namespace: {{ .Values.global.namespace }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
66 changes: 66 additions & 0 deletions .helm/nacos-k8s-sync/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Default values for nacos-k8s-sync.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1


global:
namespace: "istio-system"
mseAddr: ""
msePort: "8848"

autoscaling:
enabled: false

image:
repository: registry.cn-zhangjiakou.aliyuncs.com/arch-nlp/nacos-sync
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: ""

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
# Specifies whether a service account should be created
create: true
# Annotations to add to the service account
annotations: {}
# The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template
name: "nacos-k8s-sync-sa"

podAnnotations: {}

podSecurityContext: {}
# fsGroup: 2000

securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000

service:
type: ClusterIP
port: 80


resources:
limits:
cpu: 2
memory: 1024Mi
requests:
cpu: 500m
memory: 512Mi


nodeSelector: {}

tolerations: []

affinity: {}
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.15.3 AS build


WORKDIR /src
COPY . .
RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN STATIC=0 GOOS=linux GOARCH=amd64 LDFLAGS='-extldflags -static -s -w' go build -o main ./cmd/nacos-k8s-sync

FROM ubuntu:20.04
WORKDIR /
COPY --from=build /src/main /main
CMD ["./main"]
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FINDFILES=find . \( -path ./common-protos -o -path ./.git -o -path ./out -o -path ./.github -o -path ./licenses -o -path ./vendor \) -prune -o -type f

tidy-go:
@go mod tidy

lint-go:
@${FINDFILES} -name '*.go' \( ! \( -name '*.gen.go' -o -name '*.pb.go' \) \) -print0 | ${XARGS} common/scripts/lint_go.sh

format-go: tidy-go
@${FINDFILES} -name '*.go' \( ! \( -name '*.gen.go' -o -name '*.pb.go' \) \) -print0 | ${XARGS} common/scripts/format_go.sh

precommit: format-go lint-go
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# nacos-k8s-sync
A plugin for sync services from k8s to nacos.
### 简介
该项目用于同步Kubernetes和Nacos之间的服务信息。

目前该项目仅支持 Kubernetes Service -> Nacos Service 的同步

### TODO
- ~~增加高性能zap的logger~~
- 增加 Nacos Service -> Kubernetes Service 的同步
- 监听K8s集群中的多个Namespace
- Nacos支持多Namespace注册
- 服务信息的获取方式的兜底方案,比如从Service的Spec获取
- 单元测试

### 代码提交需知
- 需要运行一下 `make precommit`,处理完imports排序后并保证编译成功,方可提交
34 changes: 34 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"flag"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/nacos-group/nacos-k8s-sync/pkg/logger"
)

// WaitSignal awaits for SIGINT or SIGTERM and closes the channel
func WaitSignal(stop chan struct{}) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
close(stop)
logger.Sync()
}

// AddFlags adds all command line flags to the given command.
func AddFlags(rootCmd *cobra.Command) {
rootCmd.Flags().AddGoFlagSet(flag.CommandLine)
}

// PrintFlags logs the flags in the flagset
func PrintFlags(flags *pflag.FlagSet) {
flags.VisitAll(func(flag *pflag.Flag) {
logger.Infof("FLAG: --%s=%q", flag.Name, flag.Value)
})
}
Loading

0 comments on commit 75fa9af

Please sign in to comment.