package client import ( "context" "github.com/argoproj/argo-workflows/v3/pkg/apiclient" "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow" "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflowtemplate" "github.com/pkg/errors" log "github.com/sirupsen/logrus" "k8s.io/client-go/tools/clientcmd" "os" "sync" ) var LogLevel string var watchWorkflowTokenMap sync.Map type Client struct { ctx context.Context client apiclient.Client workflowTemplateService workflowtemplate.WorkflowTemplateServiceClient workflowService workflow.WorkflowServiceClient workflowWatcherManager *workflowWatcherManager } func NewClient(kubeConfigEnv string, opts ...Option) (*Client, error) { options := new(Options) for _, opt := range opts { opt(options) } level, err := log.ParseLevel(options.logLevel) if err != nil { level = log.ErrorLevel } log.SetLevel(level) err = os.Setenv("KUBECONFIG", kubeConfigEnv) if err != nil { return nil, errors.New(err.Error()) } apiClientCtx, apiClient, err := apiclient.NewClientFromOpts(apiclient.Opts{ ArgoServerOpts: apiclient.ArgoServerOpts{}, ClientConfigSupplier: func() clientcmd.ClientConfig { return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), nil) }, Context: context.Background(), }) if err != nil { return nil, errors.New(err.Error()) } workflowTemplateService, err := apiClient.NewWorkflowTemplateServiceClient() if err != nil { return nil, errors.New(err.Error()) } return &Client{ ctx: apiClientCtx, client: apiClient, workflowTemplateService: workflowTemplateService, workflowService: apiClient.NewWorkflowServiceClient(), workflowWatcherManager: newWorkflowWatcherManager(), }, nil } func Destroy(c *Client) { if c == nil { return } c.unregisterAllWorkflowWatchers() c.ctx.Done() c.client = nil c.workflowTemplateService = nil c.workflowService = nil }