Skip to content

Modify local environment #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion cmd/common/api_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -101,6 +102,8 @@ func FetchServiceResources(serviceName, endpoint string, shortNamesMap map[strin
}
creds := credentials.NewTLS(tlsConfig)
opts = append(opts, grpc.WithTransportCredentials(creds))
} else if scheme == "grpc" {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
return nil, fmt.Errorf("unsupported scheme: %s", scheme)
}
Expand Down Expand Up @@ -150,7 +153,33 @@ func FetchServiceResources(serviceName, endpoint string, shortNamesMap map[strin
if strings.HasPrefix(s, "grpc.reflection.") {
continue
}
if !strings.Contains(s, fmt.Sprintf(".%s.", serviceName)) {

displayServiceName := serviceName
if strings.HasPrefix(endpoint, "grpc://") && (strings.Contains(endpoint, "localhost") || strings.Contains(endpoint, "127.0.0.1")) {
parts := strings.Split(s, ".")
if len(parts) > 2 {
serviceDesc, err := refClient.ResolveService(s)
if err != nil {
log.Printf("Failed to resolve service %s: %v", s, err)
continue
}

resourceName := s[strings.LastIndex(s, ".")+1:]
verbs := []string{}
for _, method := range serviceDesc.GetMethods() {
verbs = append(verbs, method.GetName())
}

sort.Strings(verbs)
data = append(data, []string{
displayServiceName,
strings.Join(verbs, ", "),
resourceName,
"",
})
continue
}
} else if !strings.Contains(s, fmt.Sprintf(".%s.", serviceName)) {
continue
}

Expand Down
77 changes: 77 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"log"
"os"
Expand All @@ -11,6 +12,9 @@ import (
"github.com/cloudforet-io/cfctl/cmd/common"
"github.com/cloudforet-io/cfctl/pkg/configs"
"github.com/cloudforet-io/cfctl/pkg/transport"
"github.com/jhump/protoreflect/grpcreflect"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
"gopkg.in/yaml.v3"

"github.com/spf13/viper"
Expand Down Expand Up @@ -257,6 +261,79 @@ func addDynamicServiceCommands() error {
endpointName := config.Endpoint
var apiEndpoint string

// For local environment
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))
if err != nil {
pterm.DefaultBox.WithTitle("Local gRPC Server Not Found").
WithTitleTopCenter().
WithBoxStyle(pterm.NewStyle(pterm.FgYellow)).
Printfln("Unable to connect to local gRPC server.\nPlease make sure your gRPC server is running on %s", config.Endpoint)
return nil
}
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {

}
}(conn)

ctx := context.Background()
refClient := grpcreflect.NewClientV1Alpha(ctx, grpc_reflection_v1alpha.NewServerReflectionClient(conn))
defer refClient.Reset()

services, err := refClient.ListServices()
if err != nil {
return err
}

// Check if plugin service exists
hasPlugin := false
microservices := make(map[string]bool)

for _, service := range services {
// Skip grpc reflection and health check services
if strings.HasPrefix(service, "grpc.") {
continue
}

// Handle plugin service
if strings.Contains(service, ".plugin.") {
hasPlugin = true
continue
}

// Handle SpaceONE microservices
if strings.Contains(service, "spaceone.api.") {
parts := strings.Split(service, ".")
if len(parts) >= 4 {
serviceName := parts[2]
// Skip core service and version prefixes
if serviceName != "core" && !strings.HasPrefix(serviceName, "v") {
microservices[serviceName] = true
}
}
}
}

if hasPlugin {
cmd := createServiceCommand(config.Environment)
cmd.GroupID = "available"
rootCmd.AddCommand(cmd)
}

// Add commands for other microservices
for serviceName := range microservices {
cmd := createServiceCommand(serviceName)
cmd.GroupID = "available"
rootCmd.AddCommand(cmd)
}

return nil
}

if strings.HasPrefix(endpointName, "grpc+ssl://") {
apiEndpoint = endpointName
} else if strings.HasPrefix(endpointName, "http://") || strings.HasPrefix(endpointName, "https://") {
Expand Down
12 changes: 12 additions & 0 deletions pkg/configs/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ func GetServiceEndpoint(config *Setting, serviceName string) (string, error) {
return "", fmt.Errorf("endpoint not found in environment config")
}

if strings.HasPrefix(envConfig.Endpoint, "grpc://") {
if strings.Contains(envConfig.Endpoint, "localhost") {
return envConfig.Endpoint, nil
}
}

// Get console API endpoint
apiEndpoint, err := GetAPIEndpoint(envConfig.Endpoint)
if err != nil {
Expand Down Expand Up @@ -163,6 +169,12 @@ func GetServiceEndpoint(config *Setting, serviceName string) (string, error) {
}

func FetchEndpointsMap(endpoint string) (map[string]string, error) {
if strings.HasPrefix(endpoint, "grpc://") {
endpointsMap := make(map[string]string)
endpointsMap["local"] = endpoint
return endpointsMap, nil
}

// Get identity service endpoint
identityEndpoint, hasIdentityService, err := GetIdentityEndpoint(endpoint)
listEndpointsUrl := endpoint + "/identity/endpoint/list"
Expand Down
2 changes: 1 addition & 1 deletion pkg/format/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func RenderTable(data [][]string) {
previousService := ""

// Create table with headers
table := pterm.TableData{{"Service", "Verb", "Resource", "Short Names"}}
table := pterm.TableData{{"Service", "Verb", "Resource", "Alias"}}

for _, row := range data {
service := row[0]
Expand Down
9 changes: 5 additions & 4 deletions pkg/transport/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func FetchService(serviceName string, verb string, resourceName string, options
var apiEndpoint string
var identityEndpoint string
var hasIdentityService bool
if config.Environment == "local" {
if strings.HasPrefix(config.Environments[config.Environment].Endpoint, "grpc://") {
hostPort = strings.TrimPrefix(config.Environments[config.Environment].Endpoint, "grpc://")
} else {
apiEndpoint, err = configs.GetAPIEndpoint(config.Environments[config.Environment].Endpoint)
Expand Down Expand Up @@ -768,12 +768,13 @@ func discoverService(refClient *grpcreflect.Client, serviceName string, resource
}

for _, service := range services {
if strings.Contains(service, fmt.Sprintf("spaceone.api.%s", serviceName)) &&
strings.HasSuffix(service, resourceName) {
if strings.Contains(service, ".plugin.") && strings.HasSuffix(service, resourceName) {
return service, nil
}
}

if strings.Contains(service, serviceName) &&
for _, service := range services {
if strings.Contains(service, fmt.Sprintf("spaceone.api.%s", serviceName)) &&
strings.HasSuffix(service, resourceName) {
return service, nil
}
Expand Down
Loading