mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-19 04:26:39 +00:00
Fix: query the resource duplicately (#4714)
* Fix: query the resource duplicately Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: add an e2e test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
This commit is contained in:
@@ -80,18 +80,26 @@ func (c *AppCollector) CollectResourceFromApp(ctx context.Context) ([]Resource,
|
||||
}
|
||||
|
||||
// ListApplicationResources list application applied resources from tracker
|
||||
func (c *AppCollector) ListApplicationResources(ctx context.Context, app *v1beta1.Application, queryTree bool) ([]*types.AppliedResource, error) {
|
||||
func (c *AppCollector) ListApplicationResources(ctx context.Context, app *v1beta1.Application) ([]*types.AppliedResource, error) {
|
||||
rootRT, currentRT, historyRTs, _, err := resourcetracker.ListApplicationResourceTrackers(ctx, c.k8sClient, app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var managedResources []*types.AppliedResource
|
||||
existResources := make(map[common.ClusterObjectReference]bool, len(app.Spec.Components))
|
||||
for _, rt := range append(historyRTs, rootRT, currentRT) {
|
||||
if rt != nil {
|
||||
for _, managedResource := range rt.Spec.ManagedResources {
|
||||
if isResourceInTargetCluster(c.opt.Filter, managedResource.ClusterObjectReference) &&
|
||||
isResourceInTargetComponent(c.opt.Filter, managedResource.Component) &&
|
||||
(queryTree || isResourceMatchKindAndVersion(c.opt.Filter, managedResource.Kind, managedResource.APIVersion)) {
|
||||
(c.opt.WithTree || isResourceMatchKindAndVersion(c.opt.Filter, managedResource.Kind, managedResource.APIVersion)) {
|
||||
if c.opt.WithTree {
|
||||
// If we want to query the tree, we only need to query once for the same resource.
|
||||
if _, exist := existResources[managedResource.ClusterObjectReference]; exist {
|
||||
continue
|
||||
}
|
||||
existResources[managedResource.ClusterObjectReference] = true
|
||||
}
|
||||
managedResources = append(managedResources, &types.AppliedResource{
|
||||
Cluster: func() string {
|
||||
if managedResource.Cluster != "" {
|
||||
@@ -123,7 +131,7 @@ func (c *AppCollector) ListApplicationResources(ctx context.Context, app *v1beta
|
||||
}
|
||||
}
|
||||
|
||||
if !queryTree {
|
||||
if !c.opt.WithTree {
|
||||
return managedResources, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ func (h *provider) GeneratorServiceEndpoints(ctx monitorContext.Context, wfCtx w
|
||||
serviceEndpoints := make([]querytypes.ServiceEndpoint, 0)
|
||||
var clusterGatewayNodeIP = make(map[string]string)
|
||||
collector := NewAppCollector(h.cli, opt)
|
||||
resources, err := collector.ListApplicationResources(ctx, app, opt.WithTree)
|
||||
resources, err := collector.ListApplicationResources(ctx, app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ func (h *provider) ListAppliedResources(ctx monitorContext.Context, wfCtx wfCont
|
||||
if err = h.cli.Get(ctx, appKey, app); err != nil {
|
||||
return v.FillObject(err.Error(), "err")
|
||||
}
|
||||
appResList, err := collector.ListApplicationResources(ctx, app, opt.WithTree)
|
||||
appResList, err := collector.ListApplicationResources(ctx, app)
|
||||
if err != nil {
|
||||
return v.FillObject(err.Error(), "err")
|
||||
}
|
||||
@@ -152,7 +152,7 @@ func (h *provider) CollectResources(ctx monitorContext.Context, wfCtx wfContext.
|
||||
if err = h.cli.Get(ctx, appKey, app); err != nil {
|
||||
return v.FillObject(err.Error(), "err")
|
||||
}
|
||||
appResList, err := collector.ListApplicationResources(ctx, app, opt.WithTree)
|
||||
appResList, err := collector.ListApplicationResources(ctx, app)
|
||||
if err != nil {
|
||||
return v.FillObject(err.Error(), "err")
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
|
||||
"github.com/kubevela/workflow/pkg/cue/model/value"
|
||||
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
querytypes "github.com/oam-dev/kubevela/pkg/velaql/providers/query/types"
|
||||
)
|
||||
|
||||
@@ -62,11 +63,21 @@ func buildResourceArray(res querytypes.AppliedResource, parent, node *querytypes
|
||||
|
||||
func buildResourceItem(res querytypes.AppliedResource, workload querytypes.Workload, object unstructured.Unstructured) querytypes.ResourceItem {
|
||||
return querytypes.ResourceItem{
|
||||
Cluster: res.Cluster,
|
||||
Workload: workload,
|
||||
Component: res.Component,
|
||||
Object: object,
|
||||
PublishVersion: res.PublishVersion,
|
||||
DeployVersion: res.DeployVersion,
|
||||
Cluster: res.Cluster,
|
||||
Workload: workload,
|
||||
Component: res.Component,
|
||||
Object: object,
|
||||
PublishVersion: func() string {
|
||||
if object.GetAnnotations()[oam.AnnotationPublishVersion] != "" {
|
||||
return object.GetAnnotations()[oam.AnnotationPublishVersion]
|
||||
}
|
||||
return res.PublishVersion
|
||||
}(),
|
||||
DeployVersion: func() string {
|
||||
if object.GetAnnotations()[oam.AnnotationDeployVersion] != "" {
|
||||
return object.GetAnnotations()[oam.AnnotationDeployVersion]
|
||||
}
|
||||
return res.DeployVersion
|
||||
}(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ func collectResource(ctx context.Context, c client.Client, opt query.Option) ([]
|
||||
return nil, err
|
||||
}
|
||||
collector := query.NewAppCollector(c, opt)
|
||||
appResList, err := collector.ListApplicationResources(context.Background(), app, opt.WithTree)
|
||||
appResList, err := collector.ListApplicationResources(context.Background(), app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -164,6 +164,29 @@ var _ = Describe("Test velaQL rest api", func() {
|
||||
}, time.Minute*1, 3*time.Second).Should(BeNil())
|
||||
})
|
||||
|
||||
It("Test query application pod when upgrading the app", func() {
|
||||
|
||||
// Create a new RT to simulate upgrading the application
|
||||
rt := &v1beta1.ResourceTracker{}
|
||||
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{
|
||||
Name: fmt.Sprintf("%s-v1-%s", appName, namespace),
|
||||
}, rt)).Should(BeNil())
|
||||
newRT := rt.DeepCopy()
|
||||
newRT.Name = fmt.Sprintf("%s-v2-%s", appName, namespace)
|
||||
newRT.Spec.ApplicationGeneration = 0
|
||||
newRT.UID = ""
|
||||
newRT.ResourceVersion = ""
|
||||
Expect(k8sClient.Create(context.TODO(), newRT)).Should(BeNil())
|
||||
queryRes := get(fmt.Sprintf("/query?velaql=%s{appName=%s,appNs=%s,name=%s}.%s", "test-component-pod-view", appName, namespace, component1Name, "status"))
|
||||
status := new(Status)
|
||||
fmt.Println(status.Error)
|
||||
Expect(decodeResponseBody(queryRes, status)).Should(Succeed())
|
||||
Expect(len(status.PodList)).Should(Equal(1))
|
||||
Expect(status.PodList[0].Component).Should(Equal(component1Name))
|
||||
// Clear the test data
|
||||
Expect(k8sClient.Delete(context.TODO(), newRT))
|
||||
})
|
||||
|
||||
It("Test collect pod from cronJob", func() {
|
||||
cronJob := new(v1beta1.ComponentDefinition)
|
||||
Expect(yaml.Unmarshal([]byte(cronJobComponentDefinition), cronJob)).Should(BeNil())
|
||||
|
||||
Reference in New Issue
Block a user