Skip to content

Commit 64bbf75

Browse files
authored
Merge pull request #24 from whalecold/feat/customize
feat/cusomize: support customize parameter by env
2 parents 7ea3f2d + eef272c commit 64bbf75

File tree

4 files changed

+114
-8
lines changed

4 files changed

+114
-8
lines changed

core/manager/auth/jwt.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const (
3838
clusterIDEnvKey = "ISTIO_META_CLUSTER_ID"
3939

4040
clusterIDMetadataKey = "clusterid" // Istiod retrieves clusterid and use it for auth of JWT.
41+
42+
// the env for customize jwt token path
43+
KitexXdsTokenPath = "KITEX_XDS_SA_TOKEN_PATH"
4144
)
4245

4346
var (
@@ -103,7 +106,10 @@ var jwtTokenValueFmt = func(jwtToken string) string {
103106
}
104107

105108
func getJWTToken() (string, error) {
106-
saToken := jwtTokenPath
109+
saToken := os.Getenv(KitexXdsTokenPath)
110+
if saToken == "" {
111+
saToken = jwtTokenPath
112+
}
107113

108114
token, err := os.ReadFile(saToken)
109115
if err != nil {

core/manager/bootstrap.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,40 @@ const (
3333
PodName = "POD_NAME"
3434
MetaNamespace = "NAMESPACE"
3535
InstanceIP = "INSTANCE_IP"
36-
IstiodAddr = "istiod.istio-system.svc:15010"
3736
KitexXdsDomain = "KITEX_XDS_DOMAIN"
3837
// use json to marshal it.
3938
KitexXdsMetas = "KITEX_XDS_METAS"
40-
IstiodSvrName = "istiod.istio-system.svc"
41-
IstioVersion = "ISTIO_VERSION"
39+
40+
IstioAddrEnvKey = "KITEX_XDS_ISTIO_ADDR"
41+
IstioServiceNameEnvKey = "KITEX_XDS_ISTIO_SERVICE_NAME"
42+
IstioAuthEnvKey = "KITEX_XDS_ISTIO_AUTH"
43+
IstioVersion = "ISTIO_VERSION"
44+
IstioMetaInstanceIPs = "INSTANCE_IPS"
45+
)
46+
47+
var (
48+
IstiodAddr = "istiod.istio-system.svc:15010"
49+
IstiodSvrName = "istiod.istio-system.svc"
50+
IstioAuthEnable = false
4251
)
4352

53+
func init() {
54+
istiodAddr := os.Getenv(IstioAddrEnvKey)
55+
if istiodAddr != "" {
56+
IstiodAddr = istiodAddr
57+
}
58+
59+
istiodSvrName := os.Getenv(IstioServiceNameEnvKey)
60+
if istiodSvrName != "" {
61+
IstiodSvrName = istiodSvrName
62+
}
63+
64+
istiodAuthEnable := os.Getenv(IstioAuthEnvKey)
65+
if istiodAuthEnable == "true" {
66+
IstioAuthEnable = true
67+
}
68+
}
69+
4470
type BootstrapConfig struct {
4571
// The namespace to make up fqdn.
4672
// Use POD_NAMESPACE default, the meta namespace will override that if set.
@@ -66,7 +92,7 @@ func (xsc XDSServerConfig) GetFetchXDSTimeout() time.Duration {
6692
return xsc.FetchXDSTimeout
6793
}
6894

69-
func parseMetaEnvs(envs, istioVersion string) *structpb.Struct {
95+
func parseMetaEnvs(envs, istioVersion, podIP string) *structpb.Struct {
7096
defaultMeta := &structpb.Struct{
7197
Fields: map[string]*structpb.Value{
7298
IstioVersion: {
@@ -86,6 +112,17 @@ func parseMetaEnvs(envs, istioVersion string) *structpb.Struct {
86112
klog.Warnf("[Kitex] XDS meta info is invalid %s, error %v", envs, err)
87113
return defaultMeta
88114
}
115+
if ips, ok := pbmeta.Fields[IstioMetaInstanceIPs]; ok {
116+
existips := ips.GetStringValue()
117+
if existips == "" {
118+
existips = podIP
119+
} else if !strings.Contains(existips, podIP) {
120+
existips = existips + "," + podIP
121+
}
122+
pbmeta.Fields[IstioMetaInstanceIPs] = &structpb.Value{
123+
Kind: &structpb.Value_StringValue{StringValue: existips},
124+
}
125+
}
89126
return pbmeta
90127
}
91128

@@ -161,7 +198,7 @@ func newBootstrapConfig(config *XDSServerConfig) (*BootstrapConfig, error) {
161198
configNamespace: namespace,
162199
node: &v3core.Node{
163200
Id: nodeId(podIP, podName, namespace, nodeDomain),
164-
Metadata: parseMetaEnvs(os.Getenv(KitexXdsMetas), istioVersion),
201+
Metadata: parseMetaEnvs(os.Getenv(KitexXdsMetas), istioVersion, podIP),
165202
},
166203
xdsSvrCfg: config,
167204
}

core/manager/bootstrap_test.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ func TestParseMetaEnvs(t *testing.T) {
8585
istioVersion string
8686
want *structpb.Struct
8787
}{
88+
{
89+
desc: "success",
90+
envs: `{"cluster": "c1", "domain": "d1", "ISTIO_VERSION": "1.16.5","INSTANCE_IPS": "1.2.3.4"}`,
91+
istioVersion: "1.16.3",
92+
want: &structpb.Struct{
93+
Fields: map[string]*structpb.Value{
94+
IstioVersion: {
95+
Kind: &structpb.Value_StringValue{StringValue: "1.16.5"},
96+
},
97+
IstioMetaInstanceIPs: {
98+
Kind: &structpb.Value_StringValue{StringValue: "1.2.3.4,localhost"},
99+
},
100+
"cluster": {
101+
Kind: &structpb.Value_StringValue{StringValue: "c1"},
102+
},
103+
"domain": {
104+
Kind: &structpb.Value_StringValue{StringValue: "d1"},
105+
},
106+
},
107+
},
108+
},
88109
{
89110
desc: "success",
90111
envs: `{"cluster": "c1", "domain": "d1", "ISTIO_VERSION": "1.16.5"}`,
@@ -103,6 +124,48 @@ func TestParseMetaEnvs(t *testing.T) {
103124
},
104125
},
105126
},
127+
{
128+
desc: "success",
129+
envs: `{"cluster": "c1", "domain": "d1", "ISTIO_VERSION": "1.16.5","INSTANCE_IPS": ""}`,
130+
istioVersion: "1.16.3",
131+
want: &structpb.Struct{
132+
Fields: map[string]*structpb.Value{
133+
IstioVersion: {
134+
Kind: &structpb.Value_StringValue{StringValue: "1.16.5"},
135+
},
136+
IstioMetaInstanceIPs: {
137+
Kind: &structpb.Value_StringValue{StringValue: "localhost"},
138+
},
139+
"cluster": {
140+
Kind: &structpb.Value_StringValue{StringValue: "c1"},
141+
},
142+
"domain": {
143+
Kind: &structpb.Value_StringValue{StringValue: "d1"},
144+
},
145+
},
146+
},
147+
},
148+
{
149+
desc: "success",
150+
envs: `{"cluster": "c1", "domain": "d1", "ISTIO_VERSION": "1.16.5","INSTANCE_IPS": "localhost"}`,
151+
istioVersion: "1.16.3",
152+
want: &structpb.Struct{
153+
Fields: map[string]*structpb.Value{
154+
IstioVersion: {
155+
Kind: &structpb.Value_StringValue{StringValue: "1.16.5"},
156+
},
157+
IstioMetaInstanceIPs: {
158+
Kind: &structpb.Value_StringValue{StringValue: "localhost"},
159+
},
160+
"cluster": {
161+
Kind: &structpb.Value_StringValue{StringValue: "c1"},
162+
},
163+
"domain": {
164+
Kind: &structpb.Value_StringValue{StringValue: "d1"},
165+
},
166+
},
167+
},
168+
},
106169
{
107170
desc: "default",
108171
envs: ``,
@@ -130,7 +193,7 @@ func TestParseMetaEnvs(t *testing.T) {
130193
}
131194
for _, tc := range testCases {
132195
t.Run(tc.desc, func(t *testing.T) {
133-
got := parseMetaEnvs(tc.envs, tc.istioVersion)
196+
got := parseMetaEnvs(tc.envs, tc.istioVersion, "localhost")
134197
if diff := cmp.Diff(got, tc.want, protocmp.Transform()); diff != "" {
135198
t.Fatalf("the result %s is diff(-got,+want): %s", tc.desc, diff)
136199
}

core/manager/option.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func DefaultOptions() *Options {
3838
XDSSvrConfig: &XDSServerConfig{
3939
SvrAddr: IstiodAddr,
4040
SvrName: IstiodSvrName,
41-
XDSAuth: false,
41+
XDSAuth: IstioAuthEnable,
4242
},
4343
DumpPath: defaultDumpPath,
4444
}

0 commit comments

Comments
 (0)