Skip to content

Commit

Permalink
Merge pull request #6 from appuio/review
Browse files Browse the repository at this point in the history
Badges and tests
  • Loading branch information
ccremer authored Nov 28, 2019
2 parents 68647df + ce0df57 commit caa288e
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 133 deletions.
31 changes: 2 additions & 29 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ env:
REGISTRY_IMAGE: docker.io/appuio/image-cleanup

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
#- name: Lint YAML files
# run: docker run --rm -v $(pwd):/data docker.io/cytopia/yamllint .

test:
runs-on: ubuntu-latest
steps:
Expand All @@ -30,31 +23,9 @@ jobs:
- name: Run unit tests
run: go test ./...

docker:
runs-on: ubuntu-latest
needs:
- lint
- test
steps:
- uses: actions/checkout@v1
- name: Register qemu for multiarch build
run: docker run --rm --privileged docker.io/multiarch/qemu-user-static:register --reset
- name: Build Docker image
run: >
docker build
--tag ${REGISTRY_IMAGE}:${GITHUB_REF##*/}
--build-arg VERSION=${GITHUB_REF##*/}
--build-arg GIT_COMMIT=${GITHUB_SHA:-unknown}
.
- name: Login to Docker hub
run: docker login -u ${{ secrets.DOCKER_HUB_USER }} -p ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Push Docker image
run: docker push ${REGISTRY_IMAGE}:${GITHUB_REF##*/}

release:
runs-on: ubuntu-latest
needs:
- lint
- test
if: github.ref != 'refs/heads/master'
steps:
Expand All @@ -63,6 +34,8 @@ jobs:
with:
path: /home/runner/go/pkg/mod
key: go-mod
- name: Login to Docker hub
run: docker login -u ${{ secrets.DOCKER_HUB_USER }} -p ${{ secrets.DOCKER_HUB_PASSWORD }}
- uses: goreleaser/goreleaser-action@v1
with:
args: release
Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ on:
types: [opened, reopened]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
#- name: Lint YAML files
# run: docker run --rm -v $(pwd):/data docker.io/cytopia/yamllint .

test:
runs-on: ubuntu-latest
steps:
Expand Down
11 changes: 9 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Make sure to check the documentation at http://goreleaser.com

builds:
- goarch:
- env:
- CGO_ENABLED=0 # this is needed otherwise the Docker image build is faulty
goarch:
- amd64
- arm
- arm64
Expand All @@ -11,7 +13,7 @@ builds:
- darwin
goarm:
- 7
-

archives:
- format: binary

Expand All @@ -21,6 +23,11 @@ checksum:
snapshot:
name_template: "{{ .Tag }}-snapshot"

dockers:
- image_templates:
- "docker.io/appuio/image-cleanup:{{ .Tag }}"
- "docker.io/appuio/image-cleanup:{{ .Major }}"

changelog:
sort: asc
filters:
Expand Down
32 changes: 2 additions & 30 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
#______________________________________________________________________________
#### Base Image, to save build time on local dev machine
ARG GOARCH=amd64
ARG ARCH=amd64-edge
FROM docker.io/library/golang:1.13-alpine as builder
FROM docker.io/library/alpine:3.10 as runtime

WORKDIR /go/src/app

COPY ["go.mod", "go.sum", "./"]

RUN \
go mod download

ARG VERSION=unspecified
ARG GIT_COMMIT=unspecified

COPY / .
RUN \
go build -ldflags "-X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=$(date -u '+%Y-%m-%dT%TZ')"

#______________________________________________________________________________
#### Runtime Image
ARG ARCH=amd64-edge
FROM docker.io/multiarch/alpine:${ARCH} as runtime

ENV ALPINE_MIRROR=http://dl-cdn.alpinelinux.org/alpine
ENTRYPOINT ["image-cleanup"]

RUN \
echo "${ALPINE_MIRROR}/${ALPINE_REL}/main" > /etc/apk/repositories && \
echo "${ALPINE_MIRROR}/${ALPINE_REL}/community" >> /etc/apk/repositories && \
apk update && \
apk upgrade && \
apk add --no-cache curl bash

COPY --from=builder /go/src/app/image-cleanup /usr/bin/image-cleanup
COPY image-cleanup /usr/bin/image-cleanup
USER 1000:0
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# Image Cleanup Client

[![dockeri.co](https://dockeri.co/image/appuio/image-cleanup)](https://hub.docker.com/r/appuio/image-cleanup)

![](https://img.shields.io/github/workflow/status/appuio/image-cleanup/Release)
![](https://img.shields.io/github/v/release/appuio/image-cleanup?include_prereleases)
![](https://img.shields.io/github/issues-raw/appuio/image-cleanup)
![](https://img.shields.io/github/issues-pr-raw/appuio/image-cleanup)
![](https://img.shields.io/github/license/appuio/image-cleanup)

## General

The image cleanup client is used to clean up Docker images in a Docker Registry when they are tagged using git SHA.

This helps to save space because obsolete images are being removed from the registry.

The respective licenses for attribution are placed in `/attribution`.

## Development

### Requirements
Expand All @@ -30,6 +36,8 @@ go test ./...
### Run
```
./dist/image-cleanup_linux_amd64/image-cleanup --help
# or
docker run --rm -it appuio/image-cleanup:<tag>
```

## License
Expand Down
29 changes: 16 additions & 13 deletions commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/src-d/go-billy.v4/osfs"
"gopkg.in/src-d/go-git.v4"
Expand All @@ -18,19 +19,21 @@ var gitCmd = &cobra.Command{
Short: "Print the git HEAD",
Long: `tbd`,
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
worktree := osfs.New(args[0])
gitdir, err := worktree.Chroot(".git")
if err != nil {
panic(err)
}
Run: PrintGitHEAD,
}

func PrintGitHEAD(cmd *cobra.Command, args []string) {
worktree := osfs.New(args[0])
gitdir, err := worktree.Chroot(".git")
if err != nil {
log.WithError(err).WithField("path", ".git").Fatal("Could not change root")
}

storer := filesystem.NewStorage(gitdir, nil)
repository, err := git.Open(storer, worktree)
if err != nil {
panic(err)
}
storer := filesystem.NewStorage(gitdir, nil)
repository, err := git.Open(storer, worktree)
if err != nil {
log.WithError(err).WithField("path", ".git").Fatal("Could not open git repository")
}

fmt.Println(repository.Head())
},
fmt.Println(repository.Head())
}
23 changes: 23 additions & 0 deletions commands/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package commands

import (
"github.com/spf13/cobra"
"testing"
)

func Test_printGitHEAD(t *testing.T) {
type args struct {
cmd *cobra.Command
args []string
}
tests := []struct {
name string
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
})
}
}
65 changes: 33 additions & 32 deletions commands/imagestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,39 @@ var imagestreamCmd = &cobra.Command{
Use: "imagestream",
Short: "Print imagestreams from namespace",
Long: `tbd`,
Run: func(cmd *cobra.Command, args []string) {
// Instantiate loader for kubeconfig file.
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)

namespace := resolveNamespace(kubeconfig)

// Get a rest.Config from the kubeconfig file. This will be passed into all
// the client objects we create.
restconfig, err := kubeconfig.ClientConfig()
if err != nil {
panic(err)
}

// Create an OpenShift image/v1 client.
imageclient, err := imagev1client.NewForConfig(restconfig)
if err != nil {
panic(err)
}

imagestreamlist, err := imageclient.ImageStreams(namespace).List(metav1.ListOptions{})
if err != nil {
panic(err)
}

for _, imagestream := range imagestreamlist.Items {
fmt.Println(imagestream.ObjectMeta.Name)
}

},
Run: printImageStreamsFromNamespace,
}

func printImageStreamsFromNamespace(cmd *cobra.Command, args []string) {
// Instantiate loader for kubeconfig file.
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)

namespace := resolveNamespace(kubeconfig)

// Get a rest.Config from the kubeconfig file. This will be passed into all
// the client objects we create.
restconfig, err := kubeconfig.ClientConfig()
if err != nil {
panic(err)
}

// Create an OpenShift image/v1 client.
imageclient, err := imagev1client.NewForConfig(restconfig)
if err != nil {
panic(err)
}

imagestreamlist, err := imageclient.ImageStreams(namespace).List(metav1.ListOptions{})
if err != nil {
panic(err)
}

for _, imagestream := range imagestreamlist.Items {
fmt.Println(imagestream.ObjectMeta.Name)
}
}

// Get the namespace defined in the kubeconfig
Expand Down
23 changes: 23 additions & 0 deletions commands/imagestream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package commands

import (
"github.com/spf13/cobra"
"testing"
)

func Test_printImageStreamsFromNamespace(t *testing.T) {
type args struct {
cmd *cobra.Command
args []string
}
tests := []struct {
name string
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
})
}
}
39 changes: 22 additions & 17 deletions commands/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/heroku/docker-registry-client/registry"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -18,21 +19,25 @@ var tagCmd = &cobra.Command{
Short: "Print the available tags",
Long: `tbd`,
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
image := args[0]
url := "https://registry-1.docker.io/"
username := "" // anonymous
password := "" // anonymous
hub, err := registry.New(url, username, password)

tags, err := hub.Tags(image)
if err != nil {
panic(err)
}

for _, tag := range tags {
fmt.Println(tag)
}

},
Run: printImageStreamTags,
}

func printImageStreamTags(cmd *cobra.Command, args []string) {
image := args[0]
url := "https://registry-1.docker.io/"
username := "" // anonymous
password := "" // anonymous
hub, err := registry.New(url, username, password)
if err != nil {
log.WithError(err).WithField("url", url).Fatal("Registry is currently unavailable.")
}
tags, err := hub.Tags(image)

if err != nil {
log.WithError(err).WithField("url", url).Fatal("Could not list image tags.")
}

for _, tag := range tags {
fmt.Println(tag)
}
}
Loading

0 comments on commit caa288e

Please sign in to comment.