Skip to content

Commit

Permalink
⚙️ Add manual k3s configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mudler committed May 6, 2022
1 parent 21ff2d9 commit c8b7c42
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 37 deletions.
11 changes: 10 additions & 1 deletion cli/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ func agent(apiAddress string, dir []string, force bool) error {

l := logging.Logger("c3os")

if c.C3OS == nil || c.C3OS.NetworkToken == "" {
tokenNotDefined := (c.C3OS != nil && c.C3OS.NetworkToken == "")

if c.C3OS == nil && !c.K3s.Enabled && !c.K3sAgent.Enabled {
fmt.Println("No c3os/k3s configuration provided, exiting.")
return nil
}

if tokenNotDefined && (c.K3s.Enabled || c.K3sAgent.Enabled) {
return oneTimeBootstrap(c)
} else if tokenNotDefined {
fmt.Println("No network token provided, exiting.")
return nil
}
Expand Down
1 change: 1 addition & 0 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type K3s struct {
ReplaceEnv bool `yaml:"replace_env,omitempty"`
ReplaceArgs bool `yaml:"replace_args,omitempty"`
Args []string `yaml:"args,omitempty"`
Enabled bool `yaml:"enabled,omitempty"`
}

type Config struct {
Expand Down
73 changes: 73 additions & 0 deletions cli/onetimebootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"strings"

"github.com/c3os-io/c3os/cli/config"
"github.com/c3os-io/c3os/cli/machine"
"github.com/c3os-io/c3os/cli/machine/openrc"
"github.com/c3os-io/c3os/cli/machine/systemd"
role "github.com/c3os-io/c3os/cli/role"
"github.com/c3os-io/c3os/cli/utils"
)

func oneTimeBootstrap(c *config.Config) error {
if role.SentinelExist() {
fmt.Println("Sentinel exists, nothing to do. exiting.")
return nil
}

var svc machine.Service
k3sConfig := config.K3s{}
svcName := "k3s"
svcRole := "server"

if c.K3s.Enabled {
k3sConfig = c.K3s
} else if c.K3sAgent.Enabled {
k3sConfig = c.K3sAgent
svcName = "k3s-agent"
svcRole = "agent"
}

if utils.IsOpenRCBased() {
svc, _ = openrc.NewService(
openrc.WithName(svcName),
)
} else {
svc, _ = systemd.NewService(
systemd.WithName(svcName),
)
}

envFile := fmt.Sprintf("/etc/sysconfig/%s", svcName)
if svc == nil {
return fmt.Errorf("could not detect OS")
}

// Setup systemd unit and starts it
if err := utils.WriteEnv(envFile,
k3sConfig.Env,
); err != nil {
return err
}

if err := svc.OverrideCmd(fmt.Sprintf("/usr/bin/k3s %s %s", svcRole, strings.Join(k3sConfig.Args, " "))); err != nil {
return err
}

if err := svc.SetEnvFile(envFile); err != nil {
return err
}

if err := svc.Start(); err != nil {
return err
}

if err := svc.Enable(); err != nil {
return err
}

return role.CreateSentinel()
}
17 changes: 11 additions & 6 deletions cli/role/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,19 @@ func Master(cc *config.Config) Role {
return err
}

k3sConfig := config.K3s{}
if cc.K3s.Enabled {
k3sConfig = cc.K3s
}

env := map[string]string{}
if !cc.K3s.ReplaceEnv {
if !k3sConfig.ReplaceEnv {
// Override opts with user-supplied
for k, v := range cc.K3s.Env {
for k, v := range k3sConfig.Env {
env[k] = v
}
} else {
env = cc.K3s.Env
env = k3sConfig.Env
}

// Setup systemd unit and starts it
Expand All @@ -98,10 +103,10 @@ func Master(cc *config.Config) Role {
}

args := []string{"--flannel-iface=edgevpn0"}
if cc.K3s.ReplaceArgs {
args = cc.K3s.Args
if k3sConfig.ReplaceArgs {
args = k3sConfig.Args
} else {
args = append(args, cc.K3s.Args...)
args = append(args, k3sConfig.Args...)
}

if err := svc.OverrideCmd(fmt.Sprintf("/usr/bin/k3s server %s", strings.Join(args, " "))); err != nil {
Expand Down
17 changes: 11 additions & 6 deletions cli/role/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,23 @@ func Worker(cc *config.Config) Role {
return err
}

k3sConfig := config.K3s{}
if cc.K3sAgent.Enabled {
k3sConfig = cc.K3sAgent
}

env := map[string]string{
"K3S_URL": fmt.Sprintf("https://%s:6443", masterIP),
"K3S_TOKEN": nodeToken,
}

if !cc.K3sAgent.ReplaceEnv {
if !k3sConfig.ReplaceEnv {
// Override opts with user-supplied
for k, v := range cc.K3sAgent.Env {
for k, v := range k3sConfig.Env {
env[k] = v
}
} else {
env = cc.K3sAgent.Env
env = k3sConfig.Env
}

// Setup systemd unit and starts it
Expand All @@ -82,10 +87,10 @@ func Worker(cc *config.Config) Role {
fmt.Sprintf("--node-ip %s", ip),
"--flannel-iface=edgevpn0",
}
if cc.K3sAgent.ReplaceArgs {
args = cc.K3sAgent.Args
if k3sConfig.ReplaceArgs {
args = k3sConfig.Args
} else {
args = append(args, cc.K3sAgent.Args...)
args = append(args, k3sConfig.Args...)
}

if err := svc.OverrideCmd(fmt.Sprintf("/usr/bin/k3s agent %s", strings.Join(args, " "))); err != nil {
Expand Down
31 changes: 29 additions & 2 deletions docs/content/installation/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ c3os:

## Manual K3s configuration

Automatic nodes configuration can be disabled by not specifying a `network_token` in the configuration file.
Automatic nodes configuration can be disabled by disabling the `c3os` block in the configuration file.

In that case no VPN and either k3s is configured automatically, see also the [examples](https://github.com/c3os-io/c3os/tree/master/examples) folder in the repository to configure k3s manually.
In that case, VPN is not configured, but you can still configure k3s automatically with the `k3s` and `k3s-agent` block:

```yaml
k3s:
enabled: true
# Additional env/args for k3s server instances
env:
K3S_RESOLV_CONF: ""
K3S_DATASTORE_ENDPOINT: "mysql://username:password@tcp(hostname:3306)/database-name"
args:
- --cluster-init
```
for agent:
```yaml
k3s-agent:
enabled: true
# Additional env/args for k3s server instances
env:
K3S_RESOLV_CONF: ""
K3S_DATASTORE_ENDPOINT: "mysql://username:password@tcp(hostname:3306)/database-name"
args:
- --cluster-init
```
See also the [examples](https://github.com/c3os-io/c3os/tree/master/examples) folder in the repository to configure k3s manually.
2 changes: 2 additions & 0 deletions examples/k3s-agent.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file shows a complete manual cloud-config file.
# The following starts and enables the k3s services manually by leveraging systemctl
name: "Default deployment"
stages:
network:
Expand Down
30 changes: 8 additions & 22 deletions examples/k3s-server.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
name: "Default deployment"
# The following configuration
# configures k3s manually with the c3os configuration mechanism.

stages:
network:
- if: '[ ! -f "/run/cos/recovery_mode" ]'
name: "Setup k3s"
environment_file: "/etc/systemd/system/k3s.service.env"
environment:
FOO: "bar"
systemctl:
start:
- k3s
- commands:
- |
chmod 600 /etc/systemd/system/k3s.service.env
initramfs:
# Disable password, enable only ssh access
- if: '[ ! -d "/home/c3os" ]'
name: "Ensure home directory is present"
commands:
- mkdir /home/c3os
- chmod 755 /home/c3os
- chown -R c3os /home/c3os
- name: "Setup users"
# users:
# root:
# c3os:
# password: "c3os"
authorized_keys:
c3os:
- github:mudler
commands:
- passwd -l c3os
- passwd -l c3os

k3s:
enabled: true

0 comments on commit c8b7c42

Please sign in to comment.