Skip to content

Commit 1b0ffd6

Browse files
committed
tear down if there is no site configured
1 parent 4973668 commit 1b0ffd6

File tree

3 files changed

+70
-23
lines changed

3 files changed

+70
-23
lines changed

internal/nonkube/bootstrap/teardown.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,7 @@ func Teardown(namespace string) error {
2222
if err != nil {
2323
return err
2424
}
25-
if err := removeRouter(namespace, platform); err != nil {
26-
return err
27-
}
28-
29-
if err := removeService(namespace, platform); err != nil {
30-
return err
31-
}
32-
33-
if err := removeDefinition(namespace); err != nil {
34-
return err
35-
}
36-
37-
fmt.Printf("Namespace \"%s\" has been removed\n", namespace)
38-
return nil
25+
return RemoveAll(namespace, platform)
3926

4027
}
4128

@@ -52,10 +39,19 @@ func removeDefinition(namespace string) error {
5239
func removeRouter(namespace string, platform string) error {
5340

5441
endpoint := os.Getenv("CONTAINER_ENDPOINT")
55-
if endpoint == "" {
56-
endpoint = fmt.Sprintf("unix://%s/podman/podman.sock", api.GetRuntimeDir())
42+
43+
// the container endpoint is mapped to the podman socket inside the container
44+
if api.IsRunningInContainer() {
45+
endpoint = "unix:///var/run/podman.sock"
5746
if platform == "docker" {
58-
endpoint = "unix:///run/docker.sock"
47+
endpoint = "unix:///var/run/docker.sock"
48+
}
49+
} else {
50+
if endpoint == "" {
51+
endpoint = fmt.Sprintf("unix://%s/podman/podman.sock", api.GetRuntimeDir())
52+
if platform == "docker" {
53+
endpoint = "unix:///run/docker.sock"
54+
}
5955
}
6056
}
6157

@@ -102,3 +98,22 @@ func removeService(namespace string, platform string) error {
10298

10399
return nil
104100
}
101+
102+
func RemoveAll(namespace string, platform string) error {
103+
104+
if err := removeRouter(namespace, platform); err != nil {
105+
return err
106+
}
107+
108+
if err := removeService(namespace, platform); err != nil {
109+
return err
110+
}
111+
112+
if err := removeDefinition(namespace); err != nil {
113+
return err
114+
}
115+
116+
fmt.Printf("Namespace \"%s\" has been removed\n", namespace)
117+
return nil
118+
119+
}

internal/nonkube/controller/input_resource_handler.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/skupperproject/skupper/api/types"
1111
"github.com/skupperproject/skupper/internal/cmd/skupper/common"
1212
"github.com/skupperproject/skupper/internal/nonkube/bootstrap"
13+
common2 "github.com/skupperproject/skupper/internal/nonkube/common"
1314
"github.com/skupperproject/skupper/internal/utils"
1415
"github.com/skupperproject/skupper/pkg/nonkube/api"
1516
)
@@ -22,11 +23,12 @@ type InputResourceHandler struct {
2223
inputPath string
2324
Bootstrap func(config *bootstrap.Config) (*api.SiteState, error)
2425
PostExec func(config *bootstrap.Config, siteState *api.SiteState)
26+
TearDown func(namespace string, platform string) error
2527
ConfigBootstrap bootstrap.Config
2628
lock sync.Mutex
2729
}
2830

29-
func NewInputResourceHandler(namespace string, inputPath string, bStrap func(config *bootstrap.Config) (*api.SiteState, error), postBootStrap func(config *bootstrap.Config, siteState *api.SiteState)) *InputResourceHandler {
31+
func NewInputResourceHandler(namespace string, inputPath string, bStrap func(config *bootstrap.Config) (*api.SiteState, error), postBootStrap func(config *bootstrap.Config, siteState *api.SiteState), tearDown func(namespace string, platform string) error) *InputResourceHandler {
3032

3133
systemReloadType := utils.DefaultStr(os.Getenv(types.ENV_SYSTEM_AUTO_RELOAD),
3234
types.SystemReloadTypeManual)
@@ -43,6 +45,7 @@ func NewInputResourceHandler(namespace string, inputPath string, bStrap func(con
4345

4446
handler.Bootstrap = bStrap
4547
handler.PostExec = postBootStrap
48+
handler.TearDown = tearDown
4649

4750
var binary string
4851

@@ -75,6 +78,9 @@ func NewInputResourceHandler(namespace string, inputPath string, bStrap func(con
7578
}
7679

7780
func (h *InputResourceHandler) OnCreate(name string) {
81+
h.lock.Lock()
82+
defer h.lock.Unlock()
83+
7884
h.logger.Info(fmt.Sprintf("Resource has been created: %s", name))
7985
err := h.processInputFile()
8086
if err != nil {
@@ -89,8 +95,27 @@ func (h *InputResourceHandler) OnCreate(name string) {
8995
// tries to create a new router pod, failing on this)
9096
func (h *InputResourceHandler) OnUpdate(name string) {}
9197
func (h *InputResourceHandler) OnRemove(name string) {
98+
h.lock.Lock()
99+
defer h.lock.Unlock()
100+
92101
h.logger.Info(fmt.Sprintf("Resource has been deleted: %s", name))
93-
err := h.processInputFile()
102+
103+
siteStateLoader := &common2.FileSystemSiteStateLoader{
104+
Path: h.ConfigBootstrap.InputPath,
105+
Bundle: h.ConfigBootstrap.IsBundle,
106+
}
107+
siteState, err := siteStateLoader.Load()
108+
109+
//If there is no site configured, the namespace needs to be removed
110+
if err != nil || siteState == nil || siteState.Site == nil {
111+
err = h.tearDownNamespace()
112+
if err != nil {
113+
h.logger.Error(err.Error())
114+
}
115+
return
116+
}
117+
118+
err = h.processInputFile()
94119
if err != nil {
95120
h.logger.Error(err.Error())
96121
}
@@ -102,9 +127,6 @@ func (h *InputResourceHandler) Filter(name string) bool {
102127
func (h *InputResourceHandler) OnBasePathAdded(basePath string) {}
103128

104129
func (h *InputResourceHandler) processInputFile() error {
105-
h.lock.Lock()
106-
defer h.lock.Unlock()
107-
108130
siteState, err := h.Bootstrap(&h.ConfigBootstrap)
109131
if err != nil {
110132
return fmt.Errorf("Failed to bootstrap: %s", err)
@@ -114,3 +136,13 @@ func (h *InputResourceHandler) processInputFile() error {
114136

115137
return nil
116138
}
139+
140+
func (h *InputResourceHandler) tearDownNamespace() error {
141+
h.logger.Info("No site configured, tearing down namespace")
142+
err := h.TearDown(h.namespace, string(h.ConfigBootstrap.Platform))
143+
if err != nil {
144+
return err
145+
}
146+
147+
return nil
148+
}

internal/nonkube/controller/namespace_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (w *NamespaceController) Start() {
4545
routerConfigHandler.AddCallback(routerStateHandler)
4646
collectorLifecycleHandler := NewCollectorLifecycleHandler(w.ns)
4747
routerStateHandler.SetCallback(collectorLifecycleHandler)
48-
inputResourceHandler := NewInputResourceHandler(w.ns, w.pathProvider.GetNamespace(), bootstrap.Bootstrap, bootstrap.PostBootstrap)
48+
inputResourceHandler := NewInputResourceHandler(w.ns, w.pathProvider.GetNamespace(), bootstrap.Bootstrap, bootstrap.PostBootstrap, bootstrap.RemoveAll)
4949

5050
w.watcher.Add(api.GetInternalOutputPath(w.ns, api.RouterConfigPath), routerConfigHandler)
5151
w.watcher.Add(api.GetInternalOutputPath(w.ns, api.RuntimeSiteStatePath), NewNetworkStatusHandler(w.ns))

0 commit comments

Comments
 (0)