github.com/oam-dev/kubevela@v1.9.11/pkg/velaql/providers/query/utils.go (about) 1 /* 2 Copyright 2022. The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package query 18 19 import ( 20 "encoding/json" 21 22 cuejson "cuelang.org/go/pkg/encoding/json" 23 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 24 25 "github.com/kubevela/workflow/pkg/cue/model/value" 26 27 "github.com/oam-dev/kubevela/pkg/oam" 28 querytypes "github.com/oam-dev/kubevela/pkg/velaql/providers/query/types" 29 ) 30 31 // fillQueryResult help fill query result which contains k8s object to *value.Value 32 func fillQueryResult(v *value.Value, res interface{}, paths ...string) error { 33 b, err := json.Marshal(res) 34 if err != nil { 35 return v.FillObject(err, "err") 36 } 37 expr, err := cuejson.Unmarshal(b) 38 if err != nil { 39 return v.FillObject(err, "err") 40 } 41 err = v.FillObject(expr, paths...) 42 if err != nil { 43 return err 44 } 45 return v.Error() 46 } 47 48 func buildResourceArray(res querytypes.AppliedResource, parent, node *querytypes.ResourceTreeNode, kind string, apiVersion string) (pods []querytypes.ResourceItem) { 49 if node.LeafNodes != nil { 50 for _, subNode := range node.LeafNodes { 51 pods = append(pods, buildResourceArray(res, node, subNode, kind, apiVersion)...) 52 } 53 } else if node.Kind == kind && node.APIVersion == apiVersion { 54 pods = append(pods, buildResourceItem(res, querytypes.Workload{ 55 APIVersion: parent.APIVersion, 56 Kind: parent.Kind, 57 Name: parent.Name, 58 Namespace: parent.Namespace, 59 }, node.Object)) 60 } 61 return 62 } 63 64 func buildResourceItem(res querytypes.AppliedResource, workload querytypes.Workload, object unstructured.Unstructured) querytypes.ResourceItem { 65 return querytypes.ResourceItem{ 66 Cluster: res.Cluster, 67 Workload: workload, 68 Component: res.Component, 69 Object: object, 70 PublishVersion: func() string { 71 if object.GetAnnotations()[oam.AnnotationPublishVersion] != "" { 72 return object.GetAnnotations()[oam.AnnotationPublishVersion] 73 } 74 return res.PublishVersion 75 }(), 76 DeployVersion: func() string { 77 if object.GetAnnotations()[oam.AnnotationDeployVersion] != "" { 78 return object.GetAnnotations()[oam.AnnotationDeployVersion] 79 } 80 return res.DeployVersion 81 }(), 82 } 83 }