/
disable_option.go
98 lines (78 loc) · 2.49 KB
/
disable_option.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
98
/*
Copyright 2022 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package init
import (
"fmt"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"k8s.io/utils/strings/slices"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
"github.com/karmada-io/karmada/pkg/karmadactl/util/apiclient"
)
// CommandAddonsDisableOption options for addons list.
type CommandAddonsDisableOption struct {
GlobalCommandOptions
KarmadaKubeClientSet *kubernetes.Clientset
Force bool
}
// Complete the conditions required to be able to run disable.
func (o *CommandAddonsDisableOption) Complete() error {
err := o.GlobalCommandOptions.Complete()
if err != nil {
return err
}
o.KarmadaKubeClientSet, err = apiclient.NewClientSet(o.KarmadaRestConfig)
if err != nil {
return err
}
return nil
}
// Validate Check that there are enough conditions to run the disable.
func (o *CommandAddonsDisableOption) Validate(args []string) error {
err := validAddonNames(args)
if err != nil {
return err
}
if slices.Contains(args, EstimatorResourceName) && o.Cluster == "" {
return fmt.Errorf("member cluster and config is needed when disable karmada-scheduler-estimator,use `--cluster=member --member-kubeconfig /root/.kube/config --member-context member1` to disable karmada-scheduler-estimator")
}
return nil
}
// Run start disable Karmada addons
func (o *CommandAddonsDisableOption) Run(args []string) error {
fmt.Printf("Disable Karmada addon %s\n", args)
if !o.Force && !util.DeleteConfirmation() {
return nil
}
var disableAddons = map[string]*Addon{}
// collect disabled addons
for _, item := range args {
if item == "all" {
disableAddons = Addons
break
}
if addon := Addons[item]; addon != nil {
disableAddons[item] = addon
}
}
// disable addons
for name, addon := range disableAddons {
klog.Infof("Start to disable addon %s", name)
if err := addon.Disable(o); err != nil {
klog.Errorf("Disable addon %s failed", name)
return err
}
klog.Infof("Successfully disable addon %s", name)
}
return nil
}