diff --git a/.travis.yml b/.travis.yml index 3de0fce9..83770c15 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,13 +17,29 @@ services: - docker language: go go: - - 1.13.1 + - 1.14.2 addons: apt: update: true before_script: + + - set -e + # Configure environment so changes are picked up when the Docker daemon is restarted after upgrading + - echo '{"experimental":true}' | sudo tee /etc/docker/daemon.json + - export DOCKER_CLI_EXPERIMENTAL=enabled + - docker run --rm --privileged docker/binfmt:a7996909642ee92942dcd6cff44b9b95f08dad64 + # Upgrade to Docker CE 19.03 for BuildKit support + - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - + - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" + - sudo apt-get update + - sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce=5:19.03.8~3-0~ubuntu-xenial # pin version for reproducibility + # Show info to simplify debugging and create a builder + - docker info + - docker buildx create --name builder --use + - docker buildx ls + - sudo docker version # Download kubectl, which is a requirement for using minikube. - curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.13.0/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/ # Download minikube. diff --git a/Dockerfile b/Dockerfile index 45545cf6..c55ffac7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,14 @@ FROM ubuntu:16.04 + +ARG TARGETARCH + RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/cache/apk/* -COPY ./exporter / +COPY ./exporter-${TARGETARCH} / EXPOSE 8080 -CMD ["/exporter"] +CMD ["/exporter-${TARGETARCH}"] diff --git a/Makefile b/Makefile index 6215a54b..4795a418 100644 --- a/Makefile +++ b/Makefile @@ -75,14 +75,14 @@ go-build: @echo "------------------" @echo "--> Build Chaos Exporter" @echo "------------------" - @go build ./cmd/exporter + @bash build/go-multiarch-build.sh ./cmd/exporter docker-build: @echo "------------------" @echo "--> Build chaos-exporter image" @echo "------------------" # Dockerfile available in the repo root - sudo docker build . -f Dockerfile -t $(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) + @sudo docker buildx build --file Dockerfile --progress plane --platform linux/arm64,linux/amd64 --no-cache --tag $(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) . .PHONY: test test: @@ -117,4 +117,4 @@ unused-package-check: @tidy=$$(go mod tidy); \ if [ -n "$${tidy}" ]; then \ echo "go mod tidy checking failed!"; echo "$${tidy}"; echo; \ - fi \ No newline at end of file + fi diff --git a/build/gitlab/stages/1-cluster-setup/gcp b/build/gitlab/stages/1-cluster-setup/gcp deleted file mode 100755 index 87809cc2..00000000 --- a/build/gitlab/stages/1-cluster-setup/gcp +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -set -e - -path=$(pwd) -echo $SDK_TOKEN > key.json -gcloud auth activate-service-account --key-file=key.json -gcloud config set project ${PROJECT_NAME} -export GOOGLE_APPLICATION_CREDENTIALS="$path/key.json" - -truncate -s 0 ~/logs/vpc -truncate -s 0 ~/logs/clusters - -git clone https://github.com/mayadata-io/litmus.git -cd litmus/k8s/gcp/k8s-installer/ - -# Create VPC -echo "CREATING VPC" -ansible-playbook create-vpc.yml --extra-vars "project=${PROJECT_NAME}" - -# Create k8s cluster -echo "CREATING CLUSTER" -ansible-playbook create-k8s-cluster.yml -vv --extra-vars "project=${PROJECT_NAME} nodes=1 k8s_version=1.11.1" - -mkdir $path/.kube -cat ~/.kube/config > $path/.kube/config -cat ~/.kube/config > $path/.kube/admin.conf -cat ~/logs/clusters > $path/.kube/clusters -cat ~/logs/vpc > $path/.kube/vpc - -# Export VPC network name -VPC=`cat $path/.kube/vpc` -echo $VPC -echo "Checking Cluster availability" -ansible-playbook check-cluster-availability.yml --extra-vars "nodes=1" -kubectl get nodes \ No newline at end of file diff --git a/build/gitlab/stages/2-cluster-cleanup/cluster-cleanup b/build/gitlab/stages/2-cluster-cleanup/cluster-cleanup deleted file mode 100755 index 0be27c23..00000000 --- a/build/gitlab/stages/2-cluster-cleanup/cluster-cleanup +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -set -e - -path=$(pwd) -echo $SDK_TOKEN > key.json -gcloud auth activate-service-account --key-file=key.json -gcloud config set project ${PROJECT_NAME} -export GOOGLE_APPLICATION_CREDENTIALS="$path/key.json" - -cp $path/.kube/clusters ~/logs -cp $path/.kube/vpc ~/logs -git clone https://github.com/mayadata-io/litmus.git -cd litmus/k8s/gcp/k8s-installer/ -echo "************************** Cluster cleanup started **************************" - -# Fetching cluster name -cluster_name=$(cat ~/logs/clusters) - -# Delete k8s cluster -ansible-playbook delete-k8s-cluster.yml - -# Export VPC network name -VPC=`cat $path/.kube/vpc` -echo $VPC - -# Delete VPC -ansible-playbook delete-vpc.yml --extra-vars "project=${PROJECT-NAME}" - -echo $cluster_name -rm -rf litmus \ No newline at end of file diff --git a/build/go-multiarch-build.sh b/build/go-multiarch-build.sh new file mode 100644 index 00000000..94135eb7 --- /dev/null +++ b/build/go-multiarch-build.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +package=$1 +if [[ -z "$package" ]]; then + echo "usage: $0 " + exit 1 +fi +package_split=(${package//\// }) +package_name=${package_split[-1]} + +# add the arch for which we want to build the image +platforms=("linux/amd64" "linux/arm64") +for platform in "${platforms[@]}" +do + platform_split=(${platform//\// }) + GOOS=${platform_split[0]} + GOARCH=${platform_split[1]} + output_name=exporter-$GOARCH + + # The script executes for the argument passed (in package variable) + # here the arg will be "./cmd/exporter" for creating binary + env GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package + if [ $? -ne 0 ]; then + echo 'An error has occurred! Aborting the script execution...' + exit 1 + fi +done diff --git a/buildscripts/push b/buildscripts/push index e6f26364..2eafae1c 100755 --- a/buildscripts/push +++ b/buildscripts/push @@ -19,18 +19,16 @@ then sudo docker login -u "${DNAME}" -p "${DPASS}"; # Push image to docker hub echo "Pushing ${REPONAME}/${IMGNAME}:${IMGTAG} ..."; - sudo docker push ${REPONAME}/${IMGNAME}:${IMGTAG} ; + sudo docker buildx build --file Dockerfile --push --progress plane --platform linux/arm64,linux/amd64 --no-cache --tag ${REPONAME}/${IMGNAME}:${IMGTAG} . if [ ! -z "${TRAVIS_TAG}" ] ; then # Push with different tags if tagged as a release # When github is tagged with a release, then Travis will # set the release tag in env TRAVIS_TAG echo "Pushing ${REPONAME}/${IMGNAME}:${TRAVIS_TAG} ..."; - sudo docker tag ${IMAGEID} ${REPONAME}/${IMGNAME}:${TRAVIS_TAG} - sudo docker push ${REPONAME}/${IMGNAME}:${TRAVIS_TAG}; + sudo docker buildx build --file Dockerfile --push --progress plane --platform linux/arm64,linux/amd64 --no-cache --tag ${REPONAME}/${IMGNAME}:${TRAVIS_TAG} . echo "Pushing ${REPONAME}/${IMGNAME}:latest ..."; - sudo docker tag ${IMAGEID} ${REPONAME}/${IMGNAME}:latest - sudo docker push ${REPONAME}/${IMGNAME}:latest; + sudo docker buildx build --file Dockerfile --push --progress plane --platform linux/arm64,linux/amd64 --no-cache --tag ${REPONAME}/${IMGNAME}:latest . fi; else echo "No docker credentials provided. Skip uploading ${REPONAME}/${IMGNAME}:${IMGTAG} to docker hub"; diff --git a/go.mod b/go.mod index 009f9b28..301c4315 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/litmuschaos/chaos-operator v0.0.0-20200502085045-ae0a262d3baa github.com/onsi/ginkgo v1.12.0 github.com/onsi/gomega v1.9.0 + github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.6.0 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect diff --git a/go.sum b/go.sum index e40aacc4..ccf4bd66 100644 --- a/go.sum +++ b/go.sum @@ -167,6 +167,7 @@ github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/tests/bdd/bdd_test.go b/tests/bdd/bdd_test.go index 425d7295..cfefb4f8 100644 --- a/tests/bdd/bdd_test.go +++ b/tests/bdd/bdd_test.go @@ -67,6 +67,10 @@ var _ = BeforeSuite(func() { log.Fatalf("Failed to create operator: %v", err) } time.Sleep(30 * time.Second) + podDeleteRbac := exec.Command("kubectl", "apply", "-f", "../manifest/pod-delete-rbac.yaml", "-n", "litmus") + if err := podDeleteRbac.Start(); err != nil { + log.Fatalf("Failed to create pod-delete rbac: %v", err) + } experimentCreate := exec.Command("kubectl", "apply", "-f", "https://hub.litmuschaos.io/api/chaos/master?file=charts/generic/experiments.yaml", "-n", "litmus") if err := experimentCreate.Start(); err != nil { log.Fatalf("Failed to create experiment: %v", err) @@ -140,7 +144,7 @@ var _ = BeforeSuite(func() { Applabel: "app=nginx", AppKind: "deployment", }, - ChaosServiceAccount: "litmus", + ChaosServiceAccount: "pod-delete-sa", Components: v1alpha1.ComponentParams{ Runner: v1alpha1.RunnerInfo{ Image: "litmuschaos/chaos-runner:ci", diff --git a/tests/manifest/pod-delete-rbac.yaml b/tests/manifest/pod-delete-rbac.yaml new file mode 100644 index 00000000..eb8a5b38 --- /dev/null +++ b/tests/manifest/pod-delete-rbac.yaml @@ -0,0 +1,39 @@ +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: pod-delete-sa + namespace: litmus + labels: + name: pod-delete-sa + app.kubernetes.io/part-of: litmus +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: pod-delete-sa + namespace: litmus + labels: + name: pod-delete-sa + app.kubernetes.io/part-of: litmus +rules: +- apiGroups: ["","litmuschaos.io","batch","apps"] + resources: ["pods","deployments","pods/log","events","jobs","chaosengines","chaosexperiments","chaosresults"] + verbs: ["create","list","get","patch","update","delete","deletecollection"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: pod-delete-sa + namespace: litmus + labels: + name: pod-delete-sa + app.kubernetes.io/part-of: litmus +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: pod-delete-sa +subjects: +- kind: ServiceAccount + name: pod-delete-sa + namespace: litmus