forked from kubesphere-sigs/ks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable.go
136 lines (124 loc) · 4.29 KB
/
enable.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
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
package component
import (
"context"
"fmt"
"github.com/linuxsuren/ks/kubectl-plugin/common"
kstypes "github.com/linuxsuren/ks/kubectl-plugin/types"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"strconv"
)
// EnableOption is the option for component enable command
type EnableOption struct {
Option
Edit bool
Toggle bool
}
// newComponentEnableCmd returns a command to enable (or disable) a component by name
func newComponentEnableCmd(client dynamic.Interface) (cmd *cobra.Command) {
opt := &EnableOption{
Option: Option{
Client: client,
},
}
cmd = &cobra.Command{
Use: "enable",
Short: "Enable or disable the specific KubeSphere component",
Example: `You can enable a single component with name via: ks com enable devops
Or it's possible to enable all components via: ks com enable all'`,
PreRunE: opt.enablePreRunE,
ValidArgsFunction: common.PluginAbleComponentsCompletion(),
RunE: opt.enableRunE,
}
flags := cmd.Flags()
flags.BoolVarP(&opt.Edit, "edit", "e", false,
"Indicate if you want to edit it instead of enable/disable a specified one. This flag will make others not work.")
flags.BoolVarP(&opt.Toggle, "toggle", "t", false,
"Indicate if you want to disable a component")
flags.StringVarP(&opt.Name, "name", "n", "",
"The name of target component which you want to enable/disable. Please provide option --sonarqube if you want to enable SonarQube.")
flags.StringVarP(&opt.SonarQube, "sonarqube", "", "",
"The SonarQube URL")
flags.StringVarP(&opt.SonarQube, "sonar", "", "",
"The SonarQube URL")
flags.StringVarP(&opt.SonarQubeToken, "sonarqube-token", "", "",
"The token of SonarQube")
_ = cmd.RegisterFlagCompletionFunc("name", common.PluginAbleComponentsCompletion())
// these are aliased options
_ = flags.MarkHidden("sonar")
return
}
func (o *EnableOption) enablePreRunE(cmd *cobra.Command, args []string) (err error) {
if o.Edit {
return
}
return o.componentNameCheck(cmd, args)
}
func (o *EnableOption) enableRunE(cmd *cobra.Command, args []string) (err error) {
ctx := context.TODO()
if o.Edit {
err = common.UpdateWithEditor(kstypes.GetClusterConfiguration(), "kubesphere-system", "ks-installer", o.Client)
} else {
enabled := strconv.FormatBool(!o.Toggle)
ns, name := "kubesphere-system", "ks-installer"
var patchTarget string
switch o.Name {
case "devops", "alerting", "auditing", "events", "logging", "metrics_server", "networkpolicy", "notification", "openpitrix", "servicemesh":
patchTarget = o.Name
case "sonarqube", "sonar":
if o.SonarQube == "" || o.SonarQubeToken == "" {
err = fmt.Errorf("SonarQube or token is empty, please provide --sonarqube")
} else {
name = "ks-console-config"
err = integrateSonarQube(o.Client, ns, name, o.SonarQube, o.SonarQubeToken)
}
return
case "metering":
patchTarget = "metering"
if _, err = o.Client.Resource(kstypes.GetConfigMapSchema()).Namespace("kubesphere-system").
Get(ctx, "ks-metering-config", metav1.GetOptions{}); err != nil {
var data *unstructured.Unstructured
if data, err = kstypes.GetObjectFromYaml(`
apiVersion: v1
kind: ConfigMap
metadata:
name: ks-metering-config
data:
ks-metering.yaml: |
retentionDay: 7d
billing:
priceInfo:
currencyUnit: "USD"
cpuPerCorePerHour: 1.5
memPerGigabytesPerHour: 5
ingressNetworkTrafficPerGiagabytesPerHour: 3.5
egressNetworkTrafficPerGigabytesPerHour: 0.5
pvcPerGigabytesPerHour: 2.1`); err == nil {
_, err = o.Client.Resource(kstypes.GetConfigMapSchema()).Namespace("kubesphere-system").Create(ctx, data, metav1.CreateOptions{})
}
}
case "all":
for _, item := range common.GetPluginAbleComponents() {
o.Name = item
if err = o.enableRunE(cmd, args); err != nil {
return
}
}
return
default:
err = fmt.Errorf("not support [%s] yet", o.Name)
return
}
if err == nil {
patch := fmt.Sprintf(`[{"op": "replace", "path": "/spec/%s/enabled", "value": %s}]`, patchTarget, enabled)
_, err = o.Client.Resource(kstypes.GetClusterConfiguration()).Namespace(ns).Patch(ctx,
name, types.JSONPatchType,
[]byte(patch),
metav1.PatchOptions{})
}
}
return
}