-
Notifications
You must be signed in to change notification settings - Fork 12
/
set_default.go
197 lines (176 loc) · 6.13 KB
/
set_default.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package clusterversion
import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apitypes "k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/client-go/dynamic"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
)
var (
setDefaultExample = templates.Examples(`
# set ac-mysql-8.0.30 as the default clusterversion
kbcli clusterversion set-default ac-mysql-8.0.30`,
)
unsetDefaultExample = templates.Examples(`
# unset ac-mysql-8.0.30 to default clusterversion if it's default
kbcli clusterversion unset-default ac-mysql-8.0.30`)
clusterVersionGVR = types.ClusterVersionGVR()
)
const (
annotationTrueValue = "true"
annotationFalseValue = "false"
)
type SetOrUnsetDefaultOption struct {
Factory cmdutil.Factory
IOStreams genericiooptions.IOStreams
// `set-default` sets the setDefault to true, `unset-default` sets to false
setDefault bool
}
func newSetOrUnsetDefaultOptions(f cmdutil.Factory, streams genericiooptions.IOStreams, toSet bool) *SetOrUnsetDefaultOption {
return &SetOrUnsetDefaultOption{
Factory: f,
IOStreams: streams,
setDefault: toSet,
}
}
func newSetDefaultCMD(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newSetOrUnsetDefaultOptions(f, streams, true)
cmd := &cobra.Command{
Use: "set-default NAME",
Short: "Set the clusterversion to the default clusterversion for its clusterdefinition.",
Example: setDefaultExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, clusterVersionGVR),
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.validate(args))
util.CheckErr(o.run(args))
},
}
return cmd
}
func newUnSetDefaultCMD(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newSetOrUnsetDefaultOptions(f, streams, false)
cmd := &cobra.Command{
Use: "unset-default NAME",
Short: "Unset the clusterversion if it's default.",
Example: unsetDefaultExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, clusterVersionGVR),
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.validate(args))
util.CheckErr(o.run(args))
},
}
return cmd
}
func (o *SetOrUnsetDefaultOption) run(args []string) error {
client, err := o.Factory.DynamicClient()
if err != nil {
return err
}
var allErrs []error
// unset-default logic
if !o.setDefault {
for _, cv := range args {
if err := patchDefaultClusterVersionAnnotations(client, cv, annotationFalseValue); err != nil {
allErrs = append(allErrs, err)
}
}
return utilerrors.NewAggregate(allErrs)
}
// set-default logic
cv2Cd, cd2DefaultCv, err := getMapsBetweenCvAndCd(client)
if err != nil {
return err
}
// alreadySet is to mark if two input args have the same clusterdefintion
alreadySet := make(map[string]string)
for _, cv := range args {
cd, ok := cv2Cd[cv]
if !ok {
allErrs = append(allErrs, fmt.Errorf("cluterversion \"%s\" not found", cv))
continue
}
if _, ok := cd2DefaultCv[cd]; ok && cv != cd2DefaultCv[cd] {
allErrs = append(allErrs, fmt.Errorf("clusterdefinition \"%s\" already has a default cluster version \"%s\"", cv2Cd[cv], cd2DefaultCv[cd]))
continue
}
if _, ok := alreadySet[cd]; ok {
allErrs = append(allErrs, fmt.Errorf("\"%s\" has the same clusterdefinition with \"%s\"", cv, alreadySet[cd]))
continue
}
if err := patchDefaultClusterVersionAnnotations(client, cv, annotationTrueValue); err != nil {
allErrs = append(allErrs, err)
continue
}
alreadySet[cd] = cv
}
return utilerrors.NewAggregate(allErrs)
}
func (o *SetOrUnsetDefaultOption) validate(args []string) error {
if len(args) == 0 {
return fmt.Errorf("clusterversion name should be specified, run \"kbcli clusterversion list\" to list the clusterversions")
}
return nil
}
// patchDefaultClusterVersionAnnotations patches the Annotations for the clusterversion in K8S
func patchDefaultClusterVersionAnnotations(client dynamic.Interface, cvName string, value string) error {
patchData := map[string]interface{}{
"metadata": map[string]interface{}{
"annotations": map[string]interface{}{
constant.DefaultClusterVersionAnnotationKey: value,
},
},
}
patchBytes, _ := json.Marshal(patchData)
_, err := client.Resource(clusterVersionGVR).Patch(context.Background(), cvName, apitypes.MergePatchType, patchBytes, metav1.PatchOptions{})
return err
}
func getMapsBetweenCvAndCd(client dynamic.Interface) (map[string]string, map[string]string, error) {
lists, err := client.Resource(clusterVersionGVR).List(context.Background(), metav1.ListOptions{})
if err != nil {
return nil, nil, err
}
cvToCd := make(map[string]string)
cdToDefaultCv := make(map[string]string)
for _, item := range lists.Items {
name := item.GetName()
annotations := item.GetAnnotations()
labels := item.GetLabels()
if labels == nil {
continue
}
if _, ok := labels[constant.ClusterDefLabelKey]; !ok {
continue
}
cvToCd[name] = labels[constant.ClusterDefLabelKey]
if annotations == nil {
continue
}
if annotations[constant.DefaultClusterVersionAnnotationKey] == annotationTrueValue {
cdToDefaultCv[labels[constant.ClusterDefLabelKey]] = name
}
}
return cvToCd, cdToDefaultCv, nil
}