Skip to content

Commit

Permalink
v1-crd (#299)
Browse files Browse the repository at this point in the history
* v1-crd
* fix unit tests
* changelog
* fix rendering test
* Merge branch 'master' into v1-crd
* Update codegen/kuberesource/crd_test.go

Co-authored-by: Jahvon Dockery <[email protected]>
  • Loading branch information
EItanya and jahvon authored Sep 24, 2021
1 parent d92103a commit 84c61c3
Show file tree
Hide file tree
Showing 35 changed files with 2,543 additions and 351 deletions.
6 changes: 3 additions & 3 deletions api/k8s/k8s_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
certificatesv1beta1 "k8s.io/api/certificates/v1beta1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)

const (
Expand Down Expand Up @@ -78,15 +78,15 @@ func Groups() []model.Group {
ApiRoot: k8sApiRoot,
},
{
GroupVersion: apiextensionsv1beta1.SchemeGroupVersion,
GroupVersion: apiextensionsv1.SchemeGroupVersion,
Module: "k8s.io/apiextensions-apiserver",
Resources: []model.Resource{
{
Kind: "CustomResourceDefinition",
ClusterScoped: true,
},
},
CustomTypesImportPath: "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1",
CustomTypesImportPath: "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1",
ApiRoot: k8sApiRoot,
},
}
Expand Down
4 changes: 4 additions & 0 deletions changelog/v0.20.0/crd-v1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changelog:
- type: BREAKING_CHANGE
description: Upgrade CRD generation to use V1.
issueLink: https://github.com/solo-io/skv2/issues/300
71 changes: 37 additions & 34 deletions codegen/kuberesource/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"github.com/rotisserie/eris"
"github.com/solo-io/skv2/codegen/model"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

// Create CRDs for a group
Expand All @@ -23,7 +22,7 @@ func CustomResourceDefinitions(
) (objects []metav1.Object, err error) {
for _, resource := range group.Resources {

var validationSchema *apiextv1beta1.CustomResourceValidation
var validationSchema *apiextv1.CustomResourceValidation
if group.RenderValidationSchemas {
validationSchema, err = constructValidationSchema(resource, group.OpenApiSchemas)
if err != nil {
Expand All @@ -36,11 +35,14 @@ func CustomResourceDefinitions(
return objects, nil
}

func constructValidationSchema(resource model.Resource, oapiSchemas model.OpenApiSchemas) (*apiextv1beta1.CustomResourceValidation, error) {
validationSchema := &apiextv1beta1.CustomResourceValidation{
OpenAPIV3Schema: &apiextv1beta1.JSONSchemaProps{
func constructValidationSchema(
resource model.Resource,
oapiSchemas model.OpenApiSchemas,
) (*apiextv1.CustomResourceValidation, error) {
validationSchema := &apiextv1.CustomResourceValidation{
OpenAPIV3Schema: &apiextv1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextv1beta1.JSONSchemaProps{},
Properties: map[string]apiextv1.JSONSchemaProps{},
},
}

Expand All @@ -63,7 +65,10 @@ func constructValidationSchema(resource model.Resource, oapiSchemas model.OpenAp
return validationSchema, nil
}

func getJsonSchema(schemaName string, schemas map[string]*apiextv1beta1.JSONSchemaProps) (*apiextv1beta1.JSONSchemaProps, error) {
func getJsonSchema(
schemaName string,
schemas map[string]*apiextv1.JSONSchemaProps,
) (*apiextv1.JSONSchemaProps, error) {

schema, ok := schemas[schemaName]
if !ok {
Expand All @@ -78,9 +83,9 @@ func getJsonSchema(schemaName string, schemas map[string]*apiextv1beta1.JSONSche
}

// Lifted from https://github.com/istio/tools/blob/477454adf7995dd3070129998495cdc8aaec5aff/cmd/cue-gen/crd.go#L108
func validateStructural(s *apiextv1beta1.JSONSchemaProps) error {
func validateStructural(s *apiextv1.JSONSchemaProps) error {
out := &apiext.JSONSchemaProps{}
if err := apiextv1beta1.Convert_v1beta1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(s, out, nil); err != nil {
if err := apiextv1.Convert_v1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(s, out, nil); err != nil {
return fmt.Errorf("cannot convert v1beta1 JSONSchemaProps to JSONSchemaProps: %v", err)
}

Expand All @@ -98,47 +103,50 @@ func validateStructural(s *apiextv1beta1.JSONSchemaProps) error {

func CustomResourceDefinition(
resource model.Resource,
validationSchema *apiextv1beta1.CustomResourceValidation,
validationSchema *apiextv1.CustomResourceValidation,
withoutSpecHash bool,
) *apiextv1beta1.CustomResourceDefinition {
) *apiextv1.CustomResourceDefinition {

group := resource.Group.Group
version := resource.Group.Version
kind := resource.Kind
kindLowerPlural := strings.ToLower(stringutils.Pluralize(kind))
kindLower := strings.ToLower(kind)

var status *apiextv1beta1.CustomResourceSubresourceStatus
var status *apiextv1.CustomResourceSubresourceStatus
if resource.Status != nil {
status = &apiextv1beta1.CustomResourceSubresourceStatus{}
status = &apiextv1.CustomResourceSubresourceStatus{}
}

scope := apiextv1beta1.NamespaceScoped
scope := apiextv1.NamespaceScoped
if resource.ClusterScoped {
scope = apiextv1beta1.ClusterScoped
scope = apiextv1.ClusterScoped
}

crd := &apiextv1beta1.CustomResourceDefinition{
crd := &apiextv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: apiextv1beta1.SchemeGroupVersion.String(),
APIVersion: apiextv1.SchemeGroupVersion.String(),
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", kindLowerPlural, group),
},
Spec: apiextv1beta1.CustomResourceDefinitionSpec{
Spec: apiextv1.CustomResourceDefinitionSpec{
Group: group,
Scope: scope,
Versions: []apiextv1beta1.CustomResourceDefinitionVersion{{
Name: version,
Served: true,
Storage: true,
AdditionalPrinterColumns: resource.AdditionalPrinterColumns,
}},
Subresources: &apiextv1beta1.CustomResourceSubresources{
Status: status,
Versions: []apiextv1.CustomResourceDefinitionVersion{
{
Name: version,
Served: true,
Storage: true,
AdditionalPrinterColumns: resource.AdditionalPrinterColumns,
Subresources: &apiextv1.CustomResourceSubresources{
Status: status,
},
Schema: validationSchema,
},
},
Names: apiextv1beta1.CustomResourceDefinitionNames{
Names: apiextv1.CustomResourceDefinitionNames{
Plural: kindLowerPlural,
Singular: kindLower,
Kind: kind,
Expand All @@ -160,12 +168,7 @@ func CustomResourceDefinition(

if validationSchema != nil {
// Setting PreserveUnknownFields to false ensures that objects with unknown fields are rejected.
crd.Spec.PreserveUnknownFields = pointer.BoolPtr(false)

// TODO move this block into versions once we support multiple versions of a CRD
// Including a validation schema inside the Version field when there is only a single version present yields the error:
// "per-version schemas may not all be set to identical values (top-level validation should be used instead"
crd.Spec.Validation = validationSchema
crd.Spec.PreserveUnknownFields = false
}
return crd
}
4 changes: 2 additions & 2 deletions codegen/kuberesource/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ var _ = Describe("Crd", func() {
o, err := CustomResourceDefinitions(grp)
Expect(err).NotTo(HaveOccurred())
Expect(o).To(HaveLen(1))
// note: we intentionally provide the "d18828e563010e32" hash in the test, as it shouldn't change
// note: we intentionally provide the "c7fe90c34aa430b" hash in the test, as it shouldn't change
// between runs.
Expect(o[0].GetAnnotations()).To(HaveKeyWithValue(crdutils.CRDSpecHashKey, "d18828e563010e32"))
Expect(o[0].GetAnnotations()).To(HaveKeyWithValue(crdutils.CRDSpecHashKey, "c7fe90c34aa430b"))

})
It("should not generate spec hash", func() {
Expand Down
6 changes: 3 additions & 3 deletions codegen/model/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/golang/protobuf/proto"
"github.com/solo-io/skv2/codegen/collector"
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 @@ -41,7 +41,7 @@ func (g GeneratorTypes) HasDeepcopy() bool {
}

// Mapping from protobuf message name to OpenApi schema
type OpenApiSchemas map[string]*apiextv1beta1.JSONSchemaProps
type OpenApiSchemas map[string]*apiextv1.JSONSchemaProps

type Group struct {
// the group version of the group
Expand Down Expand Up @@ -145,7 +145,7 @@ type Resource struct {
ShortNames []string

// The set of additional printer columns to apply to the CustomResourceDefinition
AdditionalPrinterColumns []apiextv1beta1.CustomResourceColumnDefinition
AdditionalPrinterColumns []apiextv1.CustomResourceColumnDefinition
}

type Field struct {
Expand Down
6 changes: 3 additions & 3 deletions codegen/render/manifests_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/solo-io/go-utils/stringutils"
"github.com/solo-io/skv2/codegen/kuberesource"
"github.com/solo-io/skv2/codegen/model"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -278,7 +278,7 @@ func marshalObjToYaml(appName string, obj metav1.Object) (string, error) {
return string(yam), err
}

func postProcessValidationSchema(oapi *openapi.OrderedMap) (*apiextv1beta1.JSONSchemaProps, error) {
func postProcessValidationSchema(oapi *openapi.OrderedMap) (*apiextv1.JSONSchemaProps, error) {
oapiJson, err := oapi.MarshalJSON()
if err != nil {
return nil, err
Expand All @@ -295,7 +295,7 @@ func postProcessValidationSchema(oapi *openapi.OrderedMap) (*apiextv1beta1.JSONS
if err != nil {
return nil, err
}
jsonSchema := &apiextv1beta1.JSONSchemaProps{}
jsonSchema := &apiextv1.JSONSchemaProps{}
if err = json.Unmarshal(bytes, jsonSchema); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/render/manifests_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var _ = Describe("ManifestsRenderer", func() {
Expect(err).NotTo(HaveOccurred())
Expect(outFiles).To(HaveLen(1))
Expect(outFiles[0].Content).To(ContainSubstring(crdutils.CRDVersionKey + ": 1.0.0"))
Expect(outFiles[0].Content).To(ContainSubstring(crdutils.CRDSpecHashKey + ": d18828e563010e32"))
Expect(outFiles[0].Content).To(ContainSubstring(crdutils.CRDSpecHashKey + ": c7fe90c34aa430b"))

})
})
Expand Down
Loading

0 comments on commit 84c61c3

Please sign in to comment.