Browse Source

Update existing stack installation on `compose up` instead of returning error

Signed-off-by: aiordache <[email protected]>
aiordache 4 years ago
parent
commit
bb25812511
2 changed files with 36 additions and 8 deletions
  1. 23 8
      kube/compose.go
  2. 13 0
      kube/helm/helm.go

+ 23 - 8
kube/compose.go

@@ -74,7 +74,7 @@ func NewComposeService() (compose.Service, error) {
 func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
 	w := progress.ContextWriter(ctx)
 
-	eventName := "Convert to Helm charts"
+	eventName := "Convert Compose file to Helm charts"
 	w.Event(progress.CreatingEvent(eventName))
 
 	chart, err := helm.GetChartInMemory(project)
@@ -83,16 +83,31 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
 	}
 	w.Event(progress.NewEvent(eventName, progress.Done, ""))
 
-	eventName = "Install Helm charts"
-	w.Event(progress.CreatingEvent(eventName))
-
-	err = s.sdk.InstallChart(project.Name, chart, func(format string, v ...interface{}) {
-		message := fmt.Sprintf(format, v...)
-		w.Event(progress.NewEvent(eventName, progress.Done, message))
-	})
+	stack, err := s.sdk.Get(project.Name)
+	if err != nil || stack == nil {
+		// install stack
+		eventName = "Install Compose stack"
+		w.Event(progress.CreatingEvent(eventName))
+
+		err = s.sdk.InstallChart(project.Name, chart, func(format string, v ...interface{}) {
+			message := fmt.Sprintf(format, v...)
+			w.Event(progress.NewEvent(eventName, progress.Done, message))
+		})
+
+	} else {
+		//update stack
+		eventName = "Updating Compose stack"
+		w.Event(progress.CreatingEvent(eventName))
+
+		err = s.sdk.UpdateChart(project.Name, chart, func(format string, v ...interface{}) {
+			message := fmt.Sprintf(format, v...)
+			w.Event(progress.NewEvent(eventName, progress.Done, message))
+		})
+	}
 	if err != nil {
 		return err
 	}
+
 	w.Event(progress.NewEvent(eventName, progress.Done, ""))
 
 	return s.client.WaitForPodState(ctx, client.WaitForStatusOptions{

+ 13 - 0
kube/helm/helm.go

@@ -84,6 +84,19 @@ func (hc *Actions) InstallChart(name string, chart *chart.Chart, logger func(for
 	return err
 }
 
+// UpdateChart upgrades chart
+func (hc *Actions) UpdateChart(name string, chart *chart.Chart, logger func(format string, v ...interface{})) error {
+	err := hc.initialize(logger)
+	if err != nil {
+		return err
+	}
+
+	actUpgrade := action.NewUpgrade(hc.Config)
+	actUpgrade.Namespace = hc.Namespace
+	_, err = actUpgrade.Run(name, chart, map[string]interface{}{})
+	return err
+}
+
 // Uninstall uninstall chart
 func (hc *Actions) Uninstall(name string, logger func(format string, v ...interface{})) error {
 	err := hc.initialize(logger)