Skip to content

Commit

Permalink
Add pre_cleanup and post_cleanup stages
Browse files Browse the repository at this point in the history
Signed-off-by: Prasad Ghangal <[email protected]>
  • Loading branch information
PrasadG193 committed Jun 29, 2021
1 parent a7ff480 commit 56ec4e2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
34 changes: 21 additions & 13 deletions pkg/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,29 @@ func runInstall(ctx context.Context, app App, c *config.AppConfig, m Method, app
for _, phase := range c.App.PreInstall {
for _, a := range phase.Apps {
if err := Run(ctx, m, a, namespace, filepath.Join(filepath.Dir(appConfigPath), a+".yaml")); err != nil {
return handleInstallError(ctx, err, event, app, namespace)
return handleInstallError(ctx, err, event, app, appName, namespace)
}
}
for _, a := range phase.Steps {
if err := execCommand(a); err != nil {
return handleInstallError(ctx, err, event, app, namespace)
if err := execCommand(ctx, a); err != nil {
return handleInstallError(ctx, err, event, app, appName, namespace)
}
}
}
// Run install
if err := app.Install(ctx, appName, namespace, c.App.Version, nil); err != nil {
return handleInstallError(ctx, err, event, app, namespace)
return handleInstallError(ctx, err, event, app, appName, namespace)
}
// Run postinstall
for _, phase := range c.App.PostInstall {
for _, a := range phase.Apps {
if err := Run(ctx, m, a, namespace, filepath.Join(filepath.Dir(appConfigPath), a+".yaml")); err != nil {
return handleInstallError(ctx, err, event, app, namespace)
return handleInstallError(ctx, err, event, app, appName, namespace)
}
}
for _, a := range phase.Steps {
if err := execCommand(a); err != nil {
return handleInstallError(ctx, err, event, app, namespace)
if err := execCommand(ctx, a); err != nil {
return handleInstallError(ctx, err, event, app, appName, namespace)
}
}
}
Expand All @@ -129,9 +129,9 @@ func runUninstall(ctx context.Context, app App, c *config.AppConfig, m Method, a
// Event report
event := events.NewKbrewEvent(c)

// Execute cleanup steps
for _, a := range c.App.Cleanup.Steps {
if err := execCommand(a); err != nil {
// Execute precleanup steps
for _, a := range c.App.PreCleanup.Steps {
if err := execCommand(ctx, a); err != nil {
return handleUninstallError(ctx, err, event, appName, namespace)
}
}
Expand Down Expand Up @@ -159,6 +159,13 @@ func runUninstall(ctx context.Context, app App, c *config.AppConfig, m Method, a
}
}

// Execute postcleanup steps
for _, a := range c.App.PostCleanup.Steps {
if err := execCommand(ctx, a); err != nil {
return handleUninstallError(ctx, err, event, appName, namespace)
}
}

if viper.GetBool(config.AnalyticsEnabled) {
if err1 := event.Report(context.TODO(), events.ECUninstallSuccess, nil, nil); err1 != nil {
fmt.Printf("DEBUG: Failed to report event. %s\n", err1.Error())
Expand All @@ -168,7 +175,7 @@ func runUninstall(ctx context.Context, app App, c *config.AppConfig, m Method, a
return nil
}

func handleInstallError(ctx context.Context, err error, event *events.KbrewEvent, app App, namespace string) error {
func handleInstallError(ctx context.Context, err error, event *events.KbrewEvent, app App, appName, namespace string) error {
if err == nil {
return nil
}
Expand All @@ -181,6 +188,7 @@ func handleInstallError(ctx context.Context, err error, event *events.KbrewEvent
}

if ctx.Err() != nil && ctx.Err() == context.DeadlineExceeded {
fmt.Printf("ERROR: Timed out while installing %s app in %s namespace\n", appName, namespace)
if err1 := event.Report(context.TODO(), events.ECInstallTimeout, err, nil); err1 != nil {
fmt.Printf("DEBUG: Failed to report event. %s\n", err1.Error())
}
Expand Down Expand Up @@ -219,8 +227,8 @@ func handleUninstallError(ctx context.Context, err error, event *events.KbrewEve
return err
}

func execCommand(cmd string) error {
c := exec.Command("sh", "-c", cmd)
func execCommand(ctx context.Context, cmd string) error {
c := exec.CommandContext(ctx, "sh", "-c", cmd)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
Expand Down
6 changes: 3 additions & 3 deletions pkg/apps/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func New(c config.App) *App {

// Install installs the application specified by name, version and namespace.
func (ha *App) Install(ctx context.Context, name, namespace, version string, options map[string]string) error {
fmt.Printf("Installing helm app %s/%s\n", ha.App.Repository.Name, name)
fmt.Printf("Installing helm app %s/%s in %s namespace\n", ha.App.Repository.Name, name, namespace)
//TODO: Resolve Deps
// Validate and install chart
// TODO(@prasad): Use go sdks
Expand All @@ -52,7 +52,7 @@ func (ha *App) Install(ctx context.Context, name, namespace, version string, opt
_, err := helmCommand(ctx, statusMethod, name, "", namespace, "", nil)
if err == nil {
// helm release already exists, return from here
fmt.Printf("helm app %s/%s already exists. Skipping...\n", ha.App.Repository.Name, name)
fmt.Printf("helm app %s/%s already exists in %s namespace. Skipping...\n", ha.App.Repository.Name, name, namespace)
return nil
}

Expand All @@ -63,7 +63,7 @@ func (ha *App) Install(ctx context.Context, name, namespace, version string, opt

// Uninstall uninstalls the application specified by name and namespace.
func (ha *App) Uninstall(ctx context.Context, name, namespace string) error {
fmt.Printf("Unistalling helm app %s\n", name)
fmt.Printf("Unistalling helm app %s from %s namespace\n", name, namespace)
//TODO: Resolve Deps
// Validate and install chart
// TODO(@prasad): Use go sdks
Expand Down
5 changes: 3 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ type App struct {
Version string `yaml:"version,omitempty"`
PreInstall []PreInstall `yaml:"pre_install,omitempty"`
PostInstall []PostInstall `yaml:"post_install,omitempty"`
Cleanup AppCleanup `yaml:"cleanup"`
PreCleanup AppCleanup `yaml:"pre_cleanup,omitempty"`
PostCleanup AppCleanup `yaml:"post_cleanup,omitempty"`
}

// Repository is the repo for kbrew app
Expand All @@ -83,7 +84,7 @@ type PostInstall struct {

// AppCleanup contains steps to be executed before uninstalling applications
type AppCleanup struct {
Steps []string
Steps []string `yaml:"steps,omitempty"`
}

// NewApp parses kbrew recipe configuration and returns AppConfig instance
Expand Down
2 changes: 0 additions & 2 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kube

import (
"context"
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -69,7 +68,6 @@ func FetchNonRunningPods(ctx context.Context, workloads []corev1.ObjectReference

pods := []corev1.Pod{}
for _, wRef := range workloads {
fmt.Println("WORKLOAD", wRef)
switch wRef.Kind {
case "Pod":
pod, err := clis.KubeCli.CoreV1().Pods(wRef.Namespace).Get(ctx, wRef.Name, metav1.GetOptions{})
Expand Down

0 comments on commit 56ec4e2

Please sign in to comment.