From 3cd4cba88264dff17faa3c8beb3a4d26cbed5032 Mon Sep 17 00:00:00 2001
From: kaiyou <dev@kaiyou.fr>
Date: Fri, 25 Nov 2022 13:06:58 +0100
Subject: [PATCH] Many small fixes

---
 cmd/hepto.go              |  5 +++--
 pkg/cluster/kubeconfig.go |  7 ++++---
 pkg/cluster/services.go   | 22 ++++++++++++++++++----
 3 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/cmd/hepto.go b/cmd/hepto.go
index 81d6413..f0e59c7 100644
--- a/cmd/hepto.go
+++ b/cmd/hepto.go
@@ -29,14 +29,14 @@ func main() {
 		// mount with very simple very formatted arguments in that order:
 		//   mount -t tmpfs -o size=1234 /src /dst
 		err = unix.Mount(os.Args[5], os.Args[6], os.Args[2], 0, os.Args[4])
-	} else if bin == "containerd" || (bin == "exe" && len(os.Args) > 1 && os.Args[1] == "publish") {
+	} else if bin == "containerd" || (len(os.Args) > 1 && os.Args[1] == "publish") {
 		// Containerd is also available under hepto name, guess based on
 		// call arguments
 		// This is some of an edge case, where containerd uses os.Executable
 		// to get the current binary path (hence hepto single binary) then
 		// passes that path as -publish-binary to its shim for callback
 		err = containerd.App().Run(os.Args)
-	} else if bin == "containerd-shim-runc-v2" || (bin == "exe" && len(os.Args) > 1 && os.Args[1] == "-namespace") {
+	} else if bin == "containerd-shim-runc-v2" || (len(os.Args) > 1 && os.Args[1] == "-namespace") {
 		// Run the containerd shim
 		// It is also available under hepto name, for similar reasons as
 		// containerd, hence the different guess condition
@@ -46,6 +46,7 @@ func main() {
 		}
 		shimv2.RunManager(context.Background(), manager.NewShimManager("io.containerd.runc.v2"))
 	} else if bin == "runc" {
+    // Runc, as called by containerd shim
 		runc.Run()
 	} else if bin == "ctr" {
 		// Run containerd cli client, for debugging purposes
diff --git a/pkg/cluster/kubeconfig.go b/pkg/cluster/kubeconfig.go
index 6f1bdbf..e4d7530 100644
--- a/pkg/cluster/kubeconfig.go
+++ b/pkg/cluster/kubeconfig.go
@@ -113,13 +113,14 @@ version = 2
   address = "{{.Socket}}"
 
 [plugins."io.containerd.snapshotter.v1.overlayfs"]
-  root_path = "/overlay"
+  root_path = "{{.OverlayDir}}"
   upperdir_label = false
 `
 
 type ContainerdConfig struct {
-	RootDir string
-	Socket  string
+	RootDir    string
+	OverlayDir string
+	Socket     string
 }
 
 func (ContainerdConfig) Template() string {
diff --git a/pkg/cluster/services.go b/pkg/cluster/services.go
index 14045bd..bf5495c 100644
--- a/pkg/cluster/services.go
+++ b/pkg/cluster/services.go
@@ -14,6 +14,9 @@ import (
 
 const etcdPath = "/etcd"
 const binPath = "/bin"
+const containerdPath = "/containerd"
+const overlayPath = "/overlay"
+const imagePath = "/images"
 
 func (c *Cluster) watchService(ctx context.Context) {
 	<-ctx.Done()
@@ -49,7 +52,11 @@ func (c *Cluster) updateServices() {
 }
 
 func (c *Cluster) installBin() {
-	err := os.Setenv("PATH", binPath)
+	self, err := os.Executable()
+	if err != nil {
+		c.settings.Logger.Error(err, "could not get executable path")
+	}
+	err = os.Setenv("PATH", binPath)
 	if err != nil {
 		c.settings.Logger.Error(err, "could not set binaries path")
 		os.Exit(1)
@@ -60,7 +67,7 @@ func (c *Cluster) installBin() {
 		os.Exit(1)
 	}
 	for _, name := range []string{"kubectl", "ctr", "mount", "containerd-shim", "containerd-shim-runc-v2", "runc"} {
-		err = os.Symlink("/proc/1/exe", path.Join(binPath, name))
+		err = os.Symlink(self, path.Join(binPath, name))
 		if err != nil {
 			c.settings.Logger.Error(err, "could not install binary", "name", name)
 		}
@@ -152,12 +159,19 @@ func (c *Cluster) startK8sMaster() {
 }
 
 func (c *Cluster) startK8sNode(masterIP net.IP) {
+	// Create tmp, required for containerd
+	err := os.Mkdir("/tmp", 0o777)
+	if err != nil {
+		c.settings.Logger.Error(err, "failed to create /tmp")
+		os.Exit(1)
+	}
 	// Setup logging
 	wrappers.SetK8sLogger(c.settings.Logger)
 	// Containerd
 	containerdConfig := ContainerdConfig{
-		RootDir: "/containerd",
-		Socket:  "/containerd.sock",
+		RootDir:    containerdPath,
+		OverlayDir: overlayPath,
+		Socket:     "/run/containerd/containerd.sock",
 	}
 	containerdConfigPath := "/etc/containerd/config.toml"
 	c.WriteConfig(containerdConfig, containerdConfigPath)
-- 
GitLab