From 17afabc1ffd256c2cad2545c22697ec5d9c60b38 Mon Sep 17 00:00:00 2001 From: Somefive Date: Sun, 9 Oct 2022 14:08:02 +0800 Subject: [PATCH] Feat: support context.cluster (#4836) Signed-off-by: Somefive Signed-off-by: Somefive --- .../v1alpha2/application/generator.go | 5 ++++ pkg/cue/process/handle.go | 2 ++ pkg/cue/process/keyword.go | 2 ++ .../multicluster_test.go | 28 +++++++++++++++++++ .../app-component-with-cluster-context.yaml | 13 +++++++++ .../testdata/def/cluster-config.yaml | 19 +++++++++++++ 6 files changed, 69 insertions(+) create mode 100644 test/e2e-multicluster-test/testdata/app/app-component-with-cluster-context.yaml create mode 100644 test/e2e-multicluster-test/testdata/def/cluster-config.yaml diff --git a/pkg/controller/core.oam.dev/v1alpha2/application/generator.go b/pkg/controller/core.oam.dev/v1alpha2/application/generator.go index b332c7276..b6697bc96 100644 --- a/pkg/controller/core.oam.dev/v1alpha2/application/generator.go +++ b/pkg/controller/core.oam.dev/v1alpha2/application/generator.go @@ -22,6 +22,7 @@ import ( "strings" "time" + pkgmulticluster "github.com/kubevela/pkg/multicluster" utilfeature "k8s.io/apiserver/pkg/util/feature" "github.com/oam-dev/kubevela/pkg/features" @@ -426,6 +427,10 @@ func (h *AppHandler) prepareWorkloadAndManifests(ctx context.Context, if rk := replicaKeyFromContext(ctx); rk != "" { ctxData.ReplicaKey = rk } + ctxData.Cluster = pkgmulticluster.Local + if cluster, ok := pkgmulticluster.ClusterFrom(ctx); ok && cluster != "" { + ctxData.Cluster = cluster + } }) if err != nil { return nil, nil, errors.WithMessage(err, "GenerateComponentManifest") diff --git a/pkg/cue/process/handle.go b/pkg/cue/process/handle.go index b19416ceb..2f03c1ee1 100644 --- a/pkg/cue/process/handle.go +++ b/pkg/cue/process/handle.go @@ -28,6 +28,7 @@ import ( // ContextData is the core data of process context type ContextData struct { Namespace string + Cluster string AppName string CompName string StepName string @@ -66,5 +67,6 @@ func NewContext(data ContextData) process.Context { ctx.PushData(ContextReplicaKey, data.ReplicaKey) revNum, _ := util.ExtractRevisionNum(data.AppRevisionName, "-") ctx.PushData(ContextAppRevisionNum, revNum) + ctx.PushData(ContextCluster, data.Cluster) return ctx } diff --git a/pkg/cue/process/keyword.go b/pkg/cue/process/keyword.go index 417b0b7f3..4752756c9 100644 --- a/pkg/cue/process/keyword.go +++ b/pkg/cue/process/keyword.go @@ -39,6 +39,8 @@ const ( ContextAppAnnotations = "appAnnotations" // ContextNamespace is the namespace of the app ContextNamespace = "namespace" + // ContextCluster is the cluster currently focusing on + ContextCluster = "cluster" // ContextPublishVersion is the publish version of the app ContextPublishVersion = "publishVersion" // ContextWorkflowName is the name of the workflow diff --git a/test/e2e-multicluster-test/multicluster_test.go b/test/e2e-multicluster-test/multicluster_test.go index 4c10ac80c..238a2b811 100644 --- a/test/e2e-multicluster-test/multicluster_test.go +++ b/test/e2e-multicluster-test/multicluster_test.go @@ -43,6 +43,7 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/common" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" + kubevelatypes "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/multicluster" "github.com/oam-dev/kubevela/pkg/oam" ) @@ -667,5 +668,32 @@ var _ = Describe("Test multicluster scenario", func() { g.Expect(k8sClient.Get(workerCtx, client.ObjectKey{Namespace: testNamespace, Name: "data-worker"}, &appsv1.Deployment{})).Should(Succeed()) }, 20*time.Second).Should(Succeed()) }) + + It("Test application with component using cluster context", func() { + By("Create definition") + bs, err := os.ReadFile("./testdata/def/cluster-config.yaml") + Expect(err).Should(Succeed()) + def := &v1beta1.ComponentDefinition{} + Expect(yaml.Unmarshal(bs, def)).Should(Succeed()) + def.SetNamespace(kubevelatypes.DefaultKubeVelaNS) + Expect(k8sClient.Create(hubCtx, def)).Should(Succeed()) + bs, err = os.ReadFile("./testdata/app/app-component-with-cluster-context.yaml") + Expect(err).Should(Succeed()) + app := &v1beta1.Application{} + Expect(yaml.Unmarshal(bs, app)).Should(Succeed()) + app.SetNamespace(testNamespace) + Expect(k8sClient.Create(hubCtx, app)).Should(Succeed()) + key := client.ObjectKeyFromObject(app) + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(hubCtx, key, app)).Should(Succeed()) + g.Expect(app.Status.Phase).Should(Equal(common.ApplicationRunning)) + }, 20*time.Second).Should(Succeed()) + cm := &corev1.ConfigMap{} + Expect(k8sClient.Get(hubCtx, types.NamespacedName{Namespace: testNamespace, Name: "test"}, cm)).Should(Succeed()) + Expect(cm.Data["cluster"]).Should(Equal("local")) + Expect(k8sClient.Get(workerCtx, types.NamespacedName{Namespace: testNamespace, Name: "test"}, cm)).Should(Succeed()) + Expect(cm.Data["cluster"]).Should(Equal("cluster-worker")) + Expect(k8sClient.Delete(hubCtx, def)).Should(Succeed()) + }) }) }) diff --git a/test/e2e-multicluster-test/testdata/app/app-component-with-cluster-context.yaml b/test/e2e-multicluster-test/testdata/app/app-component-with-cluster-context.yaml new file mode 100644 index 000000000..41a854087 --- /dev/null +++ b/test/e2e-multicluster-test/testdata/app/app-component-with-cluster-context.yaml @@ -0,0 +1,13 @@ +apiVersion: core.oam.dev/v1beta1 +kind: Application +metadata: + name: app-component-with-cluster-context +spec: + components: + - name: test + type: cluster-config + policies: + - name: topology + type: topology + properties: + clusters: ["local", "cluster-worker"] diff --git a/test/e2e-multicluster-test/testdata/def/cluster-config.yaml b/test/e2e-multicluster-test/testdata/def/cluster-config.yaml new file mode 100644 index 000000000..e2cf607a4 --- /dev/null +++ b/test/e2e-multicluster-test/testdata/def/cluster-config.yaml @@ -0,0 +1,19 @@ +apiVersion: core.oam.dev/v1beta1 +kind: ComponentDefinition +metadata: + name: cluster-config +spec: + schematic: + cue: + template: | + output: { + apiVersion: "v1" + kind: "ConfigMap" + metadata: name: context.name + data: cluster: context.cluster + } + parameter: {} + workload: + definition: + apiVersion: v1 + kind: ConfigMap \ No newline at end of file