Skip to content

Commit

Permalink
feat: support K8S json patch (#4908)
Browse files Browse the repository at this point in the history
Signed-off-by: book987 <book78987book@gmail.com>
  • Loading branch information
book987 committed Jan 21, 2021
1 parent 957ef67 commit 6e961ec
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
20 changes: 20 additions & 0 deletions test/e2e/functional/k8s-patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: k8s-patch-
labels:
argo-e2e: "true"
spec:
entrypoint: patch-pod
templates:
- name: patch-pod
resource:
action: patch
mergeStrategy: json
flags:
- pods
- "{{pod.name}}"
manifest: |
- op: add
path: /metadata/annotations/foo
value: bar
12 changes: 12 additions & 0 deletions test/e2e/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,18 @@ spec:
})
}

func (s *FunctionalSuite) TestK8SJSONPatch() {
s.Given().
Workflow("@functional/k8s-patch.yaml").
When().
SubmitWorkflow().
WaitForWorkflow().
Then().
ExpectWorkflow(func(t *testing.T, metadata *metav1.ObjectMeta, status *wfv1.WorkflowStatus) {
assert.Equal(t, wfv1.WorkflowSucceeded, status.Phase)
})
}

func TestFunctionalSuite(t *testing.T) {
suite.Run(t, new(FunctionalSuite))
}
15 changes: 7 additions & 8 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2658,14 +2658,13 @@ func (woc *wfOperationCtx) executeResource(ctx context.Context, nodeName string,

tmpl = tmpl.DeepCopy()

// Try to unmarshal the given manifest.
obj := unstructured.Unstructured{}
err := yaml.Unmarshal([]byte(tmpl.Resource.Manifest), &obj)
if err != nil {
return node, err
}

if tmpl.Resource.SetOwnerReference {
obj := unstructured.Unstructured{}
err := yaml.Unmarshal([]byte(tmpl.Resource.Manifest), &obj)
if err != nil {
return node, err
}

ownerReferences := obj.GetOwnerReferences()
obj.SetOwnerReferences(append(ownerReferences, *metav1.NewControllerRef(woc.wf, wfv1.SchemeGroupVersion.WithKind(workflow.WorkflowKind))))
bytes, err := yaml.Marshal(obj.Object)
Expand All @@ -2677,7 +2676,7 @@ func (woc *wfOperationCtx) executeResource(ctx context.Context, nodeName string,

mainCtr := woc.newExecContainer(common.MainContainerName, tmpl)
mainCtr.Command = []string{"argoexec", "resource", tmpl.Resource.Action}
_, err = woc.createWorkflowPod(ctx, nodeName, *mainCtr, tmpl, &createWorkflowPodOpts{onExitPod: opts.onExitTemplate, executionDeadline: opts.executionDeadline})
_, err := woc.createWorkflowPod(ctx, nodeName, *mainCtr, tmpl, &createWorkflowPodOpts{onExitPod: opts.onExitTemplate, executionDeadline: opts.executionDeadline})
if err != nil {
return woc.requeueIfTransientErr(err, node.Name)
}
Expand Down
5 changes: 4 additions & 1 deletion workflow/executor/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func (we *WorkflowExecutor) getKubectlArguments(action string, manifestPath stri
args = append(args, flags...)
}

if len(buff) != 0 {
// Action "patch" require flag "-p" with resource arguments.
// But kubectl disallow specify both "-f" flag and resource arguments.
// Flag "-f" should be excluded for action "patch" here.
if len(buff) != 0 && action != "patch" {
args = append(args, "-f")
args = append(args, manifestPath)
} else if len(flags) <= 0 {
Expand Down
5 changes: 2 additions & 3 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/valyala/fasttemplate"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
apivalidation "k8s.io/apimachinery/pkg/util/validation"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -692,8 +691,8 @@ func (ctx *templateValidationCtx) validateLeaf(scope map[string]interface{}, tmp
}
}
if !placeholderGenerator.IsPlaceholder(tmpl.Resource.Manifest) {
// Try to unmarshal the given manifest.
obj := unstructured.Unstructured{}
// Try to unmarshal the given manifest, just ensuring it's a valid YAML.
var obj interface{}
err := yaml.Unmarshal([]byte(tmpl.Resource.Manifest), &obj)
if err != nil {
return errors.Errorf(errors.CodeBadRequest, "templates.%s.resource.manifest must be a valid yaml", tmpl.Name)
Expand Down

0 comments on commit 6e961ec

Please sign in to comment.