Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/cluster/internal/providers/docker/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,28 @@ func (p *provider) Info() (*providers.ProviderInfo, error) {
return p.info, err
}

// ContainerSave saves images to dest, as in `docker save`
func (p *provider) ContainerSave(images []string, dest string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving images from storage isn't really a property of implementing nodes, and this is already a pretty complicated object. I think we should probably split this out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also lose the ability to import across providers cleanly, which while a bit weird we probably already have users doing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving images from storage isn't really a property of implementing nodes, and this is already a pretty complicated object. I think we should probably split this out.

Any idea for locations? I would suggest pkg/cmd/kind/load/providers/*/provider.go

We also lose the ability to import across providers cleanly, which while a bit weird we probably already have users doing.

Can you explain that? load docker-image will be backwards compatible.

commandArgs := append([]string{"save", "-o", dest}, images...)
return exec.Command("docker", commandArgs...).Run()
}

// ContainerImageID return the Id of the container image
func (p *provider) ContainerImageID(containerNameOrID string) (string, error) {
cmd := exec.Command("docker", "image", "inspect",
"-f", "{{ .Id }}",
containerNameOrID, // ... against the container
)
lines, err := exec.OutputLines(cmd)
if err != nil {
return "", err
}
if len(lines) != 1 {
return "", errors.Errorf("Container image ID should only be one line, got %d lines", len(lines))
}
return lines[0], nil
}

// dockerInfo corresponds to `docker info --format '{{json .}}'`
type dockerInfo struct {
CgroupDriver string `json:"CgroupDriver"` // "systemd", "cgroupfs", "none"
Expand Down
22 changes: 22 additions & 0 deletions pkg/cluster/internal/providers/nerdctl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,28 @@ func (p *provider) Info() (*providers.ProviderInfo, error) {
return p.info, err
}

// ContainerSave saves images to dest, as in `nerdctl save`
func (p *provider) ContainerSave(images []string, dest string) error {
commandArgs := append([]string{"save", "-o", dest}, images...)
return exec.Command(p.Binary(), commandArgs...).Run()
}

// ContainerImageID return the Id of the container image
func (p *provider) ContainerImageID(containerNameOrID string) (string, error) {
cmd := exec.Command(p.Binary(), "image", "inspect",
"-f", "{{ .Id }}",
containerNameOrID, // ... against the container
)
lines, err := exec.OutputLines(cmd)
if err != nil {
return "", err
}
if len(lines) != 1 {
return "", errors.Errorf("Container image ID should only be one line, got %d lines", len(lines))
}
return lines[0], nil
}

// dockerInfo corresponds to `docker info --format '{{json .}}'`
type dockerInfo struct {
CgroupDriver string `json:"CgroupDriver"` // "systemd", "cgroupfs", "none"
Expand Down
22 changes: 22 additions & 0 deletions pkg/cluster/internal/providers/podman/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,28 @@ func (p *provider) Info() (*providers.ProviderInfo, error) {
return p.info, nil
}

// ContainerSave saves images to dest, as in `podman save`
func (p *provider) ContainerSave(images []string, dest string) error {
commandArgs := append([]string{"save", "-o", dest}, images...)
return exec.Command("podman", commandArgs...).Run()
}

// ContainerImageID return the Id of the container image
func (p *provider) ContainerImageID(containerNameOrID string) (string, error) {
cmd := exec.Command("podman", "image", "inspect",
"-f", "{{ .Id }}",
containerNameOrID, // ... against the container
)
lines, err := exec.OutputLines(cmd)
if err != nil {
return "", err
}
if len(lines) != 1 {
return "", errors.Errorf("Container image ID should only be one line, got %d lines", len(lines))
}
return lines[0], nil
}

// podmanInfo corresponds to `podman info --format 'json`.
// The structure is different from `docker info --format '{{json .}}'`,
// and lacks information about the availability of the cgroup controllers.
Expand Down
4 changes: 4 additions & 0 deletions pkg/cluster/internal/providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type Provider interface {
CollectLogs(dir string, nodes []nodes.Node) error
// Info returns the provider info
Info() (*ProviderInfo, error)
// ContainerSave saves images to dest, as in `docker save`
ContainerSave(images []string, dest string) error
// ContainerImageId return the Id of the container image
ContainerImageID(containerNameOrID string) (string, error)
}

// ProviderInfo is the info of the provider
Expand Down
8 changes: 8 additions & 0 deletions pkg/cluster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ func (p *Provider) CollectLogs(name, dir string) error {
return errors.NewAggregate(errs)
}

func (p *Provider) ContainerSave(imageNames []string, imagesTarPath string) error {
return p.provider.ContainerSave(imageNames, imagesTarPath)
}

func (p *Provider) ContainerImageID(containerNameOrID string) (string, error) {
return p.provider.ContainerImageID(containerNameOrID)
}

func collectNodeLogs(logger log.Logger, n nodes.Node, dir string) error {
execToPathFn := func(cmd exec.Cmd, path string) func() error {
return func() error {
Expand Down
262 changes: 262 additions & 0 deletions pkg/cmd/kind/load/container-image/container-image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
/*
Copyright 2019 The Kubernetes Authors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new files should drop the year (previously they should've had the year of creation, so 2025 here)

dropping the year is ... new kubernetes/steering#299

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package load implements the `load` command
package load

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
"sigs.k8s.io/kind/pkg/cmd"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/fs"
"sigs.k8s.io/kind/pkg/log"

"sigs.k8s.io/kind/pkg/internal/cli"
"sigs.k8s.io/kind/pkg/internal/runtime"
)

type (
imageTagFetcher func(nodes.Node, string) (map[string]bool, error)
)

type flagpole struct {
Name string
Nodes []string
}

// NewCommand returns a new cobra.Command for loading an image into a cluster
func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
flags := &flagpole{}
cmd := &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("a list of image names is required")
}
return nil
},
Use: "container-image <IMAGE> [IMAGE...]",
Aliases: []string{"image"},
Short: "Loads container images from host into nodes",
Long: "Loads container images from host into all or specified nodes by name",
RunE: func(cmd *cobra.Command, args []string) error {
cli.OverrideDefaultName(cmd.Flags())
return runE(logger, flags, args)
},
}
cmd.Flags().StringVarP(
&flags.Name,
"name",
"n",
cluster.DefaultName,
"the cluster context name",
)
cmd.Flags().StringSliceVar(
&flags.Nodes,
"nodes",
nil,
"comma separated list of nodes to load images into",
)
return cmd
}

func runE(logger log.Logger, flags *flagpole, args []string) error {
provider := cluster.NewProvider(
cluster.ProviderWithLogger(logger),
runtime.GetDefault(logger),
)

// Check that the image exists locally and gets its ID, if not return error
imageNames := removeDuplicates(args)
var imageIDs []string
for _, imageName := range imageNames {
imageID, err := provider.ContainerImageID(imageName)
if err != nil {
return fmt.Errorf("image: %q not present locally", imageName)
}
imageIDs = append(imageIDs, imageID)
}

// Check if the cluster nodes exist
nodeList, err := provider.ListInternalNodes(flags.Name)
if err != nil {
return err
}
if len(nodeList) == 0 {
return fmt.Errorf("no nodes found for cluster %q", flags.Name)
}

// map cluster nodes by their name
nodesByName := map[string]nodes.Node{}
for _, node := range nodeList {
// TODO(bentheelder): this depends on the fact that ListByCluster()
// will have name for nameOrId.
nodesByName[node.String()] = node
}

// pick only the user selected nodes and ensure they exist
// the default is all nodes unless flags.Nodes is set
candidateNodes := nodeList
if len(flags.Nodes) > 0 {
candidateNodes = []nodes.Node{}
for _, name := range flags.Nodes {
node, ok := nodesByName[name]
if !ok {
return fmt.Errorf("unknown node: %q", name)
}
candidateNodes = append(candidateNodes, node)
}
}

// pick only the nodes that don't have the image
selectedNodes := map[string]nodes.Node{}
fns := []func() error{}
for i, imageName := range imageNames {
imageID := imageIDs[i]
processed := false
for _, node := range candidateNodes {
exists, reTagRequired, sanitizedImageName := checkIfImageReTagRequired(node, imageID, imageName, nodeutils.ImageTags)
if exists && !reTagRequired {
continue
}

if reTagRequired {
// We will try to re-tag the image. If the re-tag fails, we will fall back to the default behavior of loading
// the images into the nodes again
logger.V(0).Infof("Image with ID: %s already present on the node %s but is missing the tag %s. re-tagging...", imageID, node.String(), sanitizedImageName)
if err := nodeutils.ReTagImage(node, imageID, sanitizedImageName); err != nil {
logger.Errorf("failed to re-tag image on the node %s due to an error %s. Will load it instead...", node.String(), err)
selectedNodes[node.String()] = node
} else {
processed = true
}
continue
}
id, err := nodeutils.ImageID(node, imageName)
if err != nil || id != imageID {
selectedNodes[node.String()] = node
logger.V(0).Infof("Image: %q with ID %q not yet present on node %q, loading...", imageName, imageID, node.String())
}
continue
}
if len(selectedNodes) == 0 && !processed {
logger.V(0).Infof("Image: %q with ID %q found to be already present on all nodes.", imageName, imageID)
}
}

// return early if no node needs the image
if len(selectedNodes) == 0 {
return nil
}

// Setup the tar path where the images will be saved
dir, err := fs.TempDir("", "images-tar")
if err != nil {
return errors.Wrap(err, "failed to create tempdir")
}
defer os.RemoveAll(dir)
imagesTarPath := filepath.Join(dir, "images.tar")
// Save the images into a tar
err = provider.ContainerSave(imageNames, imagesTarPath)
if err != nil {
return err
}

// Load the images on the selected nodes
for _, selectedNode := range selectedNodes {
selectedNode := selectedNode // capture loop variable
fns = append(fns, func() error {
return loadImage(imagesTarPath, selectedNode)
})
}
return errors.UntilErrorConcurrent(fns)
}

// TODO: we should consider having a cluster method to load images

// loads an image tarball onto a node
func loadImage(imageTarName string, node nodes.Node) error {
f, err := os.Open(imageTarName)
if err != nil {
return errors.Wrap(err, "failed to open image")
}
defer f.Close()
return nodeutils.LoadImageArchive(node, f)
}

// removeDuplicates removes duplicates from a string slice
func removeDuplicates(slice []string) []string {
result := []string{}
seenKeys := make(map[string]struct{})
for _, k := range slice {
if _, seen := seenKeys[k]; !seen {
result = append(result, k)
seenKeys[k] = struct{}{}
}
}
return result
}

// checkIfImageExists makes sure we only perform the reverse lookup of the ImageID to tag map
func checkIfImageReTagRequired(node nodes.Node, imageID, imageName string, tagFetcher imageTagFetcher) (exists, reTagRequired bool, sanitizedImage string) {
tags, err := tagFetcher(node, imageID)
if len(tags) == 0 || err != nil {
exists = false
return
}
exists = true
sanitizedImage = sanitizeImage(imageName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be inaccurate on podman where the user may have configured a different default image host because :reasons:

for our own node images, forcing the default host we actually use makes sense, for user's images ... maybe not

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestion? Just not sanitizing will lead to an error then—or make the command interactive which might lead to new problems …

if ok := tags[sanitizedImage]; ok {
reTagRequired = false
return
}
reTagRequired = true
return
}

// sanitizeImage is a helper to return human readable image name
// This is a modified version of the same function found under providers/podman/images.go
func sanitizeImage(image string) (sanitizedName string) {
const (
defaultDomain = "docker.io/"
officialRepoName = "library"
)
sanitizedName = image

if !strings.ContainsRune(image, '/') {
sanitizedName = officialRepoName + "/" + image
}

i := strings.IndexRune(sanitizedName, '/')
if i == -1 || (!strings.ContainsAny(sanitizedName[:i], ".:") && sanitizedName[:i] != "localhost") {
sanitizedName = defaultDomain + sanitizedName
}

i = strings.IndexRune(sanitizedName, ':')
if i == -1 {
sanitizedName += ":latest"
}

return
}
Loading