spegel/main.go

145 lines
4.2 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 {
2023-01-25 13:14:41 +01:00
MirrorRegistries []url.URL `arg:"--mirror-registries,required"`
ImageFilter string `arg:"--image-filter"`
2023-01-26 18:48:02 +01:00
RegistryAddr string `arg:"--registry-addr" default:":5000"`
RouterAddr string `arg:"--router-addr" default:":5001"`
MetricsAddr string `arg:"--metrics-addr" default:":9090"`
2023-01-24 15:47:27 +01:00
ContainerdSock string `arg:"--containerd-sock" default:"/run/containerd/containerd.sock"`
ContainerdNamespace string `arg:"--containerd-namespace" default:"k8s.io"`
ContainerdRegistryConfigPath string `arg:"--containerd-registry-config-path" default:"/etc/containerd/certs.d"`
ContainerdMirrorAdd bool `arg:"--containerd-mirror-add" default:"true"`
ContainerdMirrorRemove bool `arg:"--containerd-mirror-remove" default:"true"`
2023-01-26 18:48:02 +01:00
KubeconfigPath string `arg:"--kubeconfig-path"`
LeaderElectionNamespace string `arg:"--leader-election-namespace" default:"spegel"`
LeaderElectionName string `arg:"--leader-election-name" default:"spegel-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
2023-01-31 23:17:06 +01:00
func run(log logr.Logger, args *arguments) 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
}
2023-01-31 23:17:06 +01:00
defer containerdClient.Close()
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
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 {
2023-01-26 18:48:02 +01:00
return state.Track(ctx, containerdClient, router, 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()
})
if args.ContainerdMirrorAdd {
fs := afero.NewOsFs()
defer func() {
err := mirror.RemoveMirrorConfiguration(ctx, fs, args.ContainerdRegistryConfigPath, args.MirrorRegistries)
if err != nil {
log.Error(err, "failed to remove mirror configuration")
}
}()
err := mirror.AddMirrorConfiguration(ctx, fs, args.ContainerdRegistryConfigPath, args.RegistryAddr, args.MirrorRegistries)
if err != nil {
return err
}
}
2023-01-24 15:47:27 +01:00
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
}