kube-fetcher/cmd/fetcher/main.go
Erik Brakkee b31e03bc7a Initial pull implemented.
Support for proxy repositories by pulling from the proxy and then tagging the image.
2025-03-09 14:18:29 +01:00

81 lines
2.8 KiB
Go

package main
import (
goflags "flag"
"git.wamblee.org/public/kube-fetcher/pkg/support"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
"os"
"time"
)
func main() {
klogFlags := goflags.NewFlagSet("", goflags.PanicOnError)
klog.InitFlags(klogFlags)
clientset := support.GetKubernetesConnection()
config := &Config{}
cmd := &cobra.Command{
Use: "kube-fetcher",
Short: "Fetch images on a kubernetes node",
Long: `
Queries k8s for all running pods and makes sure that all
images referenced in pods are made available on the local k8s node and pinned
so they don't get garbage collected'`,
RunE: func(cmd *cobra.Command, args []string) error {
serializer := make(chan func())
go func() {
for action := range serializer {
action()
}
}()
watcher := NewWatcher(clientset, config.monitoringWindowSize, config.KubernetesNamespace, serializer)
fetcher := NewFetcher(clientset, config, watcher)
// TODO config option
fetcher.pullAndPin(config.InitialPullAll)
ticker := time.NewTicker(config.PollInterval)
for {
select {
case <-ticker.C:
serializer <- func() {
klog.V(3).Infof("Fetcher.pullAndPin")
fetcher.pullAndPin(false)
}
}
}
},
}
cmd.PersistentFlags().StringVar(&config.KubernetesNamespace, "kubernetes-namespace",
"", "Kubernetes containerdNamespace to inspect (default is all namespaces)")
cmd.PersistentFlags().StringVar(&config.SocketPath, "socket",
"/run/containerd/containerd.sock", "Containerd socket")
cmd.PersistentFlags().StringVar(&config.ContainerdNamespace, "containerd-namespace",
"k8s.io", "Containerd namespace to use")
cmd.PersistentFlags().StringVar(&config.Nodename, "nodename", "",
"Kubernetes node name the fetcher is running on, it will only fetch images running on other nodes")
cmd.PersistentFlags().DurationVar(&config.ReadyDuration, "ready-duration",
1*time.Hour, "Time a pod must be ready before its image will be fetched")
cmd.PersistentFlags().BoolVar(&config.includeControllerNodes, "include-controllers",
false, "Include controller nodes")
cmd.PersistentFlags().DurationVar(&config.monitoringWindowSize, "monitoring-window",
6*time.Hour, "Monitoring window to see what pods were active")
cmd.PersistentFlags().DurationVar(&config.PollInterval, "poll-interval",
1*time.Minute, "Poll interval for checking whether to pull images. ")
cmd.PersistentFlags().StringToStringVar(&config.mirrors,
"mirror", make(map[string]string), "Specify regsitry mirror in the form registrey=mirror, e.g. docker.io=my.mirror. The option can be repeated.")
cmd.PersistentFlags().BoolVar(&config.InitialPullAll, "initial-pull-all",
false, "Initially pull all images, this can be usefule for populating a caching proxy.")
cmd.Flags().AddGoFlagSet(klogFlags)
err := cmd.Execute()
if err != nil {
klog.Errorf("Error: %v", err)
os.Exit(1)
}
}