Skip to content

Commit

Permalink
Test for type-level preserve-unknown-fields
Browse files Browse the repository at this point in the history
This adds a test for type-level `pruning:PreserveUnknownFields` to the
crd test.
  • Loading branch information
DirectXMan12 committed Oct 14, 2020
1 parent 90f7678 commit 72b140e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
35 changes: 35 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
package cronjob

import (
"encoding/json"
"fmt"

batchv1beta1 "k8s.io/api/batch/v1beta1"
Expand Down Expand Up @@ -127,6 +128,9 @@ type CronJobSpec struct {
// +kubebuilder:validation:nullable
UnprunedEmbeddedResource runtime.RawExtension `json:"unprunedEmbeddedResource"`

// This tests that a type-level pruning maker works.
UnprunedFromType Preserved `json:"unprunedFomType"`

// This tests that associative lists work.
// +listType=map
// +listMapKey=name
Expand All @@ -142,6 +146,37 @@ type CronJobSpec struct {
StructWithSeveralFields NestedObject `json:"structWithSeveralFields"`
}

// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
type Preserved struct {
ConcreteField string `json:"concreteField"`
Rest map[string]interface{} `json:"-"`
}
func (p *Preserved) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &p.Rest); err != nil {
return err
}
conc, found := p.Rest["concreteField"]
if !found {
return nil
}
concStr, isStr := conc.(string)
if !isStr {
return fmt.Errorf("concreteField was not string")
}
delete(p.Rest, "concreteField")
p.ConcreteField = concStr
return nil
}
func (p *Preserved) MarshalJSON() ([]byte, error) {
full := make(map[string]interface{}, len(p.Rest)+1)
for k, v := range p.Rest {
full[k] = v
}
full["concreteField"] = p.ConcreteField
return json.Marshal(full)
}

type NestedObject struct {
Foo string `json:"foo"`
Bar bool `json:"bar"`
Expand Down
10 changes: 10 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5090,6 +5090,15 @@ spec:
type: object
x-kubernetes-embedded-resource: true
x-kubernetes-preserve-unknown-fields: true
unprunedFomType:
description: This tests that a type-level pruning maker works.
properties:
concreteField:
type: string
required:
- concreteField
type: object
x-kubernetes-preserve-unknown-fields: true
unprunedJSON:
properties:
bar:
Expand Down Expand Up @@ -5117,6 +5126,7 @@ spec:
- twoOfAKindPart0
- twoOfAKindPart1
- unprunedEmbeddedResource
- unprunedFomType
- unprunedJSON
type: object
status:
Expand Down
4 changes: 2 additions & 2 deletions pkg/rbac/zz_generated.markerhelp.go

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

0 comments on commit 72b140e

Please sign in to comment.