Skip to content

Commit

Permalink
nsc: added ingress list.
Browse files Browse the repository at this point in the history
  • Loading branch information
hugosantos committed Jul 29, 2023
1 parent 161a5e1 commit 2306a38
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/nsc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
root.AddCommand(cluster.NewDockerCmd()) // nsc docker
root.AddCommand(cluster.NewDescribeCmd()) // nsc describe
root.AddCommand(cluster.NewExecScoped()) // nsc exec-scoped
root.AddCommand(cluster.NewIngressCmd()) // nsc ingress

root.AddCommand(sdk.NewSdkCmd(true))

Expand Down
63 changes: 63 additions & 0 deletions internal/cli/cmd/cluster/ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

package cluster

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
)

func NewIngressCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ingress",
Short: "Ingress-related activities.",
}

cmd.AddCommand(newListIngressesCmd())

return cmd
}

func newListIngressesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists the registered ingresses on the specified instance.",
Args: cobra.MaximumNArgs(1),
}

cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
cluster, _, err := selectRunningCluster(ctx, args)
if err != nil {
return err
}

if cluster == nil {
return nil
}

lst, err := api.ListIngresses(ctx, api.Methods, cluster.ClusterId)
if err != nil {
return err
}

for _, ingress := range lst.ExportedInstancePort {
parts := []string{fmt.Sprintf("port: %d", ingress.Port)}
if ingress.Description != "" {
parts = append(parts, ingress.Description)
}
fmt.Fprintf(console.Stdout(ctx), "https://%s (%s)\n", ingress.IngressFqdn, strings.Join(parts, "; "))
}

return nil
})

return cmd
}
16 changes: 16 additions & 0 deletions internal/providers/nscloud/api/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type API struct {
GetClusterLogs fnapi.Call[GetLogsRequest]
GetProfile fnapi.Call[emptypb.Empty]
RegisterIngress fnapi.Call[RegisterIngressRequest]
ListIngresses fnapi.Call[ListIngressesRequest]
}

var (
Expand Down Expand Up @@ -197,6 +198,11 @@ func MakeAPI() API {
FetchToken: fnapi.FetchToken,
Method: "nsl.vm.api.VMService/RegisterIngress",
},

ListIngresses: fnapi.Call[ListIngressesRequest]{
FetchToken: fnapi.FetchToken,
Method: "nsl.vm.api.VMService/ListIngresses",
},
}
}

Expand Down Expand Up @@ -659,6 +665,16 @@ func RegisterIngress(ctx context.Context, api API, req RegisterIngressRequest) (
})
}

func ListIngresses(ctx context.Context, api API, clusterID string) (*ListIngressesResponse, error) {
return tasks.Return(ctx, tasks.Action("nscloud.register-ingress"), func(ctx context.Context) (*ListIngressesResponse, error) {
var response ListIngressesResponse
if err := api.ListIngresses.Do(ctx, ListIngressesRequest{ClusterId: clusterID}, ResolveEndpoint, fnapi.DecodeJSONResponse(&response)); err != nil {
return nil, err
}
return &response, nil
})
}

type clusterCreateProgress struct {
status atomic.String
}
Expand Down
16 changes: 16 additions & 0 deletions internal/providers/nscloud/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,19 @@ type IngressBackendEndpoint struct {
type RegisterIngressResponse struct {
Fqdn string `json:"fqdn,omitempty"`
}

type ListIngressesRequest struct {
ClusterId string `json:"cluster_id,omitempty"`
}

type ListIngressesResponse struct {
ExportedInstancePort []*ExportedInstancePort `json:"exported_instance_port,omitempty"`
}

type ExportedInstancePort struct {
Proto string `json:"proto,omitempty"`
Port int32 `json:"port,omitempty"`
IngressFqdn string `json:"ingress_fqdn,omitempty"`
HttpMatchRule []*ContainerPort_HttpMatchRule `json:"http_match_rule,omitempty"`
Description string `json:"description,omitempty"`
}

0 comments on commit 2306a38

Please sign in to comment.