Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions cmd/kubernetes/kubernetes_app_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package kubernetes
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
Expand All @@ -14,6 +17,8 @@ import (
"github.com/spf13/cobra"
)

const marketplaceBaseURL = "https://raw.githubusercontent.com/civo/kubernetes-marketplace/master"

var kubernetesAppRemoveCmd = &cobra.Command{
Use: "remove",
Example: "civo kubernetes application remove NAME --cluster CLUSTER_NAME",
Expand Down Expand Up @@ -52,11 +57,19 @@ var kubernetesAppRemoveCmd = &cobra.Command{
defer os.Remove(tmpFile.Name())
for _, split := range allApps {
appName := split

// TODO: Ideally this would come from the Civo API, but the Civo API doesn't currently return uninstall.sh
// https://www.civo.com/api/kubernetes#listing-applications
filepath := fmt.Sprintf("bash <(curl -s https://raw.githubusercontent.com/civo/kubernetes-marketplace/master/%s/uninstall.sh)", appName)
cmdConfig := exec.Command("/bin/bash", "-c", filepath)
raw, err := getMarketplaceAppUninstallScript(marketplaceBaseURL, appName)
if err != nil {
utility.Error("Failed to get uninstall script: %v", err)
os.Exit(1)
}

cmdConfig := exec.Command("/bin/bash", "-s")
var b bytes.Buffer

cmdConfig.Stdin = bytes.NewReader(raw)
cmdConfig.Stdout = &b
cmdConfig.Stderr = &b
cmdConfig.Env = os.Environ()
Expand All @@ -65,10 +78,8 @@ var kubernetesAppRemoveCmd = &cobra.Command{
if err := cmdConfig.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
utility.Error("Failed to uninstall application %s (exited with code %d)\n", appName, exitError.ExitCode())
cmd := exec.Command("curl", "-s", fmt.Sprintf("https://raw.githubusercontent.com/civo/kubernetes-marketplace/master/%s/uninstall.sh", appName))
output, _ := cmd.CombinedOutput()
fmt.Println("--------------- Uninstall script ---------------")
fmt.Println(string(output))
fmt.Println(string(raw))
fmt.Println("--------------- Uninstall output ---------------")
if strings.Contains(b.String(), "command not found") {
fmt.Printf("Uninstall script for application %s not found \n", appName)
Expand Down Expand Up @@ -96,3 +107,16 @@ var kubernetesAppRemoveCmd = &cobra.Command{

},
}

func getMarketplaceAppUninstallScript(baseURL, appName string) ([]byte, error) {
scriptURL := fmt.Sprintf("%s/%s/uninstall.sh", baseURL, url.PathEscape(appName))
resp, err := http.Get(scriptURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch uninstall script for %s: %w", appName, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("uninstall script for application %s not found (HTTP %d)", appName, resp.StatusCode)
}
return io.ReadAll(resp.Body)
}
50 changes: 50 additions & 0 deletions cmd/kubernetes/kubernetes_app_remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package kubernetes

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestGetMarketplaceAppUninstallScript(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/metrics-server/uninstall.sh" {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("echo uninstalling"))
return
}
w.WriteHeader(http.StatusNotFound)
}))
defer srv.Close()

tests := []struct {
name string
appName string
wantScript string
wantErr bool
}{
{
name: "returns the script for an existing app",
appName: "metrics-server",
wantScript: "echo uninstalling",
wantErr: false,
},
{
name: "returns an error for a missing app",
appName: "does-not-exist",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getMarketplaceAppUninstallScript(srv.URL, tt.appName)
if (err != nil) != tt.wantErr {
t.Fatalf("getMarketplaceAppUninstallScript(%q) error = %v, wantErr %v", tt.appName, err, tt.wantErr)
}
if !tt.wantErr && string(got) != tt.wantScript {
t.Errorf("got %q, want %q", string(got), tt.wantScript)
}
})
}
}
Loading