mirror of
https://github.com/argoproj/argo-cd
synced 2026-05-24 09:50:08 +00:00
Add argo-cd client wrapper
This commit is contained in:
parent
c9eefa0c18
commit
9e3e948108
6 changed files with 173 additions and 126 deletions
99
client/client.go
Normal file
99
client/client.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/argoproj/argo-cd/server/application"
|
||||
"github.com/argoproj/argo-cd/server/cluster"
|
||||
"github.com/argoproj/argo-cd/server/repository"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
NewConn() (*grpc.ClientConn, error)
|
||||
NewRepoClient() (*grpc.ClientConn, repository.RepositoryServiceClient, error)
|
||||
NewRepoClientOrDie() (*grpc.ClientConn, repository.RepositoryServiceClient)
|
||||
NewClusterClient() (*grpc.ClientConn, cluster.ClusterServiceClient, error)
|
||||
NewClusterClientOrDie() (*grpc.ClientConn, cluster.ClusterServiceClient)
|
||||
NewApplicationClient() (*grpc.ClientConn, application.ApplicationServiceClient, error)
|
||||
NewApplicationClientOrDie() (*grpc.ClientConn, application.ApplicationServiceClient)
|
||||
}
|
||||
|
||||
type ClientOptions struct {
|
||||
ServerAddr string
|
||||
Insecure bool
|
||||
}
|
||||
|
||||
type client struct {
|
||||
ClientOptions
|
||||
}
|
||||
|
||||
func NewClient(opts *ClientOptions) Client {
|
||||
return &client{
|
||||
ClientOptions: *opts,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *client) NewConn() (*grpc.ClientConn, error) {
|
||||
var dialOpts []grpc.DialOption
|
||||
if c.Insecure {
|
||||
dialOpts = append(dialOpts, grpc.WithInsecure())
|
||||
} else {
|
||||
return nil, errors.New("secure authentication unsupported")
|
||||
} // else if opts.Credentials != nil {
|
||||
// dialOpts = append(dialOpts, grpc.WithTransportCredentials(opts.Credentials))
|
||||
//}
|
||||
return grpc.Dial(c.ServerAddr, dialOpts...)
|
||||
}
|
||||
|
||||
func (c *client) NewRepoClient() (*grpc.ClientConn, repository.RepositoryServiceClient, error) {
|
||||
conn, err := c.NewConn()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
repoIf := repository.NewRepositoryServiceClient(conn)
|
||||
return conn, repoIf, nil
|
||||
}
|
||||
|
||||
func (c *client) NewRepoClientOrDie() (*grpc.ClientConn, repository.RepositoryServiceClient) {
|
||||
conn, repoIf, err := c.NewRepoClient()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to establish connection to %s: %v", c.ServerAddr, err)
|
||||
}
|
||||
return conn, repoIf
|
||||
}
|
||||
|
||||
func (c *client) NewClusterClient() (*grpc.ClientConn, cluster.ClusterServiceClient, error) {
|
||||
conn, err := c.NewConn()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
clusterIf := cluster.NewClusterServiceClient(conn)
|
||||
return conn, clusterIf, nil
|
||||
}
|
||||
|
||||
func (c *client) NewClusterClientOrDie() (*grpc.ClientConn, cluster.ClusterServiceClient) {
|
||||
conn, clusterIf, err := c.NewClusterClient()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to establish connection to %s: %v", c.ServerAddr, err)
|
||||
}
|
||||
return conn, clusterIf
|
||||
}
|
||||
|
||||
func (c *client) NewApplicationClient() (*grpc.ClientConn, application.ApplicationServiceClient, error) {
|
||||
conn, err := c.NewConn()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
appIf := application.NewApplicationServiceClient(conn)
|
||||
return conn, appIf, nil
|
||||
}
|
||||
|
||||
func (c *client) NewApplicationClientOrDie() (*grpc.ClientConn, application.ApplicationServiceClient) {
|
||||
conn, repoIf, err := c.NewApplicationClient()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to establish connection to %s: %v", c.ServerAddr, err)
|
||||
}
|
||||
return conn, repoIf
|
||||
}
|
||||
|
|
@ -6,20 +6,18 @@ import (
|
|||
"os"
|
||||
"text/tabwriter"
|
||||
|
||||
argocdclient "github.com/argoproj/argo-cd/client"
|
||||
"github.com/argoproj/argo-cd/errors"
|
||||
argoappv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
|
||||
"github.com/argoproj/argo-cd/server/application"
|
||||
"github.com/argoproj/argo-cd/util"
|
||||
"github.com/ghodss/yaml"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
// NewApplicationCommand returns a new instance of an `argocd app` command
|
||||
func NewApplicationCommand() *cobra.Command {
|
||||
|
||||
func NewApplicationCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "app",
|
||||
Short: fmt.Sprintf("%s app COMMAND", cliName),
|
||||
|
|
@ -29,16 +27,16 @@ func NewApplicationCommand() *cobra.Command {
|
|||
},
|
||||
}
|
||||
|
||||
command.AddCommand(NewApplicationAddCommand())
|
||||
command.AddCommand(NewApplicationGetCommand())
|
||||
command.AddCommand(NewApplicationSyncCommand())
|
||||
command.AddCommand(NewApplicationListCommand())
|
||||
command.AddCommand(NewApplicationRemoveCommand())
|
||||
command.AddCommand(NewApplicationAddCommand(clientOpts))
|
||||
command.AddCommand(NewApplicationGetCommand(clientOpts))
|
||||
command.AddCommand(NewApplicationSyncCommand(clientOpts))
|
||||
command.AddCommand(NewApplicationListCommand(clientOpts))
|
||||
command.AddCommand(NewApplicationRemoveCommand(clientOpts))
|
||||
return command
|
||||
}
|
||||
|
||||
// NewApplicationAddCommand returns a new instance of an `argocd app add` command
|
||||
func NewApplicationAddCommand() *cobra.Command {
|
||||
func NewApplicationAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var (
|
||||
repoURL string
|
||||
appPath string
|
||||
|
|
@ -64,7 +62,7 @@ func NewApplicationAddCommand() *cobra.Command {
|
|||
},
|
||||
},
|
||||
}
|
||||
conn, appIf := NewApplicationClient()
|
||||
conn, appIf := argocdclient.NewClient(clientOpts).NewApplicationClientOrDie()
|
||||
defer util.Close(conn)
|
||||
_, err := appIf.Create(context.Background(), &app)
|
||||
errors.CheckError(err)
|
||||
|
|
@ -78,7 +76,7 @@ func NewApplicationAddCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
// NewApplicationGetCommand returns a new instance of an `argocd app get` command
|
||||
func NewApplicationGetCommand() *cobra.Command {
|
||||
func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "get",
|
||||
Short: fmt.Sprintf("%s app get APPNAME", cliName),
|
||||
|
|
@ -87,7 +85,7 @@ func NewApplicationGetCommand() *cobra.Command {
|
|||
c.HelpFunc()(c, args)
|
||||
os.Exit(1)
|
||||
}
|
||||
conn, appIf := NewApplicationClient()
|
||||
conn, appIf := argocdclient.NewClient(clientOpts).NewApplicationClientOrDie()
|
||||
defer util.Close(conn)
|
||||
for _, appName := range args {
|
||||
app, err := appIf.Get(context.Background(), &application.ApplicationQuery{Name: appName})
|
||||
|
|
@ -102,7 +100,7 @@ func NewApplicationGetCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
// NewApplicationRemoveCommand returns a new instance of an `argocd app list` command
|
||||
func NewApplicationRemoveCommand() *cobra.Command {
|
||||
func NewApplicationRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "rm",
|
||||
Short: fmt.Sprintf("%s app rm APPNAME", cliName),
|
||||
|
|
@ -111,7 +109,7 @@ func NewApplicationRemoveCommand() *cobra.Command {
|
|||
c.HelpFunc()(c, args)
|
||||
os.Exit(1)
|
||||
}
|
||||
conn, appIf := NewApplicationClient()
|
||||
conn, appIf := argocdclient.NewClient(clientOpts).NewApplicationClientOrDie()
|
||||
defer util.Close(conn)
|
||||
for _, appName := range args {
|
||||
_, err := appIf.Delete(context.Background(), &application.ApplicationQuery{Name: appName})
|
||||
|
|
@ -123,12 +121,12 @@ func NewApplicationRemoveCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
// NewApplicationListCommand returns a new instance of an `argocd app rm` command
|
||||
func NewApplicationListCommand() *cobra.Command {
|
||||
func NewApplicationListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: fmt.Sprintf("%s app list", cliName),
|
||||
Run: func(c *cobra.Command, args []string) {
|
||||
conn, appIf := NewApplicationClient()
|
||||
conn, appIf := argocdclient.NewClient(clientOpts).NewApplicationClientOrDie()
|
||||
defer util.Close(conn)
|
||||
apps, err := appIf.List(context.Background(), &application.ApplicationQuery{})
|
||||
errors.CheckError(err)
|
||||
|
|
@ -144,7 +142,7 @@ func NewApplicationListCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
// NewApplicationSyncCommand returns a new instance of an `argocd app sync` command
|
||||
func NewApplicationSyncCommand() *cobra.Command {
|
||||
func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var (
|
||||
dryRun bool
|
||||
)
|
||||
|
|
@ -156,7 +154,7 @@ func NewApplicationSyncCommand() *cobra.Command {
|
|||
c.HelpFunc()(c, args)
|
||||
os.Exit(1)
|
||||
}
|
||||
conn, appIf := NewApplicationClient()
|
||||
conn, appIf := argocdclient.NewClient(clientOpts).NewApplicationClientOrDie()
|
||||
defer util.Close(conn)
|
||||
appName := args[0]
|
||||
syncReq := application.ApplicationSyncRequest{
|
||||
|
|
@ -177,21 +175,3 @@ func NewApplicationSyncCommand() *cobra.Command {
|
|||
command.Flags().BoolVar(&dryRun, "dry-run", false, "Preview apply without affecting cluster")
|
||||
return command
|
||||
}
|
||||
|
||||
func NewApplicationClient() (*grpc.ClientConn, application.ApplicationServiceClient) {
|
||||
// TODO: get this from a config or command line flag
|
||||
serverAddr := "localhost:8080"
|
||||
var dialOpts []grpc.DialOption
|
||||
// TODO: add insecure config option and --insecure global flag
|
||||
if true {
|
||||
dialOpts = append(dialOpts, grpc.WithInsecure())
|
||||
} // else if opts.Credentials != nil {
|
||||
// dialOpts = append(dialOpts, grpc.WithTransportCredentials(opts.Credentials))
|
||||
//}
|
||||
conn, err := grpc.Dial(serverAddr, dialOpts...)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to establish connection to %s: %v", serverAddr, err)
|
||||
}
|
||||
appIf := application.NewApplicationServiceClient(conn)
|
||||
return conn, appIf
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
argocdclient "github.com/argoproj/argo-cd/client"
|
||||
"github.com/argoproj/argo-cd/common"
|
||||
"github.com/argoproj/argo-cd/errors"
|
||||
argoappv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
|
||||
|
|
@ -17,13 +18,12 @@ import (
|
|||
"github.com/ghodss/yaml"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
// NewClusterCommand returns a new instance of an `argocd cluster` command
|
||||
func NewClusterCommand(pathOptions *clientcmd.PathOptions) *cobra.Command {
|
||||
func NewClusterCommand(clientOpts *argocdclient.ClientOptions, pathOpts *clientcmd.PathOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "cluster",
|
||||
Short: fmt.Sprintf("%s cluster COMMAND", cliName),
|
||||
|
|
@ -33,20 +33,20 @@ func NewClusterCommand(pathOptions *clientcmd.PathOptions) *cobra.Command {
|
|||
},
|
||||
}
|
||||
|
||||
command.AddCommand(NewClusterAddCommand(pathOptions))
|
||||
command.AddCommand(NewClusterGetCommand())
|
||||
command.AddCommand(NewClusterListCommand())
|
||||
command.AddCommand(NewClusterRemoveCommand())
|
||||
command.AddCommand(NewClusterAddCommand(clientOpts, pathOpts))
|
||||
command.AddCommand(NewClusterGetCommand(clientOpts))
|
||||
command.AddCommand(NewClusterListCommand(clientOpts))
|
||||
command.AddCommand(NewClusterRemoveCommand(clientOpts))
|
||||
return command
|
||||
}
|
||||
|
||||
// NewClusterAddCommand returns a new instance of an `argocd cluster add` command
|
||||
func NewClusterAddCommand(pathOptions *clientcmd.PathOptions) *cobra.Command {
|
||||
func NewClusterAddCommand(clientOpts *argocdclient.ClientOptions, pathOpts *clientcmd.PathOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: fmt.Sprintf("%s cluster add CONTEXT", cliName),
|
||||
Run: func(c *cobra.Command, args []string) {
|
||||
var configAccess clientcmd.ConfigAccess = pathOptions
|
||||
var configAccess clientcmd.ConfigAccess = pathOpts
|
||||
if len(args) == 0 {
|
||||
log.Error("Choose a context name from:")
|
||||
printContexts(configAccess)
|
||||
|
|
@ -68,7 +68,7 @@ func NewClusterAddCommand(pathOptions *clientcmd.PathOptions) *cobra.Command {
|
|||
// Install RBAC resources for managing the cluster
|
||||
conf.BearerToken = common.InstallClusterManagerRBAC(conf)
|
||||
|
||||
conn, clusterIf := NewClusterClient()
|
||||
conn, clusterIf := argocdclient.NewClient(clientOpts).NewClusterClientOrDie()
|
||||
defer util.Close(conn)
|
||||
clst := NewCluster(args[0], conf)
|
||||
clst, err = clusterIf.Create(context.Background(), clst)
|
||||
|
|
@ -76,7 +76,7 @@ func NewClusterAddCommand(pathOptions *clientcmd.PathOptions) *cobra.Command {
|
|||
fmt.Printf("Cluster '%s' added\n", clst.Name)
|
||||
},
|
||||
}
|
||||
command.PersistentFlags().StringVar(&pathOptions.LoadingRules.ExplicitPath, pathOptions.ExplicitFileFlag, pathOptions.LoadingRules.ExplicitPath, "use a particular kubeconfig file")
|
||||
command.PersistentFlags().StringVar(&pathOpts.LoadingRules.ExplicitPath, pathOpts.ExplicitFileFlag, pathOpts.LoadingRules.ExplicitPath, "use a particular kubeconfig file")
|
||||
return command
|
||||
}
|
||||
|
||||
|
|
@ -144,7 +144,7 @@ func NewCluster(name string, conf *rest.Config) *argoappv1.Cluster {
|
|||
}
|
||||
|
||||
// NewClusterGetCommand returns a new instance of an `argocd cluster get` command
|
||||
func NewClusterGetCommand() *cobra.Command {
|
||||
func NewClusterGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "get",
|
||||
Short: fmt.Sprintf("%s cluster get SERVER", cliName),
|
||||
|
|
@ -153,7 +153,7 @@ func NewClusterGetCommand() *cobra.Command {
|
|||
c.HelpFunc()(c, args)
|
||||
os.Exit(1)
|
||||
}
|
||||
conn, clusterIf := NewClusterClient()
|
||||
conn, clusterIf := argocdclient.NewClient(clientOpts).NewClusterClientOrDie()
|
||||
defer util.Close(conn)
|
||||
for _, clusterName := range args {
|
||||
clst, err := clusterIf.Get(context.Background(), &cluster.ClusterQuery{Server: clusterName})
|
||||
|
|
@ -168,7 +168,7 @@ func NewClusterGetCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
// NewClusterRemoveCommand returns a new instance of an `argocd cluster list` command
|
||||
func NewClusterRemoveCommand() *cobra.Command {
|
||||
func NewClusterRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "rm",
|
||||
Short: fmt.Sprintf("%s cluster rm SERVER", cliName),
|
||||
|
|
@ -177,7 +177,7 @@ func NewClusterRemoveCommand() *cobra.Command {
|
|||
c.HelpFunc()(c, args)
|
||||
os.Exit(1)
|
||||
}
|
||||
conn, clusterIf := NewClusterClient()
|
||||
conn, clusterIf := argocdclient.NewClient(clientOpts).NewClusterClientOrDie()
|
||||
defer util.Close(conn)
|
||||
for _, clusterName := range args {
|
||||
// TODO(jessesuen): find the right context and remove manager RBAC artifacts
|
||||
|
|
@ -191,12 +191,12 @@ func NewClusterRemoveCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
// NewClusterListCommand returns a new instance of an `argocd cluster rm` command
|
||||
func NewClusterListCommand() *cobra.Command {
|
||||
func NewClusterListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: fmt.Sprintf("%s cluster list", cliName),
|
||||
Run: func(c *cobra.Command, args []string) {
|
||||
conn, clusterIf := NewClusterClient()
|
||||
conn, clusterIf := argocdclient.NewClient(clientOpts).NewClusterClientOrDie()
|
||||
defer util.Close(conn)
|
||||
clusters, err := clusterIf.List(context.Background(), &cluster.ClusterQuery{})
|
||||
errors.CheckError(err)
|
||||
|
|
@ -210,21 +210,3 @@ func NewClusterListCommand() *cobra.Command {
|
|||
}
|
||||
return command
|
||||
}
|
||||
|
||||
func NewClusterClient() (*grpc.ClientConn, cluster.ClusterServiceClient) {
|
||||
// TODO: get this from a config or command line flag
|
||||
serverAddr := "localhost:8080"
|
||||
var dialOpts []grpc.DialOption
|
||||
// TODO: add insecure config option and --insecure global flag
|
||||
if true {
|
||||
dialOpts = append(dialOpts, grpc.WithInsecure())
|
||||
} // else if opts.Credentials != nil {
|
||||
// dialOpts = append(dialOpts, grpc.WithTransportCredentials(opts.Credentials))
|
||||
//}
|
||||
conn, err := grpc.Dial(serverAddr, dialOpts...)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to establish connection to %s: %v", serverAddr, err)
|
||||
}
|
||||
clusterIf := cluster.NewClusterServiceClient(conn)
|
||||
return conn, clusterIf
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/argoproj/argo-cd/common"
|
||||
"github.com/argoproj/argo-cd/errors"
|
||||
"github.com/spf13/cobra"
|
||||
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -19,9 +22,10 @@ var (
|
|||
)
|
||||
|
||||
// NewInstallCommand returns a new instance of `argocd install` command
|
||||
func NewInstallCommand(globalArgs *globalFlags) *cobra.Command {
|
||||
func NewInstallCommand() *cobra.Command {
|
||||
var (
|
||||
installParams common.InstallParameters
|
||||
clientConfig clientcmd.ClientConfig
|
||||
)
|
||||
var command = &cobra.Command{
|
||||
Use: "install",
|
||||
|
|
@ -29,7 +33,7 @@ func NewInstallCommand(globalArgs *globalFlags) *cobra.Command {
|
|||
Long: "Install the argocd components",
|
||||
Run: func(c *cobra.Command, args []string) {
|
||||
//conf := GetKubeConfig(globalArgs.kubeConfigPath, globalArgs.kubeConfigOverrides)
|
||||
conf, err := globalArgs.clientConfig.ClientConfig()
|
||||
conf, err := clientConfig.ClientConfig()
|
||||
errors.CheckError(err)
|
||||
extensionsClient := apiextensionsclient.NewForConfigOrDie(conf)
|
||||
kubeClient := kubernetes.NewForConfigOrDie(conf)
|
||||
|
|
@ -42,6 +46,17 @@ func NewInstallCommand(globalArgs *globalFlags) *cobra.Command {
|
|||
command.Flags().StringVar(&installParams.ControllerName, "controller-name", common.DefaultControllerDeploymentName, "name of controller deployment")
|
||||
command.Flags().StringVar(&installParams.ControllerImage, "controller-image", DefaultControllerImage, "use a specified controller image")
|
||||
command.Flags().StringVar(&installParams.ServiceAccount, "service-account", "", "use a specified service account for the workflow-controller deployment")
|
||||
addKubectlFlagsToCmd(command, globalArgs)
|
||||
clientConfig = addKubectlFlagsToCmd(command)
|
||||
return command
|
||||
}
|
||||
|
||||
func addKubectlFlagsToCmd(cmd *cobra.Command) clientcmd.ClientConfig {
|
||||
// The "usual" clientcmd/kubectl flags
|
||||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
|
||||
overrides := clientcmd.ConfigOverrides{}
|
||||
kflags := clientcmd.RecommendedConfigOverrideFlags("")
|
||||
cmd.PersistentFlags().StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to a kube config. Only required if out-of-cluster")
|
||||
clientcmd.BindOverrideFlags(&overrides, cmd.PersistentFlags(), kflags)
|
||||
return clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, &overrides, os.Stdin)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"syscall"
|
||||
"text/tabwriter"
|
||||
|
||||
argocdclient "github.com/argoproj/argo-cd/client"
|
||||
"github.com/argoproj/argo-cd/errors"
|
||||
appsv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
|
||||
"github.com/argoproj/argo-cd/server/repository"
|
||||
|
|
@ -16,11 +17,10 @@ import (
|
|||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// NewRepoCommand returns a new instance of an `argocd repo` command
|
||||
func NewRepoCommand() *cobra.Command {
|
||||
func NewRepoCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "repo",
|
||||
Short: fmt.Sprintf("%s repo COMMAND", cliName),
|
||||
|
|
@ -30,14 +30,14 @@ func NewRepoCommand() *cobra.Command {
|
|||
},
|
||||
}
|
||||
|
||||
command.AddCommand(NewRepoAddCommand())
|
||||
command.AddCommand(NewRepoListCommand())
|
||||
command.AddCommand(NewRepoRemoveCommand())
|
||||
command.AddCommand(NewRepoAddCommand(clientOpts))
|
||||
command.AddCommand(NewRepoListCommand(clientOpts))
|
||||
command.AddCommand(NewRepoRemoveCommand(clientOpts))
|
||||
return command
|
||||
}
|
||||
|
||||
// NewRepoAddCommand returns a new instance of an `argocd repo add` command
|
||||
func NewRepoAddCommand() *cobra.Command {
|
||||
func NewRepoAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var (
|
||||
repo appsv1.Repository
|
||||
)
|
||||
|
|
@ -61,7 +61,7 @@ func NewRepoAddCommand() *cobra.Command {
|
|||
err = git.TestRepo(repo.Repo, repo.Username, repo.Password)
|
||||
}
|
||||
errors.CheckError(err)
|
||||
conn, repoIf := NewRepoClient()
|
||||
conn, repoIf := argocdclient.NewClient(clientOpts).NewRepoClientOrDie()
|
||||
defer util.Close(conn)
|
||||
createdRepo, err := repoIf.Create(context.Background(), &repo)
|
||||
errors.CheckError(err)
|
||||
|
|
@ -88,7 +88,7 @@ func promptCredentials(repo *appsv1.Repository) {
|
|||
}
|
||||
|
||||
// NewRepoRemoveCommand returns a new instance of an `argocd repo list` command
|
||||
func NewRepoRemoveCommand() *cobra.Command {
|
||||
func NewRepoRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "rm",
|
||||
Short: fmt.Sprintf("%s repo rm REPO", cliName),
|
||||
|
|
@ -97,7 +97,7 @@ func NewRepoRemoveCommand() *cobra.Command {
|
|||
c.HelpFunc()(c, args)
|
||||
os.Exit(1)
|
||||
}
|
||||
conn, repoIf := NewRepoClient()
|
||||
conn, repoIf := argocdclient.NewClient(clientOpts).NewRepoClientOrDie()
|
||||
defer util.Close(conn)
|
||||
for _, repoURL := range args {
|
||||
_, err := repoIf.Delete(context.Background(), &repository.RepoQuery{Repo: repoURL})
|
||||
|
|
@ -109,12 +109,12 @@ func NewRepoRemoveCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
// NewRepoListCommand returns a new instance of an `argocd repo rm` command
|
||||
func NewRepoListCommand() *cobra.Command {
|
||||
func NewRepoListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
||||
var command = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: fmt.Sprintf("%s repo list", cliName),
|
||||
Run: func(c *cobra.Command, args []string) {
|
||||
conn, repoIf := NewRepoClient()
|
||||
conn, repoIf := argocdclient.NewClient(clientOpts).NewRepoClientOrDie()
|
||||
defer util.Close(conn)
|
||||
repos, err := repoIf.List(context.Background(), &repository.RepoQuery{})
|
||||
errors.CheckError(err)
|
||||
|
|
@ -128,21 +128,3 @@ func NewRepoListCommand() *cobra.Command {
|
|||
}
|
||||
return command
|
||||
}
|
||||
|
||||
func NewRepoClient() (*grpc.ClientConn, repository.RepositoryServiceClient) {
|
||||
// TODO: get this from a config or command line flag
|
||||
serverAddr := "localhost:8080"
|
||||
var dialOpts []grpc.DialOption
|
||||
// TODO: add insecure config option and --insecure global flag
|
||||
if true {
|
||||
dialOpts = append(dialOpts, grpc.WithInsecure())
|
||||
} // else if opts.Credentials != nil {
|
||||
// dialOpts = append(dialOpts, grpc.WithTransportCredentials(opts.Credentials))
|
||||
//}
|
||||
conn, err := grpc.Dial(serverAddr, dialOpts...)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to establish connection to %s: %v", serverAddr, err)
|
||||
}
|
||||
repoIf := repository.NewRepositoryServiceClient(conn)
|
||||
return conn, repoIf
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,19 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
argocdclient "github.com/argoproj/argo-cd/client"
|
||||
"github.com/argoproj/argo-cd/util/cli"
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
type globalFlags struct {
|
||||
clientConfig clientcmd.ClientConfig
|
||||
}
|
||||
|
||||
// NewCommand returns a new instance of an argocd command
|
||||
func NewCommand() *cobra.Command {
|
||||
var (
|
||||
globalArgs globalFlags
|
||||
clientOpts argocdclient.ClientOptions
|
||||
pathOpts = clientcmd.NewDefaultPathOptions()
|
||||
)
|
||||
pathOptions := clientcmd.NewDefaultPathOptions()
|
||||
|
||||
var command = &cobra.Command{
|
||||
Use: cliName,
|
||||
Short: "argocd controls a ArgoCD server",
|
||||
|
|
@ -27,20 +23,13 @@ func NewCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
command.AddCommand(cli.NewVersionCmd(cliName))
|
||||
command.AddCommand(NewClusterCommand(pathOptions))
|
||||
command.AddCommand(NewApplicationCommand())
|
||||
command.AddCommand(NewRepoCommand())
|
||||
command.AddCommand(NewInstallCommand(&globalArgs))
|
||||
command.AddCommand(NewClusterCommand(&clientOpts, pathOpts))
|
||||
command.AddCommand(NewApplicationCommand(&clientOpts))
|
||||
command.AddCommand(NewRepoCommand(&clientOpts))
|
||||
command.AddCommand(NewInstallCommand())
|
||||
|
||||
command.PersistentFlags().StringVar(&clientOpts.ServerAddr, "server", "localhost:8080", "ArgoCD server address")
|
||||
command.PersistentFlags().BoolVar(&clientOpts.Insecure, "insecure", true, "Disable transport security for the client connection")
|
||||
|
||||
return command
|
||||
}
|
||||
|
||||
func addKubectlFlagsToCmd(cmd *cobra.Command, globalArgs *globalFlags) {
|
||||
// The "usual" clientcmd/kubectl flags
|
||||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
|
||||
overrides := clientcmd.ConfigOverrides{}
|
||||
kflags := clientcmd.RecommendedConfigOverrideFlags("")
|
||||
cmd.PersistentFlags().StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to a kube config. Only required if out-of-cluster")
|
||||
clientcmd.BindOverrideFlags(&overrides, cmd.PersistentFlags(), kflags)
|
||||
globalArgs.clientConfig = clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, &overrides, os.Stdin)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue