provider-k3s/main.go

118 lines
3.0 KiB
Go
Raw Normal View History

2022-08-08 15:55:32 +00:00
package main
import (
"bytes"
"fmt"
"github.com/c3os-io/c3os/sdk/clusterplugin"
yip "github.com/mudler/yip/pkg/schema"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"path/filepath"
2022-08-08 23:57:37 +00:00
kyaml "sigs.k8s.io/yaml"
2022-08-08 15:55:32 +00:00
)
const (
configurationPath = "/etc/rancher/k3s/config.d"
serverSystemName = "k3s"
agentSystemName = "k3s-agent"
)
type K3sConfig struct {
ClusterInit bool `yaml:"cluster-init,omitempty"`
Token string `yaml:"token,omitempty"`
Server string `yaml:"server,omitempty"`
TLSSan []string `yaml:"tls-san,omitempty"`
2022-08-08 15:55:32 +00:00
}
func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {
k3sConfig := K3sConfig{
Token: cluster.ClusterToken,
2022-08-08 15:55:32 +00:00
}
switch cluster.Role {
case clusterplugin.RoleInit:
2022-08-08 23:57:37 +00:00
k3sConfig.ClusterInit = true
k3sConfig.TLSSan = []string{cluster.ControlPlaneHost}
case clusterplugin.RoleControlPlane:
k3sConfig.Server = fmt.Sprintf("https://%s:6443", cluster.ControlPlaneHost)
k3sConfig.TLSSan = []string{cluster.ControlPlaneHost}
case clusterplugin.RoleWorker:
k3sConfig.Server = fmt.Sprintf("https://%s:6443", cluster.ControlPlaneHost)
2022-08-08 23:57:37 +00:00
}
2022-08-08 15:55:32 +00:00
systemName := serverSystemName
if cluster.Role == clusterplugin.RoleWorker {
systemName = agentSystemName
}
// ensure we always have a valid user config
if cluster.Options == "" {
cluster.Options = "{}"
}
var providerConfig bytes.Buffer
_ = yaml.NewEncoder(&providerConfig).Encode(&k3sConfig)
2022-08-08 23:57:37 +00:00
userOptions, _ := kyaml.YAMLToJSON([]byte(cluster.Options))
options, _ := kyaml.YAMLToJSON(providerConfig.Bytes())
2022-08-08 15:55:32 +00:00
cfg := yip.YipConfig{
Name: "K3s C3OS Cluster Provider",
Stages: map[string][]yip.Stage{
"boot.before": {
{
Name: "Install K3s Configuration Files",
Files: []yip.File{
{
Path: filepath.Join(configurationPath, "90_userdata.yaml"),
Permissions: 0400,
2022-08-08 23:57:37 +00:00
Content: string(userOptions),
2022-08-08 15:55:32 +00:00
},
{
Path: filepath.Join(configurationPath, "99_userdata.yaml"),
Permissions: 0400,
2022-08-08 23:57:37 +00:00
Content: string(options),
2022-08-08 15:55:32 +00:00
},
},
2022-08-08 23:57:37 +00:00
Commands: []string{
2022-09-14 18:59:30 +00:00
fmt.Sprintf("jq -s 'def flatten: reduce .[] as $i([]; if $i | type == \"array\" then . + ($i | flatten) else . + [$i] end); [.[] | to_entries] | flatten | reduce .[] as $dot ({}; .[$dot.key] += $dot.value)' %s/*.yaml > /etc/rancher/k3s/config.yaml", configurationPath),
2022-08-08 23:57:37 +00:00
},
2022-08-08 15:55:32 +00:00
},
{
Name: "Enable OpenRC Services",
If: "[ -x /sbin/openrc-run ]",
Commands: []string{
fmt.Sprintf("rc-update add %s default >/dev/null", systemName),
fmt.Sprintf("service %s start", systemName),
2022-08-08 15:55:32 +00:00
},
},
{
Name: "Enable Systemd Services",
If: "[ -x /bin/systemctl ]",
Systemctl: yip.Systemctl{
Enable: []string{
systemName,
},
Start: []string{
systemName,
},
},
},
},
},
}
return cfg
}
func main() {
plugin := clusterplugin.ClusterPlugin{
Provider: clusterProvider,
}
if err := plugin.Run(); err != nil {
logrus.Fatal(err)
}
}