Skip to content

Commit

Permalink
Generate v1 CRDs (#451)
Browse files Browse the repository at this point in the history
* Generate v1 CRDs
* fix changelog, rerun codegen
* Updating resourceclients
* switch branch
* undo changes to resource clients
* Use v1 in customresource definition
* Show failing tests
* Preserve unknown fields
* Update v1-crd-generation.yaml

spelled "description" correctly
* Explicitly ignore all but the first version schema version
* breaking change
* move changelog
* remove test references to v1beta1
  • Loading branch information
elcasteel authored Sep 28, 2021
1 parent 33fda1f commit e3e386a
Show file tree
Hide file tree
Showing 24 changed files with 323 additions and 295 deletions.
6 changes: 3 additions & 3 deletions api/external/kubernetes/customresourcedefinition/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
"github.com/solo-io/solo-kit/pkg/utils/kubeutils"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)

type CustomResourceDefinition v1beta1.CustomResourceDefinition
type CustomResourceDefinition v1.CustomResourceDefinition

var _ resources.Resource = new(CustomResourceDefinition)

func (p *CustomResourceDefinition) Clone() *CustomResourceDefinition {
vp := v1beta1.CustomResourceDefinition(*p)
vp := v1.CustomResourceDefinition(*p)
copy := vp.DeepCopy()
newP := CustomResourceDefinition(*copy)
return &newP
Expand Down
5 changes: 5 additions & 0 deletions changelog/v0.24.0/v1-crd-generation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
changelog:
- type: BREAKING_CHANGE
issueLink: https://github.com/solo-io/gloo/issues/4304
resolvesIssue: false
description: Update generated CRDs to use v1 apis.
4 changes: 2 additions & 2 deletions pkg/api/external/kubernetes/customresourcedefinition/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/solo-io/solo-kit/pkg/multicluster/clustercache"
apiexts "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apiextsinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
apiextslisters "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1beta1"
apiextslisters "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1"
"k8s.io/client-go/rest"
)

