From ec982ef9ccc86d877d137c02be09cd5a0be633d2 Mon Sep 17 00:00:00 2001 From: "yjinjo@berkeley.edu" Date: Fri, 17 Jan 2025 15:36:06 +0900 Subject: [PATCH] refactor: get services dynamically in cluster environment Signed-off-by: yjinjo@berkeley.edu --- .gitignore | 1 + cmd/root.go | 9 ++++++--- pkg/configs/endpoint.go | 21 +++++++++++++-------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index a103f6a..c793342 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea dist/ test/yaml/ +.DS_Store diff --git a/cmd/root.go b/cmd/root.go index 466186e..204af73 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -262,7 +262,9 @@ func addDynamicServiceCommands() error { var apiEndpoint string // For local environment - if strings.HasPrefix(config.Endpoint, "grpc://") { + if strings.Contains(config.Endpoint, ".svc.cluster.local") { + apiEndpoint = endpointName + } else if strings.HasPrefix(config.Endpoint, "grpc://") { endpoint := strings.TrimPrefix(config.Endpoint, "grpc://") conn, err := grpc.Dial(endpoint, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(time.Second)) @@ -319,9 +321,10 @@ func addDynamicServiceCommands() error { } if hasPlugin { - cmd := createServiceCommand(config.Environment) + cmd := createServiceCommand("static") cmd.GroupID = "available" rootCmd.AddCommand(cmd) + return nil } // Add commands for other microservices @@ -334,7 +337,7 @@ func addDynamicServiceCommands() error { return nil } - if strings.HasPrefix(endpointName, "grpc+ssl://") { + if strings.HasPrefix(endpointName, "grpc+ssl://") || strings.HasPrefix(endpointName, "grpc://") { apiEndpoint = endpointName } else if strings.HasPrefix(endpointName, "http://") || strings.HasPrefix(endpointName, "https://") { apiEndpoint, err = configs.GetAPIEndpoint(endpointName) diff --git a/pkg/configs/endpoint.go b/pkg/configs/endpoint.go index d7e58c1..75c3230 100644 --- a/pkg/configs/endpoint.go +++ b/pkg/configs/endpoint.go @@ -170,9 +170,9 @@ func GetServiceEndpoint(config *Environments, serviceName string) (string, error } func FetchEndpointsMap(endpoint string) (map[string]string, error) { - if strings.HasPrefix(endpoint, "grpc://") { + if strings.HasPrefix(endpoint, "grpc://localhost") { endpointsMap := make(map[string]string) - endpointsMap["local"] = endpoint + endpointsMap["static"] = endpoint return endpointsMap, nil } @@ -187,16 +187,21 @@ func FetchEndpointsMap(endpoint string) (map[string]string, error) { if !hasIdentityService { // Handle gRPC+SSL protocol directly - if strings.HasPrefix(endpoint, "grpc+ssl://") { + if strings.HasPrefix(endpoint, "grpc+ssl://") || strings.HasPrefix(endpoint, "grpc://") { + protocol := "grpc+ssl://" + if strings.HasPrefix(endpoint, "grpc://") { + protocol = "grpc://" + } + // Parse the endpoint - parts := strings.Split(endpoint, "/") - endpoint = strings.Join(parts[:len(parts)-1], "/") - parts = strings.Split(endpoint, "://") - if len(parts) != 2 { + hostPart := strings.TrimPrefix(endpoint, protocol) + hostPart = strings.TrimSuffix(hostPart, "/") + + hostParts := strings.Split(hostPart, ".") + if len(hostParts) == 0 { return nil, fmt.Errorf("invalid endpoint format: %s", endpoint) } - hostParts := strings.Split(parts[1], ".") svc := hostParts[0] baseDomain := strings.Join(hostParts[1:], ".")