Skip to content
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
18 changes: 10 additions & 8 deletions cmd/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/url"
"os"

Expand Down Expand Up @@ -56,12 +57,6 @@ Examples:
return err
}

// Create rest client
c, err := client.New(tanzuServer)
if err != nil {
return err
}

// Find credentials from kubeconfig context
contextName := u.Host
if _, ok := conf.Contexts[contextName]; !ok {
Expand All @@ -78,14 +73,21 @@ Examples:
if _, ok := conf.AuthInfos[authName]; !ok {
return errors.New("credentials missing! Please run 'tcli login' to authenticate")
}
c.(*client.RestClient).SetToken(conf.AuthInfos[authName].Token)
c.(*client.RestClient).SetInsecure(insecureSkipVerify)

// Check if there is a namespace set in the context that we can use so that we don't have to specify the --namespace flag
if _, ok := conf.Contexts[contextName]; ok && len(tanzuNamespace) == 0 {
tanzuNamespace = conf.Contexts[contextName].Namespace
}

token := conf.AuthInfos[authName].Token

// Create rest client
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
c, err := client.New(tanzuServer, client.WithLogger(logger), client.WithCredentials(client.TokenCredentials(token)), client.WithInsecure(insecureSkipVerify))
if err != nil {
return err
}

cluster, err := c.Cluster(ctx, tanzuNamespace, tanzuCluster)
if err != nil {
return err
Expand Down
26 changes: 14 additions & 12 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/url"
"os"
"strings"
Expand Down Expand Up @@ -68,12 +69,6 @@ Examples:
return err
}

// Create rest client
c, err := client.New(tanzuServer)
if err != nil {
return err
}

// Find credentials from kubeconfig context
contextName := u.Host
if _, ok := conf.Contexts[contextName]; !ok {
Expand All @@ -90,10 +85,16 @@ Examples:
if _, ok := conf.AuthInfos[authName]; !ok {
return errors.New("credentials missing! Please run 'tcli login' to authenticate")
}
c.(*client.RestClient).SetToken(conf.AuthInfos[authName].Token)
c.(*client.RestClient).SetInsecure(insecureSkipVerify)

// Check if there is a namespace set in the context that we can use so that we don't have to specify the --namespace flag
token := conf.AuthInfos[authName].Token

// Create rest client
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
c, err := client.New(tanzuServer, client.WithLogger(logger), client.WithCredentials(client.TokenCredentials(token)), client.WithInsecure(insecureSkipVerify))
if err != nil {
return err
}

if _, ok := conf.Contexts[contextName]; ok && len(tanzuNamespace) == 0 {
tanzuNamespace = conf.Contexts[contextName].Namespace
}
Expand All @@ -111,7 +112,7 @@ Examples:
tanzuPassword = string(bytePassword)
fmt.Printf("\n")
}
return listNamespaces(ctx, c, tanzuUsername, tanzuPassword)
return listNamespaces(ctx, tanzuServer, tanzuUsername, tanzuPassword, insecureSkipVerify)
case "clusters", "clu", "tkc":
return listClusters(ctx, c, tanzuNamespace)
case "releases", "rel", "tkr":
Expand Down Expand Up @@ -153,11 +154,12 @@ func listReleases(ctx context.Context, c client.Client) error {
return nil
}

func listNamespaces(ctx context.Context, c client.Client, username, password string) error {
err := c.Login(ctx, username, password)
func listNamespaces(ctx context.Context, server, username, password string, insecure bool) error {
c, err := client.New(server, client.WithCredentials(client.BasicCredentials(username, password)), client.WithInsecure(insecure))
if err != nil {
return err
}

nsList, err := c.Namespaces(ctx)
if err != nil {
return err
Expand Down
12 changes: 9 additions & 3 deletions cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"encoding/base64"
"errors"
"fmt"
"log/slog"
"net/url"
"os"
"syscall"

"github.com/middlewaregruppen/tcli/pkg/client"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"
Expand Down Expand Up @@ -86,16 +89,19 @@ Examples:
return err
}

c, err := client.New(tanzuServer)
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
c, err := client.New(tanzuServer, client.WithLogger(logger), client.WithCredentials(client.BasicCredentials(tanzuUsername, tanzuPassword)))
if err != nil {
return err
}
c.(*client.RestClient).SetInsecure(insecureSkipVerify)
err = c.Login(ctx, tanzuUsername, tanzuPassword)
sess, err := c.Login(ctx, tanzuUsername, tanzuPassword)
if err != nil {
return err
}

logrus.Debug("successfully logged in to cluster")

ns, err := c.Namespaces(ctx)
if err != nil {
return err
Expand All @@ -108,7 +114,7 @@ Examples:

authName := fmt.Sprintf("wcp:%s:%s", u.Host, tanzuUsername)
auth := api.NewAuthInfo()
auth.Token = c.(*client.RestClient).Token
auth.Token = sess.SessionID

context := api.NewContext()
context.Cluster = u.Host
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ type Client interface {
Releases(ctx context.Context) (*v1alpha2.TanzuKubernetesReleaseList, error)
Cluster(ctx context.Context, ns, name string) (*v1alpha2.TanzuKubernetesCluster, error)
Clusters(ctx context.Context, ns string) (*v1.Table, error)
Login(ctx context.Context, u, p string) error
Login(ctx context.Context, u, p string) (*LoginResponse, error)
LoginCluster(ctx context.Context, cluster, namespace string) (*LoginClusterResponse, error)
}
Loading