forked from siderolabs/talos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement CRI image management and pre-pull on K8s upgrade
Fixes siderolabs#6391 Implement a set of APIs and commands to manage images in the CRI, and pre-pull images on Kubernetes upgrades. Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
- Loading branch information
Showing
27 changed files
with
2,835 additions
and
595 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package talos | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"text/tabwriter" | ||
"time" | ||
|
||
"github.com/dustin/go-humanize" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers" | ||
"github.com/siderolabs/talos/pkg/images" | ||
"github.com/siderolabs/talos/pkg/machinery/api/common" | ||
"github.com/siderolabs/talos/pkg/machinery/api/machine" | ||
"github.com/siderolabs/talos/pkg/machinery/client" | ||
"github.com/siderolabs/talos/pkg/machinery/config/container" | ||
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1" | ||
) | ||
|
||
type imageCmdFlagsType struct { | ||
namespace string | ||
} | ||
|
||
var imageCmdFlags imageCmdFlagsType | ||
|
||
func (flags imageCmdFlagsType) apiNamespace() (common.ContainerdNamespace, error) { | ||
switch flags.namespace { | ||
case "cri": | ||
return common.ContainerdNamespace_NS_CRI, nil | ||
case "system": | ||
return common.ContainerdNamespace_NS_SYSTEM, nil | ||
default: | ||
return 0, fmt.Errorf("unsupported namespace %q", flags.namespace) | ||
} | ||
} | ||
|
||
// imagesCmd represents the image command. | ||
var imageCmd = &cobra.Command{ | ||
Use: "image", | ||
Aliases: []string{}, | ||
Short: "Manage CRI containter images", | ||
Long: ``, | ||
Args: cobra.NoArgs, | ||
} | ||
|
||
// imageListCmd represents the image list command. | ||
var imageListCmd = &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"l", "ls"}, | ||
Short: "List CRI images", | ||
Long: ``, | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return WithClient(func(ctx context.Context, c *client.Client) error { | ||
ns, err := imageCmdFlags.apiNamespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rcv, err := c.ImageList(ctx, ns) | ||
if err != nil { | ||
return fmt.Errorf("error listing images: %w", err) | ||
} | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) | ||
fmt.Fprintln(w, "NODE\tIMAGE\tDIGEST\tSIZE\tCREATED") | ||
|
||
if err = helpers.ReadGRPCStream(rcv, func(msg *machine.ImageListResponse, node string, multipleNodes bool) error { | ||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", | ||
node, | ||
msg.Name, | ||
msg.Digest, | ||
humanize.Bytes(uint64(msg.Size)), | ||
msg.CreatedAt.AsTime().Format(time.RFC3339), | ||
) | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return w.Flush() | ||
}) | ||
}, | ||
} | ||
|
||
// imagePullCmd represents the image pull command. | ||
var imagePullCmd = &cobra.Command{ | ||
Use: "pull", | ||
Aliases: []string{"p"}, | ||
Short: "Pull an image into CRI", | ||
Long: ``, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return WithClient(func(ctx context.Context, c *client.Client) error { | ||
ns, err := imageCmdFlags.apiNamespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = c.ImagePull(ctx, ns, args[0]) | ||
if err != nil { | ||
return fmt.Errorf("error pulling image: %w", err) | ||
} | ||
|
||
return nil | ||
}) | ||
}, | ||
} | ||
|
||
// imageDefaultCmd represents the image default command. | ||
var imageDefaultCmd = &cobra.Command{ | ||
Use: "default", | ||
Short: "List the default images used by Talos", | ||
Long: ``, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
images := images.List(container.NewV1Alpha1(&v1alpha1.Config{ | ||
MachineConfig: &v1alpha1.MachineConfig{ | ||
MachineKubelet: &v1alpha1.KubeletConfig{}, | ||
}, | ||
ClusterConfig: &v1alpha1.ClusterConfig{ | ||
EtcdConfig: &v1alpha1.EtcdConfig{}, | ||
APIServerConfig: &v1alpha1.APIServerConfig{}, | ||
ControllerManagerConfig: &v1alpha1.ControllerManagerConfig{}, | ||
SchedulerConfig: &v1alpha1.SchedulerConfig{}, | ||
CoreDNSConfig: &v1alpha1.CoreDNS{}, | ||
ProxyConfig: &v1alpha1.ProxyConfig{}, | ||
}, | ||
})) | ||
|
||
fmt.Printf("%s\n", images.Flannel) | ||
fmt.Printf("%s\n", images.FlannelCNI) | ||
fmt.Printf("%s\n", images.CoreDNS) | ||
fmt.Printf("%s\n", images.Etcd) | ||
fmt.Printf("%s\n", images.KubeAPIServer) | ||
fmt.Printf("%s\n", images.KubeControllerManager) | ||
fmt.Printf("%s\n", images.KubeScheduler) | ||
fmt.Printf("%s\n", images.KubeProxy) | ||
fmt.Printf("%s\n", images.Kubelet) | ||
fmt.Printf("%s\n", images.Installer) | ||
fmt.Printf("%s\n", images.Pause) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
imageCmd.PersistentFlags().StringVar(&imageCmdFlags.namespace, "namespace", "cri", "namespace to use: `system` (etcd and kubelet images) or `cri` for all Kubernetes workloads") | ||
addCommand(imageCmd) | ||
|
||
imageCmd.AddCommand(imageDefaultCmd) | ||
imageCmd.AddCommand(imageListCmd) | ||
imageCmd.AddCommand(imagePullCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.