Skip to content

Commit

Permalink
feat:support sync polaris config to k8s configmap
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Dec 19, 2023
1 parent b993db2 commit 86a867a
Show file tree
Hide file tree
Showing 19 changed files with 1,032 additions and 133 deletions.
81 changes: 81 additions & 0 deletions cmd/polaris-controller/app/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Tencent is pleased to support the open source community by making Polaris available.
//
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

package app

import (
"io/ioutil"

"gopkg.in/yaml.v2"

"github.com/polarismesh/polaris-controller/cmd/polaris-controller/app/options"
"github.com/polarismesh/polaris-controller/common/log"
)

// ServiceSync controller 用到的配置
type ProxyMetadata struct {
ServerAddress string `yaml:"serverAddress"`
ClusterName string `yaml:"clusterName"`
CAAddress string `yaml:"caAddress"`
}

// DefaultConfig controller 用到的配置
type DefaultConfig struct {
ProxyMetadata ProxyMetadata `yaml:"serviceSync"`
}

// SidecarInject sidecar 注入相关
type SidecarInject struct {
Mode string `yaml:"mode"`
Ignores []IgnorePod `yaml:"ignorePods"`
}

type IgnorePod struct {
Namespace string `yaml:"namespace"`
PodName string `yaml:"podName"`
}

type Server struct {
// 健康探测时间间隔
HealthCheckDuration string `yaml:"healthCheckDuration"`
// 定时对账时间间隔
ResyncDuration string `yaml:"resyncDuration"`
}

type controllerConfig struct {
Logger map[string]*log.Options `yaml:"logger"`
ClusterName string `yaml:"clusterName"`
Server Server `yaml:"server"`
ServiceSync *options.ServiceSync `yaml:"serviceSync"`
ConfigSync *options.ConfigSync `yaml:"configSync"`
SidecarInject SidecarInject `yaml:"sidecarInject"`
}

func readConfFromFile() (*controllerConfig, error) {
buf, err := ioutil.ReadFile(MeshFile)
if err != nil {
log.Errorf("read file error, %v", err)
return nil, err
}

c := &controllerConfig{}
err = yaml.Unmarshal(buf, c)
if err != nil {
log.Errorf("unmarshal config error, %v", err)
return nil, err
}

return c, nil
}
45 changes: 41 additions & 4 deletions cmd/polaris-controller/app/options/polaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,45 @@ type PolarisControllerOptions struct {
*PolarisControllerConfiguration
}

// ServiceSync 服务同步相关配置
type ServiceSync struct {
Mode string `yaml:"mode"`
ServerAddress string `yaml:"serverAddress"`
// 以下配置仅 polaris-server 开启 console auth
// 调用 polaris-server OpenAPI 的凭据
PolarisAccessToken string `yaml:"accessToken"`
// Operator 用于数据同步的帐户ID
Operator string `yaml:"operator"`
// Enable 开启同步
Enable bool `yaml:"enable"`
}

// ConfigSync 服务同步相关配置
type ConfigSync struct {
Mode string `yaml:"mode"`
ServerAddress string `yaml:"serverAddress"`
// 以下配置仅 polaris-server 开启 console auth
// 调用 polaris-server OpenAPI 的凭据
PolarisAccessToken string `yaml:"accessToken"`
// Operator 用于数据同步的帐户ID
Operator string `yaml:"operator"`
// AllowDelete 允许向 Polaris 发起删除操作
AllowDelete bool `yaml:"allowDelete"`
// SyncDirection 配置同步方向, kubernetesToPolaris/polarisToKubernetes/both
// kubernetesToPolaris: 配置数据只能从 kubernetes 同步到 polaris
// polarisToKubernetes: 配置数据只能从 polaris 同步到 kubernetes
// both: 配置数据能从 kubernetes 同步到 polaris, 也能从 polaris 同步到 kubernetes, 但是不会出现循环同步
SyncDirection string `yaml:"syncDirection"`
// ConflictMode 同步冲突策略
ConflictMode string `yaml:"conflictMode"`
// Enable 开启同步
Enable bool `yaml:"enable"`
// IgnoreNamespaces 忽略同步的命名空间,默认不忽略
IgnoreNamespaces []string `yaml:"ignoreNamespaces"`
// DefaultGroup 配置分组同步默认分组名称
DefaultGroup string `yaml:"defaultGroup"`
}

