-
Notifications
You must be signed in to change notification settings - Fork 786
/
delete_addon.go
97 lines (81 loc) · 2.38 KB
/
delete_addon.go
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
package cmd
import (
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/spf13/cobra"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"fmt"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
)
// DeleteAddonOptions are the flags for delete commands
type DeleteAddonOptions struct {
*CommonOptions
Purge bool
}
// NewCmdDeleteAddon creates a command object for the generic "get" action, which
// retrieves one or more resources from a server.
func NewCmdDeleteAddon(commonOpts *CommonOptions) *cobra.Command {
options := &DeleteAddonOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "addon",
Short: "Deletes one or more addons",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
SuggestFor: []string{"remove", "rm"},
}
cmd.AddCommand(NewCmdDeleteAddonCloudBees(commonOpts))
cmd.AddCommand(NewCmdDeleteAddonFlagger(commonOpts))
cmd.AddCommand(NewCmdDeleteAddonGitea(commonOpts))
cmd.AddCommand(NewCmdDeleteAddonSSO(commonOpts))
cmd.AddCommand(NewCmdDeleteAddonKnativeBuild(commonOpts))
options.addFlags(cmd)
return cmd
}
func (options *DeleteAddonOptions) addFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&options.Purge, "purge", "p", true, "Removes the release name from helm so it can be reused again")
}
// Run implements this command
func (o *DeleteAddonOptions) Run() error {
args := o.Args
if len(args) == 0 {
return o.Cmd.Help()
}
charts := kube.AddonCharts
for _, arg := range args {
chart := charts[arg]
if chart == "" {
return util.InvalidArg(arg, util.SortedMapKeys(charts))
}
err := o.deleteChart(arg, o.Purge)
if err != nil {
return fmt.Errorf("Failed to delete chart %s: %s", chart, err)
}
err = o.cleanupServiceLink(arg)
if err != nil {
return fmt.Errorf("Failed to delete the service link for addon %s", arg)
}
}
return nil
}
func (o *DeleteAddonOptions) cleanupServiceLink(addonName string) error {
serviceName, ok := kube.AddonServices[addonName]
if !ok {
// No cleanup is required if no service link is associated with the Addon
return nil
}
client, err := o.KubeClient()
if err != nil {
return err
}
svc, err := services.FindService(client, serviceName)
if err != nil {
return err
}
return client.CoreV1().Services(svc.GetNamespace()).Delete(svc.GetName(), &meta_v1.DeleteOptions{})
}