Skip to content

Commit

Permalink
Merge pull request #24 from whalecold/feat/customize
Browse files Browse the repository at this point in the history
feat/cusomize: support customize parameter by env
  • Loading branch information
CoderPoet authored Jan 12, 2024
2 parents 7ea3f2d + eef272c commit 64bbf75
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 8 deletions.
8 changes: 7 additions & 1 deletion core/manager/auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (
clusterIDEnvKey = "ISTIO_META_CLUSTER_ID"

clusterIDMetadataKey = "clusterid" // Istiod retrieves clusterid and use it for auth of JWT.

// the env for customize jwt token path
KitexXdsTokenPath = "KITEX_XDS_SA_TOKEN_PATH"
)

var (
Expand Down Expand Up @@ -103,7 +106,10 @@ var jwtTokenValueFmt = func(jwtToken string) string {
}

func getJWTToken() (string, error) {
saToken := jwtTokenPath
saToken := os.Getenv(KitexXdsTokenPath)
if saToken == "" {
saToken = jwtTokenPath
}

token, err := os.ReadFile(saToken)
if err != nil {
Expand Down
47 changes: 42 additions & 5 deletions core/manager/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,40 @@ const (
PodName = "POD_NAME"
MetaNamespace = "NAMESPACE"
InstanceIP = "INSTANCE_IP"
IstiodAddr = "istiod.istio-system.svc:15010"
KitexXdsDomain = "KITEX_XDS_DOMAIN"
// use json to marshal it.
KitexXdsMetas = "KITEX_XDS_METAS"
IstiodSvrName = "istiod.istio-system.svc"
IstioVersion = "ISTIO_VERSION"

IstioAddrEnvKey = "KITEX_XDS_ISTIO_ADDR"
IstioServiceNameEnvKey = "KITEX_XDS_ISTIO_SERVICE_NAME"
IstioAuthEnvKey = "KITEX_XDS_ISTIO_AUTH"
IstioVersion = "ISTIO_VERSION"
IstioMetaInstanceIPs = "INSTANCE_IPS"
)

var (
IstiodAddr = "istiod.istio-system.svc:15010"
IstiodSvrName = "istiod.istio-system.svc"
IstioAuthEnable = false
)

func init() {
istiodAddr := os.Getenv(IstioAddrEnvKey)
if istiodAddr != "" {
IstiodAddr = istiodAddr
}

istiodSvrName := os.Getenv(IstioServiceNameEnvKey)
if istiodSvrName != "" {
IstiodSvrName = istiodSvrName
}

istiodAuthEnable := os.Getenv(IstioAuthEnvKey)
if istiodAuthEnable == "true" {
IstioAuthEnable = true
}
}

type BootstrapConfig struct {
// The namespace to make up fqdn.
// Use POD_NAMESPACE default, the meta namespace will override that if set.
Expand All @@ -66,7 +92,7 @@ func (xsc XDSServerConfig) GetFetchXDSTimeout() time.Duration {
return xsc.FetchXDSTimeout
}

func parseMetaEnvs(envs, istioVersion string) *structpb.Struct {
func parseMetaEnvs(envs, istioVersion, podIP string) *structpb.Struct {
defaultMeta := &structpb.Struct{
Fields: map[string]*structpb.Value{
IstioVersion: {
Expand All @@ -86,6 +112,17 @@ func parseMetaEnvs(envs, istioVersion string) *structpb.Struct {
klog.Warnf("[Kitex] XDS meta info is invalid %s, error %v", envs, err)
return defaultMeta
}
if ips, ok := pbmeta.Fields[IstioMetaInstanceIPs]; ok {
existips := ips.GetStringValue()
if existips == "" {
existips = podIP
} else if !strings.Contains(existips, podIP) {
existips = existips + "," + podIP
}
pbmeta.Fields[IstioMetaInstanceIPs] = &structpb.Value{
Kind: &structpb.Value_StringValue{StringValue: existips},
}
}
return pbmeta
}

Expand Down Expand Up @@ -161,7 +198,7 @@ func newBootstrapConfig(config *XDSServerConfig) (*BootstrapConfig, error) {
configNamespace: namespace,
node: &v3core.Node{
Id: nodeId(podIP, podName, namespace, nodeDomain),
Metadata: parseMetaEnvs(os.Getenv(KitexXdsMetas), istioVersion),
Metadata: parseMetaEnvs(os.Getenv(KitexXdsMetas), istioVersion, podIP),
},
xdsSvrCfg: config,
}
Expand Down
65 changes: 64 additions & 1 deletion core/manager/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ func TestParseMetaEnvs(t *testing.T) {
istioVersion string
want *structpb.Struct
}{
{
desc: "success",
envs: `{"cluster": "c1", "domain": "d1", "ISTIO_VERSION": "1.16.5","INSTANCE_IPS": "1.2.3.4"}`,
istioVersion: "1.16.3",
want: &structpb.Struct{
Fields: map[string]*structpb.Value{
IstioVersion: {
Kind: &structpb.Value_StringValue{StringValue: "1.16.5"},
},
IstioMetaInstanceIPs: {
Kind: &structpb.Value_StringValue{StringValue: "1.2.3.4,localhost"},
},
"cluster": {
Kind: &structpb.Value_StringValue{StringValue: "c1"},
},
"domain": {
Kind: &structpb.Value_StringValue{StringValue: "d1"},
},
},
},
},
{
desc: "success",
envs: `{"cluster": "c1", "domain": "d1", "ISTIO_VERSION": "1.16.5"}`,
Expand All @@ -103,6 +124,48 @@ func TestParseMetaEnvs(t *testing.T) {
},
},
},
{
desc: "success",
envs: `{"cluster": "c1", "domain": "d1", "ISTIO_VERSION": "1.16.5","INSTANCE_IPS": ""}`,
istioVersion: "1.16.3",
want: &structpb.Struct{
Fields: map[string]*structpb.Value{
IstioVersion: {
Kind: &structpb.Value_StringValue{StringValue: "1.16.5"},
},
IstioMetaInstanceIPs: {
Kind: &structpb.Value_StringValue{StringValue: "localhost"},
},
"cluster": {
Kind: &structpb.Value_StringValue{StringValue: "c1"},
},
"domain": {
Kind: &structpb.Value_StringValue{StringValue: "d1"},
},
},
},
},
{
desc: "success",
envs: `{"cluster": "c1", "domain": "d1", "ISTIO_VERSION": "1.16.5","INSTANCE_IPS": "localhost"}`,
istioVersion: "1.16.3",
want: &structpb.Struct{
Fields: map[string]*structpb.Value{
IstioVersion: {
Kind: &structpb.Value_StringValue{StringValue: "1.16.5"},
},
IstioMetaInstanceIPs: {
Kind: &structpb.Value_StringValue{StringValue: "localhost"},
},
"cluster": {
Kind: &structpb.Value_StringValue{StringValue: "c1"},
},
"domain": {
Kind: &structpb.Value_StringValue{StringValue: "d1"},
},
},
},
},
{
desc: "default",
envs: ``,
Expand Down Expand Up @@ -130,7 +193,7 @@ func TestParseMetaEnvs(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
got := parseMetaEnvs(tc.envs, tc.istioVersion)
got := parseMetaEnvs(tc.envs, tc.istioVersion, "localhost")
if diff := cmp.Diff(got, tc.want, protocmp.Transform()); diff != "" {
t.Fatalf("the result %s is diff(-got,+want): %s", tc.desc, diff)
}
Expand Down
2 changes: 1 addition & 1 deletion core/manager/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func DefaultOptions() *Options {
XDSSvrConfig: &XDSServerConfig{
SvrAddr: IstiodAddr,
SvrName: IstiodSvrName,
XDSAuth: false,
XDSAuth: IstioAuthEnable,
},
DumpPath: defaultDumpPath,
}
Expand Down

0 comments on commit 64bbf75

Please sign in to comment.