Skip to content

Commit

Permalink
Add Support for v1alpha3 Log API in tkn-results cli
Browse files Browse the repository at this point in the history
  • Loading branch information
khrm committed Sep 4, 2024
1 parent 800f209 commit 7b361fb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
58 changes: 58 additions & 0 deletions pkg/cli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/tektoncd/results/pkg/cli/config"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
pb3 "github.com/tektoncd/results/proto/v1alpha3/results_go_proto"
"golang.org/x/oauth2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -200,6 +201,63 @@ func (f *Factory) certs() (*x509.CertPool, error) {
return certs, nil
}

// PluginLogsClient creates a new Results gRPC client for the given factory settings.
func (f *Factory) PluginLogsClient(ctx context.Context, overrideAPIAddr string) (pb3.LogsClient, error) {
token, err := f.token(ctx)
if err != nil {
return nil, err
}

var creds credentials.TransportCredentials
if f.cfg.Insecure {
creds = credentials.NewTLS(&tls.Config{
//nolint:gosec // needed for --insecure flag
InsecureSkipVerify: true,
})
} else {
certs, err := f.certs()
if err != nil {
return nil, err
}
creds = credentials.NewClientTLSFromCert(certs, f.cfg.SSL.ServerNameOverride)
}

ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
addr := f.cfg.Address
if overrideAPIAddr != "" {
addr = overrideAPIAddr
}
conn, err := grpc.DialContext(ctx, addr, grpc.WithBlock(), //nolint:staticcheck
grpc.WithTransportCredentials(creds),
grpc.WithDefaultCallOptions(grpc.PerRPCCredentials(oauth.TokenSource{
TokenSource: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}),
})),
)
if err != nil {
fmt.Printf("Dial: %v\n", err)
return nil, err
}
return pb3.NewLogsClient(conn), nil
}

// DefaultPluginLogsClient creates a new default logs client.
func DefaultPluginLogsClient(ctx context.Context, overrideAPIAddr string) (pb3.LogsClient, error) {
f, err := NewDefaultFactory()

if err != nil {
return nil, err
}

client, err := f.PluginLogsClient(ctx, overrideAPIAddr)

if err != nil {
return nil, err
}

return client, nil
}

func (f *Factory) token(ctx context.Context) (string, error) {
if f.cfg == nil {
return "", nil
Expand Down
20 changes: 17 additions & 3 deletions pkg/cli/cmd/logs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import (
"fmt"
"os"

httpbody "google.golang.org/genproto/googleapis/api/httpbody"
grpc "google.golang.org/grpc"

"github.com/spf13/cobra"
"github.com/tektoncd/results/pkg/cli/config"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/format"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
pb3 "github.com/tektoncd/results/proto/v1alpha3/results_go_proto"
)

// GetLogCommand returns a cobra sub command that will fetch a log by name
Expand All @@ -34,9 +39,18 @@ func GetLogCommand(params *flags.Params) *cobra.Command {
Short: "Get Log by <log-name>",
Long: "Get Log by <log-name>. <log-name> is typically of format <namespace>/results/<parent-run-uuid>/logs/<child-run-uuid>",
RunE: func(cmd *cobra.Command, args []string) error {
resp, err := params.LogsClient.GetLog(cmd.Context(), &pb.GetLogRequest{
Name: args[0],
})
var resp grpc.ServerStreamingClient[httpbody.HttpBody]
var err error

if config.GetConfig().UseV1Alpha2 {
resp, err = params.LogsClient.GetLog(cmd.Context(), &pb.GetLogRequest{
Name: args[0],
})
} else {
resp, err = params.PluginLogsClient.GetLog(cmd.Context(), &pb3.GetLogRequest{
Name: args[0],
})
}
if err != nil {
fmt.Printf("GetLog: %v\n", err)
return err
Expand Down
9 changes: 9 additions & 0 deletions pkg/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func Root() *cobra.Command {

params.LogsClient = logClient

pluginLogsClient, err := client.DefaultPluginLogsClient(cmd.Context(), overrideAPIAdr)

if err != nil {
return err
}

params.PluginLogsClient = pluginLogsClient

return nil
},
PersistentPostRun: func(_ *cobra.Command, _ []string) {
Expand All @@ -87,6 +95,7 @@ func Root() *cobra.Command {
cmd.PersistentFlags().String("sa-ns", "", "ServiceAccount Namespace, if not given, it will be taken from current context")
cmd.PersistentFlags().Bool("portforward", true, "enable auto portforwarding to tekton-results-api-service, when addr is set and portforward is true, tkn-results will portforward tekton-results-api-service automatically")
cmd.PersistentFlags().Bool("insecure", false, "determines whether to run insecure GRPC tls request")
cmd.PersistentFlags().Bool("v1alpha2", false, "use v1alpha2 API for get log command")

cmd.AddCommand(ListCommand(params), records.Command(params), logs.Command(params))

Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type Config struct {
Portforward bool
// Insecure determines whether to use insecure GRPC tls communication
Insecure bool

// v1alpha2
UseV1Alpha2 bool
}

// SSLConfig contains SSL configuration information.
Expand Down Expand Up @@ -116,6 +119,7 @@ func setConfig() error {

cfg.Portforward = viper.GetBool("portforward")
cfg.Insecure = viper.GetBool("insecure")
cfg.UseV1Alpha2 = viper.GetBool("v1alpha2")
return nil
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/cli/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package flags
import (
"github.com/spf13/cobra"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
pb3 "github.com/tektoncd/results/proto/v1alpha3/results_go_proto"
)

// Params contains a ResultsClient and LogsClient
type Params struct {
ResultsClient pb.ResultsClient
LogsClient pb.LogsClient
ResultsClient pb.ResultsClient
LogsClient pb.LogsClient
PluginLogsClient pb3.LogsClient
}

// ListOptions is used on commands that list Results, Records or Logs
Expand Down

0 comments on commit 7b361fb

Please sign in to comment.