Kubernetes operator How do I generate the code for a response based on a custom type?

Source: Internet
Author: User
Tags k8s

share the main purpose of this article,

is how to use kubernetes to customize the type, such as sparkapplication, to use scripts to generate the code of the response

These codes are specifically for custom type Sparkapplication Object Services.

0. The final effect is as follows:

1. Test Environment Description

VMware + Centos 7

2, we need to write the document, I test need 3 (may not test enough)

Doc.go

Register.go

Types.go

3. Create go project, use K8s's characteristics to customize the type

The main content of Types.go is as follows (you need to customize the type according to your actual situation)

I'm just a part of it here:

Content of Doc.go:

+k8s:deepcopy-gen=package,register//Package V1beta1 is the v1beta1 version of the api.//+groupname=sparkoperatorpack Age V1beta1

Content of Register.go:

package v1beta1import  (   metav1  "K8s.io/apimachinery/pkg/apis/meta/v1"      "K8s.io/apimachinery/pkg/runtime"     "K8s.io/apimachinery/pkg/runtime/schema"      "Xingej-go/xingej-k8s-spark/sparkoperatoronk8sforapp/pkg/apis/spark") var  (    Schemebuilder = runtime. Newschemebuilder (addknowntypes)    addtoscheme   = schemebuilder.addtoscheme )// schemegroupversion is the group version used to register  These objects.var schemegroupversion = schema. Groupversion{group: spark. Groupname, version: spark. Version}func init ()  {   schemebuilder.register (Adddefaultingfunc)}// Resource  takes an unqualified resource and returns a Group-qualified  Groupresource.func resource (resource string)  schema. GroupresourcE {   return schemegroupversion.withresource (Resource). Groupresource ()}func addknowntypes (scheme *runtime. Scheme)  error {   scheme. Addknowntypes (schemegroupversion,      &sparkapplication{},       &SparkApplicationList{},   )    metav1. Addtogroupversion (scheme, schemegroupversion)    return nil}func  Adddefaultingfunc (Scheme *runtime. Scheme)  error {   return registerdefaults (scheme)}

Note that when you write the declaration function addknowntypes, you throw an exception, don't worry, because the lack of files (the root cause should be your custom type, and not added to the schema),

After the code is generated, it disappears automatically.


Once you have written the above file, you can generate the code using the script.

4, I wrote a script according to Git reference instance, the following update-codegen.sh

Specific Description:

Four parameters:

The first parameter: All, is to say, to generate all the modules, such as clientset,informers,listers, etc.

Second parameter: xingej-go/xingej-k8s-spark/spark-operator-on-k8s-for-app/pkg/client This is the directory where you want to generate the code, the name of the directory is the client, It's called generated.

The third parameter: Xingej-go/xingej-k8s-spark/spark-operator-on-k8s-for-app/pkg/apis This directory is your own creation, including at least types.go that directory

Fourth parameter: "SPARK:V1BETA1": This is the directory, Spark is the directory under the APIs, V1beta1 is the directory under Spark

Scripting ideas:

is to enter vendor under the Code-generator directory, using the generate-groups.sh script inside, generate code.

It's basically the same as the scripting idea in the reference instance on Git.

Other than that:

After running generate-groups.sh, 5 tools, such as Client-gen,deepcopy-gen, will be generated in the bin directory of the Gopath directory.

is to use these tools to generate code.


5, if you want to generate zz_generated.defaults.go files, you need to do two things:

A, on a custom type, add more than one tag, such as //+k8s:defaulter-gen=true

b, modify the generate-groups.sh script, add the corresponding module, such as the following form:

In fact, it is also based on the IF statement above, the reference is written.

Of course, after the build, the contents of the Zz_generated.defaults.go file appear to be noncompliant, as follows:

Code generated by Defaulter-gen. does not edit.package V1beta1import (runtime "K8s.io/apimachinery/pkg/runtime")//Reg  Isterdefaults adds defaulters functions to the given scheme.//public to allow building arbitrary schemes.//all generated Defaulters is Covering-they call all nested Defaulters.func registerdefaults (scheme *runtime. Scheme) error {return nil}

In the generated function, nothing is done.

If you feel that you cannot satisfy the requirements, you can also add the necessary logic in the function registerdefaults

6, the main problems encountered:

error:failed executing generator:some packages had errors:

Type K8s.io/apimachinery/pkg/runtime. Object in k8s.deepcopy-gen:interfaces tag of type k8s.io/apimachinery/pkg/runtime. Object is ont a interface, but: ""

Problem description, label K8s.io/apimachinery/pkg/runtime. There is a problem with the Object type, not an interface;

It's the place declared below.

Resolution:

Because of the path problem, a vendor is added less

For specific reasons, it's not clear (and it doesn't take much time), because this is the third time I've generated the code, the first two environments have been removed,

Add the Venddor on it as shown in:

, however, there is another problem, that is, in the generated file Zz_generated.deepcopy.go, the import package will report an exception:


Hope to help others.









Kubernetes operator How do I generate the code for a response based on a custom type?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.