From ed1a0eea5419b099d181167cde8a9c2bf0f865dd Mon Sep 17 00:00:00 2001 From: FogDong Date: Mon, 21 Nov 2022 16:03:31 +0800 Subject: [PATCH] Feat: add patch in kube.apply Signed-off-by: FogDong --- pkg/providers/kube/handle_test.go | 17 ++++++++++++++--- pkg/stdlib/actions/v1/pkgs/kube.cue | 2 ++ pkg/stdlib/packages_test.go | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/providers/kube/handle_test.go b/pkg/providers/kube/handle_test.go index 179c94c..dc37bc8 100644 --- a/pkg/providers/kube/handle_test.go +++ b/pkg/providers/kube/handle_test.go @@ -190,20 +190,31 @@ cluster: "" v, err := value.NewValue(fmt.Sprintf(` value: {%s} cluster: "" -patch: metadata: name: "test-app-1"`, s), nil, "") +patch: { + metadata: name: "test-app-1" + spec: containers: [{ + // +patchStrategy=replace + env: [{ + name: "APP" + value: "nginx-new" + }] + }] +}`, s), nil, "") Expect(err).ToNot(HaveOccurred()) mCtx := monitorContext.NewTraceContext(context.Background(), "") err = p.Apply(mCtx, ctx, v, nil) Expect(err).ToNot(HaveOccurred()) - workload, err := component.Workload.Unstructured() + pod := &corev1.Pod{} Expect(err).ToNot(HaveOccurred()) Eventually(func() error { return k8sClient.Get(context.Background(), client.ObjectKey{ Namespace: "default", Name: "test-app-1", - }, workload) + }, pod) }, time.Second*2, time.Millisecond*300).Should(BeNil()) + Expect(pod.Name).To(Equal("test-app-1")) + Expect(pod.Spec.Containers[0].Env[0].Value).To(Equal("nginx-new")) }) It("list", func() { diff --git a/pkg/stdlib/actions/v1/pkgs/kube.cue b/pkg/stdlib/actions/v1/pkgs/kube.cue index 27cc3b1..bff43cc 100644 --- a/pkg/stdlib/actions/v1/pkgs/kube.cue +++ b/pkg/stdlib/actions/v1/pkgs/kube.cue @@ -6,6 +6,8 @@ cluster: *"" | string // +usage=The resource to apply value: {...} + // +usage=The patcher that will be applied to the resource, you can define the strategy of list merge through comments. Reference doc here: https://kubevela.io/docs/platform-engineers/traits/patch-trait#patch-in-workflow-step + patch?: {...} ... } diff --git a/pkg/stdlib/packages_test.go b/pkg/stdlib/packages_test.go index 4237981..2fbbd41 100644 --- a/pkg/stdlib/packages_test.go +++ b/pkg/stdlib/packages_test.go @@ -103,5 +103,5 @@ apply: op.test`) r.NoError(v.Err()) s, err = sets.ToString(v) r.NoError(err) - r.Equal(s, "#do: \"apply\"\n#provider: \"kube\"\n\n// +usage=The cluster to use\ncluster: *\"\" | string\n// +usage=The resource to apply\nvalue: {}\n") + r.Contains(s, "apply") }