From 30fb4e2108a5cd2839ea77c41978ba4f5a545b11 Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Mon, 16 May 2022 23:33:23 +0000 Subject: [PATCH 1/5] Fix run collector text analyze file path mismatch --- pkg/collect/run.go | 8 ++++---- pkg/collect/{run-pod.go => run_pod.go} | 0 2 files changed, 4 insertions(+), 4 deletions(-) rename pkg/collect/{run-pod.go => run_pod.go} (100%) diff --git a/pkg/collect/run.go b/pkg/collect/run.go index c16b1188..ab38b65a 100644 --- a/pkg/collect/run.go +++ b/pkg/collect/run.go @@ -32,7 +32,10 @@ func Run(c *Collector, runCollector *troubleshootv1beta2.Run) (CollectorResult, } runPodCollector := &troubleshootv1beta2.RunPod{ - Name: runCollector.CollectorName, + CollectorMeta: troubleshootv1beta2.CollectorMeta{ + CollectorName: runCollector.CollectorName, + }, + Name: runCollector.Name, Namespace: namespace, Timeout: runCollector.Timeout, ImagePullSecret: runCollector.ImagePullSecret, @@ -189,9 +192,6 @@ func runWithoutTimeout(ctx context.Context, c *Collector, pod *corev1.Pod, runPo output := NewResult() collectorName := runPodCollector.Name - if collectorName == "" { - collectorName = runPodCollector.CollectorName - } limits := troubleshootv1beta2.LogLimits{ MaxLines: 10000, diff --git a/pkg/collect/run-pod.go b/pkg/collect/run_pod.go similarity index 100% rename from pkg/collect/run-pod.go rename to pkg/collect/run_pod.go From 5c269e2aaf0471a160eda5fdbe55a562a935c7ea Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Tue, 17 May 2022 15:59:41 +0000 Subject: [PATCH 2/5] E2E preflight tests --- .github/workflows/build-test-deploy.yaml | 83 +++++++++++++++++--- Makefile | 8 +- examples/preflight/e2e.yaml | 98 ++++++++++++++++++++++++ 3 files changed, 173 insertions(+), 16 deletions(-) create mode 100644 examples/preflight/e2e.yaml diff --git a/.github/workflows/build-test-deploy.yaml b/.github/workflows/build-test-deploy.yaml index ac029dbf..8c205cc9 100644 --- a/.github/workflows/build-test-deploy.yaml +++ b/.github/workflows/build-test-deploy.yaml @@ -7,7 +7,7 @@ on: - "main" tags: - "v*.*.*" - + jobs: build: runs-on: ubuntu-18.04 @@ -24,8 +24,8 @@ jobs: - uses: actions/checkout@v2 - - run: make - + - run: make + compile-preflight: runs-on: ubuntu-latest needs: build @@ -39,12 +39,12 @@ jobs: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH shell: bash - uses: actions/checkout@master - - run: make preflight + - run: make generate preflight - uses: actions/upload-artifact@v1 with: name: preflight path: bin/preflight - + validate-preflight: runs-on: ubuntu-latest needs: compile-preflight @@ -83,6 +83,45 @@ jobs: exit $EXIT_STATUS + validate-preflight-e2e: + runs-on: ubuntu-latest + needs: compile-preflight + steps: + - uses: actions/checkout@master + - name: Download preflight binary + uses: actions/download-artifact@v1 + with: + name: preflight + path: bin/ + - uses: engineerd/setup-kind@v0.5.0 + - run: chmod +x bin/preflight + - run: | + ./bin/preflight --interactive=false --format=json examples/preflight/e2e.yaml > result.json + cat result.json + + EXIT_STATUS=0 + if grep -q "was not collected" result.json; then + echo "Some files were not collected" + EXIT_STATUS=1 + fi + + if (( `jq '.pass | length' result.json` < 1 )); then + echo "No passing preflights found" + EXIT_STATUS=1 + fi + + if (( `jq '.warn | length' result.json` > 0 )); then + echo "Warnings found" + EXIT_STATUS=1 + fi + + if (( `jq '.fail | length' result.json` > 0 )); then + echo "Failed preflights found" + EXIT_STATUS=1 + fi + + exit $EXIT_STATUS + compile-supportbundle: runs-on: ubuntu-latest needs: build @@ -96,7 +135,7 @@ jobs: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH shell: bash - uses: actions/checkout@master - - run: make support-bundle + - run: make generate support-bundle - uses: actions/upload-artifact@v1 with: name: support-bundle @@ -118,16 +157,35 @@ jobs: - run: ./bin/support-bundle ./examples/support-bundle/sample-supportbundle.yaml - run: ./bin/support-bundle https://kots.io + compile-collect: + runs-on: ubuntu-latest + needs: build + steps: + - uses: actions/setup-go@v1 + with: + go-version: '1.17' + - name: setup env + run: | + echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV + echo "$(go env GOPATH)/bin" >> $GITHUB_PATH + shell: bash + - uses: actions/checkout@master + - run: make generate collect + - uses: actions/upload-artifact@v1 + with: + name: collect + path: bin/collect + goreleaser-test: runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/v') != true steps: - name: Checkout uses: actions/checkout@v2 - + - name: Unshallow run: git fetch --prune --unshallow - + - uses: actions/setup-go@v1 with: go-version: "1.17" @@ -142,12 +200,13 @@ jobs: runs-on: ubuntu-latest needs: - validate-preflight + - validate-preflight-e2e - validate-supportbundle if: startsWith(github.ref, 'refs/tags/v') steps: - name: Checkout uses: actions/checkout@v2 - + - uses: azure/docker-login@v1 with: username: repldeploy2 @@ -155,7 +214,7 @@ jobs: - name: Unshallow run: git fetch --prune --unshallow - + - uses: actions/setup-go@v1 with: go-version: "1.17" @@ -169,14 +228,14 @@ jobs: echo $COSIGN_KEY | base64 -d > ./cosign.key env: COSIGN_KEY: ${{secrets.COSIGN_KEY}} - + - name: Generate SBOM run: | make sbom env: COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }} COSIGN_KEY: ${{ secrets.COSIGN_KEY }} - + - name: Run GoReleaser uses: goreleaser/goreleaser-action@v2 with: diff --git a/Makefile b/Makefile index 5ba7992f..77349fad 100644 --- a/Makefile +++ b/Makefile @@ -46,19 +46,19 @@ test: generate fmt vet go test ${BUILDFLAGS} ./pkg/... ./cmd/... -coverprofile cover.out .PHONY: support-bundle -support-bundle: generate fmt vet +support-bundle: go build ${BUILDFLAGS} ${LDFLAGS} -o bin/support-bundle github.com/replicatedhq/troubleshoot/cmd/troubleshoot .PHONY: preflight -preflight: generate fmt vet +preflight: go build ${BUILDFLAGS} ${LDFLAGS} -o bin/preflight github.com/replicatedhq/troubleshoot/cmd/preflight .PHONY: analyze -analyze: generate fmt vet +analyze: go build ${BUILDFLAGS} ${LDFLAGS} -o bin/analyze github.com/replicatedhq/troubleshoot/cmd/analyze .PHONY: collect -collect: generate fmt vet +collect: go build ${BUILDFLAGS} ${LDFLAGS} -o bin/collect github.com/replicatedhq/troubleshoot/cmd/collect .PHONY: fmt diff --git a/examples/preflight/e2e.yaml b/examples/preflight/e2e.yaml new file mode 100644 index 00000000..c21f22ff --- /dev/null +++ b/examples/preflight/e2e.yaml @@ -0,0 +1,98 @@ +apiVersion: troubleshoot.sh/v1beta2 +kind: Preflight +metadata: + name: example +spec: + collectors: + - data: + name: config/replicas.txt + data: "5" + - run: + collectorName: "static-hi" + image: 'alpine:3.5' + command: ["echo", "hi static!"] + analyzers: + - clusterVersion: + outcomes: + - fail: + when: "< 1.13.0" + message: This application requires at least Kubernetes 1.13.0 or later, and recommends 1.15.0. + uri: https://www.kubernetes.io + - warn: + when: "< 1.15.0" + message: Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.15.0 or later. + uri: https://kubernetes.io + - pass: + when: ">= 1.15.0" + message: Your cluster meets the recommended and required versions of Kubernetes. + - distribution: + outcomes: + - pass: + when: "== k3s" + message: K3S is a supported distribution + - warn: + message: Unable to determine the distribution of Kubernetes + - textAnalyze: + checkName: Replica Count + fileName: config/replicas.txt + regexGroups: '(?P\d+)' + outcomes: + - fail: + when: "Replicas < 5" + message: That's not enough replicas! + - pass: + message: You have at least 5 replicas + - textAnalyze: + checkName: Said hi! + fileName: /static-hi.log + regex: 'hi static' + outcomes: + - fail: + message: Didn't say hi. + - pass: + message: Said hi! + - nodeResources: + checkName: Must be exactly 1 node in the cluster + outcomes: + - pass: + when: "= 1" + message: This cluster has exactly 1 node + - fail: + message: This application requires exactly 1 node + - nodeResources: + checkName: Must have 1 node with at least 2 cores + filters: + cpuCapacity: "2" + outcomes: + - pass: + when: "= 1" + message: This cluster has exactly 1 node with at least 2 cores + - fail: + message: This application requires exactly 1 node with at least 2 cores + - nodeResources: + checkName: Must have 1 node with 2Gi (available) memory and at least 2 cores (on a single node) + filters: + allocatableMemory: 2Gi + cpuCapacity: "2" + outcomes: + - pass: + when: "= 1" + message: This cluster has exactly 1 node with at least 2Gi available memory and 2 cores + - fail: + message: This application requires exactly 1 node with at least 2Gi available memory and 2 cores + - nodeResources: + checkName: There must be exactly 1 node in the cluster + outcomes: + - pass: + when: "count() = 1" + message: This has exactly 1 node in the cluster + - fail: + message: This application requires exactly 1 node in the cluster + - nodeResources: + checkName: There must be a total of at least 2Gi of memory on all nodes + outcomes: + - fail: + when: "sum(memoryCapacity) < 2Gi" + message: This application requires that 2Gi or more memory be available to the cluster + - pass: + message: This cluster has sufficient memory From d2d19a2490d560ae0a7b5aa8802b644ecefcc676 Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Tue, 17 May 2022 17:00:19 +0000 Subject: [PATCH 3/5] Change build step name to test --- .github/workflows/build-test-deploy.yaml | 7 ++----- Makefile | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-test-deploy.yaml b/.github/workflows/build-test-deploy.yaml index 8c205cc9..d757e17d 100644 --- a/.github/workflows/build-test-deploy.yaml +++ b/.github/workflows/build-test-deploy.yaml @@ -9,7 +9,7 @@ on: - "v*.*.*" jobs: - build: + test: runs-on: ubuntu-18.04 steps: - uses: actions/setup-go@v1 @@ -24,11 +24,10 @@ jobs: - uses: actions/checkout@v2 - - run: make + - run: make test compile-preflight: runs-on: ubuntu-latest - needs: build steps: - uses: actions/setup-go@v1 with: @@ -124,7 +123,6 @@ jobs: compile-supportbundle: runs-on: ubuntu-latest - needs: build steps: - uses: actions/setup-go@v1 with: @@ -159,7 +157,6 @@ jobs: compile-collect: runs-on: ubuntu-latest - needs: build steps: - uses: actions/setup-go@v1 with: diff --git a/Makefile b/Makefile index 77349fad..6a81c789 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ endef BUILDFLAGS = -tags "netgo containers_image_ostree_stub exclude_graphdriver_devicemapper exclude_graphdriver_btrfs containers_image_openpgp" -installsuffix netgo -all: test +all: test support-bundle preflight collect .PHONY: ffi ffi: fmt vet From 8c15bb1faea0b2d36883e636b0fd55742c73c455 Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Tue, 17 May 2022 17:02:48 +0000 Subject: [PATCH 4/5] build-test-deploy concurrency 1 --- .github/workflows/build-test-deploy.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/build-test-deploy.yaml b/.github/workflows/build-test-deploy.yaml index d757e17d..709c8488 100644 --- a/.github/workflows/build-test-deploy.yaml +++ b/.github/workflows/build-test-deploy.yaml @@ -8,6 +8,10 @@ on: tags: - "v*.*.*" +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + jobs: test: runs-on: ubuntu-18.04 From 6b5b9297ba45de48852192df2fbcc59fb68b9814 Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Tue, 17 May 2022 17:45:19 +0000 Subject: [PATCH 5/5] Use k3s rather than kind for tests --- .github/workflows/build-test-deploy.yaml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build-test-deploy.yaml b/.github/workflows/build-test-deploy.yaml index 709c8488..90e74239 100644 --- a/.github/workflows/build-test-deploy.yaml +++ b/.github/workflows/build-test-deploy.yaml @@ -52,12 +52,15 @@ jobs: runs-on: ubuntu-latest needs: compile-preflight steps: + - uses: replicatedhq/action-k3s@main + id: k3s + with: + version: v1.23.6-k3s1 - name: Download preflight binary uses: actions/download-artifact@v1 with: name: preflight path: bin/ - - uses: engineerd/setup-kind@v0.5.0 - run: chmod +x bin/preflight - run: | ./bin/preflight --interactive=false --format=json https://preflight.replicated.com > result.json @@ -91,12 +94,15 @@ jobs: needs: compile-preflight steps: - uses: actions/checkout@master + - uses: replicatedhq/action-k3s@main + id: k3s + with: + version: v1.23.6-k3s1 - name: Download preflight binary uses: actions/download-artifact@v1 with: name: preflight path: bin/ - - uses: engineerd/setup-kind@v0.5.0 - run: chmod +x bin/preflight - run: | ./bin/preflight --interactive=false --format=json examples/preflight/e2e.yaml > result.json @@ -148,12 +154,15 @@ jobs: needs: compile-supportbundle steps: - uses: actions/checkout@v1 + - uses: replicatedhq/action-k3s@main + id: k3s + with: + version: v1.23.6-k3s1 - name: Download support-bundle binary uses: actions/download-artifact@v1 with: name: support-bundle path: bin/ - - uses: engineerd/setup-kind@v0.5.0 - run: chmod +x bin/support-bundle - run: ./bin/support-bundle ./examples/support-bundle/sample-collectors.yaml - run: ./bin/support-bundle ./examples/support-bundle/sample-supportbundle.yaml