mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-09-27 10:02:10 +08:00
110 lines
2.6 KiB
Go
110 lines
2.6 KiB
Go
package argo
|
|
|
|
import (
|
|
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
|
|
"github.com/argoproj/argo/workflow/common"
|
|
"github.com/argoproj/pkg/json"
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
|
)
|
|
|
|
type Workflow = wfv1.Workflow
|
|
|
|
type Parameter = wfv1.Parameter
|
|
|
|
type Options struct {
|
|
Name string
|
|
Namespace string
|
|
GeneratedName string
|
|
Entrypoint string
|
|
Parameters []Parameter
|
|
ServiceAccount string
|
|
Labels *map[string]string
|
|
}
|
|
|
|
func unmarshalWorkflows(wfBytes []byte, strict bool) (wfs []Workflow, err error) {
|
|
var wf Workflow
|
|
var jsonOpts []json.JSONOpt
|
|
if strict {
|
|
jsonOpts = append(jsonOpts, json.DisallowUnknownFields)
|
|
}
|
|
err = json.Unmarshal(wfBytes, &wf, jsonOpts...)
|
|
if err == nil {
|
|
return []Workflow{wf}, nil
|
|
}
|
|
wfs, err = common.SplitWorkflowYAMLFile(wfBytes, strict)
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (c *Client) create(wf *Workflow, opts *Options) (createdWorkflow *Workflow, err error) {
|
|
if opts == nil {
|
|
opts = &Options{}
|
|
}
|
|
if opts.Name != "" {
|
|
wf.ObjectMeta.Name = opts.Name
|
|
}
|
|
if opts.GeneratedName != "" {
|
|
wf.ObjectMeta.GenerateName = opts.GeneratedName
|
|
}
|
|
if opts.Entrypoint != "" {
|
|
wf.Spec.Entrypoint = opts.Entrypoint
|
|
}
|
|
if opts.ServiceAccount != "" {
|
|
wf.Spec.ServiceAccountName = opts.ServiceAccount
|
|
}
|
|
if len(opts.Parameters) > 0 {
|
|
newParams := make([]wfv1.Parameter, 0)
|
|
passedParams := make(map[string]bool)
|
|
for _, param := range opts.Parameters {
|
|
newParams = append(newParams, param)
|
|
passedParams[param.Name] = true
|
|
}
|
|
|
|
for _, param := range wf.Spec.Arguments.Parameters {
|
|
if _, ok := passedParams[param.Name]; ok {
|
|
// this parameter was overridden via command line
|
|
continue
|
|
}
|
|
newParams = append(newParams, param)
|
|
}
|
|
wf.Spec.Arguments.Parameters = newParams
|
|
}
|
|
if opts.Labels != nil {
|
|
wf.ObjectMeta.Labels = *opts.Labels
|
|
}
|
|
|
|
createdWorkflow, err = c.Clientset.ArgoprojV1alpha1().Workflows(opts.Namespace).Create(wf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (c *Client) Create(manifest []byte, opts *Options) (createdWorkflows []*Workflow, err error) {
|
|
workflows, err := unmarshalWorkflows(manifest, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, wf := range workflows {
|
|
createdWorkflow, err := c.create(&wf, opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
createdWorkflows = append(createdWorkflows, createdWorkflow)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (c *Client) Get(name string, opts *Options) (workflow *Workflow, err error) {
|
|
workflow, err = c.Clientset.ArgoprojV1alpha1().Workflows(opts.Namespace).Get(name, v1.GetOptions{})
|
|
|
|
return
|
|
}
|