Skip to content

Commit 6e3bb37

Browse files
committed
支持将地域作为可选配置,默认通过 TKE 的 metadata 内部接口获取当前地域信息
1 parent 7165a53 commit 6e3bb37

File tree

7 files changed

+86
-42
lines changed

7 files changed

+86
-42
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 版本说明
22

3+
## v1.0.1 (2024-09-04)
4+
5+
* 支持将地域作为可选配置,默认通过 TKE 的 metadata 内部接口获取当前地域信息。
6+
37
## v1.0.0 (2024-08-28)
48

59
* 不兼容变更:`DedicatedCLBService``status` 有不兼容变更。

charts/tke-extend-network-controller/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 1.0.0
18+
version: 1.0.1
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "1.0.0"
24+
appVersion: "1.0.1"

charts/tke-extend-network-controller/templates/secret-env.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ metadata:
77
{{- include "tke-extend-network-controller.labels" . | nindent 4 }}
88
type: Opaque
99
stringData:
10-
REGION: '{{ required "A valid .region required!" .Values.region }}'
10+
{{- with .Values.region }}
11+
REGION: '{{ . }}'
12+
{{- end }}
1113
SECRET_ID: '{{ required "A valid .secretID required!" .Values.secretID }}'
1214
SECRET_KEY: '{{ required "A valid .secretKey required!" .Values.secretKey }}'
1315
VPCID: '{{ required "A valid .vpcID required!" .Values.vpcID }}'

cmd/app/cmd.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/imroc/tke-extend-network-controller/pkg/clb"
98
"github.com/spf13/cobra"
109
"github.com/spf13/pflag"
1110
"github.com/spf13/viper"
@@ -16,27 +15,20 @@ var RootCommand = cobra.Command{
1615
Use: "tke-extend-network-controller",
1716
Short: "A network controller for TKE",
1817
Run: func(cmd *cobra.Command, args []string) {
19-
clb.Init(
20-
viper.GetString(secretId),
21-
viper.GetString(secretKey),
22-
viper.GetString(region),
23-
viper.GetString(vpcId),
24-
viper.GetString(clusterId),
25-
)
2618
runManager()
2719
},
2820
}
2921

3022
const (
31-
metricsBindAddress = "metrics-bind-address"
32-
leaderElect = "leader-elect"
33-
healthProbeBindAddress = "health-probe-bind-address"
34-
secretId = "secret-id"
35-
secretKey = "secret-key"
36-
region = "region"
37-
vpcId = "vpcid"
38-
clusterId = "cluster-id"
39-
workerCount = "worker-count"
23+
metricsBindAddressFlag = "metrics-bind-address"
24+
leaderElectFlag = "leader-elect"
25+
healthProbeBindAddressFlag = "health-probe-bind-address"
26+
secretIdFlag = "secret-id"
27+
secretKeyFlag = "secret-key"
28+
regionFlag = "region"
29+
vpcIdFlag = "vpcid"
30+
clusterIdFlag = "cluster-id"
31+
workerCountFlag = "worker-count"
4032
)
4133

