Skip to content

Commit

Permalink
refactor: add verb and resources for prod
Browse files Browse the repository at this point in the history
Signed-off-by: Youngjin Jo <youngjinjo@megazone.com>
  • Loading branch information
yjinjo committed Dec 24, 2024
1 parent 6ce2b9e commit b81b8f5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/common/fetchService.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func FetchService(serviceName string, verb string, resourceName string, options
os.Exit(1)
}

// TODO: Remove this once all services are migrated to new endpoint format
if !hasIdentityService {
hostPort = fmt.Sprintf("%s.kr1.api.spaceone.megazone.io:443", convertServiceNameToEndpoint(serviceName))
} else {
Expand Down
56 changes: 42 additions & 14 deletions cmd/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,52 @@ func fetchVerbResourceMap(serviceName string, config *Config) (map[string][]stri
InsecureSkipVerify: false,
}
apiEndpoint, _ := other.GetAPIEndpoint(envConfig.Endpoint)
identityEndpoint, _, err := other.GetIdentityEndpoint(apiEndpoint)
identityEndpoint, hasIdentityService, err := other.GetIdentityEndpoint(apiEndpoint)

trimmedEndpoint := strings.TrimPrefix(identityEndpoint, "grpc+ssl://")
parts := strings.Split(trimmedEndpoint, ".")
if len(parts) < 4 {
return nil, fmt.Errorf("invalid endpoint format: %s", trimmedEndpoint)
}
if !hasIdentityService {
// Get endpoints map first
endpointsMap, err := other.FetchEndpointsMap(apiEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to fetch endpoints map: %v", err)
}

// Replace 'identity' with the converted service name
parts[0] = convertServiceNameToEndpoint(serviceName)
serviceEndpoint := strings.Join(parts, ".")
// Find the endpoint for the current service
endpoint, exists := endpointsMap[serviceName]
if !exists {
return nil, fmt.Errorf("endpoint not found for service: %s", serviceName)
}

creds := credentials.NewTLS(tlsConfig)
conn, err = grpc.Dial(serviceEndpoint, grpc.WithTransportCredentials(creds))
if err != nil {
return nil, fmt.Errorf("connection failed: %v", err)
}
// Parse the endpoint
parts := strings.Split(endpoint, "://")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid endpoint format: %s", endpoint)
}

// Extract hostPort (remove the /v1 suffix if present)
hostPort := strings.Split(parts[1], "/")[0]

creds := credentials.NewTLS(tlsConfig)
conn, err = grpc.Dial(hostPort, grpc.WithTransportCredentials(creds))
if err != nil {
return nil, fmt.Errorf("connection failed: %v", err)
}
} else {
trimmedEndpoint := strings.TrimPrefix(identityEndpoint, "grpc+ssl://")
parts := strings.Split(trimmedEndpoint, ".")
if len(parts) < 4 {
return nil, fmt.Errorf("invalid endpoint format: %s", trimmedEndpoint)
}

// Replace 'identity' with the converted service name
parts[0] = convertServiceNameToEndpoint(serviceName)
serviceEndpoint := strings.Join(parts, ".")

creds := credentials.NewTLS(tlsConfig)
conn, err = grpc.Dial(serviceEndpoint, grpc.WithTransportCredentials(creds))
if err != nil {
return nil, fmt.Errorf("connection failed: %v", err)
}
}
}
defer conn.Close()

Expand Down

0 comments on commit b81b8f5

Please sign in to comment.