operator-sdk: Invalid CRD generated when using pkg/status.Conditions

What did you do?

  • Built operator-sdk from master
  • Used pkg/status.Conditions in my CRD

What did you expect to see? A valid CRD with status.conditions as a array of condition objects

What did you see instead? Under which circumstances?

The CustomResourceDefinition "..." is invalid: spec.validation.openAPIV3Schema.properties[status].properties[conditions].items: Required value: must be specified

The CRD has an additionalProperties under the conditions object instead of items.

Environment

  • operator-sdk version: "v0.15.0-80-g373ebf37", commit: "373ebf3751b61a37f3589687043b0eda095cf156"

  • go version: go1.14 darwin/amd64

  • Kubernetes version information:

Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.3", GitCommit:"06ad960bfd03b39c8310aaf92d1e7c12ce618213", GitTreeState:"clean", BuildDate:"2020-02-13T18:08:14Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.2", GitCommit:"59603c6e503c87169aea6106f57b9f242f64df89", GitTreeState:"clean", BuildDate:"2020-01-18T23:22:30Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"linux/amd64"}
  • Kubernetes cluster kind: kubeadm 1.17

  • Are you writing your operator in ansible, helm, or go?

Go

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 21 (16 by maintainers)

Most upvoted comments

H @joelanford and @kraman,

I could reproduce the issue 😃

To do that we need to use the sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.5 to generate the CRD instead of the current implementation used in the SDK.

Following the steps.

1- Define the type as:

// MemcachedSpec defines the desired state of Memcached
// +k8s:openapi-gen=true
type MemcachedSpec struct {
	// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
	// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
	// Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html

	// Size is the size of the memcached deployment
	Size int32 `json:"size"`

	// +kubebuilder:validation:type=array
	Conditions status.Conditions `json:"conditions"`
}

2 - Add the Makefile with: (See: https://github.com/camilamacedo86/operator-sdk-samples/blob/test-issue/go/memcached-operator/Makefile#L87)

# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

# Generate manifests e.g. CRD, RBAC etc.
.PHONY: manifests
manifests: controller-gen
	$(CONTROLLER_GEN) paths=./... crd:trivialVersions=true output:crd:artifacts:config=deploy/crds

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
	go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.5
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

3 - Run the target make manifests (to gen with controller-gen)

4 - Apply the CRD gen by the controller-gen@v0.2.5

 $ make apply-gen
kubectl create namespace memcached
namespace/memcached created
kubectl apply -f deploy/crds/cache.example.com_memcacheds.yaml -n memcached
The CustomResourceDefinition "memcacheds.cache.example.com" is invalid: 
* spec.validation.openAPIV3Schema.properties[spec].properties[conditions].items: Required value: must be specified
* spec.validation.openAPIV3Schema.properties[status].properties[conditions].items: Required value: must be specified
make: [Makefile:23: apply-gen] Error 1 (ignored)

So, now will be are working to identify the root cause of this issue and solve it.