client.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package client
  2. import (
  3. "context"
  4. "github.com/argoproj/argo-workflows/v3/pkg/apiclient"
  5. "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow"
  6. "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflowtemplate"
  7. "github.com/pkg/errors"
  8. "k8s.io/client-go/tools/clientcmd"
  9. "os"
  10. )
  11. type Client struct {
  12. ctx context.Context
  13. client apiclient.Client
  14. workflowTemplateService workflowtemplate.WorkflowTemplateServiceClient
  15. workflowService workflow.WorkflowServiceClient
  16. }
  17. func NewClient(kubeConfigEnv string) (*Client, error) {
  18. err := os.Setenv("KUBECONFIG", kubeConfigEnv)
  19. if err != nil {
  20. return nil, errors.New(err.Error())
  21. }
  22. apiClientCtx, apiClient, err := apiclient.NewClientFromOpts(apiclient.Opts{
  23. ArgoServerOpts: apiclient.ArgoServerOpts{},
  24. ClientConfigSupplier: func() clientcmd.ClientConfig {
  25. return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), nil)
  26. },
  27. Context: context.Background(),
  28. })
  29. if err != nil {
  30. return nil, errors.New(err.Error())
  31. }
  32. workflowTemplateService, err := apiClient.NewWorkflowTemplateServiceClient()
  33. if err != nil {
  34. return nil, errors.New(err.Error())
  35. }
  36. return &Client{
  37. ctx: apiClientCtx,
  38. client: apiClient,
  39. workflowTemplateService: workflowTemplateService,
  40. workflowService: apiClient.NewWorkflowServiceClient(),
  41. }, nil
  42. }