diff --git a/pkg/utils/common/common.go b/pkg/utils/common/common.go index 63ae82cbe..460aa63fe 100644 --- a/pkg/utils/common/common.go +++ b/pkg/utils/common/common.go @@ -329,19 +329,6 @@ func RealtimePrintCommandOutput(cmd *exec.Cmd, logFile string) error { return nil } -// ClusterObject2Map convert ClusterObjectReference to a readable map -func ClusterObject2Map(refs []common.ClusterObjectReference) map[string]string { - clusterResourceRefTmpl := "Cluster: %s | Namespace: %s | Kind: %s | Name: %s" - objs := make(map[string]string, len(refs)) - for _, r := range refs { - if r.Cluster == "" { - r.Cluster = "local" - } - objs[r.Cluster+"/"+r.Namespace+"/"+r.Name+"/"+r.Kind] = fmt.Sprintf(clusterResourceRefTmpl, r.Cluster, r.Namespace, r.Kind, r.Name) - } - return objs -} - // ResourceLocation indicates the resource location type ResourceLocation struct { Cluster string diff --git a/pkg/velaql/providers/query/collector.go b/pkg/velaql/providers/query/collector.go index 24f6ecce3..e4b51968a 100644 --- a/pkg/velaql/providers/query/collector.go +++ b/pkg/velaql/providers/query/collector.go @@ -305,9 +305,11 @@ func isResourceInTargetCluster(opt FilterOption, resource common.ClusterObjectRe if opt.Cluster == "" && opt.ClusterNamespace == "" { return true } - if (opt.Cluster == resource.Cluster || (opt.Cluster == "local" && resource.Cluster == "")) && opt.ClusterNamespace == resource.ObjectReference.Namespace { + if (opt.Cluster == resource.Cluster || (opt.Cluster == "local" && resource.Cluster == "")) && + (opt.ClusterNamespace == resource.ObjectReference.Namespace || opt.ClusterNamespace == "") { return true } + return false } diff --git a/references/cli/velaql.go b/references/cli/velaql.go index 7d4d7c917..9b3dc3cc6 100644 --- a/references/cli/velaql.go +++ b/references/cli/velaql.go @@ -256,13 +256,7 @@ func GetServiceEndpoints(ctx context.Context, appName string, namespace string, "appName": appName, "appNs": namespace, } - if f.Component != "" { - params["name"] = f.Component - } - if f.Cluster != "" && f.ClusterNamespace != "" { - params["cluster"] = f.Cluster - params["clusterNs"] = f.ClusterNamespace - } + setFilterParams(f, params) velaQL := MakeVelaQL("service-endpoints-view", params, "status") queryView, err := velaql.ParseVelaQL(velaQL) @@ -292,13 +286,7 @@ func GetApplicationPods(ctx context.Context, appName string, namespace string, v "appName": appName, "appNs": namespace, } - if f.Component != "" { - params["name"] = f.Component - } - if f.Cluster != "" && f.ClusterNamespace != "" { - params["cluster"] = f.Cluster - params["clusterNs"] = f.ClusterNamespace - } + setFilterParams(f, params) velaQL := MakeVelaQL("component-pod-view", params, "status") queryView, err := velaql.ParseVelaQL(velaQL) @@ -328,13 +316,7 @@ func GetApplicationServices(ctx context.Context, appName string, namespace strin "appName": appName, "appNs": namespace, } - if f.Component != "" { - params["name"] = f.Component - } - if f.Cluster != "" && f.ClusterNamespace != "" { - params["cluster"] = f.Cluster - params["clusterNs"] = f.ClusterNamespace - } + setFilterParams(f, params) velaQL := MakeVelaQL("component-service-view", params, "status") queryView, err := velaql.ParseVelaQL(velaQL) if err != nil { @@ -357,6 +339,20 @@ func GetApplicationServices(ctx context.Context, appName string, namespace strin return response.Services, nil } +// setFilterParams will convert Filter fields to velaQL params +func setFilterParams(f Filter, params map[string]string) { + if f.Component != "" { + params["name"] = f.Component + } + if f.Cluster != "" { + params["cluster"] = f.Cluster + } + if f.ClusterNamespace != "" { + params["clusterNs"] = f.ClusterNamespace + } + +} + // QueryValue get queryValue from velaQL func QueryValue(ctx context.Context, velaC common.Args, queryView *velaql.QueryView) (*value.Value, error) { dm, err := velaC.GetDiscoveryMapper() diff --git a/test/e2e-multicluster-test/multicluster_cli_test.go b/test/e2e-multicluster-test/multicluster_cli_test.go index 5c2c258de..c0d185da8 100644 --- a/test/e2e-multicluster-test/multicluster_cli_test.go +++ b/test/e2e-multicluster-test/multicluster_cli_test.go @@ -18,8 +18,10 @@ package e2e_multicluster_test import ( "context" - "io/ioutil" + "github.com/onsi/gomega/gbytes" + "io" "net/http" + "os" "os/exec" "strings" "time" @@ -48,7 +50,7 @@ var _ = Describe("Test multicluster CLI commands", func() { BeforeEach(func() { hubCtx, workerCtx, namespace = initializeContextAndNamespace() app = &v1beta1.Application{} - bs, err := ioutil.ReadFile("./testdata/app/example-vela-cli-tool-test-app.yaml") + bs, err := os.ReadFile("./testdata/app/example-vela-cli-tool-test-app.yaml") Expect(err).Should(Succeed()) appYaml := strings.ReplaceAll(string(bs), "TEST_NAMESPACE", namespace) Expect(yaml.Unmarshal([]byte(appYaml), app)).Should(Succeed()) @@ -87,7 +89,7 @@ var _ = Describe("Test multicluster CLI commands", func() { go func() { defer GinkgoRecover() command := exec.Command("vela", "port-forward", app.Name, "-n", namespace) - session, err := gexec.Start(command, ioutil.Discard, ioutil.Discard) + session, err := gexec.Start(command, io.Discard, io.Discard) Expect(err).Should(Succeed()) <-stopChannel session.Terminate() @@ -113,7 +115,7 @@ var _ = Describe("Test multicluster CLI commands", func() { for _, format := range []string{"inline", "wide", "table", "list"} { outputs, err := execCommand("status", app.Name, "-n", namespace, "--tree", "--detail", "--detail-format", format) Expect(err).Should(Succeed()) - Expect(string(outputs)).Should(SatisfyAll( + Expect(outputs).Should(SatisfyAll( ContainSubstring("alias-worker-tree"), ContainSubstring("Deployment/exec-podinfo"), ContainSubstring("updated"), @@ -121,6 +123,31 @@ var _ = Describe("Test multicluster CLI commands", func() { )) } }) + + It("Test vela logs", func() { + var ( + err error + session *gexec.Session + waitingTime = 2 * time.Minute + stopChannel = make(chan struct{}, 1) + ) + defer func() { + stopChannel <- struct{}{} + }() + + go func() { + defer GinkgoRecover() + command := exec.Command("vela", "logs", app.Name, "-n", namespace, "--cluster", WorkerClusterName) + session, err = gexec.Start(command, nil, nil) + Expect(err).Should(Succeed()) + <-stopChannel + session.Terminate() + Expect(session).Should(gexec.Exit()) + }() + Expect(err).Should(Succeed()) + Eventually(session, waitingTime).Should(gbytes.Say("exec-podinfo")) + Eventually(session, waitingTime).Should(gbytes.Say("httpd started")) + }) }) })