From f8ba3d5d00d2e89fadfdf880496ab20331fa07c1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 14 Feb 2022 13:05:44 +0800 Subject: [PATCH] [Backport release-1.2] Fix: can not collector pod list with rollout trait (#3240) * Fix: can not collector pod list with rollout trait Signed-off-by: barnettZQG (cherry picked from commit 6365df4737b2fbd71e6d2c0191d4c66b0ac80093) * Fix: cue format error Signed-off-by: barnettZQG (cherry picked from commit 0a59d0c0515b55c8d645b66413582b48fbed0aab) * Fix: default values and optional parameters cannot coexist Signed-off-by: barnettZQG (cherry picked from commit d8e08b09d85c00bdfad194a85caf40cb50386c49) Co-authored-by: barnettZQG --- .../templates/defwithtemplate/rollout.yaml | 15 +++++---- .../templates/defwithtemplate/rollout.yaml | 15 +++++---- pkg/velaql/providers/query/collector.go | 33 +++++++++++++++++-- .../definitions/internal/trait/rollout.cue | 16 +++++---- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/charts/vela-core/templates/defwithtemplate/rollout.yaml b/charts/vela-core/templates/defwithtemplate/rollout.yaml index 4a5c69c23..2c037daf1 100644 --- a/charts/vela-core/templates/defwithtemplate/rollout.yaml +++ b/charts/vela-core/templates/defwithtemplate/rollout.yaml @@ -5,8 +5,6 @@ kind: TraitDefinition metadata: annotations: definition.oam.dev/description: Rollout the component. - labels: - custom.definition.oam.dev/ui-hidden: "true" name: rollout namespace: {{.Values.systemDefinitionNamespace}} spec: @@ -22,8 +20,13 @@ spec: namespace: context.namespace } spec: { - targetRevisionName: parameter.targetRevision - componentName: context.name + if parameter.targetRevision != _|_ { + targetRevisionName: parameter.targetRevision + } + if parameter.targetRevision == _|_ { + targetRevisionName: context.revision + } + componentName: context.name rolloutPlan: { rolloutStrategy: "IncreaseFirst" if parameter.rolloutBatches != _|_ { @@ -37,8 +40,8 @@ spec: } } parameter: { - targetRevision: *context.revision | string - targetSize: int + targetRevision?: string + targetSize: int rolloutBatches?: [...rolloutBatch] batchPartition?: int } diff --git a/charts/vela-minimal/templates/defwithtemplate/rollout.yaml b/charts/vela-minimal/templates/defwithtemplate/rollout.yaml index 4a5c69c23..2c037daf1 100644 --- a/charts/vela-minimal/templates/defwithtemplate/rollout.yaml +++ b/charts/vela-minimal/templates/defwithtemplate/rollout.yaml @@ -5,8 +5,6 @@ kind: TraitDefinition metadata: annotations: definition.oam.dev/description: Rollout the component. - labels: - custom.definition.oam.dev/ui-hidden: "true" name: rollout namespace: {{.Values.systemDefinitionNamespace}} spec: @@ -22,8 +20,13 @@ spec: namespace: context.namespace } spec: { - targetRevisionName: parameter.targetRevision - componentName: context.name + if parameter.targetRevision != _|_ { + targetRevisionName: parameter.targetRevision + } + if parameter.targetRevision == _|_ { + targetRevisionName: context.revision + } + componentName: context.name rolloutPlan: { rolloutStrategy: "IncreaseFirst" if parameter.rolloutBatches != _|_ { @@ -37,8 +40,8 @@ spec: } } parameter: { - targetRevision: *context.revision | string - targetSize: int + targetRevision?: string + targetSize: int rolloutBatches?: [...rolloutBatch] batchPartition?: int } diff --git a/pkg/velaql/providers/query/collector.go b/pkg/velaql/providers/query/collector.go index 532e4715d..bda021fae 100644 --- a/pkg/velaql/providers/query/collector.go +++ b/pkg/velaql/providers/query/collector.go @@ -203,9 +203,7 @@ func NewPodCollector(gvk schema.GroupVersionKind) PodCollector { if collector, ok := podCollectorMap[gvk]; ok { return collector } - return func(cli client.Client, obj *unstructured.Unstructured, cluster string) ([]*unstructured.Unstructured, error) { - return nil, nil - } + return velaComponentPodCollector } // standardWorkloadPodCollector collect pods created by standard workload @@ -402,6 +400,35 @@ func helmReleasePodCollector(cli client.Client, obj *unstructured.Unstructured, return collectedPods, nil } +func velaComponentPodCollector(cli client.Client, obj *unstructured.Unstructured, cluster string) ([]*unstructured.Unstructured, error) { + ctx := multicluster.ContextWithClusterName(context.Background(), cluster) + + listOpts := []client.ListOption{ + client.MatchingLabels(map[string]string{"app.oam.dev/component": obj.GetName()}), + client.InNamespace(obj.GetNamespace()), + } + + podList := corev1.PodList{} + if err := cli.List(ctx, &podList, listOpts...); err != nil { + return nil, err + } + + pods := make([]*unstructured.Unstructured, len(podList.Items)) + for i := range podList.Items { + pod, err := oamutil.Object2Unstructured(podList.Items[i]) + if err != nil { + return nil, err + } + pod.SetGroupVersionKind( + corev1.SchemeGroupVersion.WithKind( + reflect.TypeOf(corev1.Pod{}).Name(), + ), + ) + pods[i] = pod + } + return pods, nil +} + func getEventFieldSelector(obj *unstructured.Unstructured) fields.Selector { field := fields.Set{} field["involvedObject.name"] = obj.GetName() diff --git a/vela-templates/definitions/internal/trait/rollout.cue b/vela-templates/definitions/internal/trait/rollout.cue index 4e1719b38..d44211ca0 100644 --- a/vela-templates/definitions/internal/trait/rollout.cue +++ b/vela-templates/definitions/internal/trait/rollout.cue @@ -1,9 +1,6 @@ rollout: { type: "trait" annotations: {} - labels: { - "ui-hidden": "true" - } description: "Rollout the component." attributes: { manageWorkload: true @@ -26,8 +23,13 @@ template: { namespace: context.namespace } spec: { - targetRevisionName: parameter.targetRevision - componentName: context.name + if parameter.targetRevision != _|_ { + targetRevisionName: parameter.targetRevision + } + if parameter.targetRevision == _|_ { + targetRevisionName: context.revision + } + componentName: context.name rolloutPlan: { rolloutStrategy: "IncreaseFirst" if parameter.rolloutBatches != _|_ { @@ -42,8 +44,8 @@ template: { } parameter: { - targetRevision: *context.revision | string - targetSize: int + targetRevision?: string + targetSize: int rolloutBatches?: [...rolloutBatch] batchPartition?: int }