Skip to content

Commit

Permalink
refactor: get services dynamically in cluster environment
Browse files Browse the repository at this point in the history
  • Loading branch information
yjinjo committed Jan 17, 2025
1 parent a6e1090 commit ec982ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
dist/
test/yaml/
.DS_Store
9 changes: 6 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
21 changes: 13 additions & 8 deletions pkg/configs/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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:], ".")

Expand Down

0 comments on commit ec982ef

Please sign in to comment.