-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
crd.go
156 lines (133 loc) · 5.29 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
// Copyright 2019 The prometheus-operator 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 framework
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"time"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/yaml"
"github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring"
)
// GetCRD gets a custom resource definition from the apiserver.
func (f *Framework) GetCRD(ctx context.Context, name string) (*v1.CustomResourceDefinition, error) {
crd, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("unable to get CRD with name %v: %w", name, err)
}
return crd, nil
}
// ListCRDs gets a list of custom resource definitions from the apiserver.
func (f *Framework) ListCRDs(ctx context.Context) (*v1.CustomResourceDefinitionList, error) {
crds, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("unable to list CRDs: %w", err)
}
return crds, nil
}
// CreateOrUpdateCRD creates a custom resource definition on the apiserver.
func (f *Framework) CreateOrUpdateCRD(ctx context.Context, crd *v1.CustomResourceDefinition) error {
c, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, crd.Name, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("getting CRD %s: %w", crd.Spec.Names.Kind, err)
}
if apierrors.IsNotFound(err) {
// CRD doesn't exists -> Create
_, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, crd, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("create CRD %s: %w", crd.Spec.Names.Kind, err)
}
} else {
// must set this field from existing CRD to prevent update fail
crd.ObjectMeta.ResourceVersion = c.ObjectMeta.ResourceVersion
// CRD already exists -> Update
_, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, crd, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("update CRD %s: %w", crd.Spec.Names.Kind, err)
}
}
return nil
}
func (f *Framework) DeleteCRD(ctx context.Context, name string) error {
err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("unable to delete CRD with name %v: %w", name, err)
}
return nil
}
// MakeCRD creates a CustomResourceDefinition object from yaml manifest.
func (f *Framework) MakeCRD(source string) (*v1.CustomResourceDefinition, error) {
manifest, err := SourceToIOReader(source)
if err != nil {
return nil, fmt.Errorf("get manifest from source %s: %w", source, err)
}
content, err := io.ReadAll(manifest)
if err != nil {
return nil, fmt.Errorf("get manifest content: %w", err)
}
crd := v1.CustomResourceDefinition{}
err = yaml.Unmarshal(content, &crd)
if err != nil {
return nil, fmt.Errorf("unmarshal CRD asset file %s: %w", source, err)
}
return &crd, nil
}
// WaitForCRDReady waits for a Custom Resource Definition to be available for use.
func WaitForCRDReady(listFunc func(opts metav1.ListOptions) (runtime.Object, error)) error {
err := wait.PollUntilContextTimeout(context.Background(), 3*time.Second, 10*time.Minute, false, func(_ context.Context) (bool, error) {
_, err := listFunc(metav1.ListOptions{})
if err != nil {
if se, ok := err.(*apierrors.StatusError); ok {
if se.Status().Code == http.StatusNotFound {
return false, nil
}
}
return false, fmt.Errorf("failed to list CRD: %w", err)
}
return true, nil
})
if err != nil {
return fmt.Errorf("timed out waiting for Custom Resource: %w", err)
}
return nil
}
// CreateCRDAndWaitUntilReady creates a Custom Resource Definition from yaml
// manifest on the apiserver and wait until it is available for use.
func (f *Framework) CreateOrUpdateCRDAndWaitUntilReady(ctx context.Context, crdName string, listFunc func(opts metav1.ListOptions) (runtime.Object, error)) error {
crdName = strings.ToLower(crdName)
group := monitoring.GroupName
assetPath := f.exampleDir + "/prometheus-operator-crd-full/" + group + "_" + crdName + ".yaml"
crd, err := f.MakeCRD(assetPath)
if err != nil {
return fmt.Errorf("create CRD: %s from manifest: %s: %w", crdName, assetPath, err)
}
crd.ObjectMeta.Name = crd.Spec.Names.Plural + "." + group
crd.Spec.Group = group
err = f.CreateOrUpdateCRD(ctx, crd)
if err != nil {
return fmt.Errorf("create CRD %s on the apiserver: %w", crdName, err)
}
err = WaitForCRDReady(listFunc)
if err != nil {
return fmt.Errorf("%s CRD not ready: %w", crdName, err)
}
return nil
}