Skip to content

Commit a518a6a

Browse files
author
Youngjin Jo
authored
Merge pull request #125 from yjinjo/master
Get services dynamically in cluster environment
2 parents a6e1090 + ec982ef commit a518a6a

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
dist/
33
test/yaml/
4+
.DS_Store

cmd/root.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ func addDynamicServiceCommands() error {
262262
var apiEndpoint string
263263

264264
// For local environment
265-
if strings.HasPrefix(config.Endpoint, "grpc://") {
265+
if strings.Contains(config.Endpoint, ".svc.cluster.local") {
266+
apiEndpoint = endpointName
267+
} else if strings.HasPrefix(config.Endpoint, "grpc://") {
266268
endpoint := strings.TrimPrefix(config.Endpoint, "grpc://")
267269

268270
conn, err := grpc.Dial(endpoint, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(time.Second))
@@ -319,9 +321,10 @@ func addDynamicServiceCommands() error {
319321
}
320322

321323
if hasPlugin {
322-
cmd := createServiceCommand(config.Environment)
324+
cmd := createServiceCommand("static")
323325
cmd.GroupID = "available"
324326
rootCmd.AddCommand(cmd)
327+
return nil
325328
}
326329

327330
// Add commands for other microservices
@@ -334,7 +337,7 @@ func addDynamicServiceCommands() error {
334337
return nil
335338
}
336339

337-
if strings.HasPrefix(endpointName, "grpc+ssl://") {
340+
if strings.HasPrefix(endpointName, "grpc+ssl://") || strings.HasPrefix(endpointName, "grpc://") {
338341
apiEndpoint = endpointName
339342
} else if strings.HasPrefix(endpointName, "http://") || strings.HasPrefix(endpointName, "https://") {
340343
apiEndpoint, err = configs.GetAPIEndpoint(endpointName)

pkg/configs/endpoint.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ func GetServiceEndpoint(config *Environments, serviceName string) (string, error
170170
}
171171

172172
func FetchEndpointsMap(endpoint string) (map[string]string, error) {
173-
if strings.HasPrefix(endpoint, "grpc://") {
173+
if strings.HasPrefix(endpoint, "grpc://localhost") {
174174
endpointsMap := make(map[string]string)
175-
endpointsMap["local"] = endpoint
175+
endpointsMap["static"] = endpoint
176176
return endpointsMap, nil
177177
}
178178

@@ -187,16 +187,21 @@ func FetchEndpointsMap(endpoint string) (map[string]string, error) {
187187

188188
if !hasIdentityService {
189189
// Handle gRPC+SSL protocol directly
190-
if strings.HasPrefix(endpoint, "grpc+ssl://") {
190+
if strings.HasPrefix(endpoint, "grpc+ssl://") || strings.HasPrefix(endpoint, "grpc://") {
191+
protocol := "grpc+ssl://"
192+
if strings.HasPrefix(endpoint, "grpc://") {
193+
protocol = "grpc://"
194+
}
195+
191196
// Parse the endpoint
192-
parts := strings.Split(endpoint, "/")
193-
endpoint = strings.Join(parts[:len(parts)-1], "/")
194-
parts = strings.Split(endpoint, "://")
195-
if len(parts) != 2 {
197+
hostPart := strings.TrimPrefix(endpoint, protocol)
198+
hostPart = strings.TrimSuffix(hostPart, "/")
199+
200+
hostParts := strings.Split(hostPart, ".")
201+
if len(hostParts) == 0 {
196202
return nil, fmt.Errorf("invalid endpoint format: %s", endpoint)
197203
}
198204

199-
hostParts := strings.Split(parts[1], ".")
200205
svc := hostParts[0]
201206
baseDomain := strings.Join(hostParts[1:], ".")
202207

0 commit comments

Comments
 (0)