Skip to content

Commit 4ee7b07

Browse files
committed
Add apiextensions.k8s.io/v1 support for CRDs
1 parent 3e17174 commit 4ee7b07

File tree

5 files changed

+72
-29
lines changed

5 files changed

+72
-29
lines changed

lib/resourceapply/apiext.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package resourceapply
22

33
import (
44
"github.com/openshift/cluster-version-operator/lib/resourcemerge"
5+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
56
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
7+
apiextclientv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
68
apiextclientv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
7-
apiextlistersv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1beta1"
89
apierrors "k8s.io/apimachinery/pkg/api/errors"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/utils/pointer"
1112
)
1213

13-
func ApplyCustomResourceDefinition(client apiextclientv1beta1.CustomResourceDefinitionsGetter, required *apiextv1beta1.CustomResourceDefinition) (*apiextv1beta1.CustomResourceDefinition, bool, error) {
14+
func ApplyCustomResourceDefinitionV1beta1(client apiextclientv1beta1.CustomResourceDefinitionsGetter, required *apiextv1beta1.CustomResourceDefinition) (*apiextv1beta1.CustomResourceDefinition, bool, error) {
1415
existing, err := client.CustomResourceDefinitions().Get(required.Name, metav1.GetOptions{})
1516
if apierrors.IsNotFound(err) {
1617
actual, err := client.CustomResourceDefinitions().Create(required)
@@ -25,7 +26,7 @@ func ApplyCustomResourceDefinition(client apiextclientv1beta1.CustomResourceDefi
2526
}
2627

2728
modified := pointer.BoolPtr(false)
28-
resourcemerge.EnsureCustomResourceDefinition(modified, existing, *required)
29+
resourcemerge.EnsureCustomResourceDefinitionV1beta1(modified, existing, *required)
2930
if !*modified {
3031
return existing, false, nil
3132
}
@@ -34,8 +35,8 @@ func ApplyCustomResourceDefinition(client apiextclientv1beta1.CustomResourceDefi
3435
return actual, true, err
3536
}
3637

37-
func ApplyCustomResourceDefinitionFromCache(lister apiextlistersv1beta1.CustomResourceDefinitionLister, client apiextclientv1beta1.CustomResourceDefinitionsGetter, required *apiextv1beta1.CustomResourceDefinition) (*apiextv1beta1.CustomResourceDefinition, bool, error) {
38-
existing, err := lister.Get(required.Name)
38+
func ApplyCustomResourceDefinitionV1(client apiextclientv1.CustomResourceDefinitionsGetter, required *apiextv1.CustomResourceDefinition) (*apiextv1.CustomResourceDefinition, bool, error) {
39+
existing, err := client.CustomResourceDefinitions().Get(required.Name, metav1.GetOptions{})
3940
if apierrors.IsNotFound(err) {
4041
actual, err := client.CustomResourceDefinitions().Create(required)
4142
return actual, true, err
@@ -48,9 +49,8 @@ func ApplyCustomResourceDefinitionFromCache(lister apiextlistersv1beta1.CustomRe
4849
return nil, false, nil
4950
}
5051

51-
existing = existing.DeepCopy()
5252
modified := pointer.BoolPtr(false)
53-
resourcemerge.EnsureCustomResourceDefinition(modified, existing, *required)
53+
resourcemerge.EnsureCustomResourceDefinitionV1(modified, existing, *required)
5454
if !*modified {
5555
return existing, false, nil
5656
}

lib/resourcebuilder/apiext.go

+40-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"github.com/openshift/cluster-version-operator/lib"
99
"github.com/openshift/cluster-version-operator/lib/resourceapply"
1010
"github.com/openshift/cluster-version-operator/lib/resourceread"
11+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1112
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
13+
apiextclientv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
1214
apiextclientv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
1315
"k8s.io/apimachinery/pkg/api/errors"
1416
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -17,15 +19,17 @@ import (
1719
)
1820

1921
type crdBuilder struct {
20-
client *apiextclientv1beta1.ApiextensionsV1beta1Client
21-
raw []byte
22-
modifier MetaV1ObjectModifierFunc
22+
raw []byte
23+
modifier MetaV1ObjectModifierFunc
24+
clientV1beta1 *apiextclientv1beta1.ApiextensionsV1beta1Client
25+
clientV1 *apiextclientv1.ApiextensionsV1Client
2326
}
2427

2528
func newCRDBuilder(config *rest.Config, m lib.Manifest) Interface {
2629
return &crdBuilder{
27-
client: apiextclientv1beta1.NewForConfigOrDie(withProtobuf(config)),
28-
raw: m.Raw,
30+
raw: m.Raw,
31+
clientV1beta1: apiextclientv1beta1.NewForConfigOrDie(withProtobuf(config)),
32+
clientV1: apiextclientv1.NewForConfigOrDie(withProtobuf(config)),
2933
}
3034
}
3135

@@ -39,34 +43,53 @@ func (b *crdBuilder) WithModifier(f MetaV1ObjectModifierFunc) Interface {
3943
}
4044

4145
func (b *crdBuilder) Do(ctx context.Context) error {
42-
crd := resourceread.ReadCustomResourceDefinitionV1Beta1OrDie(b.raw)
43-
if b.modifier != nil {
44-
b.modifier(crd)
45-
}
46-
_, updated, err := resourceapply.ApplyCustomResourceDefinition(b.client, crd)
47-
if err != nil {
48-
return err
46+
crd := resourceread.ReadCustomResourceDefinitionOrDie(b.raw)
47+
48+
var updated bool
49+
var err error
50+
var name string
51+
52+
switch crd := crd.(type) {
53+
case *apiextv1beta1.CustomResourceDefinition:
54+
if b.modifier != nil {
55+
b.modifier(crd)
56+
}
57+
_, updated, err = resourceapply.ApplyCustomResourceDefinitionV1beta1(b.clientV1beta1, crd)
58+
if err != nil {
59+
return err
60+
}
61+
name = crd.Name
62+
case *apiextv1.CustomResourceDefinition:
63+
if b.modifier != nil {
64+
b.modifier(crd)
65+
}
66+
_, updated, err = resourceapply.ApplyCustomResourceDefinitionV1(b.clientV1, crd)
67+
if err != nil {
68+
return err
69+
}
70+
name = crd.Name
4971
}
72+
5073
if updated {
51-
return waitForCustomResourceDefinitionCompletion(ctx, b.client, crd)
74+
return waitForCustomResourceDefinitionCompletion(ctx, b.clientV1, name)
5275
}
5376
return nil
5477
}
5578

56-
func waitForCustomResourceDefinitionCompletion(ctx context.Context, client apiextclientv1beta1.CustomResourceDefinitionsGetter, crd *apiextv1beta1.CustomResourceDefinition) error {
79+
func waitForCustomResourceDefinitionCompletion(ctx context.Context, client apiextclientv1.CustomResourceDefinitionsGetter, crd string) error {
5780
return wait.PollImmediateUntil(defaultObjectPollInterval, func() (bool, error) {
58-
c, err := client.CustomResourceDefinitions().Get(crd.Name, metav1.GetOptions{})
81+
c, err := client.CustomResourceDefinitions().Get(crd, metav1.GetOptions{})
5982
if errors.IsNotFound(err) {
6083
// exit early to recreate the crd.
6184
return false, err
6285
}
6386
if err != nil {
64-
klog.Errorf("error getting CustomResourceDefinition %s: %v", crd.Name, err)
87+
klog.Errorf("error getting CustomResourceDefinition %s: %v", crd, err)
6588
return false, nil
6689
}
6790

6891
for _, condition := range c.Status.Conditions {
69-
if condition.Type == apiextv1beta1.Established && condition.Status == apiextv1beta1.ConditionTrue {
92+
if condition.Type == apiextv1.Established && condition.Status == apiextv1.ConditionTrue {
7093
return true, nil
7194
}
7295
}

lib/resourcebuilder/register.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
corev1 "k8s.io/api/core/v1"
88
rbacv1 "k8s.io/api/rbac/v1"
99
rbacv1beta1 "k8s.io/api/rbac/v1beta1"
10+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1011
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
1112
apiregv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
1213
apiregv1beta1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1"
@@ -15,6 +16,7 @@ import (
1516
func init() {
1617
rm := NewResourceMapper()
1718
rm.RegisterGVK(apiextv1beta1.SchemeGroupVersion.WithKind("CustomResourceDefinition"), newCRDBuilder)
19+
rm.RegisterGVK(apiextv1.SchemeGroupVersion.WithKind("CustomResourceDefinition"), newCRDBuilder)
1820
rm.RegisterGVK(apiregv1.SchemeGroupVersion.WithKind("APIService"), newAPIServiceBuilder)
1921
rm.RegisterGVK(apiregv1beta1.SchemeGroupVersion.WithKind("APIService"), newAPIServiceBuilder)
2022
rm.RegisterGVK(appsv1.SchemeGroupVersion.WithKind("Deployment"), newDeploymentBuilder)

lib/resourcemerge/apiext.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package resourcemerge
22

33
import (
4+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
45
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
6+
57
"k8s.io/apimachinery/pkg/api/equality"
68
)
79

8-
// EnsureCustomResourceDefinition ensures that the existing matches the required.
10+
// EnsureCustomResourceDefinitionV1beta1 ensures that the existing matches the required.
11+
// modified is set to true when existing had to be updated with required.
12+
func EnsureCustomResourceDefinitionV1beta1(modified *bool, existing *apiextv1beta1.CustomResourceDefinition, required apiextv1beta1.CustomResourceDefinition) {
13+
EnsureObjectMeta(modified, &existing.ObjectMeta, required.ObjectMeta)
14+
15+
// we stomp everything
16+
if !equality.Semantic.DeepEqual(existing.Spec, required.Spec) {
17+
*modified = true
18+
existing.Spec = required.Spec
19+
}
20+
}
21+
22+
// EnsureCustomResourceDefinitionV1 ensures that the existing matches the required.
923
// modified is set to true when existing had to be updated with required.
10-
func EnsureCustomResourceDefinition(modified *bool, existing *apiextv1beta1.CustomResourceDefinition, required apiextv1beta1.CustomResourceDefinition) {
24+
func EnsureCustomResourceDefinitionV1(modified *bool, existing *apiextv1.CustomResourceDefinition, required apiextv1.CustomResourceDefinition) {
1125
EnsureObjectMeta(modified, &existing.ObjectMeta, required.ObjectMeta)
1226

1327
// we stomp everything

lib/resourceread/apiext.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package resourceread
22

33
import (
4+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
45
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
56
"k8s.io/apimachinery/pkg/runtime"
67
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -15,13 +16,16 @@ func init() {
1516
if err := apiextv1beta1.AddToScheme(apiExtensionsScheme); err != nil {
1617
panic(err)
1718
}
19+
if err := apiextv1.AddToScheme(apiExtensionsScheme); err != nil {
20+
panic(err)
21+
}
1822
}
1923

20-
// ReadCustomResourceDefinitionV1Beta1OrDie reads crd object from bytes. Panics on error.
21-
func ReadCustomResourceDefinitionV1Beta1OrDie(objBytes []byte) *apiextv1beta1.CustomResourceDefinition {
24+
// ReadCustomResourceDefinitionOrDie reads crd object from bytes as v1 or v1beta1. Panics on error.
25+
func ReadCustomResourceDefinitionOrDie(objBytes []byte) runtime.Object {
2226
requiredObj, err := runtime.Decode(apiExtensionsCodecs.UniversalDecoder(apiextv1beta1.SchemeGroupVersion), objBytes)
2327
if err != nil {
2428
panic(err)
2529
}
26-
return requiredObj.(*apiextv1beta1.CustomResourceDefinition)
30+
return requiredObj
2731
}

0 commit comments

Comments
 (0)