forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add update command for cron,template,cluster-template. Fixes: a…
…rgoproj#5464 argoproj#7344 (argoproj#12803) Signed-off-by: shuangkun <[email protected]> Co-authored-by: Simon Behar <[email protected]>
- Loading branch information
Showing
25 changed files
with
780 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package clustertemplate | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client" | ||
"github.com/argoproj/argo-workflows/v3/pkg/apiclient/clusterworkflowtemplate" | ||
) | ||
|
||
type cliUpdateOpts struct { | ||
output string // --output | ||
strict bool // --strict | ||
} | ||
|
||
func NewUpdateCommand() *cobra.Command { | ||
var cliUpdateOpts cliUpdateOpts | ||
command := &cobra.Command{ | ||
Use: "update FILE1 FILE2...", | ||
Short: "update a cluster workflow template", | ||
Example: `# Update a Cluster Workflow Template: | ||
argo cluster-template update FILE1 | ||
# Update a Cluster Workflow Template and print it as YAML: | ||
argo cluster-template update FILE1 --output yaml | ||
# Update a Cluster Workflow Template with relaxed validation: | ||
argo cluster-template update FILE1 --strict false | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 { | ||
cmd.HelpFunc()(cmd, args) | ||
os.Exit(1) | ||
} | ||
|
||
updateClusterWorkflowTemplates(cmd.Context(), args, &cliUpdateOpts) | ||
}, | ||
} | ||
command.Flags().StringVarP(&cliUpdateOpts.output, "output", "o", "", "Output format. One of: name|json|yaml|wide") | ||
command.Flags().BoolVar(&cliUpdateOpts.strict, "strict", true, "perform strict workflow validation") | ||
return command | ||
} | ||
|
||
func updateClusterWorkflowTemplates(ctx context.Context, filePaths []string, cliOpts *cliUpdateOpts) { | ||
if cliOpts == nil { | ||
cliOpts = &cliUpdateOpts{} | ||
} | ||
ctx, apiClient := client.NewAPIClient(ctx) | ||
serviceClient, err := apiClient.NewClusterWorkflowTemplateServiceClient() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
clusterWorkflowTemplates := generateClusterWorkflowTemplates(filePaths, cliOpts.strict) | ||
|
||
for _, wftmpl := range clusterWorkflowTemplates { | ||
current, err := serviceClient.GetClusterWorkflowTemplate(ctx, &clusterworkflowtemplate.ClusterWorkflowTemplateGetRequest{ | ||
Name: wftmpl.Name, | ||
}) | ||
if err != nil { | ||
log.Fatalf("Failed to get existing cluster workflow template %q to update: %v", wftmpl.Name, err) | ||
} | ||
wftmpl.ResourceVersion = current.ResourceVersion | ||
updated, err := serviceClient.UpdateClusterWorkflowTemplate(ctx, &clusterworkflowtemplate.ClusterWorkflowTemplateUpdateRequest{ | ||
Template: &wftmpl, | ||
}) | ||
if err != nil { | ||
log.Fatalf("Failed to update cluster workflow template: %s, %v", wftmpl.Name, err) | ||
} | ||
printClusterWorkflowTemplate(updated, cliOpts.output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package clustertemplate | ||
|
||
import ( | ||
"log" | ||
|
||
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" | ||
"github.com/argoproj/argo-workflows/v3/workflow/util" | ||
) | ||
|
||
func generateClusterWorkflowTemplates(filePaths []string, strict bool) []wfv1.ClusterWorkflowTemplate { | ||
fileContents, err := util.ReadManifest(filePaths...) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var clusterWorkflowTemplates []wfv1.ClusterWorkflowTemplate | ||
for _, body := range fileContents { | ||
cwftmpls, err := unmarshalClusterWorkflowTemplates(body, strict) | ||
if err != nil { | ||
log.Fatalf("Failed to parse cluster workflow template: %v", err) | ||
} | ||
clusterWorkflowTemplates = append(clusterWorkflowTemplates, cwftmpls...) | ||
} | ||
|
||
if len(clusterWorkflowTemplates) == 0 { | ||
log.Fatalln("No cluster workflow template found in given files") | ||
} | ||
|
||
return clusterWorkflowTemplates | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package cron | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client" | ||
cronworkflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow" | ||
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" | ||
"github.com/argoproj/argo-workflows/v3/workflow/util" | ||
) | ||
|
||
type cliUpdateOpts struct { | ||
output string // --output | ||
strict bool // --strict | ||
} | ||
|
||
func NewUpdateCommand() *cobra.Command { | ||
var ( | ||
cliUpdateOpts cliUpdateOpts | ||
submitOpts wfv1.SubmitOpts | ||
parametersFile string | ||
) | ||
command := &cobra.Command{ | ||
Use: "update FILE1 FILE2...", | ||
Short: "update a cron workflow", | ||
Example: `# Update a Cron Workflow Template: | ||
argo cron update FILE1 | ||
# Update a Cron Workflow Template and print it as YAML: | ||
argo cron update FILE1 --output yaml | ||
# Update a Cron Workflow Template with relaxed validation: | ||
argo cron update FILE1 --strict false | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
checkArgs(cmd, args, parametersFile, &submitOpts) | ||
|
||
updateCronWorkflows(cmd.Context(), args, &cliUpdateOpts, &submitOpts) | ||
}, | ||
} | ||
|
||
util.PopulateSubmitOpts(command, &submitOpts, ¶metersFile, false) | ||
command.Flags().StringVarP(&cliUpdateOpts.output, "output", "o", "", "Output format. One of: name|json|yaml|wide") | ||
command.Flags().BoolVar(&cliUpdateOpts.strict, "strict", true, "perform strict workflow validation") | ||
return command | ||
} | ||
|
||
func updateCronWorkflows(ctx context.Context, filePaths []string, cliOpts *cliUpdateOpts, submitOpts *wfv1.SubmitOpts) { | ||
ctx, apiClient := client.NewAPIClient(ctx) | ||
serviceClient, err := apiClient.NewCronWorkflowServiceClient() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
cronWorkflows := generateCronWorkflows(filePaths, cliOpts.strict) | ||
|
||
for _, cronWf := range cronWorkflows { | ||
newWf := wfv1.Workflow{Spec: cronWf.Spec.WorkflowSpec} | ||
err := util.ApplySubmitOpts(&newWf, submitOpts) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if cronWf.Namespace == "" { | ||
cronWf.Namespace = client.Namespace() | ||
} | ||
current, err := serviceClient.GetCronWorkflow(ctx, &cronworkflowpkg.GetCronWorkflowRequest{ | ||
Name: cronWf.Name, | ||
Namespace: cronWf.Namespace, | ||
}) | ||
if err != nil { | ||
log.Fatalf("Failed to get existing cron workflow %q to update: %v", cronWf.Name, err) | ||
} | ||
cronWf.ResourceVersion = current.ResourceVersion | ||
updated, err := serviceClient.UpdateCronWorkflow(ctx, &cronworkflowpkg.UpdateCronWorkflowRequest{ | ||
Namespace: cronWf.Namespace, | ||
CronWorkflow: &cronWf, | ||
}) | ||
if err != nil { | ||
log.Fatalf("Failed to update workflow template: %v", err) | ||
} | ||
fmt.Print(getCronWorkflowGet(updated)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.