Expand All @@ -34,7 +34,7 @@ func NewKubeCustomResourceDefinitionCache(ctx context.Context, apiExtsClient api

apiExtsInformerFactory := apiextsinformers.NewSharedInformerFactory(apiExtsClient, resyncDuration)

customResourceDefinitions := apiExtsInformerFactory.Apiextensions().V1beta1().CustomResourceDefinitions()
customResourceDefinitions := apiExtsInformerFactory.Apiextensions().V1().CustomResourceDefinitions()

k := &kubeCustomResourceDefinitionCache{
customResourceDefinitionLister: customResourceDefinitions.Lister(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"os"

"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

apiexts "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"

Expand Down Expand Up @@ -39,22 +39,33 @@ var _ = Describe("DeploymentBaseClient", func() {
client = newResourceClient(apiExts, kubeCache)
})
AfterEach(func() {
_ = apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(ctx, crdName, metav1.DeleteOptions{})
_ = apiExts.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, crdName, metav1.DeleteOptions{})
})
It("converts a kubernetes deployment to solo-kit resource", func() {
originalKubeCrd, err := apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Create(ctx, &v1beta1.CustomResourceDefinition{
originalKubeCrd, err := apiExts.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, &v1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: crdName,
},
Spec: v1beta1.CustomResourceDefinitionSpec{
Group: "integrationtests.solokit.solo.io",
Version: "v1",
Scope: v1beta1.ClusterScoped,
Names: v1beta1.CustomResourceDefinitionNames{
Spec: v1.CustomResourceDefinitionSpec{
Group: "integrationtests.solokit.solo.io",
Scope: v1.ClusterScoped,
Names: v1.CustomResourceDefinitionNames{
Plural: "testcrds",
Kind: "TestCrd",
ShortNames: []string{"tc"},
},
Versions: []v1.CustomResourceDefinitionVersion{
{
Name: "v1",
Schema: &v1.CustomResourceValidation{
OpenAPIV3Schema: &v1.JSONSchemaProps{
Type: "object",
},
},
Storage: true,
Served: true,
},
},
},
}, metav1.CreateOptions{})
Expect(err).NotTo(HaveOccurred())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"sort"

v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"github.com/solo-io/solo-kit/api/external/kubernetes/customresourcedefinition"

"github.com/solo-io/solo-kit/pkg/api/v1/clients"
Expand All @@ -12,7 +14,6 @@ import (
skkube "github.com/solo-io/solo-kit/pkg/api/v1/resources/common/kubernetes"
"github.com/solo-io/solo-kit/pkg/errors"

"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiexts "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -40,7 +41,7 @@ func NewCustomResourceDefinitionClient(apiExts apiexts.Interface, cache KubeCust
return skkube.NewCustomResourceDefinitionClientWithBase(resourceClient)
}

func FromKubeCustomResourceDefinition(customResourceDefinition *v1beta1.CustomResourceDefinition) *skkube.CustomResourceDefinition {
func FromKubeCustomResourceDefinition(customResourceDefinition *v1.CustomResourceDefinition) *skkube.CustomResourceDefinition {

customResourceDefinitionCopy := customResourceDefinition.DeepCopy()
kubeCustomResourceDefinition := customresourcedefinition.CustomResourceDefinition(*customResourceDefinitionCopy)
Expand All @@ -51,13 +52,13 @@ func FromKubeCustomResourceDefinition(customResourceDefinition *v1beta1.CustomRe
return resource
}

func ToKubeCustomResourceDefinition(resource resources.Resource) (*v1beta1.CustomResourceDefinition, error) {
func ToKubeCustomResourceDefinition(resource resources.Resource) (*v1.CustomResourceDefinition, error) {
customResourceDefinitionResource, ok := resource.(*skkube.CustomResourceDefinition)
if !ok {
return nil, errors.Errorf("internal error: invalid resource %v passed to customResourceDefinition-only client", resources.Kind(resource))
}

customResourceDefinition := v1beta1.CustomResourceDefinition(customResourceDefinitionResource.CustomResourceDefinition)
customResourceDefinition := v1.CustomResourceDefinition(customResourceDefinitionResource.CustomResourceDefinition)

return &customResourceDefinition, nil
}
Expand All @@ -70,7 +71,7 @@ func (rc *customResourceDefinitionResourceClient) Read(namespace, name string, o
}
opts = opts.WithDefaults()

customResourceDefinitionObj, err := rc.apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Get(opts.Ctx, name, metav1.GetOptions{})
customResourceDefinitionObj, err := rc.apiExts.ApiextensionsV1().CustomResourceDefinitions().Get(opts.Ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil, errors.NewNotExistErr(namespace, name, err)
Expand Down Expand Up @@ -110,11 +111,11 @@ func (rc *customResourceDefinitionResourceClient) Write(resource resources.Resou
if meta.ResourceVersion != original.GetMetadata().ResourceVersion {
return nil, errors.NewResourceVersionErr(meta.Namespace, meta.Name, meta.ResourceVersion, original.GetMetadata().ResourceVersion)
}
if _, err := rc.apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Update(opts.Ctx, customResourceDefinitionObj, metav1.UpdateOptions{}); err != nil {
if _, err := rc.apiExts.ApiextensionsV1().CustomResourceDefinitions().Update(opts.Ctx, customResourceDefinitionObj, metav1.UpdateOptions{}); err != nil {
return nil, errors.Wrapf(err, "updating kube customResourceDefinitionObj %v", customResourceDefinitionObj.Name)
}
} else {
if _, err := rc.apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Create(opts.Ctx, customResourceDefinitionObj, metav1.CreateOptions{}); err != nil {
if _, err := rc.apiExts.ApiextensionsV1().CustomResourceDefinitions().Create(opts.Ctx, customResourceDefinitionObj, metav1.CreateOptions{}); err != nil {
return nil, errors.Wrapf(err, "creating kube customResourceDefinitionObj %v", customResourceDefinitionObj.Name)
}
}
Expand All @@ -132,7 +133,7 @@ func (rc *customResourceDefinitionResourceClient) Delete(namespace, name string,
return nil
}

if err := rc.apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(opts.Ctx, name, metav1.DeleteOptions{}); err != nil {
if err := rc.apiExts.ApiextensionsV1().CustomResourceDefinitions().Delete(opts.Ctx, name, metav1.DeleteOptions{}); err != nil {
return errors.Wrapf(err, "deleting customResourceDefinitionObj %v", name)
}
return nil
Expand Down Expand Up @@ -167,6 +168,6 @@ func (rc *customResourceDefinitionResourceClient) Watch(namespace string, opts c
}

func (rc *customResourceDefinitionResourceClient) exist(ctx context.Context, namespace, name string) bool {
_, err := rc.apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
_, err := rc.apiExts.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
return err == nil
}
4 changes: 2 additions & 2 deletions pkg/api/v1/clients/factory/resource_client_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ var _ = Describe("ResourceClientFactory", func() {
Expect(err).NotTo(HaveOccurred())

// ensure the crd is not registered
err = apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(ctx, v1.MockResourceCrd.FullName(), v12.DeleteOptions{})
err = apiExts.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, v1.MockResourceCrd.FullName(), v12.DeleteOptions{})
if err != nil {
Expect(err).To(BeErrTypeMatcher{
ExpectedErrType: "not found",
IsErrType: errors.IsNotFound,
})
}
Eventually(func() bool {
_, err := apiExts.ApiextensionsV1beta1().CustomResourceDefinitions().Get(ctx, v1.MockResourceCrd.FullName(), v12.GetOptions{})
_, err := apiExts.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, v1.MockResourceCrd.FullName(), v12.GetOptions{})
return err != nil && errors.IsNotFound(err)
}, time.Minute, time.Second*5).Should(BeTrue())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = Describe("Clientset", func() {
err = helpers.AddAndRegisterCrd(ctx, mocksv1.MockResourceCrd, apiextsClient)
Expect(err).NotTo(HaveOccurred())

c, err := apiextsClient.ApiextensionsV1beta1().CustomResourceDefinitions().List(ctx, v1.ListOptions{})
c, err := apiextsClient.ApiextensionsV1().CustomResourceDefinitions().List(ctx, v1.ListOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(len(c.Items)).To(BeNumerically(">=", 1))
var found bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var _ = Describe("ClusterClientFactory", func() {
Expect(err).NotTo(HaveOccurred())
clientset, err := apiexts.NewForConfig(cfg)
Expect(err).NotTo(HaveOccurred())
err = clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(ctx, "mocks.testing.solo.io", metav1.DeleteOptions{})
err = clientset.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, "mocks.testing.solo.io", metav1.DeleteOptions{})
testutils.ErrorNotOccuredOrNotFound(err)
})

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
Expect(err).NotTo(HaveOccurred())
{{- range $uniqueCrds}}
err = clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(ctx, "{{lowercase .}}", metav1.DeleteOptions{})
err = clientset.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, "{{lowercase .}}", metav1.DeleteOptions{})
testutils.ErrorNotOccuredOrNotFound(err)
{{- end}}
Expect(lock.ReleaseLock()).NotTo(HaveOccurred())
Expand Down
32 changes: 13 additions & 19 deletions pkg/code-generator/schemagen/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ import (

"github.com/rotisserie/eris"

"github.com/ghodss/yaml"
code_generator "github.com/solo-io/solo-kit/pkg/code-generator"
"github.com/solo-io/solo-kit/pkg/code-generator/writer"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/utils/pointer"

"github.com/ghodss/yaml"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
kubeyaml "k8s.io/apimachinery/pkg/util/yaml"
)

const (
v1beta1 = "apiextensions.k8s.io/v1beta1"
v1 = "apiextensions.k8s.io/v1"
)

var (
Expand All @@ -45,12 +43,8 @@ func NewCrdWriter(crdDirectory string) *CrdWriter {
}
}

func (c *CrdWriter) ApplyValidationSchemaToCRD(crd apiextv1beta1.CustomResourceDefinition, validationSchema *apiextv1beta1.CustomResourceValidation) error {
crd.Spec.Validation = validationSchema
// Setting PreserveUnknownFields to false ensures that objects with unknown fields are rejected.
// This is deprecated and will default to false in future versions.
crd.Spec.PreserveUnknownFields = pointer.BoolPtr(false)

func (c *CrdWriter) ApplyValidationSchemaToCRD(crd apiextv1.CustomResourceDefinition, validationSchema *apiextv1.CustomResourceValidation) error {
crd.Spec.Versions[0].Schema = validationSchema
crdBytes, err := yaml.Marshal(crd)
if err != nil {
return err
Expand All @@ -62,12 +56,12 @@ func (c *CrdWriter) ApplyValidationSchemaToCRD(crd apiextv1beta1.CustomResourceD
})
}

func getFilenameForCRD(crd apiextv1beta1.CustomResourceDefinition) string {
return fmt.Sprintf("%s_%s_%s.yaml", crd.Spec.Group, crd.Spec.Version, crd.Spec.Names.Kind)
func getFilenameForCRD(crd apiextv1.CustomResourceDefinition) string {
return fmt.Sprintf("%s_%s_%s.yaml", crd.Spec.Group, crd.Spec.Versions[0].Name, crd.Spec.Names.Kind)
}

func GetCRDsFromDirectory(crdDirectory string) ([]apiextv1beta1.CustomResourceDefinition, error) {
var crds []apiextv1beta1.CustomResourceDefinition
func GetCRDsFromDirectory(crdDirectory string) ([]apiextv1.CustomResourceDefinition, error) {
var crds []apiextv1.CustomResourceDefinition

err := filepath.Walk(crdDirectory, func(crdFile string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -94,8 +88,8 @@ func GetCRDsFromDirectory(crdDirectory string) ([]apiextv1beta1.CustomResourceDe
return crds, err
}

func GetCRDFromFile(pathToFile string) (apiextv1beta1.CustomResourceDefinition, error) {
crd := apiextv1beta1.CustomResourceDefinition{}
func GetCRDFromFile(pathToFile string) (apiextv1.CustomResourceDefinition, error) {
crd := apiextv1.CustomResourceDefinition{}

r, err := os.Open(pathToFile)
if err != nil {
Expand All @@ -118,8 +112,8 @@ func GetCRDFromFile(pathToFile string) (apiextv1beta1.CustomResourceDefinition,
chunk := bytes.TrimSpace(doc)

err = yaml.Unmarshal(chunk, &crd)
if err == nil && v1beta1 != crd.APIVersion {
return crd, ApiVersionMismatch(v1beta1, crd.APIVersion)
if err == nil && v1 != crd.APIVersion {
return crd, ApiVersionMismatch(v1, crd.APIVersion)
}

return crd, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/code-generator/schemagen/cue.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/solo-io/go-utils/stringutils"
"github.com/solo-io/solo-kit/pkg/code-generator/collector"
"github.com/solo-io/solo-kit/pkg/code-generator/model"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func NewCueGenerator(importsCollector collector.Collector, absoluteRoot string)
}
}

func (c *cueGenerator) GetJsonSchemaForProject(project *model.Project) (map[schema.GroupVersionKind]*apiextv1beta1.JSONSchemaProps, error) {
func (c *cueGenerator) GetJsonSchemaForProject(project *model.Project) (map[schema.GroupVersionKind]*apiextv1.JSONSchemaProps, error) {
protobufExtractor, err := c.getProtobufExtractorForProject(project)
if err != nil {
return nil, err
Expand Down Expand Up @@ -123,8 +123,8 @@ func (c *cueGenerator) getProtobufExtractorForProject(project *model.Project) (*
return protobufExtractor, nil
}

func (c *cueGenerator) convertOpenApiSchemasToJsonSchemas(project *model.Project, schemas OpenApiSchemas) (map[schema.GroupVersionKind]*apiextv1beta1.JSONSchemaProps, error) {
jsonSchemasByGVK := make(map[schema.GroupVersionKind]*apiextv1beta1.JSONSchemaProps)
func (c *cueGenerator) convertOpenApiSchemasToJsonSchemas(project *model.Project, schemas OpenApiSchemas) (map[schema.GroupVersionKind]*apiextv1.JSONSchemaProps, error) {
jsonSchemasByGVK := make(map[schema.GroupVersionKind]*apiextv1.JSONSchemaProps)

for schemaKey, schemaValue := range schemas {
schemaGVK := c.getGVKForSchemaKey(project, schemaKey)
Expand All @@ -141,7 +141,7 @@ func (c *cueGenerator) convertOpenApiSchemasToJsonSchemas(project *model.Project
return jsonSchemasByGVK, nil
}

func (c *cueGenerator) convertOpenApiSchemaToJsonSchema(schemaKey string, schema *openapi.OrderedMap) (*apiextv1beta1.JSONSchemaProps, error) {
func (c *cueGenerator) convertOpenApiSchemaToJsonSchema(schemaKey string, schema *openapi.OrderedMap) (*apiextv1.JSONSchemaProps, error) {
if schema == nil {
return nil, eris.Errorf("no open api schema for %s", schemaKey)
}
Expand All @@ -164,7 +164,7 @@ func (c *cueGenerator) convertOpenApiSchemaToJsonSchema(schemaKey string, schema
return nil, err
}

jsonSchema := &apiextv1beta1.JSONSchemaProps{}
jsonSchema := &apiextv1.JSONSchemaProps{}
if err = json.Unmarshal(bytes, jsonSchema); err != nil {
return nil, eris.Errorf("Cannot unmarshal raw OpenAPI schema to JSONSchemaProps for %v: %v", schemaKey, err)
}
Expand Down
Loading

0 comments on commit e3e386a

Please sign in to comment.