Skip to content

Support overriding CRD type validation for structs #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joelanford opened this issue Feb 28, 2019 · 7 comments
Closed

Support overriding CRD type validation for structs #155

joelanford opened this issue Feb 28, 2019 · 7 comments
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@joelanford
Copy link
Member

Currently, it seems the validation type for structs is always assumed to be object.

func (b *APIs) parseObjectValidation(t *types.Type, found sets.String, comments []string, isRoot bool) (v1beta1.JSONSchemaProps, string) {
buff := &bytes.Buffer{}
props := v1beta1.JSONSchemaProps{
Type: "object",
Description: parseDescription(comments),
}

However, structs can be serialized into types other than object. Is there a way to override the type detection for structs?

I'm trying to do something like the following:

package status

// Conditions manages status conditions
type Conditions struct {
    conditions map[ConditionType]Condition
}

// MarshalJSON marshals Conditions into a JSON array 
func (c Conditions) MarshalJSON() ([]byte, error) {
    conditionList := []Condition{}
    for k, v := range c.conditions {
        conditionList = append(conditionList, v)
    }
    return json.Marshal(conditionList)
}
package v1alpha

// +k8s:openapi-gen=true
type MyAppStatus struct {
    // When the CRD generator creates the validations, it sees that Conditions
    // is a struct, but since it is marshalled as a JSON list, the default type detection
    // doesn't work.
    //
    // Perhaps support for a new Type validation tag could be added?
    // +kubebuilder:validation:Type=array
    Conditions status.Conditions `json:"conditions"`
}
@negz
Copy link

negz commented Mar 25, 2019

I have a similar use case for primitive types, in which I'd like to define an 'enum' as an integer in my Go code, but have it marshal to a JSON string. This is handy because it allows a sane default for the zero type of an enum. For example:

// WorkloadState represents the state of a workload.
type WorkloadState int

// MarshalJSON returns a JSON representation of a WorkloadState.
func (s WorkloadState) MarshalJSON() ([]byte, error) {
	return json.Marshal(s.String())
}

// UnmarshalJSON returns a WorkloadState from its JSON representation.
func (s *WorkloadState) UnmarshalJSON(b []byte) error {
	var ss string
	if err := json.Unmarshal(b, &ss); err != nil {
		return err
	}
	switch ss {
	case WorkloadStateSynced.String():
		*s = WorkloadStateSynced
	default:
		*s = WorkloadStateUnknown
	}
	return nil
}

// Workload states.
const (
	WorkloadStateUnknown WorkloadState = iota
	WorkloadStateSynced
)

// WorkloadStatus represents the status of a workload.
type WorkloadStatus struct {
	// ...
	State   WorkloadState           `json:"state"`
}

https://github.com/kubernetes-sigs/controller-tools/blob/45a8321/pkg/internal/codegen/parse/crd.go#L285

Currently the controller-tools CRD generation code does not appear to support overriding the type detected by reflect. For my use case we could perhaps use the same notation/pattern as openapi-gen's custom type definitions.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 23, 2019
@negz
Copy link

negz commented Jul 2, 2019

This is still something that would make life easier as a user of controller-tools. FWIW I've also wanted to use this proposed functionality for the original purpose @joelanford mentioned (i.e. storing conditions as a map, but marshalling them to an array) as well as the case I mentioned.

@negz
Copy link

negz commented Jul 2, 2019

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 2, 2019
negz added a commit to negz/crossplane that referenced this issue Jul 2, 2019
This effectively reverts crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
negz added a commit to negz/crossplane that referenced this issue Jul 2, 2019
This effectively reverts crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
negz added a commit to negz/crossplane that referenced this issue Jul 2, 2019
This effectively reverts crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
negz added a commit to negz/crossplane that referenced this issue Jul 5, 2019
This effectively reverts crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
jbw976 pushed a commit to jbw976/crossplane that referenced this issue Aug 20, 2019
This effectively reverts crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
jbw976 pushed a commit to jbw976/crossplane that referenced this issue Aug 20, 2019
This effectively reverts crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
jbw976 pushed a commit to jbw976/crossplane that referenced this issue Aug 20, 2019
This effectively reverts crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
hasheddan pushed a commit to hasheddan/crossplane-runtime-temp that referenced this issue Aug 20, 2019
This effectively reverts crossplane/crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
jbw976 pushed a commit to crossplane-contrib/provider-azure that referenced this issue Aug 30, 2019
This effectively reverts crossplane/crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
jbw976 pushed a commit to crossplane-contrib/provider-aws that referenced this issue Sep 1, 2019
This effectively reverts crossplane/crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.

Signed-off-by: Nic Cope <[email protected]>
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 30, 2019
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Oct 30, 2019
@joelanford
Copy link
Member Author

This was resolved in #200.

namku pushed a commit to namku/provider-aws that referenced this issue Mar 9, 2021
This effectively reverts crossplane/crossplane#325.

I still think it would be ideal to represent BindingState as an int with a sane
zero value that marshaled to a JSON string, but it is currently impossible to
override the type of the field that is used when generating an OpenAPI spec per
kubernetes-sigs/controller-tools#155. Until that issue
is closed it seems better to simply make this a string with a meaningless zero
value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests

4 participants