Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k3s: preload images for all platforms. Fixes #219 #298

Merged
merged 3 commits into from
May 19, 2022
Merged
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
14 changes: 9 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,24 +216,25 @@ func (c colimaApp) SSH(layer bool, args ...string) error {
return c.guest.RunInteractive(args...)
}

cmdArgs, layer, err := lima.ShowSSH(config.CurrentProfile().ID, layer, "args")
resp, err := lima.ShowSSH(config.CurrentProfile().ID, layer, "args")
if err != nil {
return fmt.Errorf("error getting ssh config: %w", err)
}

if !layer {
if !resp.Layer {
return c.guest.RunInteractive(args...)
}

cmdArgs := resp.Output

wd, err := os.Getwd()
if err != nil {
log.Debug(fmt.Errorf("cannot get working dir: %w", err))
}

if len(args) > 0 {
args = append([]string{"-q", "-t", "127.0.0.1", "--"}, args...)
args = append([]string{"-q", "-t", resp.IPAddress, "--"}, args...)
} else if wd != "" {
args = []string{"-q", "-t", "127.0.0.1", "--", "cd " + wd + " 2> /dev/null; bash --login"}
args = []string{"-q", "-t", resp.IPAddress, "--", "cd " + wd + " 2> /dev/null; bash --login"}
}

args = append(strings.Fields(cmdArgs), args...)
Expand All @@ -255,6 +256,9 @@ func (c colimaApp) Status() error {
log.Println(config.CurrentProfile().DisplayName, "is running")
log.Println("arch:", c.guest.Arch())
log.Println("runtime:", currentRuntime)
if conf, err := lima.InstanceConfig(); err == nil {
log.Println("mountType:", conf.MountType)
}
if currentRuntime == docker.Name {
log.Println("socket:", "unix:https://"+docker.HostSocketFile())
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ssh-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var sshConfigCmd = &cobra.Command{
Long: `Show configuration of the SSH connection to the VM.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
out, _, err := lima.ShowSSH(config.CurrentProfile().ID, sshConfigCmdArgs.layer, sshConfigCmdArgs.format)
resp, err := lima.ShowSSH(config.CurrentProfile().ID, sshConfigCmdArgs.layer, sshConfigCmdArgs.format)
if err == nil {
fmt.Println(out)
fmt.Println(resp.Output)
}
return err
},
Expand Down
4 changes: 1 addition & 3 deletions environment/container/docker/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package docker

import (
"fmt"
"path/filepath"

"github.com/abiosoft/colima/config"
Expand All @@ -14,8 +13,7 @@ func LegacyDefaultHostSocketFile() string {
}

func (d dockerRuntime) isContextCreated() bool {
command := fmt.Sprintf(`docker context ls -q | grep "^%s$"`, config.CurrentProfile().ID)
return d.host.RunQuiet("sh", "-c", command) == nil
return d.host.RunQuiet("docker", "context", "inspect", config.CurrentProfile().ID) == nil
}

func (d dockerRuntime) setupContext() error {
Expand Down
2 changes: 1 addition & 1 deletion environment/container/kubernetes/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func installK3sCache(
case containerd.Name:
a.Stage("loading oci images")
a.Add(func() error {
if err := guest.Run("sudo", "ctr", "-n", "k8s.io", "images", "import", downloadPathTar); err != nil {
if err := guest.Run("sudo", "ctr", "-n", "k8s.io", "images", "import", "--all-platforms", downloadPathTar); err != nil {
log.Warnln(fmt.Errorf("error loading oci images: %w", err))
log.Warnln("startup may delay a bit as images will be pulled from oci registry")
}
Expand Down
18 changes: 13 additions & 5 deletions environment/vm/lima/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ func getRuntime(conf config.Config) string {
}

// IPAddress returns the ip address for profile.
// It returns the PTP address if networking is enabled or falls back to 127.0.0.1
// It returns the PTP address if networking is enabled or falls back to 127.0.0.1.
// It is guaranteed to return a value.
// TODO: unnecessary round-trip is done to get instance details from Lima.
func IPAddress(profileID string) string {
// profile = toUserFriendlyName(profile)
Expand All @@ -210,14 +211,18 @@ func IPAddress(profileID string) string {

// ShowSSH runs the show-ssh command in Lima.
// returns the ssh output, if in layer, and an error if any
func ShowSSH(profileID string, layer bool, format string) (string, bool, error) {
func ShowSSH(profileID string, layer bool, format string) (resp struct {
Output string
IPAddress string
Layer bool
}, err error) {
var buf bytes.Buffer
cmd := cli.Command("limactl", "show-ssh", "--format", format, profileID)
cmd.Stdout = &buf
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return "", false, fmt.Errorf("error retrieving ssh config: %w", err)
return resp, fmt.Errorf("error retrieving ssh config: %w", err)
}

ip := IPAddress(profileID)
Expand All @@ -239,10 +244,13 @@ func ShowSSH(profileID string, layer bool, format string) (string, bool, error)
case "cmd", "args":
out = replaceSSHCmd(out, profileID, ip, port)
default:
return "", false, fmt.Errorf("unsupported format '%v'", format)
return resp, fmt.Errorf("unsupported format '%v'", format)
}

return out, port > 0, nil
resp.Output = out
resp.IPAddress = ip
resp.Layer = port > 0
return resp, nil
}

func replaceSSHCmd(cmd string, name string, ip string, port int) string {
Expand Down