Skip to content

Commit

Permalink
Add Helm 3 DeleteRelease (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonAlling authored and absoludity committed Jan 9, 2020
1 parent d46a6d8 commit 35e1f70
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/kubeops/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,18 @@ func GetRelease(cfg agent.Config, w http.ResponseWriter, req *http.Request, para
}
response.NewDataResponse(newDashboardCompatibleRelease(*release)).Write(w)
}

func DeleteRelease(cfg agent.Config, w http.ResponseWriter, req *http.Request, params handlerutil.Params) {
releaseName := params[nameParam]
purge := handlerutil.QueryParamIsTruthy("purge", req)
// Helm 3 has --purge by default; --keep-history in Helm 3 corresponds to omitting --purge in Helm 2.
// https://stackoverflow.com/a/59210923/2135002
keepHistory := !purge
err := agent.DeleteRelease(cfg.ActionConfig, releaseName, keepHistory)
if err != nil {
response.NewErrorResponse(handlerutil.ErrorCode(err), err.Error()).Write(w)
return
}
w.Header().Set("Status-Code", "200")
w.Write([]byte("OK"))
}
3 changes: 3 additions & 0 deletions cmd/kubeops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func main() {
apiv1.Methods("GET").Path("/namespaces/{namespace}/releases/{releaseName}").Handler(negroni.New(
negroni.Wrap(withAgentConfig(handler.GetRelease)),
))
apiv1.Methods("DELETE").Path("/namespaces/{namespace}/releases/{releaseName}").Handler(negroni.New(
negroni.Wrap(withAgentConfig(handler.DeleteRelease)),
))

// assetsvc reverse proxy
authGate := auth.AuthGate()
Expand Down
8 changes: 8 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ func GetRelease(actionConfig *action.Configuration, name string) (*release.Relea
return release, nil
}

func DeleteRelease(actionConfig *action.Configuration, name string, keepHistory bool) error {
// Namespace is already known by the RESTClientGetter.
cmd := action.NewUninstall(actionConfig)
cmd.KeepHistory = keepHistory
_, err := cmd.Run(name)
return err
}

func NewActionConfig(storageForDriver StorageForDriver, config *rest.Config, clientset *kubernetes.Clientset, namespace string) (*action.Configuration, error) {
actionConfig := new(action.Configuration)
store := storageForDriver(namespace, clientset)
Expand Down
5 changes: 5 additions & 0 deletions pkg/handlerutil/handlerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ func ParseAndGetChart(req *http.Request, cu chartUtils.Resolver, requireV1Suppor
}
return chartDetails, ch, nil
}

func QueryParamIsTruthy(param string, req *http.Request) bool {
value := req.URL.Query().Get(param)
return value == "1" || value == "true"
}

0 comments on commit 35e1f70

Please sign in to comment.