Skip to content

Commit

Permalink
Merge pull request #203 from DirectXMan12/bug/incompatible-crds
Browse files Browse the repository at this point in the history
🐛 Fix incompatible CRDs
  • Loading branch information
k8s-ci-robot committed May 7, 2019
2 parents 455fb28 + 92086f2 commit 89f6323
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
39 changes: 37 additions & 2 deletions pkg/crd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"go/types"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"

crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
Expand All @@ -29,12 +30,17 @@ import (
)

// Generator is a genall.Generator that generates CRDs.
type Generator struct{}
type Generator struct {
// TrivialVersions indicates that we should produce a legacy
// "trival-version" CRD compatible with older (pre 1.13) Kubernetes API
// servers. The storage version's schema will be used as the CRD's schema.
TrivialVersions bool `marker:",optional"`
}

func (Generator) RegisterMarkers(into *markers.Registry) error {
return crdmarkers.Register(into)
}
func (Generator) Generate(ctx *genall.GenerationContext) error {
func (g Generator) Generate(ctx *genall.GenerationContext) error {
parser := &Parser{
Collector: ctx.Collector,
Checker: ctx.Checker,
Expand All @@ -61,6 +67,9 @@ func (Generator) Generate(ctx *genall.GenerationContext) error {
for _, groupKind := range kubeKinds {
parser.NeedCRDFor(groupKind)
crd := parser.CustomResourceDefinitions[groupKind]
if g.TrivialVersions {
toTrivialVersions(&crd)
}
fileName := fmt.Sprintf("%s_%s.yaml", crd.Spec.Group, crd.Spec.Names.Plural)
if err := ctx.WriteYAML(crd, fileName); err != nil {
return err
Expand All @@ -70,6 +79,32 @@ func (Generator) Generate(ctx *genall.GenerationContext) error {
return nil
}

// toTrivialVersions strips out all schemata except for the storage schema,
// and moves that up into the root object. This makes the CRD compatible
// with pre 1.13 clusters.
func toTrivialVersions(crd *apiext.CustomResourceDefinition) {
var canonicalSchema *apiext.CustomResourceValidation
var canonicalSubresources *apiext.CustomResourceSubresources
var canonicalColumns []apiext.CustomResourceColumnDefinition
for i, ver := range crd.Spec.Versions {
if ver.Storage == true {
canonicalSchema = ver.Schema
canonicalSubresources = ver.Subresources
canonicalColumns = ver.AdditionalPrinterColumns
}
crd.Spec.Versions[i].Schema = nil
crd.Spec.Versions[i].Subresources = nil
crd.Spec.Versions[i].AdditionalPrinterColumns = nil
}
if canonicalSchema == nil {
return
}

crd.Spec.Validation = canonicalSchema
crd.Spec.Subresources = canonicalSubresources
crd.Spec.AdditionalPrinterColumns = canonicalColumns
}

// findMetav1 locates the actual package representing metav1 amongst
// the imports of the roots.
func findMetav1(roots []*loader.Package) *loader.Package {
Expand Down
6 changes: 3 additions & 3 deletions pkg/crd/markers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ type PrintColumn struct {
Name string
Type string
JSONPath string `marker:"JSONPath"` // legacy cruft
Description string
Format string
Priority int32
Description string `marker:",optional"`
Format string `marker:",optional"`
Priority int32 `marker:",optional"`
}

func (s PrintColumn) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
Expand Down
6 changes: 6 additions & 0 deletions pkg/crd/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,11 @@ func (p *Parser) NeedCRDFor(groupKind schema.GroupKind) {
packages[0].AddError(fmt.Errorf("CRD for %s has no storage version", groupKind))
}

// NB(directxman12): CRD's status doesn't have omitempty markers, which means things
// get serialized as null, which causes the validator to freak out. Manually set
// these to empty till we get a better solution.
crd.Status.Conditions = []apiext.CustomResourceDefinitionCondition{}
crd.Status.StoredVersions = []string{}

p.CustomResourceDefinitions[groupKind] = crd
}

0 comments on commit 89f6323

Please sign in to comment.