diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e959567..02b6168 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,13 +24,113 @@ jobs: with: go-version: 1.17 - - name: Build - run: | - make bin/image-cache-daemon bin/warden + - run: GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -o bin/image-cache-daemon-amd64 main.go + - run: GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-w -s" -o bin/image-cache-daemon-arm64 main.go + - run: GOARCH=arm CGO_ENABLED=0 go build -ldflags="-w -s" -o bin/image-cache-daemon-arm main.go + - uses: actions/upload-artifact@v2 + with: + name: builds + path: bin/ + + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/cache@v2 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-test-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go-test- + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.17 - name: Test run: go test -v ./... - + + # Always build images just to ensure that they're buildable, + # but only push them to the registry if this is a push to master + build-images: + runs-on: ubuntu-latest +# needs: +# - test +# - lint +# - e2e + steps: + - uses: actions/checkout@v2 + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - run: env + - name: Login to GHCR + if: github.event_name != 'pull_request' + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build Warden Image + uses: docker/build-push-action@v2 + with: + file: Dockerfile.warden + push: ${{ github.event_name == 'push'}} + platforms: linux/amd64,linux/arm64,linux/arm/v7 + tags: | + ghcr.io/dcherman/image-cache-daemon-warden:latest + ghcr.io/dcherman/image-cache-daemon-warden:${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + - name: Build Image Cache Daemon Image + uses: docker/build-push-action@v2 + with: + file: Dockerfile + push: ${{ github.event_name == 'push'}} + platforms: linux/amd64,linux/arm64,linux/arm/v7 + tags: | + ghcr.io/dcherman/image-cache-daemon:latest + ghcr.io/dcherman/image-cache-daemon:${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + + + e2e: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - uses: nolar/setup-k3d-k3s@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Build Warden Image + uses: docker/build-push-action@v2 + with: + file: Dockerfile.warden + load: true + tags: | + ghcr.io/dcherman/image-cache-daemon-warden:latest + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Build Image Cache Daemon Image + uses: docker/build-push-action@v2 + with: + file: Dockerfile + load: true + tags: | + ghcr.io/dcherman/image-cache-daemon:latest + cache-from: type=gha + cache-to: type=gha,mode=max + + - run: k3d image import ghcr.io/dcherman/image-cache-daemon:latest + - run: k3d image import ghcr.io/dcherman/image-cache-daemon-warden:latest + lint: runs-on: ubuntu-latest steps: diff --git a/manifests/base/install.yaml b/manifests/base/install.yaml new file mode 100644 index 0000000..368844e --- /dev/null +++ b/manifests/base/install.yaml @@ -0,0 +1,97 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: image-cache-daemon +spec: + selector: + matchLabels: + app: image-cache-daemon + template: + metadata: + labels: + app: image-cache-daemon + spec: + serviceAccountName: image-cache-daemon + containers: + - name: image-cache-daemon + image: exiges/image-cache-daemon:latest + imagePullPolicy: Always + env: + - name: NODE_NAME + valueFrom: + fieldRef: + fieldPath: spec.nodeName + - name: POD_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name + - name: POD_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: POD_UID + valueFrom: + fieldRef: + fieldPath: metadata.uid +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: image-cache-daemon +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: image-cache-daemon-cluster-role +rules: + - apiGroups: + - argoproj.io + resources: + - workflowtemplates + - cronworkflows + - clusterworkflowtemplates + verbs: + - get + - list + - watch +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: image-cache-daemon-cluster-role-binding +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: image-cache-daemon-cluster-role +subjects: + - kind: ServiceAccount + name: image-cache-daemon + namespace: image-cache-daemon +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: image-cache-daemon-role +rules: + - apiGroups: + - "" + resources: + - pods + verbs: + - get + - list + - watch + - delete + - create +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: image-cache-daemon-role-binding +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: image-cache-daemon-role +subjects: +- kind: ServiceAccount + name: image-cache-daemon \ No newline at end of file diff --git a/manifests/base/kustomization.yaml b/manifests/base/kustomization.yaml new file mode 100644 index 0000000..4283b92 --- /dev/null +++ b/manifests/base/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - install.yaml \ No newline at end of file diff --git a/manifests/ci/ds.yaml b/manifests/ci/ds.yaml new file mode 100644 index 0000000..3a14ce5 --- /dev/null +++ b/manifests/ci/ds.yaml @@ -0,0 +1,11 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: image-cache-daemon +spec: + template: + spec: + containers: + - name: image-cache-daemon + args: + - --warden-image=warden:ci \ No newline at end of file diff --git a/manifests/ci/kustomization.yaml b/manifests/ci/kustomization.yaml new file mode 100644 index 0000000..36e4ae9 --- /dev/null +++ b/manifests/ci/kustomization.yaml @@ -0,0 +1,10 @@ +images: + - name: exiges/image-cache-daemon + newName: image-cache-daemon + newTag: ci + +resources: + - ../base + +patchesStrategicMerge: + - ds.yaml \ No newline at end of file diff --git a/test/e2e/node_test.go b/test/e2e/node_test.go new file mode 100644 index 0000000..3d76f4d --- /dev/null +++ b/test/e2e/node_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "os" + + "os/exec" + "testing" + + "github.com/stretchr/testify/assert" +) + +func runCommandWithOutput(command string, args ...string) error { + cmd := exec.Command(command, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd.Run() +} + +func TestImageCaching(t *testing.T) { + t.SkipNow() + + err := runCommandWithOutput("k3d", "cluster", "create", "foobar", "--no-lb") + + if !assert.NoError(t, err) { + return + } + + t.Cleanup(func() { + if err := runCommandWithOutput("k3d", "cluster", "delete", "foobar"); err != nil { + t.Errorf("failed to cleanup test cluster: %v", err) + } + }) + + err = runCommandWithOutput("docker", "build", "-f", "Dockerfile.warden", "-t", "warden:ci", ".") + + if !assert.NoError(t, err) { + return + } +}