Skip to content
Closed
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
4 changes: 4 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
startCmd.Flags().StringVar(&startCmdArgs.Kubernetes.Version, "kubernetes-version", defaultKubernetesVersion, "must match a k3s version https://github.com/k3s-io/k3s/releases")
startCmd.Flags().StringSliceVar(&startCmdArgs.Flags.LegacyKubernetesDisable, "kubernetes-disable", nil, "components to disable for k3s e.g. traefik,servicelb")
startCmd.Flags().StringSliceVar(&startCmdArgs.Kubernetes.K3sArgs, "k3s-arg", defaultK3sArgs, "additional args to pass to k3s")
startCmd.Flags().IntVar(&startCmdArgs.Kubernetes.ListenPort, "k3s-listen-port", 0, "k3s server listen port")

Check failure on line 237 in cmd/start.go

View workflow job for this annotation

GitHub Actions / lint

startCmdArgs.Kubernetes.ListenPort undefined (type config.Kubernetes has no field or method ListenPort) (typecheck)

Check failure on line 237 in cmd/start.go

View workflow job for this annotation

GitHub Actions / docker

startCmdArgs.Kubernetes.ListenPort undefined (type config.Kubernetes has no field or method ListenPort)

Check failure on line 237 in cmd/start.go

View workflow job for this annotation

GitHub Actions / kubernetes-docker

startCmdArgs.Kubernetes.ListenPort undefined (type config.Kubernetes has no field or method ListenPort)

Check failure on line 237 in cmd/start.go

View workflow job for this annotation

GitHub Actions / containerd

startCmdArgs.Kubernetes.ListenPort undefined (type config.Kubernetes has no field or method ListenPort)

Check failure on line 237 in cmd/start.go

View workflow job for this annotation

GitHub Actions / kubernetes-containerd

startCmdArgs.Kubernetes.ListenPort undefined (type config.Kubernetes has no field or method ListenPort)

Check failure on line 237 in cmd/start.go

View workflow job for this annotation

GitHub Actions / incus

startCmdArgs.Kubernetes.ListenPort undefined (type config.Kubernetes has no field or method ListenPort)

Check failure on line 237 in cmd/start.go

View workflow job for this annotation

GitHub Actions / build-linux

startCmdArgs.Kubernetes.ListenPort undefined (type config.Kubernetes has no field or method ListenPort)

Check failure on line 237 in cmd/start.go

View workflow job for this annotation

GitHub Actions / build-macos

startCmdArgs.Kubernetes.ListenPort undefined (type config.Kubernetes has no field or method ListenPort)
startCmd.Flag("with-kubernetes").Hidden = true
startCmd.Flag("kubernetes-disable").Hidden = true

Expand Down Expand Up @@ -475,6 +476,9 @@
if !cmd.Flag("k3s-arg").Changed && current.Kubernetes.K3sArgs != nil {
startCmdArgs.Kubernetes.K3sArgs = current.Kubernetes.K3sArgs
}
if !cmd.Flag("k3s-listen-port").Changed && current.Kubernetes.Port > 0 {
startCmdArgs.Kubernetes.Port = current.Kubernetes.Port
}
if !cmd.Flag("runtime").Changed {
startCmdArgs.Runtime = current.Runtime
}
Expand Down
4 changes: 2 additions & 2 deletions colima.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

with pkgs;

