Skip to content
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

[Issue] no kind "CronTab" is registered for version "stable.example.com/v1" #17

Closed
yang-wang11 opened this issue Jan 18, 2022 · 8 comments

Comments

@yang-wang11
Copy link

I made a copy from cnat to crontab, below is its definition of CRD

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  name: crontabs.stable.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com
  # list of versions supported by this CustomResourceDefinition
  versions:
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
      additionalPrinterColumns:
      - name: schedule 
        type: string 
        jsonPath: .spec.cronSpec      
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                cronSpec:
                  type: string
                image:
                  type: string
                replicas:
                  type: integer
  # either Namespaced or Cluster
  scope: Namespaced
  names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: crontabs
    # singular name to be used as an alias on the CLI and for display
    singular: crontab
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: CronTab
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:
    - ct

  

the code of types.go

package v1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type CrontabSpecV1 struct {
	CronSpec string `json:"cronSpec"`
	Image    string `json:"image"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type Crontab struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec CrontabSpecV1 `json:"spec,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type CrontabList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`

	Items []Crontab `json:"items"`
}

and the code of register.go

// Define your schema name and the version
var SchemeGroupVersion = schema.GroupVersion{
	Group:   "stable.example.com",
	Version: "v1",
}

var (
	SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
	AddToScheme   = SchemeBuilder.AddToScheme
)

// Resource takes an unqualified resource and returns a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
	return SchemeGroupVersion.WithResource(resource).GroupResource()
}

func Kind(kind string) schema.GroupKind {
	return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// Adds the list of known types to the given scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
	scheme.AddKnownTypes(SchemeGroupVersion,
		&CrontabList{},
		&Crontab{},
	)
	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
	return nil
}

after the commandgenerate-groups.sh "deepcopy,client,informer,lister" be executed, following code raise an error

	client, e := versioned.NewForConfig(config)
	if e != nil {
		panic(e.Error())
	}
	crontab, err := client.StableV1().Crontabs("default").Get(context.TODO(), "cron-1", metav1.GetOptions{})

the error message was

no kind "CronTab" is registered for version "stable.example.com/v1" in scheme "pkg/crd/typedClient/client/clientset/versioned/scheme/register.go:15"

however the kind CronTab has already been registered in register.go, not sure why it happened. is there anyone who can help on that? thanks.

@mhausenblas
Copy link
Member

Thanks for raising this @yang-wang11 and I'm unsure I follow what exactly your steps were. You mentioned generate-groups.sh (I assume you're referring to https://github.com/kubernetes/code-generator?) Maybe @sttts has a better view?

@yang-wang11
Copy link
Author

yang-wang11 commented Jan 18, 2022

@mhausenblas @sttts yes, I follow the book step by step(chapter 4-5). I did the following changes:

  1. upgrade to v1 instead of v1alpha1 for CRD
  2. remove the sub-resource status for CRD

you may see below code generated from generate-groups.sh, it seems to look good.

var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme)
var ParameterCodec = runtime.NewParameterCodec(Scheme)
var localSchemeBuilder = runtime.SchemeBuilder{
	stablev1.AddToScheme,
}
var AddToScheme = localSchemeBuilder.AddToScheme

func init() {
	v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
	utilruntime.Must(AddToScheme(Scheme))
}

I tried to force execute its init function. the error does seem not to disappear.

_ "typedClient/client/clientset/versioned/scheme"

@sttts
Copy link
Contributor

sttts commented Jan 18, 2022

Note the different spelling: CronTab vs. Crontab. The scheme has the latter, the clients wants the former.

@sttts
Copy link
Contributor

sttts commented Jan 18, 2022

no kind "CronTab" is registered for version "stable.example.com/v1" in scheme "pkg/crd/typedClient/client/clientset/versioned/scheme/register.go:15"

Is this a copy-and-pasted error message? Am not sure where client-gen gets the CronTab type from. It is based on the Go type, not the CRD yaml. Hence, it should print a "Crontab" error, not "CronTab" 🤔

@yang-wang11
Copy link
Author

@sttts thanks for the help.
I'm a little confused, is that mean I need to rename my go type from Crontab to CronTab? like below

func addKnownTypes(scheme *runtime.Scheme) error {
	scheme.AddKnownTypes(SchemeGroupVersion,
		&CronTabList{},
		&CronTab{},
	)
	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
	return nil
}

@sttts
Copy link
Contributor

sttts commented Jan 18, 2022

If you add a type to a scheme, its Go type name is used as kind (via reflection).

Now I also understand what is going on. You are getting a CronTab from the server and the client tries to find a CronTab in its scheme. It only finds a Crontab, which is not helpful. So the server returns an unexpected object. Those two have to match.

Where Crontab is added to the scheme in register.go, you can give a custom name with the capital T. Then it will work too.

@yang-wang11
Copy link
Author

no kind "CronTab" is registered for version "stable.example.com/v1" in scheme "pkg/crd/typedClient/client/clientset/versioned/scheme/register.go:15"

Is this a copy-and-pasted error message? Am not sure where client-gen gets the CronTab type from. It is based on the Go type, not the CRD yaml. Hence, it should print a "Crontab" error, not "CronTab" 🤔

yes, this error message copy from the console.

@yang-wang11
Copy link
Author

@sttts @mhausenblas thanks for the help. it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants