-
Notifications
You must be signed in to change notification settings - Fork 12
/
describe.go
175 lines (150 loc) · 4.93 KB
/
describe.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
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
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 clusterdefinition
import (
"context"
"fmt"
"io"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/client-go/dynamic"
clientset "k8s.io/client-go/kubernetes"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
"github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types"
"github.com/apecloud/kubeblocks/pkg/dataprotection/utils/boolptr"
"github.com/apecloud/kbcli/pkg/printer"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
)
var (
describeExample = templates.Examples(`
# describe a specified cluster definition
kbcli clusterdefinition describe myclusterdef`)
)
type describeOptions struct {
factory cmdutil.Factory
client clientset.Interface
dynamic dynamic.Interface
namespace string
names []string
genericiooptions.IOStreams
}
func NewDescribeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := &describeOptions{
factory: f,
IOStreams: streams,
}
cmd := &cobra.Command{
Use: "describe",
Short: "Describe ClusterDefinition.",
Example: describeExample,
Aliases: []string{"desc"},
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterDefGVR()),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.complete(args))
cmdutil.CheckErr(o.run())
},
}
return cmd
}
func (o *describeOptions) complete(args []string) error {
var err error
if len(args) == 0 {
return fmt.Errorf("cluster definition name should be specified")
}
o.names = args
if o.client, err = o.factory.KubernetesClientSet(); err != nil {
return err
}
if o.dynamic, err = o.factory.DynamicClient(); err != nil {
return err
}
if o.namespace, _, err = o.factory.ToRawKubeConfigLoader().Namespace(); err != nil {
return err
}
return nil
}
func (o *describeOptions) run() error {
for _, name := range o.names {
if err := o.describeClusterDef(name); err != nil {
return err
}
}
return nil
}
func (o *describeOptions) describeClusterDef(name string) error {
// get cluster definition
clusterDef := &v1alpha1.ClusterDefinition{}
if err := util.GetK8SClientObject(o.dynamic, clusterDef, types.ClusterDefGVR(), "", name); err != nil {
return err
}
// get backup policy templates of the cluster definition
opts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", constant.ClusterDefLabelKey, name),
}
backupTemplatesListObj, err := o.dynamic.Resource(types.BackupPolicyTemplateGVR()).List(context.TODO(), opts)
if err != nil {
return err
}
var backupPolicyTemplates []*v1alpha1.BackupPolicyTemplate
for _, item := range backupTemplatesListObj.Items {
backupTemplate := v1alpha1.BackupPolicyTemplate{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(item.Object, &backupTemplate); err != nil {
return err
}
backupPolicyTemplates = append(backupPolicyTemplates, &backupTemplate)
}
showClusterDef(clusterDef, o.Out)
showBackupConfig(backupPolicyTemplates, o.Out)
return nil
}
func showClusterDef(cd *v1alpha1.ClusterDefinition, out io.Writer) {
if cd == nil {
return
}
fmt.Fprintf(out, "Name: %s\t Type: %s\n\n", cd.Name, cd.Spec.Type)
}
func showBackupConfig(backupPolicyTemplates []*v1alpha1.BackupPolicyTemplate, out io.Writer) {
if len(backupPolicyTemplates) == 0 {
return
}
fmt.Fprintf(out, "Backup Config:\n")
tbl := printer.NewTablePrinter(out)
defaultBackupPolicyTemplate := &v1alpha1.BackupPolicyTemplate{}
// if there is only one backup policy template, it will be the default backup policy template
if len(backupPolicyTemplates) == 1 {
defaultBackupPolicyTemplate = backupPolicyTemplates[0]
}
for _, item := range backupPolicyTemplates {
if item.Annotations[dptypes.DefaultBackupPolicyTemplateAnnotationKey] == "true" {
defaultBackupPolicyTemplate = item
break
}
}
tbl.SetHeader("BACKUP-METHOD", "ACTION-SET", "SNAPSHOT-VOLUME")
for _, policy := range defaultBackupPolicyTemplate.Spec.BackupPolicies {
for _, method := range policy.BackupMethods {
snapshotVolume := "false"
if boolptr.IsSetToTrue(method.SnapshotVolumes) {
snapshotVolume = "true"
}
tbl.AddRow(method.Name, method.ActionSetName, snapshotVolume)
}
}
tbl.Print()
}