client.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/workflowtemplate"
  6. "github.com/pkg/errors"
  7. "k8s.io/client-go/tools/clientcmd"
  8. "os"
  9. )
  10. type Client struct {
  11. ctx context.Context
  12. client apiclient.Client
  13. workflowTemplateService workflowtemplate.WorkflowTemplateServiceClient
  14. }
  15. func NewClient(kubeConfigEnv string) (*Client, error) {
  16. err := os.Setenv("KUBECONFIG", kubeConfigEnv)
  17. if err != nil {
  18. return nil, errors.New(err.Error())
  19. }
  20. apiClientCtx, apiClient, err := apiclient.NewClientFromOpts(apiclient.Opts{
  21. ArgoServerOpts: apiclient.ArgoServerOpts{},
  22. ClientConfigSupplier: func() clientcmd.ClientConfig {
  23. return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), nil)
  24. },
  25. Context: context.Background(),
  26. })
  27. if err != nil {
  28. return nil, errors.New(err.Error())
  29. }
  30. workflowTemplateService, err := apiClient.NewWorkflowTemplateServiceClient()
  31. if err != nil {
  32. return nil, errors.New(err.Error())
  33. }
  34. return &Client{
  35. ctx: apiClientCtx,
  36. client: apiClient,
  37. workflowTemplateService: workflowTemplateService,
  38. }, nil
  39. }