Skip to content

Commit 06b3f11

Browse files
gossionEItanya
andauthored
clean CRDs when uninstalling kagent (#263)
Co-authored-by: Eitan Yarmush <[email protected]>
1 parent 0619021 commit 06b3f11

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

go/cli/internal/cli/install.go

+41-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"os/exec"
78
"strings"
@@ -63,7 +64,9 @@ func InstallCmd(ctx context.Context, c *ishell.Context) {
6364
// original kagent installation that had CRDs installed together with the kagent chart
6465
if strings.Contains(output, "exists and cannot be imported into the current release") {
6566
s.Stop()
66-
c.Println("Warning: CRDs already exist but not managed by helm, you might need to delete them manually to make them fully managed by helm.")
67+
c.Println("Warning: CRDs exist but aren't managed by helm.")
68+
c.Println("Run `uninstall` or delete them manually to")
69+
c.Println("ensure they're fully managed on next install.")
6770
s.Start()
6871
} else {
6972
c.Println("Error installing kagent-crds:", output)
@@ -102,6 +105,37 @@ func InstallCmd(ctx context.Context, c *ishell.Context) {
102105
c.Println("kagent installed successfully")
103106
}
104107

108+
// deleteCRDs manually deletes Kubernetes CRDs for kagent
109+
// This is a workaround for the fact that helm doesn't delete CRDs automatically
110+
func deleteCRDs(ctx context.Context, c *ishell.Context) error {
111+
crds := []string{
112+
"agents.kagent.dev",
113+
"modelconfigs.kagent.dev",
114+
"teams.kagent.dev",
115+
"toolservers.kagent.dev",
116+
}
117+
118+
var deleteErrors []string
119+
120+
for _, crd := range crds {
121+
deleteCmd := exec.CommandContext(ctx, "kubectl", "delete", "crd", crd)
122+
if out, err := deleteCmd.CombinedOutput(); err != nil {
123+
if !strings.Contains(string(out), "not found") {
124+
errMsg := fmt.Sprintf("Error deleting CRD %s: %s", crd, string(out))
125+
c.Printf(errMsg)
126+
deleteErrors = append(deleteErrors, errMsg)
127+
}
128+
} else {
129+
c.Printf("Successfully deleted CRD %s\n", crd)
130+
}
131+
}
132+
133+
if len(deleteErrors) > 0 {
134+
return fmt.Errorf("failed to delete some CRDs: %s", strings.Join(deleteErrors, "; "))
135+
}
136+
return nil
137+
}
138+
105139
func UninstallCmd(ctx context.Context, c *ishell.Context) {
106140
cfg := config.GetCfg(c)
107141
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond)
@@ -146,7 +180,12 @@ func UninstallCmd(ctx context.Context, c *ishell.Context) {
146180
// Check if this is because kagent-crds doesn't exist
147181
output := string(out)
148182
if strings.Contains(output, "not found") {
149-
c.Println("Warning: kagent-crds release not found, skipping uninstallation")
183+
c.Println("Warning: kagent-crds release not found, try to delete crds directly")
184+
// delete the CRDs directly, this is a workaround for the fact that helm doesn't delete CRDs
185+
if err := deleteCRDs(ctx, c); err != nil {
186+
c.Println("Error deleting CRDs:", err)
187+
return
188+
}
150189
} else {
151190
c.Println("Error uninstalling kagent-crds:", output)
152191
return

0 commit comments

Comments
 (0)