mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-18 20:17:04 +00:00
Feat: support context.cluster (#4836)
Signed-off-by: Somefive <yd219913@alibaba-inc.com> Signed-off-by: Somefive <yd219913@alibaba-inc.com>
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+13
@@ -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"]
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user