forked from noobaa/noobaa-operator
/
crd.go
232 lines (208 loc) · 5.22 KB
/
crd.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package crd
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"github.com/noobaa/noobaa-operator/v5/pkg/bundle"
"github.com/noobaa/noobaa-operator/v5/pkg/util"
"sigs.k8s.io/controller-runtime/pkg/client"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cli-runtime/pkg/printers"
)
// CRD is just an alias for a long name
type CRD = apiextv1.CustomResourceDefinition
// Cmd returns a CLI command
func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "crd",
Short: "Deployment of CRDs",
}
cmd.AddCommand(
CmdCreate(),
CmdDelete(),
CmdStatus(),
CmdWait(),
CmdYaml(),
)
return cmd
}
// CmdCreate returns a CLI command
func CmdCreate() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create noobaa CRDs",
Run: RunCreate,
Args: cobra.NoArgs,
}
return cmd
}
// CmdDelete returns a CLI command
func CmdDelete() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete noobaa CRDs",
Run: RunDelete,
Args: cobra.NoArgs,
}
return cmd
}
// CmdStatus returns a CLI command
func CmdStatus() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Status of noobaa CRDs",
Run: RunStatus,
Args: cobra.NoArgs,
}
return cmd
}
// CmdWait returns a CLI command
func CmdWait() *cobra.Command {
cmd := &cobra.Command{
Use: "wait",
Short: "Wait for CRD to be ready",
Run: RunWait,
Args: cobra.NoArgs,
}
return cmd
}
// CmdYaml returns a CLI command
func CmdYaml() *cobra.Command {
cmd := &cobra.Command{
Use: "yaml",
Short: "Show bundled CRDs",
Run: RunYaml,
Args: cobra.NoArgs,
}
return cmd
}
// Crds is the
type Crds struct {
All []*CRD
NooBaa *CRD
BackingStore *CRD
NamespaceStore *CRD
BucketClass *CRD
ObjectBucket *CRD
ObjectBucketClaim *CRD
}
// RunCreate runs a CLI command
func RunCreate(cmd *cobra.Command, args []string) {
ForEachCRD(CreateCRD)
}
// RunDelete runs a CLI command
func RunDelete(cmd *cobra.Command, args []string) {
ForEachCRD(DeleteCRD)
}
// RunStatus runs a CLI command
func RunStatus(cmd *cobra.Command, args []string) {
ForEachCRD(CheckCRD)
}
// RunWait runs a CLI command
func RunWait(cmd *cobra.Command, args []string) {
RunStatus(cmd, args)
WaitAllReady()
}
// RunYaml dumps a combined yaml of all the CRDs from the bundled yamls
func RunYaml(cmd *cobra.Command, args []string) {
p := printers.YAMLPrinter{}
ForEachCRD(func(c *CRD) { util.Panic(p.PrintObj(c, os.Stdout)) })
}
// LoadCrds loads the CRDs structures from the bundled yamls
func LoadCrds() *Crds {
o1 := util.KubeObject(bundle.File_deploy_crds_noobaa_io_noobaas_crd_yaml)
o2 := util.KubeObject(bundle.File_deploy_crds_noobaa_io_backingstores_crd_yaml)
o3 := util.KubeObject(bundle.File_deploy_crds_noobaa_io_namespacestores_crd_yaml)
o4 := util.KubeObject(bundle.File_deploy_crds_noobaa_io_bucketclasses_crd_yaml)
o5 := util.KubeObject(bundle.File_deploy_obc_objectbucket_io_objectbucketclaims_crd_yaml)
o6 := util.KubeObject(bundle.File_deploy_obc_objectbucket_io_objectbuckets_crd_yaml)
crds := &Crds{
NooBaa: o1.(*CRD),
BackingStore: o2.(*CRD),
NamespaceStore: o3.(*CRD),
BucketClass: o4.(*CRD),
ObjectBucketClaim: o5.(*CRD),
ObjectBucket: o6.(*CRD),
}
crds.All = []*CRD{
crds.NooBaa,
crds.BackingStore,
crds.NamespaceStore,
crds.BucketClass,
crds.ObjectBucketClaim,
crds.ObjectBucket,
}
return crds
}
// ForEachCRD iterates and calls fn for every CRD
func ForEachCRD(fn func(*CRD)) {
crds := LoadCrds()
for _, c := range crds.All {
fn(c)
}
}
// CreateCRD creates a CRD
func CreateCRD(crd *CRD) {
util.KubeCreateSkipExisting(crd)
}
// DeleteCRD deletes a CRD
func DeleteCRD(crd *CRD) {
util.KubeDelete(crd)
}
// CheckCRD checks a CRD
func CheckCRD(crd *CRD) {
log := util.Logger()
desired := crd.DeepCopyObject().(*CRD)
util.KubeCheck(crd)
if crd.Spec.Versions[0].Name != desired.Spec.Versions[0].Name {
log.Printf("❌ CRD Version Mismatch: found %s desired %s",
crd.Spec.Versions[0].Name, desired.Spec.Versions[0].Name)
}
}
// WaitAllReady waits for all CRDs to be ready
func WaitAllReady() {
log := util.Logger()
klient := util.KubeClient()
crds := LoadCrds()
intervalSec := time.Duration(3)
util.Panic(wait.PollImmediateInfinite(intervalSec*time.Second, func() (bool, error) {
allReady := true
for _, crd := range crds.All {
err := klient.Get(util.Context(), client.ObjectKey{Name: crd.Name}, crd)
util.Panic(err)
ready, err := IsReady(crd)
if err != nil {
log.Printf("❌ %s", err)
allReady = false
continue
}
if !ready {
log.Printf("❌ CRD is not ready. Need to wait ...")
allReady = false
continue
}
}
return allReady, nil
}))
}
// IsReady checks the status of a CRD
func IsReady(crd *CRD) (bool, error) {
for _, cond := range crd.Status.Conditions {
switch cond.Type {
case apiextv1.NamesAccepted:
if cond.Status == apiextv1.ConditionFalse {
return false, fmt.Errorf("CRD Name conflict: %v", cond.Reason)
}
if cond.Status != apiextv1.ConditionTrue {
return false, nil
}
case apiextv1.Established:
if cond.Status != apiextv1.ConditionTrue {
return false, nil
}
}
}
return true, nil
}