Skip to content

Commit 3957457

Browse files
committed
wip
1 parent 2c99bc8 commit 3957457

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

registry/node-update/main.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"bufio"
55
"context"
66
"fmt"
7+
"log"
78
"os"
89
"os/exec"
910
"time"
1011

12+
"github.com/containerd/nstools"
1113
"github.com/txn2/txeh"
1214

1315
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -22,6 +24,7 @@ const (
2224
prodRegistryIpEnvName = "PROD_REGISTRY_PORT_80_TCP_ADDR"
2325
buildsRegsitryIpEnvName = "BUILDS_REGISTRY_PORT_80_TCP_ADDR"
2426
containerdConfigPath = "/etc/containerd/config.toml"
27+
containerdTargetPid = 1
2528
)
2629

2730
func main() {
@@ -63,13 +66,21 @@ func main() {
6366
}
6467
fmt.Printf("containerd config updated\n")
6568

66-
fmt.Printf("restarting containerd\n")
67-
cmd := exec.Command("nsenter", "--mount=/proc/1/ns/mnt", "--", "systemctl", "restart", "containerd")
68-
output, err := cmd.CombinedOutput()
69-
if err != nil {
70-
panic(err.Error())
69+
fmt.Printf("containerd restart\n")
70+
if err = nstools.WithNamespaces(containerdTargetPid, func() error {
71+
// Code to run inside the namespace
72+
cmd := exec.Command("systemctl", "restart", "containerd")
73+
cmd.Stdin = os.Stdin
74+
cmd.Stdout = os.Stdout
75+
cmd.Stderr = os.Stderr
76+
if err := cmd.Run(); err != nil {
77+
log.Fatalf("Failed to run command in namespace: %v", err)
78+
}
79+
return nil
80+
}); err != nil {
81+
log.Fatalf("Failed to enter namespaces: %v", err)
7182
}
72-
fmt.Printf("restart output: %s", output)
83+
fmt.Printf("containerd restarted\n")
7384

7485
clientconfig, err := rest.InClusterConfig()
7586
if err != nil {

0 commit comments

Comments
 (0)