@@ -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
7780func (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)
9096func (h * InputResourceHandler ) OnUpdate (name string ) {}
9197func (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 {
102127func (h * InputResourceHandler ) OnBasePathAdded (basePath string ) {}
103128
104129func (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+ }
0 commit comments