/
uninstall.go
52 lines (47 loc) · 1.64 KB
/
uninstall.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
package catalogsource
import (
openshiftUninstallCmd "github.com/jkandasa/autoeasy/cmd/plugin/openshift/uninstall"
rootCmd "github.com/jkandasa/autoeasy/cmd/root"
csAPI "github.com/jkandasa/autoeasy/plugin/provider/openshift/api/catalog_source"
openshiftClient "github.com/jkandasa/autoeasy/plugin/provider/openshift/client"
"github.com/spf13/cobra"
"go.uber.org/zap"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func init() {
openshiftUninstallCmd.AddCommand(uninstallCsCmd)
}
var uninstallCsCmd = &cobra.Command{
Use: "catalog-source",
Short: "removes catalog source",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, catalogSourceList []string) {
// get kubernetes client
k8sClient := openshiftClient.GetKubernetesClient()
uninstallCatalogSources(k8sClient, catalogSourceList)
},
}
func uninstallCatalogSources(k8sClient client.Client, catalogSourceList []string) {
csList, err := csAPI.List(k8sClient, []client.ListOption{})
if err != nil {
zap.L().Error("error on getting catalog source list", zap.Error(err))
rootCmd.ExitWithError()
}
for _, catalogSourceName := range catalogSourceList {
found := false
for _, _cs := range csList.Items {
if _cs.Name == catalogSourceName {
found = true
err = csAPI.Delete(k8sClient, &_cs)
if err != nil {
zap.L().Error("error on deleting catalog source", zap.String("name", _cs.GetName()), zap.String("namespace", _cs.GetName()), zap.Error(err))
continue
}
zap.L().Info("uninstalled a catalog source", zap.String("name", _cs.GetName()), zap.String("namespace", _cs.GetName()), zap.String("image", _cs.Spec.Image))
}
if found {
break
}
}
}
}