Skip to content

Commit

Permalink
Client code refactor and update dependencies
Browse files Browse the repository at this point in the history
Refactor client code to accept multiple container runtimes, e.g. Docker.
This is not complete yet, though
  • Loading branch information
patrick246 committed May 2, 2024
1 parent 9abc226 commit cdd39c7
Show file tree
Hide file tree
Showing 12 changed files with 999 additions and 894 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/docker-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Docker Build
on:
push:
branches:
- 'main'
jobs:
BuildDocker:
runs-on: ubuntu-latest
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Build and push
uses: docker/build-push-action@v3
with:
pull: true
push: true
tags: ghcr.io/patrick246/k8s-outdated-image-exporter:${{ github.sha }}
platforms: linux/amd64,linux/arm64
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
FROM golang:1.16 as builder
# syntax=docker/dockerfile:1.4
FROM --platform=$BUILDPLATFORM golang:1.22.2 as builder
ARG TARGETOS
ARG TARGETARCH
WORKDIR /go/app
COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 go build -o ./bin -ldflags="-extldflags=-static" ./cmd/...
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o ./bin -ldflags="-extldflags=-static" ./cmd/...

FROM gcr.io/distroless/static
FROM gcr.io/distroless/static:nonroot
LABEL org.opencontainers.image.source=https://github.com/patrick246/k8s-outdated-image-exporter
LABEL org.opencontainers.image.licenses="AGPL-3.0"
LABEL org.opencontainers.image.authors="https://github.com/patrick246"
LABEL org.opencontainers.image.url="https://github.com/patrick246/k8s-outdated-image-exporter/pkgs/container/k8s-outdated-image-exporter"
COPY --from=builder /go/app/bin/outdated-image-exporter /outdated-image-exporter
COPY --from=builder --link /go/app/bin/outdated-image-exporter /outdated-image-exporter
ENTRYPOINT ["/outdated-image-exporter"]
109 changes: 84 additions & 25 deletions cmd/outdated-image-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,134 @@ package main
import (
"context"
"flag"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/evaluation"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/exporter"
pod_images "github.com/patrick246/k8s-outdated-image-exporter/pkg/pod-images"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/tags"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/version"
"k8s.io/client-go/util/homedir"
"log"
"fmt"
"log/slog"
"os"
"os/signal"
"path"
"syscall"
"time"

"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/util/homedir"

"github.com/patrick246/k8s-outdated-image-exporter/pkg/clients/docker"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/clients/k8s"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/evaluation"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/exporter"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/tags"
"github.com/patrick246/k8s-outdated-image-exporter/pkg/version"
)

var inClusterConfig = flag.Bool("in-cluster", true, "Controls if the in-cluster connection configuration method should be used.")
var imageCheckInterval = flag.Duration("image-check-interval", time.Hour, "How often to check for new image versions. Configuring this to a lower interval will eat up your registry request quota faster.")
var registryCredentialsPath = flag.String("registry-credentials", path.Join(homedir.HomeDir(), ".docker", "config.json"), "Path to a file containing registry credentials. This is the same format as K8s imagePullSecret contents")
var listenAddr = flag.String("listen-addr", ":8080", "The address to listen on for metrics requests")
var containerProvider = flag.String("container", "kubernetes", "Container technology used: [kubernetes, docker]")
var logLevel = flag.String("log-level", "info", "Log level: [debug, info, warning, error]")

func main() {
if err := run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "fatal: %v", err)

os.Exit(1)
}
}

func run() error {
flag.Parse()

podClient, err := pod_images.NewPodClient(pod_images.ConnectionConfig{
InClusterConfig: *inClusterConfig,
InformerResyncInterval: 5 * time.Minute,
ImageCheckInterval: *imageCheckInterval,
})
var level slog.Level
err := level.UnmarshalText([]byte(*logLevel))
if err != nil {
log.Fatal(err)
return err
}

logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
AddSource: true,
Level: level,
}))

var client evaluation.ContainerClient

switch *containerProvider {
case "kubernetes":
k8sClient, err := k8s.NewContainerClient(k8s.ConnectionConfig{
InClusterConfig: *inClusterConfig,
InformerResyncInterval: 5 * time.Minute,
ImageCheckInterval: *imageCheckInterval,
}, logger)
if err != nil {
return err
}

client = k8sClient
case "docker":
dockerClient, err := docker.NewDockerClient(logger)
if err != nil {
return err
}

client = dockerClient
default:
return fmt.Errorf("unsupported provider %q", *containerProvider)
}

authConfig, err := tags.ReadRegistryCredentialsFromFile(*registryCredentialsPath)
if err != nil {
log.Printf("No registry auth provided, looked up %s, got error: %v. Continuing without registry auth", *registryCredentialsPath, err)
logger.Warn("no registry auth provided. continuing without registry auth", "path", *registryCredentialsPath, "error", err)
}

tagLister, err := tags.NewTagLister(authConfig)
if err != nil {
log.Fatal(err)
return err
}

versionChecker, err := version.NewChecker()
if err != nil {
log.Fatal(err)
return err
}

evaluator, err := evaluation.NewEvaluator(podClient, tagLister, versionChecker)
evaluator, err := evaluation.NewEvaluator(tagLister, versionChecker, client, logger)
if err != nil {
log.Fatal(err)
return err
}

metricsCollector := exporter.NewCollector(evaluator)

err = prometheus.Register(metricsCollector)
if err != nil {
return err
}

signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGTERM)

runCtx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
err = evaluator.Run(runCtx)
if err != nil {
log.Fatal(err)
}
}()
logger.Error("failed to run evaluator", "error", err)

go func() {
err = exporter.RunServer(*listenAddr, runCtx)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}()

shutdownFunc, err := exporter.RunServer(*listenAddr)
if err != nil {
return err
}

<-signals

cancel()

err = shutdownFunc()
if err != nil {
return err
}

return nil
}
91 changes: 83 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,88 @@
module github.com/patrick246/k8s-outdated-image-exporter

go 1.16
go 1.22.0

toolchain go1.22.2

require (
github.com/docker/docker v26.1.1+incompatible
github.com/google/go-containerregistry v0.19.1
github.com/hashicorp/go-version v1.6.0
github.com/prometheus/client_golang v1.19.0
github.com/stretchr/testify v1.9.0
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
k8s.io/client-go v0.30.0
)

require (
github.com/google/go-containerregistry v0.5.0
github.com/hashicorp/go-version v1.3.0
github.com/prometheus/client_golang v0.9.3
github.com/stretchr/testify v1.6.1
k8s.io/api v0.20.0
k8s.io/apimachinery v0.20.0
k8s.io/client-go v0.20.0
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/cli v26.1.1+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.1 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.14.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.34.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240430033511-f0e62f92d13f // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit cdd39c7

Please sign in to comment.