From 22e11f143d5befb26610691189dc8ff709f9cae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A5=9A=E5=B2=B3?= Date: Thu, 14 Jul 2022 10:42:41 +0800 Subject: [PATCH 1/2] test changed files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 楚岳 add changed files action Signed-off-by: 楚岳 fix path Signed-off-by: 楚岳 more tests Signed-off-by: 楚岳 add 1 Signed-off-by: 楚岳 push finish work let test Signed-off-by: 楚岳 delelte uesless file Signed-off-by: 楚岳 fix Signed-off-by: 楚岳 fix Signed-off-by: 楚岳 fix Signed-off-by: 楚岳 add pending addon Signed-off-by: 楚岳 fix tests Signed-off-by: 楚岳 add commnets Signed-off-by: 楚岳 add test Signed-off-by: 楚岳 more fix Signed-off-by: 楚岳 add more tests Signed-off-by: 楚岳 add more tests Signed-off-by: 楚岳 add test utils Signed-off-by: 楚岳 add commnets Signed-off-by: 楚岳 add more test Signed-off-by: 楚岳 fix flink Signed-off-by: 楚岳 fix istio Signed-off-by: 楚岳 consider downStream addon Signed-off-by: 楚岳 check semver Signed-off-by: 楚岳 fix makefile Signed-off-by: 楚岳 --- .github/workflows/addon-test.yaml | 16 +- Makefile | 4 +- .../definitions/flink-cluster.cue | 5 +- .../{resources => definitions}/istio-vs.cue | 0 go.mod | 3 +- go.sum | 1 - test/e2e-test/addon-test/go.mod | 7 + test/e2e-test/addon-test/go.sum | 6 + test/e2e-test/addon-test/main.go | 320 ++++++++++++++++++ 9 files changed, 352 insertions(+), 10 deletions(-) rename experimental/addons/istio/{resources => definitions}/istio-vs.cue (100%) create mode 100644 test/e2e-test/addon-test/go.mod create mode 100644 test/e2e-test/addon-test/go.sum create mode 100644 test/e2e-test/addon-test/main.go diff --git a/.github/workflows/addon-test.yaml b/.github/workflows/addon-test.yaml index eb43f03a..dc683003 100644 --- a/.github/workflows/addon-test.yaml +++ b/.github/workflows/addon-test.yaml @@ -43,6 +43,10 @@ jobs: - name: Check out code into the Go module directory uses: actions/checkout@v2 + - name: Check addon semver + run:| + make check-addon-semver + - name: Install ginkgo run: | sudo apt-get update @@ -84,9 +88,19 @@ jobs: vela cluster join /tmp/worker.kubeconfig --name worker vela cluster list + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v23.1 + + - name: List all changed files + run: | + for file in ${{ steps.changed-files.outputs.all_changed_files }}; do + echo "$file was changed" + done + - name: Addon e2e-test run: | - make addon-test + go run test/e2e-test/addon-test/main.go ${{ steps.changed-files.outputs.all_changed_files }} diff --git a/Makefile b/Makefile index 406fe540..cde8b7ec 100644 --- a/Makefile +++ b/Makefile @@ -19,9 +19,9 @@ standard-metrics-test: standard-autoscaler-test: cd ./traits/autoscalertrait && make test -addon-test: +check-addon-semver: bash ./test/e2e-test/hack/check-semver.sh - bash ./test/e2e-test/hack/addon-vela-test.sh + terraform-addon-gen: go run hack/addons/terraform/gen.go diff --git a/addons/flink-kubernetes-operator/definitions/flink-cluster.cue b/addons/flink-kubernetes-operator/definitions/flink-cluster.cue index cafe3565..3273ffdc 100644 --- a/addons/flink-kubernetes-operator/definitions/flink-cluster.cue +++ b/addons/flink-kubernetes-operator/definitions/flink-cluster.cue @@ -1,9 +1,6 @@ "flink-cluster": { annotations: {} - attributes: workload: definition: { - apiVersion: "flink.apache.org/v1alpha1" - kind: "FlinkDeployment" - } + attributes: workload: type: "autodetects.core.oam.dev" description: "Flink Component." labels: {} type: "component" diff --git a/experimental/addons/istio/resources/istio-vs.cue b/experimental/addons/istio/definitions/istio-vs.cue similarity index 100% rename from experimental/addons/istio/resources/istio-vs.cue rename to experimental/addons/istio/definitions/istio-vs.cue diff --git a/go.mod b/go.mod index 3b660410..cec5bf5b 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/oam-dev/catalog go 1.17 require ( + github.com/Masterminds/semver/v3 v3.1.1 github.com/Masterminds/sprig/v3 v3.2.2 github.com/go-yaml/yaml v2.1.0+incompatible - golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449 helm.sh/helm/v3 v3.6.1 sigs.k8s.io/yaml v1.2.0 ) @@ -13,7 +13,6 @@ require ( require ( github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect github.com/Masterminds/goutils v1.1.1 // indirect - github.com/Masterminds/semver/v3 v3.1.1 // indirect github.com/Microsoft/go-winio v0.4.16 // indirect github.com/Microsoft/hcsshim v0.8.14 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect diff --git a/go.sum b/go.sum index 59803015..4bc67cc8 100644 --- a/go.sum +++ b/go.sum @@ -779,7 +779,6 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449 h1:xUIPaMhvROX9dhPvRCenIJtU78+lbEenGbgqB5hfHCQ= golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/test/e2e-test/addon-test/go.mod b/test/e2e-test/addon-test/go.mod new file mode 100644 index 00000000..4c241b39 --- /dev/null +++ b/test/e2e-test/addon-test/go.mod @@ -0,0 +1,7 @@ +module github.com/oam-dev/catalog/test/e2e-test/addon-test + +go 1.17 + +require sigs.k8s.io/yaml v1.3.0 + +require gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/test/e2e-test/addon-test/go.sum b/test/e2e-test/addon-test/go.sum new file mode 100644 index 00000000..a4e5658e --- /dev/null +++ b/test/e2e-test/addon-test/go.sum @@ -0,0 +1,6 @@ +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= +sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/test/e2e-test/addon-test/main.go b/test/e2e-test/addon-test/main.go new file mode 100644 index 00000000..34649a75 --- /dev/null +++ b/test/e2e-test/addon-test/main.go @@ -0,0 +1,320 @@ +/* +Copyright 2021 The KubeVela Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + + "sigs.k8s.io/yaml" +) + +type AddonMeta struct { + Name string `json:"name"` + Dependencies []Dependency `json:"dependencies"` +} + +type Dependency struct { + Name string `json:"name"` +} + +var file = "addons/velaux/template.yaml" +var regexPattern = "^addons.*" +var globalRexPattern = "^.github.*|Makefile|.*.go" + +// This can be used for pending some error addon temporally, Please fix it as soon as posible. +var pendingAddon = map[string]bool{"ocm-gateway-manager-addon": true, "model-serving": true} + +func main() { + changedFile := os.Args[1:] + changedAddon := determineNeedEnableAddon(changedFile) + if len(changedAddon) == 0 { + return + } + if err := enableAddonsByOrder(changedAddon); err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +// will check all needed enabled addons according to changed files. +func determineNeedEnableAddon (changedFile []string) map[string]bool { + needEnabledAddon := map[string]bool{} + globalRex := regexp.MustCompile(globalRexPattern) + regx := regexp.MustCompile(regexPattern) + for _, s := range changedFile { + regRes := regx.Find([]byte(s)) + if len(regRes) != 0 { + fmt.Println(string(regRes)) + list := strings.Split(string(regRes), "/") + if len(list) > 1 { + addon := list[1] + needEnabledAddon[addon] = true + } + } + + // if .github/makefile/*.go files changed, that will change the CI, so enable all addons. + if regRes := globalRex.Find([]byte(s)); len(regRes) != 0 { + // change CI related file, must test all addons + err := putInAllAddons(needEnabledAddon) + if err != nil { + return nil + } else { + fmt.Println("This pr need checkAll addons") + return needEnabledAddon + } + } + } + + for addon := range needEnabledAddon { + if err := checkAffectedAddon(addon, needEnabledAddon); err != nil { + panic(err) + } + } + + for addon := range needEnabledAddon { + checkAddonDependency(addon, needEnabledAddon) + } + + fmt.Printf("This pr need test addons: ") + for ca := range needEnabledAddon { + fmt.Printf("%s,", ca) + } + fmt.Printf("\n") + return needEnabledAddon +} + +// check affected addon. +// eg: If fluxcd addon changed, should enable addons those dependent fluxcd addon. +func checkAffectedAddon(addonName string, needEnabledAddon map[string]bool) error { + dir, err := ioutil.ReadDir("./addons") + if err != nil { + return err + } + for _, subDir := range dir { + if subDir.IsDir() { + meta, err := readAddonMeta(subDir.Name()) + if err != nil { + return err + } + if len(meta.Dependencies) != 0 { + for _, dependency := range meta.Dependencies { + if dependency.Name == addonName { + needEnabledAddon[meta.Name] = true + } + } + } + } + } + return nil +} + +func putInAllAddons (addons map[string]bool) error { + dir, err := ioutil.ReadDir("./addons") + if err != nil { + return err + } + for _, subDir := range dir { + if subDir.IsDir() { + fmt.Println(subDir.Name()) + addons[subDir.Name()] = true + } + } + return nil +} + +// these addons are depended by changed addons. +func checkAddonDependency(addon string, changedAddon map[string]bool ) { + meta, err := readAddonMeta(addon) + if err != nil { + panic(err) + } + for _, dep := range meta.Dependencies { + changedAddon[dep.Name] = true + checkAddonDependency(dep.Name, changedAddon) + } +} + +func readAddonMeta(addonName string) (*AddonMeta, error) { + metaFile, err := os.ReadFile(filepath.Join([]string{"addons", addonName, "metadata.yaml"}...)) + if err != nil { + panic(err) + } + meta := AddonMeta{} + err = yaml.Unmarshal(metaFile, &meta) + if err != nil { + return nil, err + } + return &meta, nil +} + +// This func will enable addon by order rely-on addon's relationShip dependency, +// this func is so dummy now that the order is written manually, we can generated a dependency DAG workflow in the furture. +func enableAddonsByOrder (changedAddon map[string]bool) error { + dirPattern := "addons/%s" + if changedAddon["fluxcd"] { + if err := enableOneAddon(fmt.Sprintf(dirPattern, "fluxcd")); err != nil { + return err + } + changedAddon["fluxcd"] = false + } + if changedAddon["terraform"] { + if err := enableOneAddon(fmt.Sprintf(dirPattern, "terraform")); err != nil { + return err + } + changedAddon["terraform"] = false + } + if changedAddon["velaux"] { + if err := enableOneAddon(fmt.Sprintf(dirPattern, "velaux")); err != nil { + return err + } + changedAddon["velaux"] = false + } + if changedAddon["cert-manager"] { + if err := enableOneAddon(fmt.Sprintf(dirPattern, "cert-manager")); err != nil { + return err + } + changedAddon["cert-manager"] = false + } + for s, b := range changedAddon { + if b && !pendingAddon[s] { + if err := enableOneAddon(fmt.Sprintf(dirPattern, s)); err != nil { + return err + } + if err := disableOneAddon(s); err != nil { + return err + } + switch s { + case "dex": + if err := disableOneAddon("velaux"); err != nil { + return err + } + case "flink-kubernetes-operator": + if err := disableOneAddon("cert-manager"); err != nil { + return err + } + } + } + } + return nil +} + +func enableOneAddon(dir string) error { + cmd := exec.Command("vela","addon", "enable", dir) + fmt.Println(cmd.String()) + stdout, err := cmd.StdoutPipe() + cmd.Stderr = cmd.Stdout + if err != nil { + panic(err) + } + if err = cmd.Start(); err != nil { + return err + } + for { + tmp := make([]byte, 1024) + _, err := stdout.Read(tmp) + fmt.Print(string(tmp)) + if err != nil { + break + } + } + if err = cmd.Wait(); err != nil { + return err + } + return nil +} + +func disableOneAddon (addonName string) error { + cmd := exec.Command("vela","addon", "disable", addonName) + fmt.Println(cmd.String()) + stdout, err := cmd.StdoutPipe() + cmd.Stderr = cmd.Stdout + if err != nil { + panic(err) + } + if err = cmd.Start(); err != nil { + return err + } + for { + tmp := make([]byte, 1024) + _, err := stdout.Read(tmp) + fmt.Print(string(tmp)) + if err != nil { + break + } + } + if err = cmd.Wait(); err != nil { + return err + } + return nil +} + + +// this func can be used for debug when addon enable failed. +func checkAppStatus(addonName string) { + cmd := exec.Command("vela","status", "-n", "vela-system", "addon-" + addonName) + fmt.Println(cmd.String()) + stdout, err := cmd.StdoutPipe() + cmd.Stderr = cmd.Stdout + if err != nil { + panic(err) + } + if err = cmd.Start(); err != nil { + fmt.Println(err) + } + for { + tmp := make([]byte, 1024) + _, err := stdout.Read(tmp) + fmt.Print(string(tmp)) + if err != nil { + break + } + } + if err = cmd.Wait(); err != nil { + fmt.Println(err) + } +} + +// this func can be used for debug when addon enable failed. +func checkPodStatus(namespace string) { + cmd := exec.Command("kubectl","get pods", "-n", namespace) + fmt.Println(cmd.String()) + stdout, err := cmd.StdoutPipe() + cmd.Stderr = cmd.Stdout + if err != nil { + panic(err) + } + if err = cmd.Start(); err != nil { + fmt.Println(err) + } + for { + tmp := make([]byte, 1024) + _, err := stdout.Read(tmp) + fmt.Print(string(tmp)) + if err != nil { + break + } + } + if err = cmd.Wait(); err != nil { + fmt.Println(err) + } +} From 47cf8e7c88ee5b969f32f6452f8514edb1bdbeea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A5=9A=E5=B2=B3?= Date: Fri, 15 Jul 2022 15:25:02 +0800 Subject: [PATCH 2/2] fix workflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 楚岳 --- .github/workflows/addon-test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/addon-test.yaml b/.github/workflows/addon-test.yaml index dc683003..d6bde31e 100644 --- a/.github/workflows/addon-test.yaml +++ b/.github/workflows/addon-test.yaml @@ -44,7 +44,7 @@ jobs: uses: actions/checkout@v2 - name: Check addon semver - run:| + run: | make check-addon-semver - name: Install ginkgo