Skip to content

Commit

Permalink
支持将地域作为可选配置,默认通过 TKE 的 metadata 内部接口获取当前地域信息
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Sep 4, 2024
1 parent 7165a53 commit 6e3bb37
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 版本说明

## v1.0.1 (2024-09-04)

* 支持将地域作为可选配置,默认通过 TKE 的 metadata 内部接口获取当前地域信息。

## v1.0.0 (2024-08-28)

* 不兼容变更:`DedicatedCLBService``status` 有不兼容变更。
Expand Down
4 changes: 2 additions & 2 deletions charts/tke-extend-network-controller/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ 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: 1.0.0
version: 1.0.1

# 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: "1.0.0"
appVersion: "1.0.1"
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ metadata:
{{- include "tke-extend-network-controller.labels" . | nindent 4 }}
type: Opaque
stringData:
REGION: '{{ required "A valid .region required!" .Values.region }}'
{{- with .Values.region }}
REGION: '{{ . }}'
{{- end }}
SECRET_ID: '{{ required "A valid .secretID required!" .Values.secretID }}'
SECRET_KEY: '{{ required "A valid .secretKey required!" .Values.secretKey }}'
VPCID: '{{ required "A valid .vpcID required!" .Values.vpcID }}'
Expand Down
42 changes: 17 additions & 25 deletions cmd/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strings"

"github.com/imroc/tke-extend-network-controller/pkg/clb"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -16,27 +15,20 @@ var RootCommand = cobra.Command{
Use: "tke-extend-network-controller",
Short: "A network controller for TKE",
Run: func(cmd *cobra.Command, args []string) {
clb.Init(
viper.GetString(secretId),
viper.GetString(secretKey),
viper.GetString(region),
viper.GetString(vpcId),
viper.GetString(clusterId),
)
runManager()
},
}

const (
metricsBindAddress = "metrics-bind-address"
leaderElect = "leader-elect"
healthProbeBindAddress = "health-probe-bind-address"
secretId = "secret-id"
secretKey = "secret-key"
region = "region"
vpcId = "vpcid"
clusterId = "cluster-id"
workerCount = "worker-count"
metricsBindAddressFlag = "metrics-bind-address"
leaderElectFlag = "leader-elect"
healthProbeBindAddressFlag = "health-probe-bind-address"
secretIdFlag = "secret-id"
secretKeyFlag = "secret-key"
regionFlag = "region"
vpcIdFlag = "vpcid"
clusterIdFlag = "cluster-id"
workerCountFlag = "worker-count"
)

var (
Expand All @@ -51,14 +43,14 @@ func init() {
flags := RootCommand.Flags()
zapOptions.BindFlags(flag.CommandLine)
flags.AddGoFlagSet(flag.CommandLine)
addIntFlag(flags, workerCount, 1, "The worker count of each controller.")
addStringFlag(flags, metricsBindAddress, "0", "The address the metrics endpoint binds to. Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
addStringFlag(flags, healthProbeBindAddress, ":8081", "The address the probe endpoint binds to.")
addBoolFlag(flags, leaderElect, false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
addStringFlag(flags, secretId, "", "Secret ID")
addStringFlag(flags, secretKey, "", "Secret Key")
addStringFlag(flags, region, "", "The region of TKE cluster")
addStringFlag(flags, vpcId, "", "The VPC ID of TKE cluster")
addIntFlag(flags, workerCountFlag, 1, "The worker count of each controller.")
addStringFlag(flags, metricsBindAddressFlag, "0", "The address the metrics endpoint binds to. Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
addStringFlag(flags, healthProbeBindAddressFlag, ":8081", "The address the probe endpoint binds to.")
addBoolFlag(flags, leaderElectFlag, false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
addStringFlag(flags, secretIdFlag, "", "Secret ID")
addStringFlag(flags, secretKeyFlag, "", "Secret Key")
addStringFlag(flags, regionFlag, "", "The region of TKE cluster")
addStringFlag(flags, vpcIdFlag, "", "The VPC ID of TKE cluster")
}

func addStringFlag(flags *pflag.FlagSet, name, value, usage string) {
Expand Down
41 changes: 28 additions & 13 deletions cmd/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"github.com/spf13/viper"

"github.com/imroc/tke-extend-network-controller/internal/controller"
"github.com/imroc/tke-extend-network-controller/pkg/clb"
"github.com/imroc/tke-extend-network-controller/pkg/kube"
"github.com/imroc/tke-extend-network-controller/pkg/manager"
"github.com/imroc/tke-extend-network-controller/pkg/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand Down Expand Up @@ -35,16 +37,35 @@ func init() {
}

func runManager() {
metricsAddr := viper.GetString(metricsBindAddress)
probeAddr := viper.GetString(healthProbeBindAddress)
enableLeaderElection := viper.GetBool(leaderElect)
workers := viper.GetInt(workerCount)
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zapOptions)))

region := viper.GetString(regionFlag)
if region == "" {
var err error
setupLog.Info("no region specified, trying to get current region from metadata api")
region, err = util.GetCurrentRegion()
if err != nil {
setupLog.Error(err, "failed to get current region")
os.Exit(1)
}
}
setupLog.Info("use region " + region)
clb.Init(
viper.GetString(secretIdFlag),
viper.GetString(secretKeyFlag),
region,
viper.GetString(vpcIdFlag),
viper.GetString(clusterIdFlag),
)

metricsAddr := viper.GetString(metricsBindAddressFlag)
probeAddr := viper.GetString(healthProbeBindAddressFlag)
enableLeaderElection := viper.GetBool(leaderElectFlag)
workers := viper.GetInt(workerCountFlag)
if workers <= 0 {
workers = 1
}

ctrl.SetLogger(zap.New(zap.UseFlagOptions(zapOptions)))

mgr, err := ctrl.NewManager(
ctrl.GetConfigOrDie(),
manager.GetOptions(scheme, metricsAddr, probeAddr, enableLeaderElection),
Expand All @@ -63,13 +84,7 @@ func runManager() {
setupLog.Error(err, "unable to create controller", "controller", "DedicatedCLBService")
os.Exit(1)
}
// if err = (&controller.DedicatedNatgwServiceReconciler{
// Client: mgr.GetClient(),
// Scheme: mgr.GetScheme(),
// }).SetupWithManager(mgr); err != nil {
// setupLog.Error(err, "unable to create controller", "controller", "DedicatedNatgwService")
// os.Exit(1)
// }

if err = (&controller.DedicatedCLBListenerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand Down
1 change: 0 additions & 1 deletion docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ helm repo add tke-extend-network-controller https://imroc.github.io/tke-extend-n
2. 创建 `values.yaml` 并配置:

```yaml
region: "" # TKE 集群所在地域,如 ap-guangzhou。全部地域列表参考: https://cloud.tencent.com/document/product/213/6091
vpcID: "" # TKE 集群所在 VPC ID (vpc-xxx)
clusterID: "" # TKE 集群 ID (cls-xxx)
secretID: "" # 腾讯云子账号的 SecretID
Expand Down
32 changes: 32 additions & 0 deletions pkg/util/region.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package util

import (
"fmt"
"io"
"net/http"
"strings"
"time"
)

func GetCurrentRegion() (string, error) {
client := &http.Client{
Timeout: 5 * time.Second,
}
req, err := http.NewRequest("GET", "http://metadata.tencentyun.com/latest/meta-data/placement/region", nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
region := string(body)
if !strings.HasPrefix(region, "ap-") {
return "", fmt.Errorf("bad region: %s", region)
}
return region, nil
}

0 comments on commit 6e3bb37

Please sign in to comment.