buildGo120Module {
buildGo123Module {
name = "colima";
pname = "colima";
src = ./.;
nativeBuildInputs = [ installShellFiles makeWrapper git ];
vendorSha256 = "sha256-7DIhSjHpaCyHyXKhR8KWQc2YGaD8CMq+BZHF4zIkL50=";
vendorHash = "sha256-ZwgzKCOEhgKK2LNRLjnWP6qHI4f6OGORvt3CREJf55I=";
CGO_ENABLED = 1;

subPackages = [ "cmd/colima" ];
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ type Config struct {

// Kubernetes is kubernetes configuration
type Kubernetes struct {
Enabled bool `yaml:"enabled"`
Version string `yaml:"version"`
K3sArgs []string `yaml:"k3sArgs"`
Enabled bool `yaml:"enabled"`
Version string `yaml:"version"`
K3sArgs []string `yaml:"k3sArgs"`
Port int `yaml:"port,omitempty"`
}

// Network is VM network configuration
Expand Down
5 changes: 5 additions & 0 deletions embedded/defaults/colima.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ kubernetes:
# Default: traefik is disabled
k3sArgs: [--disable=traefik]

# Kubernetes port to listen on
# A common port is 6443, though left unbound to ensure no port conflicts
# Default: pick random unbound port
port: 0

# Auto-activate on the Host for client access.
# Setting to true does the following on startup
# - sets as active Docker context (for Docker runtime).
Expand Down
21 changes: 15 additions & 6 deletions environment/container/kubernetes/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ func installK3s(host environment.HostActions,
log *logrus.Entry,
containerRuntime string,
k3sVersion string,
disable []string,
k3sArgs []string,
k3sListenPort int,
) {
installK3sBinary(host, guest, a, k3sVersion)
installK3sCache(host, guest, a, log, containerRuntime, k3sVersion)
installK3sCluster(host, guest, a, containerRuntime, k3sVersion, disable)
installK3sCluster(host, guest, a, containerRuntime, k3sVersion, k3sArgs, k3sListenPort)
}

func installK3sBinary(
Expand Down Expand Up @@ -141,6 +142,7 @@ func installK3sCluster(
containerRuntime string,
k3sVersion string,
k3sArgs []string,
k3sListenPort int,
) {
// install k3s last to ensure it is the last step
downloadPath := "/tmp/k3s-install.sh"
Expand Down Expand Up @@ -184,7 +186,7 @@ func installK3sCluster(
}

a.Add(func() error {
port, err := getPortNumber(guest)
port, err := getPortNumber(guest, k3sListenPort)
if err != nil {
return err
}
Expand All @@ -199,7 +201,7 @@ func installK3sCluster(

// getPortNumber retrieves the previously set port number.
// If missing, an available random port is set and return.
func getPortNumber(guest environment.GuestActions) (int, error) {
func getPortNumber(guest environment.GuestActions, k3sListenPort int) (int, error) {
// port previously set, reuse it
if port, err := strconv.Atoi(guest.Get(listenPortKey)); err == nil && port > 0 {
return port, nil
Expand All @@ -211,8 +213,15 @@ func getPortNumber(guest environment.GuestActions) (int, error) {
return 6443, nil
}

// new instance, assign random port
port := util.RandomAvailablePort()
var port int
if k3sListenPort > 0 {
// template configured port
port = k3sListenPort
} else {
// new instance, assign random port
port = util.RandomAvailablePort()
}

if err := guest.Set(listenPortKey, strconv.Itoa(port)); err != nil {
return 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions environment/container/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c *kubernetesRuntime) Provision(ctx context.Context) error {
installK3sCache(c.host, c.guest, a, log, runtime, conf.Version)
}
// other settings may have changed e.g. ingress
installK3sCluster(c.host, c.guest, a, runtime, conf.Version, conf.K3sArgs)
installK3sCluster(c.host, c.guest, a, runtime, conf.Version, conf.K3sArgs, conf.Port)
} else {
if c.isInstalled() {
a.Stagef("version changed to %s, downloading and installing", conf.Version)
Expand All @@ -128,7 +128,7 @@ func (c *kubernetesRuntime) Provision(ctx context.Context) error {
a.Stage("installing")
}
}
installK3s(c.host, c.guest, a, log, runtime, conf.Version, conf.K3sArgs)
installK3s(c.host, c.guest, a, log, runtime, conf.Version, conf.K3sArgs, conf.Port)
}

// this needs to happen on each startup
Expand Down
1 change: 1 addition & 0 deletions environment/vm/lima/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"time"

"github.com/abiosoft/colima/environment"
)

Expand Down
32 changes: 25 additions & 7 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
description = "Container runtimes on macOS (and Linux) with minimal setup";

inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# Last revision with go_1_23
inputs.nixpkgs.url = "github:NixOS/nixpkgs/25.05";

outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem
(system:
Expand Down
3 changes: 2 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
pkgs.mkShell {
# nativeBuildInputs is usually what you want -- tools you need to run
nativeBuildInputs = with pkgs.buildPackages; [
go_1_20
go_1_23
gotools
git
lima
qemu
Expand Down
3 changes: 2 additions & 1 deletion util/fsutil/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (DefaultFS) Open(name string) (fs.File, error) { return os.Open(name) }
func (DefaultFS) MkdirAll(path string, perm fs.FileMode) error { return os.MkdirAll(path, perm) }

// FakeFS is a mock FS. The following can be done in a test before usage.
// osutil.FS = osutil.FakeFS
//
// osutil.FS = osutil.FakeFS
var FakeFS FileSystem = fakeFS{}

type fakeFS struct{}
Expand Down
Loading