Skip to content

Commit

Permalink
refactor(cli): Introduce v1.Interface for CLI. Closes argoproj#2107 (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexec committed Jan 31, 2020
1 parent 7a19f85 commit ab1de23
Show file tree
Hide file tree
Showing 109 changed files with 1,144 additions and 704 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ commands:
name: Establish port forward
command: make pf
background: true
- run:
name: Sleep for short while
command: sleep 10s
- run:
name: Watch Kubernetes events, to help diagnose failures
command: kubectl -n argo get events --watch
background: true
- run:
name: Sleep for short while
command: sleep 10s
- run:
name: Follow logs, to help diagnose failures
command: make logs
Expand Down
1 change: 0 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ DB ?= postgres
K3D := $(shell if [ "`kubectl config current-context`" = "k3s-default" ]; then echo true; else echo false; fi)
ARGO_TOKEN = $(shell kubectl -n argo get secret -o name | grep argo-server | xargs kubectl -n argo get -o jsonpath='{.data.token}' | base64 --decode)

# test flags
export SKIP_CRON_SUITE=true
# if we are on any branch with "master", "release", or "cron" in the name we should run cron tests
ifneq ($(findstring cron,$(GIT_BRANCH)),)
export SKIP_CRON_SUITE=false
endif
ifneq ($(findstring master,$(GIT_BRANCH)),)
export SKIP_CRON_SUITE=false
endif
ifneq ($(findstring release,$(GIT_BRANCH)),)
export SKIP_CRON_SUITE=false
endif

override LDFLAGS += \
-X ${PACKAGE}.version=$(VERSION) \
-X ${PACKAGE}.buildDate=${BUILD_DATE} \
Expand Down Expand Up @@ -376,7 +389,7 @@ mysql-cli:
kubectl exec -ti `kubectl get pod -l app=mysql -o name|cut -c 5-` -- mysql -u mysql -ppassword argo

.PHONY: test-e2e
test-e2e: test-images
test-e2e: test-images cli
# Run E2E tests
go test -timeout 20m -v -count 1 -p 1 ./test/e2e/...

Expand All @@ -391,9 +404,10 @@ test-api: test-images
go test -timeout 3m -v -count 1 -p 1 -run ArgoServerSuite ./test/e2e

.PHONY: test-cli
test-cli: test-images
test-cli: test-images cli
# Run CLI tests
go test -timeout 1m -v -count 1 -p 1 -run CliSuite ./test/e2e
go test -timeout 1m -v -count 1 -p 1 -run CLISuite ./test/e2e
go test -timeout 1m -v -count 1 -p 1 -run CLIWithServerSuite ./test/e2e

# clean

Expand Down
20 changes: 8 additions & 12 deletions cmd/argo/commands/archive/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@ package archive

import (
"fmt"
"log"

"github.com/argoproj/pkg/errors"
"github.com/spf13/cobra"

"github.com/argoproj/argo/cmd/argo/commands/client"
"github.com/argoproj/argo/server/workflowarchive"
client "github.com/argoproj/argo/cmd/argo/commands/client"
workflowarchivepkg "github.com/argoproj/argo/pkg/apiclient/workflowarchive"
)

