Repository: flant/shell-operator
Pull request #100 (2020-01-22 05:36:01)

fix: make temp files unique and clean them after hook execution
- rename includeKubernetesSnapshots to includeSnapshots


Patch:
diff --git a/pkg/app/app.go b/pkg/app/app.go
index 3262e36..ea49dca 100644
--- a/pkg/app/app.go
+++ b/pkg/app/app.go
@@ -12,7 +12,7 @@ var AppDescription = "Run your custom cluster-wide scripts in reaction to Kubern
 
 var Version = "dev"
 
-var DebugMessages = "no"
+var DebugKeepTmpFiles = "no"
 
 var WorkingDir = ""
 var TempDir = "/tmp/shell-operator"
@@ -30,9 +30,9 @@ var LogType = "text"
 // SetupGlobalSettings init global flags with default values
 func SetupGlobalSettings(kpApp *kingpin.Application) {
 	kpApp.Flag("debug", "set to yes to turn on debug messages").
-		Envar("SHELL_OPERATOR_DEBUG").
-		Default(DebugMessages).
-		StringVar(&DebugMessages)
+		Envar("DEBUG_KEEP_TMP_FILES").
+		Default(DebugKeepTmpFiles).
+		StringVar(&DebugKeepTmpFiles)
 
 	kpApp.Flag("working-dir", "a path to a hooks file structure").
 		Envar("SHELL_OPERATOR_WORKING_DIR").
diff --git a/pkg/hook/binding_context/binding_context.go b/pkg/hook/binding_context/binding_context.go
index bf5bee3..1f807b3 100644
--- a/pkg/hook/binding_context/binding_context.go
+++ b/pkg/hook/binding_context/binding_context.go
@@ -53,14 +53,10 @@ func (bc BindingContext) MapV1() map[string]interface{} {
 	}
 
 	if len(bc.Metadata.IncludeSnapshots) > 0 || bc.Metadata.IncludeAllSnapshots {
-		key := "kubernetesSnapshots"
-		if bc.Metadata.BindingType == OnKubernetesEvent {
-			key = "snapshots"
-		}
 		if len(bc.Snapshots) > 0 {
-			res[key] = bc.Snapshots
+			res["snapshots"] = bc.Snapshots
 		} else {
-			res[key] = map[string]string{}
+			res["snapshots"] = map[string]string{}
 		}
 	}
 
diff --git a/pkg/hook/config/schemas.go b/pkg/hook/config/schemas.go
index 4a26375..846a1a5 100644
--- a/pkg/hook/config/schemas.go
+++ b/pkg/hook/config/schemas.go
@@ -91,7 +91,7 @@ properties:
         allowFailure:
           type: boolean
           default: false
-        includeKubernetesSnapshotsFrom:
+        includeSnapshotsFrom:
           type: array
           additionalItems: false
           minItems: 1
diff --git a/pkg/hook/controller/hook_controller.go b/pkg/hook/controller/hook_controller.go
index 7edd705..c02c04c 100644
--- a/pkg/hook/controller/hook_controller.go
+++ b/pkg/hook/controller/hook_controller.go
@@ -141,7 +141,7 @@ func (hc *hookController) HandleScheduleEvent(crontab string, createTasksFn func
 		infos := hc.ScheduleController.HandleEvent(crontab)
 		for _, info := range infos {
 			if createTasksFn != nil {
-				// Inject IncludeKubernetesSnapshots to BindingContext
+				// Inject IncludeSnapshots to BindingContext
 				if hc.KubernetesController != nil && len(info.BindingContext) > 0 && len(info.IncludeSnapshots) > 0 {
 					newBc := info.BindingContext[0]
 					newBc.Snapshots = hc.KubernetesController.SnapshotsFrom(info.IncludeSnapshots...)
@@ -203,7 +203,7 @@ func (hc *hookController) KubernetesSnapshotsFor(bindingType BindingType, bindin
 	case Schedule:
 		for _, binding := range hc.scheduleBindings {
 			if bindingName == binding.BindingName {
-				includeSnapshots = binding.IncludeKubernetesSnapshotsFrom
+				includeSnapshots = binding.IncludeSnapshotsFrom
 				break
 			}
 		}
diff --git a/pkg/hook/controller/schedule_bindings_controller.go b/pkg/hook/controller/schedule_bindings_controller.go
index a467836..218d89c 100644
--- a/pkg/hook/controller/schedule_bindings_controller.go
+++ b/pkg/hook/controller/schedule_bindings_controller.go
@@ -14,9 +14,9 @@ type ScheduleBindingToCrontabLink struct {
 	BindingName string
 	Crontab     string
 	// Useful fields to create a BindingContext
-	IncludeKubernetesSnapshots []string
-	AllowFailure               bool
-	QueueName                  string
+	IncludeSnapshots []string
+	AllowFailure     bool
+	QueueName        string
 }
 
 // KubernetesBindingsController handles kubernetes bindings for one hook.
@@ -78,11 +78,11 @@ func (c *scheduleBindingsController) HandleEvent(crontab string) []BindingExecut
 				Binding: link.BindingName,
 			}
 			bc.Metadata.BindingType = Schedule
-			bc.Metadata.IncludeSnapshots = link.IncludeKubernetesSnapshots
+			bc.Metadata.IncludeSnapshots = link.IncludeSnapshots
 
 			info := BindingExecutionInfo{
 				BindingContext:   []BindingContext{bc},
-				IncludeSnapshots: link.IncludeKubernetesSnapshots,
+				IncludeSnapshots: link.IncludeSnapshots,
 				AllowFailure:     link.AllowFailure,
 				QueueName:        link.QueueName,
 			}
@@ -96,11 +96,11 @@ func (c *scheduleBindingsController) HandleEvent(crontab string) []BindingExecut
 func (c *scheduleBindingsController) EnableScheduleBindings() {
 	for _, config := range c.ScheduleBindings {
 		c.ScheduleLinks[config.ScheduleEntry.Id] = &ScheduleBindingToCrontabLink{
-			BindingName:                config.BindingName,
-			Crontab:                    config.ScheduleEntry.Crontab,
-			IncludeKubernetesSnapshots: config.IncludeKubernetesSnapshotsFrom,
-			AllowFailure:               config.AllowFailure,
-			QueueName:                  config.Queue,
+			BindingName:      config.BindingName,
+			Crontab:          config.ScheduleEntry.Crontab,
+			IncludeSnapshots: config.IncludeSnapshotsFrom,
+			AllowFailure:     config.AllowFailure,
+			QueueName:        config.Queue,
 		}
 		c.scheduleManager.Add(config.ScheduleEntry)
 	}
diff --git a/pkg/hook/hook.go b/pkg/hook/hook.go
index 372c8e3..4ce8eb8 100644
--- a/pkg/hook/hook.go
+++ b/pkg/hook/hook.go
@@ -9,10 +9,12 @@ import (
 	"strings"
 
 	"github.com/kennygrant/sanitize"
+	uuid "gopkg.in/satori/go.uuid.v1"
 
 	. "github.com/flant/shell-operator/pkg/hook/binding_context"
 	. "github.com/flant/shell-operator/pkg/hook/types"
 
+	"github.com/flant/shell-operator/pkg/app"
 	"github.com/flant/shell-operator/pkg/executor"
 	"github.com/flant/shell-operator/pkg/hook/controller"
 )
@@ -74,6 +76,13 @@ func (h *Hook) Run(bindingType BindingType, context []BindingContext, logLabels
 	if err != nil {
 		return err
 	}
+	// remove tmp file on hook exit
+	defer func() {
+		if app.DebugKeepTmpFiles == "yes" {
+			return
+		}
+		os.Remove(contextPath)
+	}()
 
 	envs := []string{}
 	envs = append(envs, os.Environ()...)
@@ -132,7 +141,7 @@ func (h *Hook) prepareBindingContextJsonFile(context BindingContextList) (string
 		return "", err
 	}
 
-	bindingContextPath := filepath.Join(h.TmpDir, fmt.Sprintf("hook-%s-binding-context.json", h.SafeName()))
+	bindingContextPath := filepath.Join(h.TmpDir, fmt.Sprintf("hook-%s-binding-context-%s.json", h.SafeName(), uuid.NewV4().String()))
 
 	err = ioutil.WriteFile(bindingContextPath, data, 0644)
 	if err != nil {
diff --git a/pkg/hook/hook_config.go b/pkg/hook/hook_config.go
index 5e99ab6..5019aa7 100644
--- a/pkg/hook/hook_config.go
+++ b/pkg/hook/hook_config.go
@@ -55,11 +55,11 @@ type ScheduleConfigV0 struct {
 
 // Schedule configuration
 type ScheduleConfigV1 struct {
-	Name                           string   `json:"name"`
-	Crontab                        string   `json:"crontab"`
-	AllowFailure                   bool     `json:"allowFailure"`
-	IncludeKubernetesSnapshotsFrom []string `json:"includeKubernetesSnapshotsFrom"`
-	Queue                          string   `json:"queue"`
+	Name                 string   `json:"name"`
+	Crontab              string   `json:"crontab"`
+	AllowFailure         bool     `json:"allowFailure"`
+	IncludeSnapshotsFrom []string `json:"includeSnapshotsFrom"`
+	Queue                string   `json:"queue"`
 }
 
 // Legacy version of kubernetes event configuration
@@ -301,7 +301,7 @@ func (c *HookConfig) ConvertAndCheckV1() (err error) {
 		}
 	}
 
-	// schedule bindings with includeKubernetesSnapshotsFrom
+	// schedule bindings with includeSnapshotsFrom
 	// are depend on kubernetes bindings.
 	c.Schedules = []ScheduleConfig{}
 	for i, rawSchedule := range c.V1.Schedule {
@@ -388,7 +388,7 @@ func (c *HookConfig) ConvertScheduleV1(schV1 ScheduleConfigV1) (ScheduleConfig,
 		Crontab: schV1.Crontab,
 		Id:      c.ScheduleId(),
 	}
-	res.IncludeKubernetesSnapshotsFrom = schV1.IncludeKubernetesSnapshotsFrom
+	res.IncludeSnapshotsFrom = schV1.IncludeSnapshotsFrom
 
 	if schV1.Queue == "" {
 		res.Queue = "main"
@@ -414,10 +414,10 @@ func (c *HookConfig) CheckScheduleV1(schV1 ScheduleConfigV1) (allErr error) {
 		allErr = multierror.Append(allErr, fmt.Errorf("crontab is invalid: %v", err))
 	}
 
-	if len(schV1.IncludeKubernetesSnapshotsFrom) > 0 {
-		err = c.CheckIncludeSnapshots(schV1.IncludeKubernetesSnapshotsFrom...)
+	if len(schV1.IncludeSnapshotsFrom) > 0 {
+		err = c.CheckIncludeSnapshots(schV1.IncludeSnapshotsFrom...)
 		if err != nil {
-			allErr = multierror.Append(allErr, fmt.Errorf("includeKubernetesSnapshotsFrom is invalid: %v", err))
+			allErr = multierror.Append(allErr, fmt.Errorf("includeSnapshotsFrom is invalid: %v", err))
 		}
 	}
 
diff --git a/pkg/hook/types/bindings.go b/pkg/hook/types/bindings.go
index 319d675..e5d186d 100644
--- a/pkg/hook/types/bindings.go
+++ b/pkg/hook/types/bindings.go
@@ -32,9 +32,9 @@ type OnStartupConfig struct {
 
 type ScheduleConfig struct {
 	CommonBindingConfig
-	ScheduleEntry                  ScheduleEntry
-	IncludeKubernetesSnapshotsFrom []string
-	Queue                          string
+	ScheduleEntry        ScheduleEntry
+	IncludeSnapshotsFrom []string
+	Queue                string
 }
 
 type OnKubernetesEventConfig struct {


Test patch:
diff --git a/pkg/hook/config/validator_test.go b/pkg/hook/config/validator_test.go
index f3114fe..531faf7 100644
--- a/pkg/hook/config/validator_test.go
+++ b/pkg/hook/config/validator_test.go
@@ -53,7 +53,7 @@ func Test_Validate_V1_With_Error(t *testing.T) {
       "name": "qwe",
       "crontab": "*/5 * * * * *",
       "allowFailure": true,
-      "includeKubernetesSnapshotsFrom": ["monitor pods"],
+      "includeSnapshotsFrom": ["monitor pods"],
       "queue": "offload"
     }
   ],
diff --git a/pkg/hook/config/versioned_untyped_test.go b/pkg/hook/config/versioned_untyped_test.go
index 1d0aa92..038f8fe 100644
--- a/pkg/hook/config/versioned_untyped_test.go
+++ b/pkg/hook/config/versioned_untyped_test.go
@@ -85,7 +85,7 @@ param1: value1
 			    {"name":"secrets", "apiVersion":"v1", "kind":"Secret", "queue":"secrets"}
               ],
 			  "schedule":[
-			    {"name":"each 1 min", "crontab":"0 */1 * * * *", "includeKubernetesSnapshotsFrom":["pods", "secrets"]},
+			    {"name":"each 1 min", "crontab":"0 */1 * * * *", "includeSnapshotsFrom":["pods", "secrets"]},
 			    {"name":"each 5 min", "crontab":"0 */5 * * * *"},
 			    {"name":"each 7 sec", "crontab":"*/7 * * * * *", "queue":"off-schedule"}
 			  ]
@@ -113,7 +113,7 @@ kubernetes:
 schedule:
 - name: each 1 min
   crontab: "0 */1 * * * *"
-  includeKubernetesSnapshotsFrom: ["pods", "secrets"]
+  includeSnapshotsFrom: ["pods", "secrets"]
 - name: each 5 min
   crontab: "0 */5 * * * *"
 - name: each 7 sec
diff --git a/pkg/hook/hook_config_test.go b/pkg/hook/hook_config_test.go
index d286d39..b0e6214 100644
--- a/pkg/hook/hook_config_test.go
+++ b/pkg/hook/hook_config_test.go
@@ -220,7 +220,7 @@ func Test_HookConfig_VersionedConfig_LoadAndValidate(t *testing.T) {
 			    {"name":"secrets", "apiVersion":"v1", "kind":"Secret", "queue":"secrets"}
               ],
 			  "schedule":[
-			    {"name":"each 1 min", "crontab":"0 */1 * * * *", "includeKubernetesSnapshotsFrom":["pods", "secrets"]},
+			    {"name":"each 1 min", "crontab":"0 */1 * * * *", "includeSnapshotsFrom":["pods", "secrets"]},
 			    {"name":"each 5 min", "crontab":"0 */5 * * * *"},
 			    {"name":"each 7 sec", "crontab":"*/7 * * * * *", "queue":"off-schedule"}
 			  ]
@@ -241,7 +241,7 @@ func Test_HookConfig_VersionedConfig_LoadAndValidate(t *testing.T) {
 				sch1min := hookConfig.Schedules[0]
 				g.Expect(sch1min.BindingName).To(Equal("each 1 min"))
 				g.Expect(sch1min.Queue).To(Equal(""))
-				g.Expect(sch1min.IncludeKubernetesSnapshotsFrom).Should(HaveLen(2))
+				g.Expect(sch1min.IncludeSnapshotsFrom).Should(HaveLen(2))
 
 				sch5min := hookConfig.Schedules[1]
 				g.Expect(sch5min.BindingName).To(Equal("each 5 min"))
@@ -268,7 +268,7 @@ kubernetes:
 schedule:
 - name: each 1 min
   crontab: "0 */1 * * * *"
-  includeKubernetesSnapshotsFrom: ["pods", "secrets"]
+  includeSnapshotsFrom: ["pods", "secrets"]
 - name: each 5 min
   crontab: "0 */5 * * * *"
 - name: each 7 sec
@@ -292,7 +292,7 @@ schedule:
 				sch1min := hookConfig.Schedules[0]
 				g.Expect(sch1min.BindingName).To(Equal("each 1 min"))
 				g.Expect(sch1min.Queue).To(Equal(""))
-				g.Expect(sch1min.IncludeKubernetesSnapshotsFrom).Should(HaveLen(2))
+				g.Expect(sch1min.IncludeSnapshotsFrom).Should(HaveLen(2))
 
 				sch5min := hookConfig.Schedules[1]
 				g.Expect(sch5min.BindingName).To(Equal("each 5 min"))