From ffe07c766df02f6dfcdfd174c432b5fa77eacbef Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Tue, 2 Apr 2024 22:39:09 +0200 Subject: [PATCH] Replace XenitAB PKG with internal package --- CHANGELOG.md | 2 ++ Dockerfile | 1 + go.mod | 1 - go.sum | 2 -- internal/channel/channel.go | 27 +++++++++++++++++++++++++++ pkg/oci/containerd.go | 5 +++-- pkg/state/state.go | 4 ++-- 7 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 internal/channel/channel.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 81831a6..8947d10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [#411](https://github.com/spegel-org/spegel/pull/411) Replace XenitAB pkg with internal package. + ### Deprecated ### Removed diff --git a/Dockerfile b/Dockerfile index ebf64c5..aa141ab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ COPY go.mod go.mod COPY go.sum go.sum RUN go mod download COPY main.go main.go +COPY internal/ internal/ COPY pkg/ pkg/ RUN CGO_ENABLED=0 go build -installsuffix 'static' -o spegel . diff --git a/go.mod b/go.mod index 6a8b578..b1fa0c7 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/prometheus/client_golang v1.19.0 github.com/spf13/afero v1.11.0 github.com/stretchr/testify v1.9.0 - github.com/xenitab/pkg/channels v0.0.2 github.com/xenitab/pkg/gin v0.0.9 github.com/xenitab/pkg/kubernetes v0.0.4 go.etcd.io/bbolt v1.3.9 diff --git a/go.sum b/go.sum index d110fb5..3185356 100644 --- a/go.sum +++ b/go.sum @@ -1427,8 +1427,6 @@ github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSD github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= -github.com/xenitab/pkg/channels v0.0.2 h1:nCMsjEyhJedmAimYPOobzJl/yj7byU9h6IHEPc4QlPg= -github.com/xenitab/pkg/channels v0.0.2/go.mod h1:/MCBlje0/98BdAF7LetkSK1+lXeUpScIbTENGaWjGRg= github.com/xenitab/pkg/gin v0.0.9 h1:BGdxnKoXAJBkthQTwQdaRdN7jTiNO+/C8hIexBrasfU= github.com/xenitab/pkg/gin v0.0.9/go.mod h1:8rzqJ8X5KJOo31PBOD4/Wtlt2ac8hCjN1mpOf1YAFs4= github.com/xenitab/pkg/kubernetes v0.0.4 h1:muVWzci89l611bd4FzWlDsHm2zwwzNpxA2TvY9svebI= diff --git a/internal/channel/channel.go b/internal/channel/channel.go new file mode 100644 index 0000000..93cfbf9 --- /dev/null +++ b/internal/channel/channel.go @@ -0,0 +1,27 @@ +package channel + +import ( + "sync" +) + +func Merge[T any](cs ...<-chan T) <-chan T { + var wg sync.WaitGroup + out := make(chan T) + + output := func(c <-chan T) { + for n := range c { + out <- n + } + wg.Done() + } + wg.Add(len(cs)) + for _, c := range cs { + go output(c) + } + + go func() { + wg.Wait() + close(out) + }() + return out +} diff --git a/pkg/oci/containerd.go b/pkg/oci/containerd.go index 9c5e108..873d5ac 100644 --- a/pkg/oci/containerd.go +++ b/pkg/oci/containerd.go @@ -22,8 +22,9 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pelletier/go-toml/v2" "github.com/spf13/afero" - "github.com/xenitab/pkg/channels" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" + + "github.com/spegel-org/spegel/internal/channel" ) const ( @@ -165,7 +166,7 @@ func (c *Containerd) Subscribe(ctx context.Context) (<-chan ImageEvent, <-chan e imgCh <- ImageEvent{Image: img, Type: eventType} } }() - return imgCh, channels.Merge(errCh, cErrCh) + return imgCh, channel.Merge(errCh, cErrCh) } func (c *Containerd) ListImages(ctx context.Context) ([]Image, error) { diff --git a/pkg/state/state.go b/pkg/state/state.go index 18886fa..ecbda40 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -7,8 +7,8 @@ import ( "time" "github.com/go-logr/logr" - "github.com/xenitab/pkg/channels" + "github.com/spegel-org/spegel/internal/channel" "github.com/spegel-org/spegel/pkg/metrics" "github.com/spegel-org/spegel/pkg/oci" "github.com/spegel-org/spegel/pkg/routing" @@ -21,7 +21,7 @@ func Track(ctx context.Context, ociClient oci.Client, router routing.Router, res immediate <- time.Now() expirationTicker := time.NewTicker(routing.KeyTTL - time.Minute) defer expirationTicker.Stop() - ticker := channels.Merge(immediate, expirationTicker.C) + ticker := channel.Merge(immediate, expirationTicker.C) for { select { case <-ctx.Done():