// PolarisControllerConfiguration holds configuration for a polaris controller
type PolarisControllerConfiguration struct {
// port is the port that the controller-manager's http service runs on.
Expand All @@ -45,8 +84,8 @@ type PolarisControllerConfiguration struct {
HealthCheckDuration time.Duration
// ResyncDuration 对账任务执行时间
ResyncDuration time.Duration
// SyncConfigMap 是否开启 ConfigMap 同步
SyncConfigMap bool
// ConfigSync 配置中心同步配置
ConfigSync *ConfigSync
}

// AddFlags adds flags related to generic for controller manager to the specified FlagSet.
Expand All @@ -66,7 +105,6 @@ func (o *PolarisControllerOptions) AddFlags(fs *pflag.FlagSet) {
fs.DurationVar(&o.HealthCheckDuration, "healthcheck-duration", time.Second,
"The health checking duration of the polaris server (eg. 5h30m2s).")
fs.DurationVar(&o.ResyncDuration, "resync-duration", time.Second*30, "The resync duration (eg. 5h30m2s).")
fs.BoolVar(&o.SyncConfigMap, "sync-configmap", false, "is open sync configmap to polaris (eg. true/false, default is false).")
}

// ApplyTo fills up generic config with options.
Expand All @@ -86,6 +124,5 @@ func (o *PolarisControllerOptions) ApplyTo(cfg *PolarisControllerConfiguration)
cfg.SidecarMode = o.SidecarMode
cfg.HealthCheckDuration = o.HealthCheckDuration
cfg.ResyncDuration = o.ResyncDuration
cfg.SyncConfigMap = o.SyncConfigMap
return nil
}
65 changes: 3 additions & 62 deletions cmd/polaris-controller/app/polaris-controller-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/polarismesh/polaris-go/api"
"github.com/spf13/cobra"
"google.golang.org/grpc/grpclog"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/server/healthz"
Expand Down Expand Up @@ -211,11 +210,11 @@ func initControllerConfig(s *options.KubeControllerManagerOptions) {
// 4. 配置 polaris-sidecar 注入模式的
s.PolarisController.SidecarMode = config.SidecarInject.Mode
// 设置是否开启同步 ConfigMap
s.PolarisController.SyncConfigMap = config.ServiceSync.EnableSyncConfigMap
s.PolarisController.ConfigSync = config.ConfigSync

// 5.设置健康检查时间以及定时对账时间
s.PolarisController.HealthCheckDuration, _ = time.ParseDuration(config.ServiceSync.HealthCheckDuration)
s.PolarisController.ResyncDuration, _ = time.ParseDuration(config.ServiceSync.ResyncDuration)
s.PolarisController.HealthCheckDuration, _ = time.ParseDuration(config.Server.HealthCheckDuration)
s.PolarisController.ResyncDuration, _ = time.ParseDuration(config.Server.ResyncDuration)

common.PolarisServerAddress = polarisServerAddress
common.PolarisServerGrpcAddress = polarisapi.PolarisGrpc
Expand Down Expand Up @@ -541,64 +540,6 @@ func startPolarisController(ctx ControllerContext) (http.Handler, error) {
return nil, nil
}

// ServiceSync controller 用到的配置
type ProxyMetadata struct {
ServerAddress string `yaml:"serverAddress"`
ClusterName string `yaml:"clusterName"`
CAAddress string `yaml:"caAddress"`
}

// DefaultConfig controller 用到的配置
type DefaultConfig struct {
ProxyMetadata ProxyMetadata `yaml:"serviceSync"`
}

// ResourceSync 服务同步相关配置
type ResourceSync struct {
Mode string `yaml:"mode"`
ServerAddress string `yaml:"serverAddress"`
// 健康探测时间间隔
HealthCheckDuration string `yaml:"healthCheckDuration"`
// 定时对账时间间隔
ResyncDuration string `yaml:"resyncDuration"`
// 以下配置仅 polaris-server 开启 console auth
// 调用 polaris-server OpenAPI 的凭据
PolarisAccessToken string `yaml:"accessToken"`
// Operator 用于数据同步的帐户ID
Operator string `yaml:"operator"`
// EnableSyncConfigMap 开启同步 ConfigMap
EnableSyncConfigMap bool `yaml:"enableSyncConfigMap"`
}

// SidecarInject sidecar 注入相关
type SidecarInject struct {
Mode string `yaml:"mode"`
}

type controllerConfig struct {
Logger map[string]*log.Options `yaml:"logger"`
ClusterName string `yaml:"clusterName"`
ServiceSync *ResourceSync `yaml:"serviceSync"`
SidecarInject SidecarInject `yaml:"sidecarInject"`
}

func readConfFromFile() (*controllerConfig, error) {
buf, err := ioutil.ReadFile(MeshFile)
if err != nil {
log.Errorf("read file error, %v", err)
return nil, err
}

c := &controllerConfig{}
err = yaml.Unmarshal(buf, c)
if err != nil {
log.Errorf("unmarshal config error, %v", err)
return nil, err
}

return c, nil
}

// startPolarisAccountController 启用反对账逻辑,定时从北极星侧拉取通过TKEx注册的服务,对比一下是否在集群中还存在
//func startPolarisAccountController(ctx ControllerContext) (http.Handler, error) {
// go accounting_controller.NewPolarisAccountingController(
Expand Down
19 changes: 18 additions & 1 deletion common/log/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,24 @@ const (
SyncNamingLoggerName = "syncnaming"
// SyncConfigLoggerName config sync logger name, can use FindScope function to get the logger
SyncConfigLoggerName = "syncconfig"
// SyncConfigLoggerName config map sync logger name, can use FindScope function to get the logger
SyncConfigMapLoggerName = "synccm"
// TraceLoggerName trace logger name, can use FindScope function to get the logger
TraceLoggerName = "trace"
)

var (
syncScope = RegisterScope(SyncLoggerName, "sync logging messages.", 0)
injectScope = RegisterScope(InjectLoggerName, "pod inject logging messages.", 0)
syncNamingScope = RegisterScope(SyncNamingLoggerName, "naming sync logging messages.", 0)
syncConfigScope = RegisterScope(SyncConfigLoggerName, "config sync logging messages.", 0)
syncCmScope = RegisterScope(SyncConfigMapLoggerName, "configmap sync logging messages.", 0)
traceScope = RegisterScope(TraceLoggerName, "trace logging messages.", 0)
)

func allLoggerTypes() []string {
return []string{SyncLoggerName, InjectLoggerName, DefaultLoggerName}
return []string{SyncLoggerName, SyncNamingLoggerName, SyncConfigLoggerName,
SyncConfigMapLoggerName, InjectLoggerName, DefaultLoggerName}
}

// DefaultScope default logging scope handler
Expand All @@ -58,7 +65,17 @@ func SyncConfigScope() *Scope {
return syncConfigScope
}

// SyncConfigMapScope naming logging scope handler
func SyncConfigMapScope() *Scope {
return syncCmScope
}

// InjectScope
func InjectScope() *Scope {
return injectScope
}

// TraceScope
func TraceScope() *Scope {
return traceScope
}
6 changes: 6 additions & 0 deletions deploy/kubernetes_v1.21/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ data:
mode: "mesh"
# service sync
serviceSync:
enable: true
mode: "all"
serverAddress: #POLARIS_HOST#
# 北极星开启鉴权时需要配置
accessToken: #POLARIS_TOKEN#
configSync:
enable: true
serverAddress: #POLARIS_HOST#
# 北极星开启鉴权时需要配置
accessToken: #POLARIS_TOKEN#
defaultConfig:
proxyMetadata:
serverAddress: #POLARIS_HOST#
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ spec:
fieldRef:
fieldPath: metadata.namespace
imagePullPolicy: {{ .Values.controller.image.pullPolicy }}
resources:
limits:
cpu: {{ .Values.controller.limit.cpu }}
memory: {{ .Values.controller.limit.memory }}
volumeMounts:
- mountPath: /polaris-controller/log
name: log
Expand All @@ -48,8 +52,6 @@ spec:
mountPath: /etc/polaris-inject/config
readOnly: true
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: qcloudregistrykey
restartPolicy: Always
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
Expand Down
3 changes: 3 additions & 0 deletions deploy/kubernetes_v1.21/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ controller:
repo: polarismesh/polaris-controller
tag: #CONTROLLER_VERSION#
pullPolicy: IfNotPresent
limit:
cpu: 2
memory: 2Gi
metrics:
port: 80
type: ClusterIP
2 changes: 0 additions & 2 deletions deploy/kubernetes_v1.21/polaris-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ spec:
mountPath: /etc/polaris-inject/config
readOnly: true
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: qcloudregistrykey
restartPolicy: Always
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ spec:
fieldRef:
fieldPath: metadata.namespace
imagePullPolicy: {{ .Values.controller.image.pullPolicy }}
resources:
limits:
cpu: {{ .Values.controller.limit.cpu }}
memory: {{ .Values.controller.limit.memory }}
volumeMounts:
- mountPath: /polaris-controller/log
name: log
Expand All @@ -48,8 +52,6 @@ spec:
mountPath: /etc/polaris-inject/config
readOnly: true
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: qcloudregistrykey
restartPolicy: Always
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
Expand Down
3 changes: 3 additions & 0 deletions deploy/kubernetes_v1.22/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ controller:
repo: polarismesh/polaris-controller
tag: #CONTROLLER_VERSION#
pullPolicy: IfNotPresent
limit:
cpu: 2
memory: 2Gi
metrics:
port: 80
type: ClusterIP
Loading

0 comments on commit 86a867a

Please sign in to comment.