spegel/main.go

148 lines
5.0 KiB
Go
Raw Normal View History

2023-01-24 15:47:27 +01:00
package main
import (
"context"
2023-01-24 16:40:11 +01:00
"errors"
2023-01-24 15:47:27 +01:00
"fmt"
2023-01-24 16:40:11 +01:00
"net/http"
2023-01-24 15:47:27 +01:00
"net/url"
"os"
"os/signal"
"syscall"
2023-01-24 16:40:11 +01:00
"time"
2023-01-24 15:47:27 +01:00
"github.com/alexflint/go-arg"
"github.com/containerd/containerd"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
2023-01-24 16:40:11 +01:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2023-01-24 15:47:27 +01:00
"github.com/spf13/afero"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
2023-01-26 18:48:02 +01:00
pkgkubernetes "github.com/xenitab/pkg/kubernetes"
2023-01-24 15:47:27 +01:00
"github.com/xenitab/spegel/internal/mirror"
"github.com/xenitab/spegel/internal/registry"
2023-01-26 18:48:02 +01:00
"github.com/xenitab/spegel/internal/routing"
2023-01-24 15:47:27 +01:00
"github.com/xenitab/spegel/internal/state"
)
type arguments struct {
Registries []url.URL `arg:"--registries,required" help:"registries that are configured to be mirrored."`
MirrorRegistries []url.URL `arg:"--mirror-registries,required" help:"registries that are configured to act as mirrors."`
2023-02-06 09:23:22 +01:00
ImageFilter string `arg:"--image-filter" help:"inclusive image name filter."`
RegistryAddr string `arg:"--registry-addr,required" help:"address to server image registry."`
RouterAddr string `arg:"--router-addr,required" help:"address to serve router."`
MetricsAddr string `arg:"--metrics-addr,required" help:"address to serve metrics."`
2023-02-06 09:23:22 +01:00
ContainerdSock string `arg:"--containerd-sock" default:"/run/containerd/containerd.sock" help:"Endpoint of containerd service."`
ContainerdNamespace string `arg:"--containerd-namespace" default:"k8s.io" help:"Containerd namespace to fetch images from."`
ContainerdRegistryConfigPath string `arg:"--containerd-registry-config-path" default:"/etc/containerd/certs.d" help:"Directory where mirror configuration is written."`
ContainerdMirrorAdd bool `arg:"--containerd-mirror-add" default:"true" help:"Will add containerd mirror configuration if true."`
ContainerdMirrorRemove bool `arg:"--containerd-mirror-remove" default:"true" help:"Will remove containerd mirror configuration if true."`
KubeconfigPath string `arg:"--kubeconfig-path" help:"Path to the kubeconfig file."`
LeaderElectionNamespace string `arg:"--leader-election-namespace" default:"spegel" help:"Kubernetes namespace to write leader election data."`
LeaderElectionName string `arg:"--leader-election-name" default:"spegel-leader-election" help:"Name of leader election."`
2023-01-24 15:47:27 +01:00
}
func main() {
args := &arguments{}
arg.MustParse(args)
zapLog, err := zap.NewProduction()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log := zapr.NewLogger(zapLog)
2023-01-31 23:17:06 +01:00
err = run(log, args)
2023-01-24 15:47:27 +01:00
if err != nil {
2023-01-31 23:17:06 +01:00
log.Error(err, "")
2023-01-24 15:47:27 +01:00
os.Exit(1)
}
2023-01-31 23:17:06 +01:00
log.Info("gracefully shutdown")
}
2023-01-24 15:47:27 +01:00
func run(log logr.Logger, args *arguments) (err error) {
2023-01-26 18:48:02 +01:00
cs, err := pkgkubernetes.GetKubernetesClientset(args.KubeconfigPath)
if err != nil {
2023-01-31 23:17:06 +01:00
return err
}
containerdClient, err := containerd.New(args.ContainerdSock, containerd.WithDefaultNamespace(args.ContainerdNamespace))
if err != nil {
return fmt.Errorf("could not create containerd client: %w", err)
2023-01-26 18:48:02 +01:00
}
defer func() {
err = errors.Join(err, containerdClient.Close())
}()
2023-01-31 23:17:06 +01:00
ctx := logr.NewContext(context.Background(), log)
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
2023-01-26 18:48:02 +01:00
// It is fine to immediatly write mirror configuration because it should fallback to another nodes Spegel instance.
fs := afero.NewOsFs()
if args.ContainerdMirrorAdd {
err := mirror.AddMirrorConfiguration(ctx, fs, args.ContainerdRegistryConfigPath, args.Registries, args.MirrorRegistries)
if err != nil {
return err
}
}
if args.ContainerdMirrorRemove {
defer func() {
err = errors.Join(err, mirror.RemoveMirrorConfiguration(ctx, fs, args.ContainerdRegistryConfigPath, args.Registries))
}()
}
2023-01-24 16:40:11 +01:00
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
srv := &http.Server{
Addr: args.MetricsAddr,
Handler: mux,
}
g.Go(func() error {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
})
g.Go(func() error {
<-ctx.Done()
2023-01-25 23:52:50 +01:00
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
2023-01-24 16:40:11 +01:00
defer cancel()
return srv.Shutdown(shutdownCtx)
})
2023-01-26 18:48:02 +01:00
bootstrapper := routing.NewKubernetesBootstrapper(cs, args.LeaderElectionNamespace, args.LeaderElectionName)
router, err := routing.NewP2PRouter(ctx, args.RouterAddr, bootstrapper)
2023-01-24 15:47:27 +01:00
if err != nil {
2023-01-31 23:17:06 +01:00
return err
2023-01-24 15:47:27 +01:00
}
2023-01-25 19:08:33 +01:00
g.Go(func() error {
<-ctx.Done()
2023-01-26 18:48:02 +01:00
return router.Close()
2023-01-25 19:08:33 +01:00
})
2023-01-24 15:47:27 +01:00
g.Go(func() error {
return state.Track(ctx, containerdClient, router, args.Registries, args.ImageFilter)
2023-01-24 15:47:27 +01:00
})
2023-01-26 18:48:02 +01:00
reg, err := registry.NewRegistry(ctx, args.RegistryAddr, containerdClient, router)
2023-01-24 15:47:27 +01:00
if err != nil {
2023-01-31 23:17:06 +01:00
return err
2023-01-24 15:47:27 +01:00
}
g.Go(func() error {
return reg.ListenAndServe(ctx)
})
g.Go(func() error {
<-ctx.Done()
return reg.Shutdown()
})
log.Info("running registry", "addr", args.RegistryAddr)
err = g.Wait()
if err != nil {
2023-01-31 23:17:06 +01:00
return err
2023-01-24 15:47:27 +01:00
}
2023-01-31 23:17:06 +01:00
return nil
2023-01-24 15:47:27 +01:00
}