Skip to content

Commit

Permalink
Normalize terminology, server is always controller (#90)
Browse files Browse the repository at this point in the history
* Normalize terminology, server is controller

* Role switch when collecting status
  • Loading branch information
kke committed Feb 24, 2021
1 parent cb52528 commit ecb4577
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ metadata:
name: my-k0s-cluster
spec:
hosts:
- role: server
- role: controller
ssh:
address: 10.0.0.1
user: root
Expand Down Expand Up @@ -88,7 +88,7 @@ spec:
### Configuration file `spec` fields

* `hosts` A list of target hosts
* `role` One of `server`, `worker` or if you want the server to run workloads, use `server+worker`
* `role` One of `controller`, `worker` or if you want the controller to run workloads, use `controller+worker`
* `uploadBinary` When set to `true`, instead of having the hosts download the k0s binaries from the internet, k0sctl will download them to the local storage and upload to the target hosts
* `k0sBinaryPath` Upload a k0s binary from a local path to the host, useful for running a locally compiled development version
* `installFlags` A list of extra arguments passed to the `k0s install` command. See [k0s install command documentation](https://docs.k0sproject.io/main/cli/k0s_install/) for details
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var initCommand = &cli.Command{
Address: "10.0.0.1",
},
},
Role: "server",
Role: "controller",
},
&cluster.Host{
Connection: rig.Connection{
Expand Down
14 changes: 7 additions & 7 deletions config/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type Host struct {
rig.Connection `yaml:",inline"`

Role string `yaml:"role" validate:"oneof=server worker server+worker"`
Role string `yaml:"role" validate:"oneof=controller worker controller+worker"`
PrivateInterface string `yaml:"privateInterface,omitempty"`
PrivateAddress string `yaml:"privateAddress,omitempty" validate:"omitempty,ip"`
Environment map[string]string `yaml:"environment,flow,omitempty" default:"{}"`
Expand Down Expand Up @@ -161,8 +161,8 @@ func (h *Host) K0sInstallCommand() string {
role := h.Role
flags := h.InstallFlags

if role == "server+worker" {
role = "server"
if role == "controller+worker" {
role = "controller"
flags.AddUnlessExist("--enable-worker")
}

Expand All @@ -175,15 +175,15 @@ func (h *Host) K0sInstallCommand() string {
return h.Configurer.K0sCmdf("install %s %s", role, flags.Join())
}

// IsController returns true for server and server+worker roles
// IsController returns true for controller and controller+worker roles
func (h *Host) IsController() bool {
return h.Role == "server" || h.Role == "server+worker"
return h.Role == "controller" || h.Role == "controller+worker"
}

// K0sServiceName returns correct service name
func (h *Host) K0sServiceName() string {
if h.Role == "server+worker" {
return "k0sserver"
if h.Role == "controller+worker" {
return "k0scontroller"
}
return "k0s" + h.Role
}
Expand Down
20 changes: 10 additions & 10 deletions config/cluster/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
func TestHostK0sServiceName(t *testing.T) {
h := Host{Role: "worker"}
require.Equal(t, "k0sworker", h.K0sServiceName())
h.Role = "server"
require.Equal(t, "k0sserver", h.K0sServiceName())
h.Role = "server+worker"
require.Equal(t, "k0sserver", h.K0sServiceName())
h.Role = "controller"
require.Equal(t, "k0scontroller", h.K0sServiceName())
h.Role = "controller+worker"
require.Equal(t, "k0scontroller", h.K0sServiceName())
}

type mockconfigurer struct {
Expand Down Expand Up @@ -64,15 +64,15 @@ func TestK0sInstallCommand(t *testing.T) {

require.Equal(t, `k0s install worker --token-file "from-configurer" --config "from-configurer"`, h.K0sInstallCommand())

h.Role = "server"
h.Role = "controller"
h.Metadata.IsK0sLeader = true
require.Equal(t, `k0s install server --config "from-configurer"`, h.K0sInstallCommand())
require.Equal(t, `k0s install controller --config "from-configurer"`, h.K0sInstallCommand())
h.Metadata.IsK0sLeader = false
require.Equal(t, `k0s install server --token-file "from-configurer" --config "from-configurer"`, h.K0sInstallCommand())
require.Equal(t, `k0s install controller --token-file "from-configurer" --config "from-configurer"`, h.K0sInstallCommand())

h.Role = "server+worker"
h.Role = "controller+worker"
h.Metadata.IsK0sLeader = true
require.Equal(t, `k0s install server --enable-worker --config "from-configurer"`, h.K0sInstallCommand())
require.Equal(t, `k0s install controller --enable-worker --config "from-configurer"`, h.K0sInstallCommand())
h.Metadata.IsK0sLeader = false
require.Equal(t, `k0s install server --enable-worker --token-file "from-configurer" --config "from-configurer"`, h.K0sInstallCommand())
require.Equal(t, `k0s install controller --enable-worker --token-file "from-configurer" --config "from-configurer"`, h.K0sInstallCommand())
}
2 changes: 1 addition & 1 deletion config/cluster/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (hosts *Hosts) WithRole(s string) Hosts {
})
}

// Controllers returns hosts with the role "server"
// Controllers returns hosts with the role "controller"
func (hosts *Hosts) Controllers() Hosts {
return hosts.Filter(func(h *Host) bool { return h.IsController() })
}
Expand Down
2 changes: 1 addition & 1 deletion config/cluster/k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// K0sMinVersion is the minimum k0s version supported
const K0sMinVersion = "0.10.0-beta2"
const K0sMinVersion = "0.11.0-beta1"

// K0s holds configuration for bootstraping a k0s cluster
type K0s struct {
Expand Down
4 changes: 2 additions & 2 deletions config/cluster/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func (s *Spec) K0sLeader() *Host {
if s.k0sLeader == nil {
controllers := s.Hosts.Controllers()

// Pick the first server that reports to be running and persist the choice
// Pick the first controller that reports to be running and persist the choice
for _, h := range controllers {
if h.Metadata.K0sBinaryVersion != "" && h.Metadata.K0sRunningVersion != "" {
s.k0sLeader = h
break
}
}

// Still nil? Fall back to first "server" host, do not persist selection.
// Still nil? Fall back to first "controller" host, do not persist selection.
if s.k0sLeader == nil {
return controllers.First()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-tf/controller.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resource "aws_instance" "cluster-controller" {
instance_type = var.cluster_flavor

tags = {
Name = "server"
Name = "controller"
}
key_name = aws_key_pair.cluster-key.key_name
vpc_security_group_ids = [aws_security_group.cluster_allow_ssh.id]
Expand Down
4 changes: 3 additions & 1 deletion examples/footloose/footloose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ machines:
backend: ignite
spec:
image: weaveworks/ignite-ubuntu:18.04
name: server%d
name: controller%d
privileged: true
volumes:
- type: bind
Expand All @@ -16,6 +16,7 @@ machines:
destination: /var/lib/k0s
portMappings:
- containerPort: 22
hostPort: 9022
- containerPort: 443
- containerPort: 6443
- count: 1
Expand All @@ -32,4 +33,5 @@ machines:
destination: /var/lib/k0s
portMappings:
- containerPort: 22
hostPort: 9022
- containerPort: 6443
4 changes: 2 additions & 2 deletions examples/footloose/k0sctl.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: k0sctl.k0sproject.io/v1beta1
kind: cluster
spec:
hosts:
- role: server
- role: controller
ssh:
address: 127.0.0.1
port: 9022
Expand All @@ -11,4 +11,4 @@ spec:
address: 127.0.0.1
port: 9023
k0s:
version: 0.9.1
version: 0.11.0-beta1
18 changes: 9 additions & 9 deletions examples/hetzner-tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ variable "image" {
default = "ubuntu-18.04"
}

variable "server_type" {
variable "controller_type" {
default = "cx31"
}

variable "server_count" {
variable "controller_count" {
default = 1
}

Expand All @@ -42,17 +42,17 @@ variable "worker_type" {
default = "cx31"
}

resource "hcloud_server" "server" {
count = var.server_count
name = "${var.cluster_name}-server-${count.index}"
resource "hcloud_server" "controller" {
count = var.controller_count
name = "${var.cluster_name}-controller-${count.index}"
image = var.image
server_type = var.server_type
server_type = var.controller_type
ssh_keys = var.ssh_keys
location = var.location
labels = {
role = "server"
role = "controller"
}

}

resource "hcloud_server" "worker" {
Expand All @@ -73,7 +73,7 @@ locals {
kind = "cluster"
spec = {
hosts = [
for host in concat(hcloud_server.server, hcloud_server.worker) : {
for host in concat(hcloud_server.controller, hcloud_server.worker) : {
ssh = {
address = host.ipv4_address
user = "root"
Expand Down
9 changes: 8 additions & 1 deletion phase/gather_k0s_facts.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (p *GatherK0sFacts) investigateK0s(h *cluster.Host) error {
h.Metadata.K0sBinaryVersion = strings.TrimPrefix(output, "v")
log.Debugf("%s: has k0s binary version %s", h, h.Metadata.K0sBinaryVersion)

if h.Role == "server" && len(p.Config.Spec.K0s.Config) == 0 && h.Configurer.FileExist(h, h.K0sConfigPath()) {
if h.IsController() && len(p.Config.Spec.K0s.Config) == 0 && h.Configurer.FileExist(h, h.K0sConfigPath()) {
cfg, err := h.Configurer.ReadFile(h, h.K0sConfigPath())
if cfg != "" && err == nil {
log.Infof("%s: found existing configuration", h)
Expand All @@ -83,6 +83,13 @@ func (p *GatherK0sFacts) investigateK0s(h *cluster.Host) error {
return nil
}

switch status.Role {
case "server":
status.Role = "controller"
case "server+worker":
status.Role = "controller+worker"
}

if status.Role != h.Role {
return fmt.Errorf("%s: is configured as k0s %s but is already running as %s - role change is not supported", h, h.Role, status.Role)
}
Expand Down
2 changes: 1 addition & 1 deletion phase/get_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (p *GetKubeconfig) Run() error {
if err != nil {
return err
}
// the server admin.conf is aways pointing to localhost, thus we need to change the address
// the controller admin.conf is aways pointing to localhost, thus we need to change the address
// something usable from outside
a := p.Config.Spec.K0s.Config.DigString("spec", "api", "externalAddress")
if a == "" {
Expand Down
4 changes: 2 additions & 2 deletions smoke-test/k0sctl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: k0sctl.k0sproject.io/v1beta1
kind: cluster
spec:
hosts:
- role: server
- role: controller
ssh:
address: "127.0.0.1"
port: 9022
Expand All @@ -13,4 +13,4 @@ spec:
port: 9023
keyPath: ./id_rsa_k0s
k0s:
version: "0.10.0-beta3"
version: "0.11.0-beta1"

0 comments on commit ecb4577

Please sign in to comment.