Skip to content

Commit

Permalink
feat: support custom cluster root path
Browse files Browse the repository at this point in the history
Signed-off-by: Nianyu Shen <[email protected]>
  • Loading branch information
nianyush committed Sep 23, 2024
1 parent f477d95 commit 61e5e7d
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 241 deletions.
46 changes: 19 additions & 27 deletions .github/workflows/provider-packaging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,33 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker-practice/actions-setup-docker@master
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@master
with:
platforms: all
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@master
- uses: earthly/actions-setup@v1
with:
version: "v0.6.30"
version: "latest"
- run: earthly --ci +lint
build-provider-package:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- uses: actions/checkout@v2
- uses: docker-practice/actions-setup-docker@master
- uses: earthly/actions-setup@v1
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@master
with:
version: "v0.6.30"
- uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- run: earthly --ci --push +provider-package-all-platforms --IMAGE_REPOSITORY=ghcr.io/kairos-io
build-provider-fips-package:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- uses: actions/checkout@v2
- uses: docker-practice/actions-setup-docker@master
platforms: all
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@master
- uses: earthly/actions-setup@v1
with:
version: "v0.6.30"
- uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- run: earthly --ci --push +provider-fips-package-all-platforms --IMAGE_REPOSITORY=ghcr.io/kairos-io --FIPS_ENABLED=true
version: "latest"
- run: echo "${{ secrets.GCR_JSON_KEY_B64 }}" | base64 -d | docker login -u _json_key --password-stdin https://gcr.io
- run: earthly --ci --push +provider-package-all-platforms --IMAGE_REPOSITORY=gcr.io/spectro-dev-public/kairos-io
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# install pre-commit on your system and then
# run pre-commit install in this repository.
# You can by pass commit hooks with:
# git commit -n
repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v2.4.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
- repo: https://github.com/tekwizely/pre-commit-golang
rev: v1.0.0-rc.1
hooks:
- id: go-mod-tidy
- repo: https://github.com/golangci/golangci-lint
rev: v1.61.0
hooks:
- id: golangci-lint
name: golangci-lint
description: Fast linters runner for Go. Note that only modified files are linted, so linters like 'unused' that need to scan all files won't work as expected.
entry: golangci-lint run --new-from-rev HEAD --whole-files -v
types: [go]
language: golang
require_serial: true
pass_filenames: false
verbose: true
2 changes: 1 addition & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ VERSION:

COPY . ./

RUN echo $(git describe --exact-match --tags || echo "v0.0.0-$(git log --oneline -n 1 | cut -d" " -f1)") > VERSION
RUN echo $(git describe --exact-match --tags || echo "v0.0.0-$(git rev-parse --short=8 HEAD)") > VERSION

SAVE ARTIFACT VERSION VERSION

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
213 changes: 2 additions & 211 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,223 +1,14 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"net"
"path/filepath"
"strings"

"github.com/c3os-io/c3os/provider-rke2/pkg/provider"
"github.com/kairos-io/kairos-sdk/clusterplugin"
yip "github.com/mudler/yip/pkg/schema"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
kyaml "sigs.k8s.io/yaml"
)

const (
configurationPath = "/etc/rancher/rke2/config.d"
containerdEnvConfigPath = "/etc/default"

serverSystemName = "rke2-server"
agentSystemName = "rke2-agent"
K8SNoProxy = ".svc,.svc.cluster,.svc.cluster.local"
localImagesPath = "/opt/content/images"
)

type RKE2Config struct {
Token string `yaml:"token"`
Server string `yaml:"server"`
TLSSan []string `yaml:"tls-san"`
}

