kube-fetcher/cmd/ctrutil/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

121 lines
2.6 KiB
Go

package main
import (
goflags "flag"
"fmt"
"git.wamblee.org/public/kube-fetcher/pkg/ctrd"
"github.com/spf13/cobra"
"io"
"k8s.io/klog/v2"
"os"
)
func main() {
klogFlags := goflags.NewFlagSet("", goflags.PanicOnError)
klog.InitFlags(klogFlags)
config := &Config{}
var runtime *ctrd.Containerd
var err error
cmd := &cobra.Command{
Use: "ctrutil",
Short: "Containerd utility",
Long: `
Containerd for working with images as they are pulled by the kubelet.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
runtime, err = ctrd.NewContainerd(config.SocketPath,
config.ContainerdNamespace)
if err != nil {
return err
}
return nil
},
}
pull := &cobra.Command{
Use: "pull",
Short: "pull image",
Long: `
Pull image`,
RunE: func(cmd *cobra.Command, args []string) error {
for _, image := range args {
klog.Infof("Pulling '%s'", image)
err := runtime.Pull(image)
if err != nil {
return fmt.Errorf("Cannot pull '%s': %v", image, err)
}
}
return nil
},
}
cmd.AddCommand(pull)
blob := &cobra.Command{
Use: "blob",
Short: "blob manipulation",
Long: `
Containerd for working with images as they are pulled by the kubelet.`,
}
cmd.AddCommand(blob)
blobget := &cobra.Command{
Use: "get",
Short: "get blob data",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("Expected blob digest as argument")
}
reader, err := runtime.GetBlob(args[0])
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, reader)
return err
},
}
blob.AddCommand(blobget)
blobmeta := &cobra.Command{
Use: "meta",
Short: "get blob metadata",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("Expected blob digest as argument")
}
info, err := runtime.GetBlobMeta(args[0])
if err != nil {
return err
}
fmt.Printf("size: %v\n", info.Size)
fmt.Printf("digest: %v\n", info.Digest)
fmt.Printf("created: %v\n", info.CreatedAt)
fmt.Printf("updated: %v\n", info.UpdatedAt)
for k, v := range info.Labels {
fmt.Printf("label: %s=%s\n", k, v)
}
return nil
},
}
blob.AddCommand(blobmeta)
if 1 > 2 {
runtime.List()
}
cmd.PersistentFlags().StringVar(&config.SocketPath, "socket",
"/run/runtime/runtime.sock", "Containerd socket")
cmd.PersistentFlags().StringVar(&config.ContainerdNamespace, "runtime-namespace",
"k8s.io", "Containerd namespace to use")
cmd.Flags().AddGoFlagSet(klogFlags)
err = cmd.Execute()
if err != nil {
klog.Errorf("Error: %v", err)
os.Exit(1)
}
}