func NewDeleteCommand() *cobra.Command {
var command = &cobra.Command{
Use: "delete UID...",
Run: func(cmd *cobra.Command, args []string) {
ctx, apiClient := client.NewAPIClient()
serviceClient, err := apiClient.NewArchivedWorkflowServiceClient()
errors.CheckError(err)
for _, uid := range args {
conn := client.GetClientConn()
ctx := client.GetContext()
client := workflowarchive.NewArchivedWorkflowServiceClient(conn)
_, err := client.DeleteArchivedWorkflow(ctx, &workflowarchive.DeleteArchivedWorkflowRequest{
Uid: uid,
})
if err != nil {
log.Fatal(err)
}
_, err = serviceClient.DeleteArchivedWorkflow(ctx, &workflowarchivepkg.DeleteArchivedWorkflowRequest{Uid: uid})
errors.CheckError(err)
fmt.Printf("Archived workflow '%s' deleted\n", uid)
}
},
Expand Down
18 changes: 8 additions & 10 deletions cmd/argo/commands/archive/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"log"
"os"

"github.com/argoproj/pkg/errors"
"github.com/argoproj/pkg/humanize"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/argoproj/argo/cmd/argo/commands/client"
"github.com/argoproj/argo/server/workflowarchive"
workflowarchivepkg "github.com/argoproj/argo/pkg/apiclient/workflowarchive"
)

func NewGetCommand() *cobra.Command {
Expand All @@ -26,15 +27,12 @@ func NewGetCommand() *cobra.Command {
os.Exit(1)
}
uid := args[0]
conn := client.GetClientConn()
ctx := client.GetContext()
client := workflowarchive.NewArchivedWorkflowServiceClient(conn)
wf, err := client.GetArchivedWorkflow(ctx, &workflowarchive.GetArchivedWorkflowRequest{
Uid: uid,
})
if err != nil {
log.Fatal(err)
}

ctx, apiClient := client.NewAPIClient()
serviceClient, err := apiClient.NewArchivedWorkflowServiceClient()
errors.CheckError(err)
wf, err := serviceClient.GetArchivedWorkflow(ctx, &workflowarchivepkg.GetArchivedWorkflowRequest{Uid: uid})
errors.CheckError(err)
switch output {
case "json":
output, err := json.Marshal(wf)
Expand Down
24 changes: 12 additions & 12 deletions cmd/argo/commands/archive/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,32 @@ import (
"os"
"text/tabwriter"

"github.com/argoproj/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"github.com/argoproj/argo/cmd/argo/commands/client"
"github.com/argoproj/argo/server/workflowarchive"
workflowarchivepkg "github.com/argoproj/argo/pkg/apiclient/workflowarchive"
)

func NewListCommand() *cobra.Command {
var (
output string
namespace string
output string
)
var command = &cobra.Command{
Use: "list",
Run: func(cmd *cobra.Command, args []string) {
conn := client.GetClientConn()
ctx := client.GetContext()
client := workflowarchive.NewArchivedWorkflowServiceClient(conn)
resp, err := client.ListArchivedWorkflows(ctx, &workflowarchive.ListArchivedWorkflowsRequest{
ListOptions: &metav1.ListOptions{FieldSelector: "metadata.namespace=" + namespace},
ctx, apiClient := client.NewAPIClient()
serviceClient, err := apiClient.NewArchivedWorkflowServiceClient()
errors.CheckError(err)
namespace := client.Namespace()
resp, err := serviceClient.ListArchivedWorkflows(ctx, &workflowarchivepkg.ListArchivedWorkflowsRequest{
ListOptions: &metav1.ListOptions{
FieldSelector: "metadata.namespace=" + namespace,
},
})
if err != nil {
log.Fatal(err)
}
errors.CheckError(err)
switch output {
case "json":
output, err := json.Marshal(resp.Items)
Expand All @@ -56,6 +57,5 @@ func NewListCommand() *cobra.Command {
},
}
command.Flags().StringVarP(&output, "output", "o", "wide", "Output format. One of: json|yaml|wide")
command.Flags().StringVarP(&namespace, "namespace", "n", "", "The namespace")
return command
}
33 changes: 26 additions & 7 deletions cmd/argo/commands/client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"k8s.io/client-go/tools/clientcmd"

"github.com/argoproj/argo/pkg/apiclient"
"github.com/argoproj/argo/util/kubeconfig"
)

// DEPRECATED
var ArgoServer string

// DEPRECATED
var Config clientcmd.ClientConfig

func AddKubectlFlagsToCmd(cmd *cobra.Command) {
Expand All @@ -31,20 +34,36 @@ func AddArgoServerFlagsToCmd(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&ArgoServer, "argo-server", os.Getenv("ARGO_SERVER"), "API server `host:port`. e.g. localhost:2746. Defaults to the ARGO_SERVER environment variable.")
}

// DEPRECATED
func GetClientConn() *grpc.ClientConn {
conn, err := grpc.Dial(ArgoServer, grpc.WithInsecure())
conn, err := apiclient.NewClientConn(ArgoServer)
if err != nil {
log.Fatal(err)
}
return conn
}

func GetContext() context.Context {
authString := GetAuthString()
if authString == "" {
return context.Background()
func NewAPIClient() (context.Context, apiclient.Client) {
ctx, client, err := apiclient.NewClient(ArgoServer, func() string {
return GetAuthString()
}, Config)
if err != nil {
log.Fatal(err)
}
return ctx, client
}

func Namespace() string {
namespace, _, err := Config.Namespace()
if err != nil {
log.Fatal(err)
}
return metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", authString))
return namespace
}

// DEPRECATED should only be used by client/v1 package
func GetContext() context.Context {
return apiclient.NewContext(GetAuthString())
}

func GetAuthString() string {
Expand Down
29 changes: 19 additions & 10 deletions cmd/argo/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,31 @@ import (
"k8s.io/client-go/rest"

"github.com/argoproj/argo/cmd/argo/commands/client"
"github.com/argoproj/argo/pkg/apiclient/workflow"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/pkg/client/clientset/versioned"
"github.com/argoproj/argo/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
wfApiServer "github.com/argoproj/argo/server/workflow"
"github.com/argoproj/argo/workflow/templateresolution"
)

// Global variables
var (
restConfig *rest.Config
clientset *kubernetes.Clientset
wfClientset *versioned.Clientset
wfClient v1alpha1.WorkflowInterface
// DEPRECATED
restConfig *rest.Config
// DEPRECATED
clientset *kubernetes.Clientset
// DEPRECATED
wfClientset *versioned.Clientset
// DEPRECATED
wfClient v1alpha1.WorkflowInterface
// DEPRECATED
wftmplClient v1alpha1.WorkflowTemplateInterface
jobStatusIconMap map[wfv1.NodePhase]string
noColor bool
namespace string
// DEPRECATED
namespace string
)

const ARGO_SERVER_ENV = "ARGO_SERVER"

func init() {
cobra.OnInitialize(initializeSession)
}
Expand Down Expand Up @@ -69,6 +73,7 @@ func initializeSession() {
}
}

// DEPRECATED
func InitKubeClient() *kubernetes.Clientset {
if clientset != nil {
return clientset
Expand All @@ -88,6 +93,7 @@ func InitKubeClient() *kubernetes.Clientset {
}

// InitWorkflowClient creates a new client for the Kubernetes Workflow CRD.
// DEPRECATED
func InitWorkflowClient(ns ...string) v1alpha1.WorkflowInterface {
if wfClient != nil && (len(ns) == 0 || ns[0] == namespace) {
return wfClient
Expand Down Expand Up @@ -129,9 +135,11 @@ func ansiFormat(s string, codes ...int) string {

// LazyWorkflowTemplateGetter is a wrapper of v1alpha1.WorkflowTemplateInterface which
// supports lazy initialization.
// DEPRECATED
type LazyWorkflowTemplateGetter struct{}

// Get initializes it just before it's actually used and returns a retrieved workflow template.
// DEPRECATED
func (c LazyWorkflowTemplateGetter) Get(name string) (*wfv1.WorkflowTemplate, error) {
if wftmplClient == nil {
_ = InitWorkflowClient()
Expand All @@ -141,6 +149,7 @@ func (c LazyWorkflowTemplateGetter) Get(name string) (*wfv1.WorkflowTemplate, er

var _ templateresolution.WorkflowTemplateNamespacedGetter = &LazyWorkflowTemplateGetter{}

func GetWFApiServerGRPCClient(conn *grpc.ClientConn) (wfApiServer.WorkflowServiceClient, context.Context) {
return wfApiServer.NewWorkflowServiceClient(conn), client.GetContext()
// DEPRECATED
func GetWFApiServerGRPCClient(conn *grpc.ClientConn) (workflow.WorkflowServiceClient, context.Context) {
return workflow.NewWorkflowServiceClient(conn), client.GetContext()
}
14 changes: 7 additions & 7 deletions cmd/argo/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"os"
"time"

"github.com/argoproj/argo/cmd/argo/commands/client"
"github.com/argoproj/argo/server/workflow"

argotime "github.com/argoproj/pkg/time"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/argoproj/argo/cmd/argo/commands/client"
workflowpkg "github.com/argoproj/argo/pkg/apiclient/workflow"

"github.com/argoproj/argo/workflow/common"
)

Expand Down Expand Up @@ -107,8 +107,8 @@ func apiServerDeleteWorkflows(allWFs bool, older string, completed bool, wfNames
}
}

func getWFList(client workflow.WorkflowServiceClient, ctx context.Context, ns string, opts *metav1.ListOptions, older *time.Time) ([]string, error) {
wfReq := workflow.WorkflowListRequest{
func getWFList(client workflowpkg.WorkflowServiceClient, ctx context.Context, ns string, opts *metav1.ListOptions, older *time.Time) ([]string, error) {
wfReq := workflowpkg.WorkflowListRequest{
ListOptions: opts,
Namespace: ns,
}
Expand All @@ -128,8 +128,8 @@ func getWFList(client workflow.WorkflowServiceClient, ctx context.Context, ns st
return wfNames, nil
}

func apiServerDeleteWorkflow(client workflow.WorkflowServiceClient, ctx context.Context, wfName, ns string) {
wfReq := workflow.WorkflowDeleteRequest{
func apiServerDeleteWorkflow(client workflowpkg.WorkflowServiceClient, ctx context.Context, wfName, ns string) {
wfReq := workflowpkg.WorkflowDeleteRequest{
Name: wfName,
Namespace: ns,
}
Expand Down
Loading

0 comments on commit ab1de23

Please sign in to comment.