-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete.go
More file actions
150 lines (126 loc) · 4.05 KB
/
delete.go
File metadata and controls
150 lines (126 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and component-operator-runtime contributors
SPDX-License-Identifier: Apache-2.0
*/
package cmd
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"github.com/sap/component-operator-runtime/clm/internal/backoff"
"github.com/sap/component-operator-runtime/clm/internal/release"
"github.com/sap/component-operator-runtime/pkg/component"
"github.com/sap/component-operator-runtime/pkg/reconciler"
"github.com/sap/go-generics/slices"
)
const deleteUsage = `Delete component from Kubernetes cluster`
type deleteOptions struct {
timeout time.Duration
}
func newDeleteCmd() *cobra.Command {
options := &deleteOptions{}
cmd := &cobra.Command{
Use: "delete NAME",
Short: "Delete component",
Long: deleteUsage,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
PreRunE: func(c *cobra.Command, args []string) error {
return nil
},
RunE: func(c *cobra.Command, args []string) (err error) {
name := args[0]
namespace := c.Flag("namespace").Value.String()
clnt, err := getClient(c.Flag("kubeconfig").Value.String())
if err != nil {
return err
}
reconciler := reconciler.NewReconciler(fullName, clnt, reconciler.ReconcilerOptions{
UpdatePolicy: ref(reconciler.UpdatePolicySsaOverride),
})
releaseClient := release.NewClient(fullName, clnt)
ownerId := fullName + "/" + namespace + "/" + name
release, err := releaseClient.Get(context.TODO(), namespace, name)
if err != nil {
return err
}
if ok, msg, err := reconciler.IsDeletionAllowed(context.TODO(), &release.Inventory, ownerId); err != nil {
return err
} else if !ok {
return fmt.Errorf("%s", msg)
}
if err := releaseClient.Delete(context.TODO(), release); err != nil {
return err
}
release, err = releaseClient.Get(context.TODO(), namespace, name)
if err != nil {
return err
}
backoff := backoff.New()
var timeout <-chan time.Time
if options.timeout > 0 {
timeout = time.After(options.timeout)
}
defer func() {
if err != nil {
release.State = component.StateError
}
if updateErr := releaseClient.Update(context.TODO(), release); updateErr != nil {
err = errors.Join(err, updateErr)
}
}()
const maxErrCount = 15
errCount := 0
for {
release.State = component.StateDeleting
ok, err := reconciler.Delete(context.TODO(), &release.Inventory, ownerId)
if err != nil {
if !isEphmeralError(err) || errCount >= maxErrCount {
return err
}
errCount++
fmt.Fprintf(os.Stderr, "Error: %s (retrying %d/%d)\n", err, errCount, maxErrCount)
} else {
errCount = 0
if ok {
break
}
if err := releaseClient.Update(context.TODO(), release); err != nil {
return err
}
}
select {
case <-time.After(backoff.Next()):
case <-timeout:
return fmt.Errorf("timeout deleting release %s/%s", release.GetNamespace(), release.GetName())
}
}
fmt.Printf("Release %s/%s successfully deleted\n", release.GetNamespace(), release.GetName())
return nil
},
ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
if clnt, err := getClient(c.Flag("kubeconfig").Value.String()); err == nil {
releaseClient := release.NewClient(fullName, clnt)
namespace := c.Flag("namespace").Value.String()
if namespace == "" {
namespace = "default"
}
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()
if releases, err := releaseClient.List(ctx, namespace); err == nil {
return slices.Collect(releases, func(release *release.Release) string { return release.GetName() }), cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveDefault
},
}
flags := cmd.Flags()
flags.DurationVar(&options.timeout, "timeout", 0, "Time to wait for the operation to complete (default is to wait forever)")
return cmd
}