Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions controllers/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/kubevela/pkg/multicluster"
"github.com/kubevela/pkg/util/test/definition"

"github.com/kubevela/workflow/api/v1alpha1"
Expand Down Expand Up @@ -87,6 +88,40 @@ var _ = Describe("Test Workflow", func() {
Expect(k8sClient.DeleteAllOf(ctx, &corev1.ConfigMap{}, client.InNamespace(namespace))).Should(Succeed())
})

It("uses the reconciler client for multi-cluster kube provider calls", func() {
recordingClient := &clusterRecordingClient{Client: k8sClient}
testReconciler := &WorkflowRunReconciler{
Client: recordingClient,
Scheme: testScheme,
Recorder: reconciler.Recorder,
}

wr := wrTemplate.DeepCopy()
wr.Name = "multi-cluster-client"
wr.Spec.WorkflowSpec.Steps = []oamv1alpha1.WorkflowStep{{
WorkflowStepBase: oamv1alpha1.WorkflowStepBase{
Name: "apply-remote-config",
Type: "apply-object",
Properties: &runtime.RawExtension{Raw: []byte(`{
"cluster":"managed-cluster",
"value":{
"apiVersion":"v1",
"kind":"ConfigMap",
"metadata":{"name":"remote-config","namespace":"test-ns"},
"data":{"key":"value"}
}
}`)},
},
}}

Expect(k8sClient.Create(ctx, wr)).Should(Succeed())
tryReconcile(testReconciler, wr.Name, wr.Namespace)

Expect(recordingClient.clusters).Should(ContainElement("managed-cluster"))
remoteConfig := &corev1.ConfigMap{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Name: "remote-config", Namespace: namespace}, remoteConfig)).Should(Succeed())
})

It("get steps from workflow ref", func() {
workflow := &oamv1alpha1.Workflow{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -2086,6 +2121,27 @@ func tryReconcile(r *WorkflowRunReconciler, name, ns string) {
Expect(err).Should(BeNil())
}

type clusterRecordingClient struct {
client.Client
clusters []string
}

func (c *clusterRecordingClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
c.recordCluster(ctx)
return c.Client.Get(ctx, key, obj, opts...)
}

func (c *clusterRecordingClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
c.recordCluster(ctx)
return c.Client.Create(ctx, obj, opts...)
}

func (c *clusterRecordingClient) recordCluster(ctx context.Context) {
if cluster, ok := multicluster.ClusterFrom(ctx); ok && cluster != "" {
c.clusters = append(c.clusters, cluster)
}
}

func setupNamespace(ctx context.Context, namespace string) {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: namespace},
Expand Down
1 change: 1 addition & 0 deletions controllers/workflowrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (r *WorkflowRunReconciler) Reconcile(ctx context.Context, req ctrl.Request)
defer cancel()

ctx = types.SetNamespaceInCtx(ctx, req.Namespace)
ctx = providertypes.WithKubeClient(ctx, r.Client)
ctx = context.WithValue(ctx, providertypes.ConfigFactoryKey, wfconfig.NewK8sFactory(r.Client))
ctx = providertypes.WithLabelParams(ctx, map[string]string{
types.LabelWorkflowRunName: req.Name,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/google/go-cmp v0.7.0
github.com/hashicorp/go-version v1.6.0
github.com/kubevela/kube-trigger v0.1.2-0.20261009024712-394932042ab6
github.com/kubevela/pkg v1.11.1-0.20261019120055-ed78b6ef9194
github.com/kubevela/pkg v1.11.1-0.20261126171820-6b8007f3ec60
github.com/onsi/ginkgo/v2 v2.32.1
github.com/onsi/gomega v1.43.0
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubevela/kube-trigger v0.1.2-0.20261009024712-394932042ab6 h1:8WHCisQyEBTZqDz0y08xVQVUL1xl7rmh8P2G2hzfa0Y=
github.com/kubevela/kube-trigger v0.1.2-0.20261009024712-394932042ab6/go.mod h1:E+vnOAZNcwmMb/jaIEPEyP8kc3JxWSSJxXC+pv+RQpI=
github.com/kubevela/pkg v1.11.1-0.20261019120055-ed78b6ef9194 h1:M5lH0sdoySm3GK/sSSg/uuwTtJa1/1psMO67wfZF77w=
github.com/kubevela/pkg v1.11.1-0.20261019120055-ed78b6ef9194/go.mod h1:Iah36AOQVn0WNut6R++rVzt8KlriB8JyliuudW3pBH4=
github.com/kubevela/pkg v1.11.1-0.20261126171820-6b8007f3ec60 h1:yj/rnNUEEXDvYTvPW1rxOhOXFyb14NAOJyTKB8tm3yQ=
github.com/kubevela/pkg v1.11.1-0.20261126171820-6b8007f3ec60/go.mod h1:Iah36AOQVn0WNut6R++rVzt8KlriB8JyliuudW3pBH4=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lib/pq v1.12.0 h1:mC1zeiNamwKBecjHarAr26c/+d8V5w/u4J0I/yASbJo=
Expand Down
5 changes: 5 additions & 0 deletions pkg/providers/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ func ApplyInParallel(ctx context.Context, params *ApplyInParallelParams) (*Apply
if workloads[i].GetNamespace() == "" {
workloads[i].SetNamespace("default")
}
for k, v := range params.RuntimeParams.Labels {
if err := k8s.AddLabel(workloads[i], k, v); err != nil {
return nil, err
}
}
}
deployCtx := handleContext(ctx, params.Params.Cluster)
if err := handlers.Apply(deployCtx, params.KubeClient, params.Params.Cluster, WorkflowResourceCreator, workloads...); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/providers/kube/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,45 @@ $params: {
}, time.Second*2, time.Millisecond*300).Should(BeNil())
})

It("apply parallel with runtime labels", func() {
un1 := testUnstructured.DeepCopy()
un1.SetName("labeled-app1")
un1.SetLabels(map[string]string{
"test": "test",
})
un2 := testUnstructured.DeepCopy()
un2.SetName("labeled-app2")
un2.SetLabels(map[string]string{
"test": "test",
})
ctx := context.Background()
_, err := ApplyInParallel(ctx, &ApplyInParallelParams{
Params: ApplyInParallelVars{
Resources: []*unstructured.Unstructured{un1, un2},
},
RuntimeParams: providertypes.RuntimeParams{
Labels: map[string]string{
"hello": "world",
},
KubeClient: k8sClient,
},
})
Expect(err).ToNot(HaveOccurred())
for _, name := range []string{"labeled-app1", "labeled-app2"} {
pod := &corev1.Pod{}
Eventually(func() error {
return k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: "default",
Name: name,
}, pod)
}, time.Second*2, time.Millisecond*300).Should(BeNil())
Expect(pod.GetLabels()).Should(Equal(map[string]string{
"test": "test",
"hello": "world",
}))
}
})

