Skip to content

Commit

Permalink
PE-5115 add cluster reset event (#95)
Browse files Browse the repository at this point in the history
* add cluster reset event

Signed-off-by: Nianyu Shen <[email protected]>

* bump go to 1.23

Signed-off-by: Nianyu Shen <[email protected]>

* fix uninstall script name

Signed-off-by: Nianyu Shen <[email protected]>

---------

Signed-off-by: Nianyu Shen <[email protected]>
  • Loading branch information
nianyush committed Sep 30, 2024
1 parent 1426d16 commit 4fcff11
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 132 deletions.
4 changes: 2 additions & 2 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ ARG BASE_IMAGE=quay.io/kairos/core-opensuse-leap:v2.3.2
ARG IMAGE_REPOSITORY=quay.io/kairos

ARG LUET_VERSION=0.35.1
ARG GOLINT_VERSION=v1.56.2
ARG GOLANG_VERSION=1.22
ARG GOLINT_VERSION=v1.61.0
ARG GOLANG_VERSION=1.23

ARG K3S_VERSION=latest
ARG BASE_IMAGE_NAME=$(echo $BASE_IMAGE | grep -o [^/]*: | rev | cut -c2- | rev)
Expand Down
13 changes: 4 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
module github.com/kairos-io/provider-k3s

go 1.22.5
go 1.23.1

require (
github.com/kairos-io/kairos-sdk v0.4.2
github.com/mudler/yip v1.9.4
github.com/onsi/gomega v1.34.2
github.com/kairos-io/kairos-sdk v0.5.0
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5
github.com/mudler/yip v1.10.0
github.com/sirupsen/logrus v1.9.3
gopkg.in/yaml.v3 v3.0.1
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/itchyny/gojq v0.12.16 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/twpayne/go-vfs/v4 v4.3.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ github.com/itchyny/gojq v0.12.16 h1:yLfgLxhIr/6sJNVmYfQjTIv0jGctu6/DgDoivmxTr7g=
github.com/itchyny/gojq v0.12.16/go.mod h1:6abHbdC2uB9ogMS38XsErnfqJ94UlngIJGlRAIj4jTM=
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg=
github.com/kairos-io/kairos-sdk v0.4.2 h1:tq0a6R9qSotatFt20q/+oIHJSN+XTQWji4XgHk5Vvlw=
github.com/kairos-io/kairos-sdk v0.4.2/go.mod h1:1x8VaVt1ppXxQUlNyDRksoDglR423hxfEtjx6qWDO7w=
github.com/kairos-io/kairos-sdk v0.5.0 h1:CtYz2WXe5huKkjPSro2J1kiLVjDr6T9909uckK17Bqw=
github.com/kairos-io/kairos-sdk v0.5.0/go.mod h1:RO5ad3gCd2O2zJE5mEnPHLUy/+fRKKQL4fe7xV6FtyA=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -51,8 +51,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5 h1:FaZD86+A9mVt7lh9glAryzQblMsbJYU2VnrdZ8yHlTs=
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5/go.mod h1:WmKcT8ONmhDQIqQ+HxU+tkGWjzBEyY/KFO8LTGCu4AI=
github.com/mudler/yip v1.9.4 h1:yaiPKWG5kt/DTNCf7ZGfyWdb1j5c06zYqWF3F+SVKsE=
github.com/mudler/yip v1.9.4/go.mod h1:nqf8JFCq7a7rIkm7cSs+SOc8QbiyvVJ/xLbUw4GgzFs=
github.com/mudler/yip v1.10.0 h1:MwEIySEfSRRwTUz2BmQQpRn6+M7jqVGf/OldsepBvz0=
github.com/mudler/yip v1.10.0/go.mod h1:gwH7iGcr1Jimox2xKtN2AprEO00GzY7smvuycqCL7+Y=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY=
github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc=
Expand Down Expand Up @@ -127,8 +127,6 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 h1:POO/ycCATvegFmVuPpQzZFJ+pGZeX22Ufu6fibxDVjU=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/kairos-io/kairos-sdk/clusterplugin"
"github.com/mudler/go-pluggable"
"github.com/sirupsen/logrus"

"github.com/kairos-io/provider-k3s/pkg/provider"
Expand All @@ -20,7 +21,10 @@ func main() {
Provider: provider.ClusterProvider,
}

if err := plugin.Run(); err != nil {
if err := plugin.Run(pluggable.FactoryPlugin{
EventType: clusterplugin.EventClusterReset,
PluginHandler: provider.HandleClusterReset,
}); err != nil {
logrus.Fatal(err)
}
}
12 changes: 0 additions & 12 deletions pkg/provider/mount.tmpl

This file was deleted.

51 changes: 0 additions & 51 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provider

import (
"bytes"
"encoding/json"
"fmt"

Expand All @@ -16,16 +15,11 @@ import (
kyaml "sigs.k8s.io/yaml"

_ "embed"
"text/template"

"github.com/kairos-io/provider-k3s/api"
"github.com/kairos-io/provider-k3s/pkg/constants"
"github.com/kairos-io/provider-k3s/pkg/types"
)

//go:embed mount.tmpl
var mountTemplate string

const (
configurationPath = "/etc/rancher/k3s/config.d"
containerdEnvConfigPath = "/etc/default"
Expand Down Expand Up @@ -143,55 +137,10 @@ func parseFiles(cluster clusterplugin.Cluster, systemName string) []yip.File {
return files
}

func rootPathMountStage(rootPath string) yip.Stage {
mps := []types.MountPoint{
{
Name: "etc-rancher",
Source: filepath.Join(rootPath, "etc/rancher"),
Target: "/etc/rancher",
},
{
Name: "var-lib-rancher",
Source: filepath.Join(rootPath, "var/lib/rancher"),
Target: "/var/lib/rancher",
},
}

stage := yip.Stage{
Name: "Mount K3s data, conf directories",
}
for _, mp := range mps {
stage.Files = append(stage.Files, yip.File{
Path: filepath.Join(constants.RunSystemdSystemDir, fmt.Sprintf("%s.mount", mp.Name)),
Permissions: 0644,
Content: parseMountUnitFile(mp),
})

stage.Commands = append(stage.Commands,
fmt.Sprintf("mkdir -p %s", mp.Source),
fmt.Sprintf("mkdir -p %s", mp.Target),
fmt.Sprintf("systemctl enable --now %s.mount", mp.Name),
)
}

return stage
}

func parseMountUnitFile(mp types.MountPoint) string {
mount, _ := template.New("mount").Parse(mountTemplate)
var buf bytes.Buffer
_ = mount.Execute(&buf, mp)
return buf.String()
}

func parseStages(cluster clusterplugin.Cluster, files []yip.File, systemName string) []yip.Stage {
var stages []yip.Stage
clusterRootPath := getClusterRootPath(cluster)

if len(clusterRootPath) > 0 && clusterRootPath != "/" {
stages = append(stages, rootPathMountStage(clusterRootPath))
}

stages = append(stages, yip.Stage{
Name: constants.InstallK3sConfigFiles,
Files: files,
Expand Down
51 changes: 0 additions & 51 deletions pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"testing"

"github.com/kairos-io/kairos-sdk/clusterplugin"
yip "github.com/mudler/yip/pkg/schema"
"github.com/onsi/gomega"
)

func Test_parseOptions(t *testing.T) {
Expand Down Expand Up @@ -89,52 +87,3 @@ func Test_parseOptions(t *testing.T) {
})
}
}

func Test_rootPathMountStage(t *testing.T) {
gomega.RegisterTestingT(t)

type args struct {
rootPath string
}
tests := []struct {
name string
args args
want yip.Stage
}{
{
name: "custom root path",
args: args{
rootPath: "/writable",
},
want: yip.Stage{
Name: "Mount K3s data, conf directories",
Commands: []string{
"mkdir -p /writable/etc/rancher",
"mkdir -p /etc/rancher",
"systemctl enable --now etc-rancher.mount",
"mkdir -p /writable/var/lib/rancher",
"mkdir -p /var/lib/rancher",
"systemctl enable --now var-lib-rancher.mount",
},
Files: []yip.File{
{
Path: "/run/systemd/system/etc-rancher.mount",
Permissions: 0644,
Content: "[Unit]\nDescription=etc-rancher mount unit\nBefore=local-fs.target k3s.service k3s-agent.service\n\n[Mount]\nWhat=/writable/etc/rancher\nWhere=/etc/rancher\nType=none\nOptions=bind\n\n[Install]\nWantedBy=local-fs.target\n",
},
{
Path: "/run/systemd/system/var-lib-rancher.mount",
Permissions: 0644,
Content: "[Unit]\nDescription=var-lib-rancher mount unit\nBefore=local-fs.target k3s.service k3s-agent.service\n\n[Mount]\nWhat=/writable/var/lib/rancher\nWhere=/var/lib/rancher\nType=none\nOptions=bind\n\n[Install]\nWantedBy=local-fs.target\n",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := rootPathMountStage(tt.args.rootPath)
gomega.Expect(got).To(gomega.Equal(tt.want))
})
}
}
52 changes: 52 additions & 0 deletions pkg/provider/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package provider

import (
"encoding/json"
"fmt"
"os/exec"
"path/filepath"

"github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/clusterplugin"
"github.com/mudler/go-pluggable"
"gopkg.in/yaml.v3"
)

func HandleClusterReset(event *pluggable.Event) pluggable.EventResponse {
var payload bus.EventPayload
var config clusterplugin.Config
var response pluggable.EventResponse

// parse the boot payload
if err := json.Unmarshal([]byte(event.Data), &payload); err != nil {
response.Error = fmt.Sprintf("failed to parse boot event: %s", err.Error())
return response
}

// parse config from boot payload
if err := yaml.Unmarshal([]byte(payload.Config), &config); err != nil {
response.Error = fmt.Sprintf("failed to parse config from boot event: %s", err.Error())
return response
}

if config.Cluster == nil {
return response
}

clusterRootPath := getClusterRootPath(*config.Cluster)

var uninstallScript string
if config.Cluster.Role == clusterplugin.RoleWorker {
uninstallScript = "k3s-agent-uninstall.sh"
} else {
uninstallScript = "k3s-uninstall.sh"
}

cmd := exec.Command("/bin/sh", "-c", filepath.Join(clusterRootPath, "/opt/k3s/scripts", uninstallScript))
output, err := cmd.CombinedOutput()
if err != nil {
response.Error = fmt.Sprintf("failed to reset cluster: %s", string(output))
}

return response
}

0 comments on commit 4fcff11

Please sign in to comment.