client.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "sync"
  11. )
  12. var watchWorkflowTokenMap sync.Map
  13. type Client struct {
  14. ctx context.Context
  15. client apiclient.Client
  16. workflowTemplateService workflowtemplate.WorkflowTemplateServiceClient
  17. workflowService workflow.WorkflowServiceClient
  18. }
  19. func NewClient(kubeConfigEnv string) (*Client, error) {
  20. err := os.Setenv("KUBECONFIG", kubeConfigEnv)
  21. if err != nil {
  22. return nil, errors.New(err.Error())
  23. }
  24. apiClientCtx, apiClient, err := apiclient.NewClientFromOpts(apiclient.Opts{
  25. ArgoServerOpts: apiclient.ArgoServerOpts{},
  26. ClientConfigSupplier: func() clientcmd.ClientConfig {
  27. return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), nil)
  28. },
  29. Context: context.Background(),
  30. })
  31. if err != nil {
  32. return nil, errors.New(err.Error())
  33. }
  34. workflowTemplateService, err := apiClient.NewWorkflowTemplateServiceClient()
  35. if err != nil {
  36. return nil, errors.New(err.Error())
  37. }
  38. return &Client{
  39. ctx: apiClientCtx,
  40. client: apiClient,
  41. workflowTemplateService: workflowTemplateService,
  42. workflowService: apiClient.NewWorkflowServiceClient(),
  43. }, nil
  44. }
  45. func Destroy(c *Client) {
  46. if c == nil {
  47. return
  48. }
  49. c.ctx.Done()
  50. c.client = nil
  51. c.workflowTemplateService = nil
  52. c.workflowService = nil
  53. }