It("test error case", func() {
ctx := context.Background()
res, err := Read(ctx, &ResourceParams{
Expand Down
5 changes: 5 additions & 0 deletions pkg/providers/legacy/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ func ApplyInParallel(ctx context.Context, params *ApplyInParallelParams) (*Apply
if workloads[i].GetNamespace() == "" {
workloads[i].SetNamespace("default")
}
for k, v := range params.RuntimeParams.Labels {
if err := k8s.AddLabel(workloads[i], k, v); err != nil {
return nil, err
}
}
}
deployCtx := handleContext(ctx, params.Params.Cluster)
if err := handlers.Apply(deployCtx, params.KubeClient, params.Params.Cluster, WorkflowResourceCreator, workloads...); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/providers/legacy/kube/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,45 @@ filter: {}
}, time.Second*2, time.Millisecond*300).Should(BeNil())
})

It("apply parallel with runtime labels", func() {
un1 := testUnstructured.DeepCopy()
un1.SetName("labeled-app1")
un1.SetLabels(map[string]string{
"test": "test",
})
un2 := testUnstructured.DeepCopy()
un2.SetName("labeled-app2")
un2.SetLabels(map[string]string{
"test": "test",
})
ctx := context.Background()
_, err := ApplyInParallel(ctx, &ApplyInParallelParams{
Params: ApplyInParallelVars{
Resources: []*unstructured.Unstructured{un1, un2},
},
RuntimeParams: providertypes.RuntimeParams{
Labels: map[string]string{
"hello": "world",
},
KubeClient: k8sClient,
},
})
Expect(err).ToNot(HaveOccurred())
for _, name := range []string{"labeled-app1", "labeled-app2"} {
pod := &corev1.Pod{}
Eventually(func() error {
return k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: "default",
Name: name,
}, pod)
}, time.Second*2, time.Millisecond*300).Should(BeNil())
Expect(pod.GetLabels()).Should(Equal(map[string]string{
"test": "test",
"hello": "world",
}))
}
})

It("test error case", func() {
ctx := context.Background()
res, err := Read(ctx, &ResourceParams{
Expand Down
5 changes: 5 additions & 0 deletions pkg/providers/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func WithLabelParams(parent context.Context, labels map[string]string) context.C
return context.WithValue(parent, LabelsKey, labels)
}

// WithKubeClient returns a copy of parent in which the kube client is set.
func WithKubeClient(parent context.Context, kubeClient client.Client) context.Context {
return context.WithValue(parent, KubeClientKey, kubeClient)
}

// WithRuntimeParams returns a copy of parent in which the runtime params value is set
func WithRuntimeParams(parent context.Context, params RuntimeParams) context.Context {
ctx := context.WithValue(parent, WorkflowContextKey, params.WorkflowContext)
Expand Down
Loading