4234
var (
@@ -51,14 +43,14 @@ func init() {
5143
flags := RootCommand.Flags()
5244
zapOptions.BindFlags(flag.CommandLine)
5345
flags.AddGoFlagSet(flag.CommandLine)
54-
addIntFlag(flags, workerCount, 1, "The worker count of each controller.")
55-
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.")
56-
addStringFlag(flags, healthProbeBindAddress, ":8081", "The address the probe endpoint binds to.")
57-
addBoolFlag(flags, leaderElect, false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
58-
addStringFlag(flags, secretId, "", "Secret ID")
59-
addStringFlag(flags, secretKey, "", "Secret Key")
60-
addStringFlag(flags, region, "", "The region of TKE cluster")
61-
addStringFlag(flags, vpcId, "", "The VPC ID of TKE cluster")
46+
addIntFlag(flags, workerCountFlag, 1, "The worker count of each controller.")
47+
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.")
48+
addStringFlag(flags, healthProbeBindAddressFlag, ":8081", "The address the probe endpoint binds to.")
49+
addBoolFlag(flags, leaderElectFlag, false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
50+
addStringFlag(flags, secretIdFlag, "", "Secret ID")
51+
addStringFlag(flags, secretKeyFlag, "", "Secret Key")
52+
addStringFlag(flags, regionFlag, "", "The region of TKE cluster")
53+
addStringFlag(flags, vpcIdFlag, "", "The VPC ID of TKE cluster")
6254
}
6355

6456
func addStringFlag(flags *pflag.FlagSet, name, value, usage string) {

cmd/app/manager.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"github.com/spf13/viper"
77

88
"github.com/imroc/tke-extend-network-controller/internal/controller"
9+
"github.com/imroc/tke-extend-network-controller/pkg/clb"
910
"github.com/imroc/tke-extend-network-controller/pkg/kube"
1011
"github.com/imroc/tke-extend-network-controller/pkg/manager"
12+
"github.com/imroc/tke-extend-network-controller/pkg/util"
1113
ctrl "sigs.k8s.io/controller-runtime"
1214
"sigs.k8s.io/controller-runtime/pkg/healthz"
1315
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -35,16 +37,35 @@ func init() {
3537
}
3638

3739
func runManager() {
38-
metricsAddr := viper.GetString(metricsBindAddress)
39-
probeAddr := viper.GetString(healthProbeBindAddress)
40-
enableLeaderElection := viper.GetBool(leaderElect)
41-
workers := viper.GetInt(workerCount)
40+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zapOptions)))
41+
42+
region := viper.GetString(regionFlag)
43+
if region == "" {
44+
var err error
45+
setupLog.Info("no region specified, trying to get current region from metadata api")
46+
region, err = util.GetCurrentRegion()
47+
if err != nil {
48+
setupLog.Error(err, "failed to get current region")
49+
os.Exit(1)
50+
}
51+
}
52+
setupLog.Info("use region " + region)
53+
clb.Init(
54+
viper.GetString(secretIdFlag),
55+
viper.GetString(secretKeyFlag),
56+
region,
57+
viper.GetString(vpcIdFlag),
58+
viper.GetString(clusterIdFlag),
59+
)
60+
61+
metricsAddr := viper.GetString(metricsBindAddressFlag)
62+
probeAddr := viper.GetString(healthProbeBindAddressFlag)
63+
enableLeaderElection := viper.GetBool(leaderElectFlag)
64+
workers := viper.GetInt(workerCountFlag)
4265
if workers <= 0 {
4366
workers = 1
4467
}
4568

46-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zapOptions)))
47-
4869
mgr, err := ctrl.NewManager(
4970
ctrl.GetConfigOrDie(),
5071
manager.GetOptions(scheme, metricsAddr, probeAddr, enableLeaderElection),
@@ -63,13 +84,7 @@ func runManager() {
6384
setupLog.Error(err, "unable to create controller", "controller", "DedicatedCLBService")
6485
os.Exit(1)
6586
}
66-
// if err = (&controller.DedicatedNatgwServiceReconciler{
67-
// Client: mgr.GetClient(),
68-
// Scheme: mgr.GetScheme(),
69-
// }).SetupWithManager(mgr); err != nil {
70-
// setupLog.Error(err, "unable to create controller", "controller", "DedicatedNatgwService")
71-
// os.Exit(1)
72-
// }
87+
7388
if err = (&controller.DedicatedCLBListenerReconciler{
7489
Client: mgr.GetClient(),
7590
Scheme: mgr.GetScheme(),

docs/install.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ helm repo add tke-extend-network-controller https://imroc.github.io/tke-extend-n
4949
2. 创建 `values.yaml` 并配置:
5050

5151
```yaml
52-
region: "" # TKE 集群所在地域,如 ap-guangzhou。全部地域列表参考: https://cloud.tencent.com/document/product/213/6091
5352
vpcID: "" # TKE 集群所在 VPC ID (vpc-xxx)
5453
clusterID: "" # TKE 集群 ID (cls-xxx)
5554
secretID: "" # 腾讯云子账号的 SecretID

pkg/util/region.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"strings"
8+
"time"
9+
)
10+
11+
func GetCurrentRegion() (string, error) {
12+
client := &http.Client{
13+
Timeout: 5 * time.Second,
14+
}
15+
req, err := http.NewRequest("GET", "http://metadata.tencentyun.com/latest/meta-data/placement/region", nil)
16+
if err != nil {
17+
return "", err
18+
}
19+
resp, err := client.Do(req)
20+
if err != nil {
21+
return "", err
22+
}
23+
body, err := io.ReadAll(resp.Body)
24+
if err != nil {
25+
return "", err
26+
}
27+
region := string(body)
28+
if !strings.HasPrefix(region, "ap-") {
29+
return "", fmt.Errorf("bad region: %s", region)
30+
}
31+
return region, nil
32+
}

0 commit comments

Comments
 (0)