func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {

var importStage yip.Stage
rke2Config := RKE2Config{
Token: cluster.ClusterToken,
// RKE2 server listens on 9345 for node registration https://docs.rke2.io/install/quickstart/#3-configure-the-rke2-agent-service
Server: fmt.Sprintf("https://%s:9345", cluster.ControlPlaneHost),
TLSSan: []string{
cluster.ControlPlaneHost,
},
}

if cluster.Role == clusterplugin.RoleInit {
rke2Config.Server = ""

}

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(&rke2Config)

userOptions, _ := kyaml.YAMLToJSON([]byte(cluster.Options))
options, _ := kyaml.YAMLToJSON(providerConfig.Bytes())

proxyValues := proxyEnv(userOptions, cluster.Env)

files := []yip.File{
{
Path: filepath.Join(configurationPath, "90_userdata.yaml"),
Permissions: 0400,
Content: string(userOptions),
},
{
Path: filepath.Join(configurationPath, "99_userdata.yaml"),
Permissions: 0400,
Content: string(options),
},
}

if len(proxyValues) > 0 {
files = append(files, yip.File{
Path: filepath.Join(containerdEnvConfigPath, systemName),
Permissions: 0400,
Content: proxyValues,
})
}

stages := []yip.Stage{
{
Name: "Install RKE2 Configuration Files",
Files: files,

Commands: []string{
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/rke2/config.yaml", configurationPath),
},
},
}

if cluster.ImportLocalImages {
if cluster.LocalImagesPath == "" {
cluster.LocalImagesPath = localImagesPath
}

importStage = yip.Stage{
Commands: []string{
fmt.Sprintf("/bin/sh /opt/rke2/scripts/import.sh %s > /var/log/import.log", cluster.LocalImagesPath),
},
If: fmt.Sprintf("[ -d %s ]", cluster.LocalImagesPath),
}
stages = append(stages, importStage)
}

stages = append(stages,
yip.Stage{
Name: "Waiting to finish extracting content",
Commands: []string{
"sleep 120",
},
},
yip.Stage{
Name: "Enable Systemd Services",
Commands: []string{
fmt.Sprintf("systemctl enable %s", systemName),
fmt.Sprintf("systemctl restart %s", systemName),
},
})

cfg := yip.YipConfig{
Name: "RKE2 Kairos Cluster Provider",
Stages: map[string][]yip.Stage{
"boot.before": stages,
},
}

return cfg
}

func proxyEnv(userOptions []byte, proxyMap map[string]string) string {
var proxy []string
var noProxy string
var isProxyConfigured bool

httpProxy := proxyMap["HTTP_PROXY"]
httpsProxy := proxyMap["HTTPS_PROXY"]
userNoProxy := proxyMap["NO_PROXY"]
defaultNoProxy := getDefaultNoProxy(userOptions)

if len(httpProxy) > 0 {
proxy = append(proxy, fmt.Sprintf("HTTP_PROXY=%s", httpProxy))
proxy = append(proxy, fmt.Sprintf("CONTAINERD_HTTP_PROXY=%s", httpProxy))
isProxyConfigured = true
}

if len(httpsProxy) > 0 {
proxy = append(proxy, fmt.Sprintf("HTTPS_PROXY=%s", httpsProxy))
proxy = append(proxy, fmt.Sprintf("CONTAINERD_HTTPS_PROXY=%s", httpsProxy))
isProxyConfigured = true
}

if isProxyConfigured {
noProxy = defaultNoProxy
}

if len(userNoProxy) > 0 {
noProxy = noProxy + "," + userNoProxy
}

if len(noProxy) > 0 {
proxy = append(proxy, fmt.Sprintf("NO_PROXY=%s", noProxy))
proxy = append(proxy, fmt.Sprintf("CONTAINERD_NO_PROXY=%s", noProxy))
}

return strings.Join(proxy, "\n")
}

func getDefaultNoProxy(userOptions []byte) string {

var noProxy string

data := make(map[string]interface{})
err := json.Unmarshal(userOptions, &data)
if err != nil {
fmt.Println("error while unmarshalling user options", err)
}

if data != nil {
clusterCIDR := data["cluster-cidr"].(string)
serviceCIDR := data["service-cidr"].(string)

if len(clusterCIDR) > 0 {
noProxy = noProxy + "," + clusterCIDR
}
if len(serviceCIDR) > 0 {
noProxy = noProxy + "," + serviceCIDR
}
noProxy = noProxy + "," + getNodeCIDR() + "," + K8SNoProxy
}

return noProxy
}

func getNodeCIDR() string {
addrs, _ := net.InterfaceAddrs()
var result string
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
result = addr.String()
break
}
}
}
return result
}

func main() {
plugin := clusterplugin.ClusterPlugin{
Provider: clusterProvider,
Provider: provider.ClusterProvider,
}

if err := plugin.Run(); err != nil {
Expand Down
16 changes: 16 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package constants

const (
ConfigurationPath = "/etc/rancher/rke2/config.d"
ContainerdEnvConfigPath = "/etc/default"

ServerSystemName = "rke2-server"
AgentSystemName = "rke2-agent"
K8SNoProxy = ".svc,.svc.cluster,.svc.cluster.local"
LocalImagesPath = "/opt/content/images"
)

const (
ClusterRootPath = "cluster_root_path"
RunSystemdSystemDir = "/run/systemd/system"
)
12 changes: 12 additions & 0 deletions pkg/provider/mount.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description={{.Name}} mount unit
Before=local-fs.target k3s.service k3s-agent.service

[Mount]
What={{.Source}}
Where={{.Target}}
Type=none
Options=bind

[Install]
WantedBy=local-fs.target
Loading

0 comments on commit 61e5e7d

Please sign in to comment.