code-generator introduction - CRD client generation tool

Table of contents

background

framework construction

code injection

test


background

client-goCorresponding clientsetsums are provided for each k8s built-in resource informer. If you need to monitor and manipulate custom resource objects, there are two methods——

  • Use client-gothe provided dynamicClientto manipulate custom resource objects, of course, because dynamicClientit is based on RESTClientthe implementation, it can be used RESTClientto achieve the same purpose.
  • Use conde-generatorto generate client code, so that you can client-gomonitor and manipulate custom resources in the same way as provided for k8s built-in resource objects.

Project address: https://github.com/kubernetes/code-generator

framework construction

To generate the overall client code first, you first need to lay down the framework.

A blog post from Red Hat——

Kubernetes Deep Dive: Code Generation for CustomResources

For the convenience of operation, I created the project under the environment variable $GOPATH directory, and declared pkg as my github repository address in go.mod: 

module github.com/octoboy233/ops-operator

Since it is an Operator, it also needs to introduce dependencies first

go get -u sigs.k8s.io/controller-runtime

Copy the code https://github.com/kubernetes/code-generator/tree/master/examples/crd/apis/example/v1 and put it under ops-operator/pkg/extensions/operation/v1. ops-operator is my project name, extensions is the header in GVK Group, operation is the name of my CRD, and v1 is the Version of GVK.

Then you need to make some customized modifications according to the declaration fields of the CRD. The code here is mainly used for some deserialization operations on objects during the process of calling client-go by the Controller, as well as the exposed functions and methods registered to the Manager’s Scheme. .

code injection

Copy https://github.com/kubernetes/code-generator/releases/tag/v0.22.18-rc.0 to $GOPATH, you need to use a script in the source code to generate code.

Finally, a certain parameter is passed in through the script to execute the command

$GOPATH/src/k8s.io/code-generator/generate-groups.sh all  github.com/octoboy233/ops-operator/pkg/client github.com/octoboy233/ops-operator/pkg/extensions operation:v1

Some variable parameters in the text have been mentioned above.

test

There is already a stock object in the cluster environment. Next, write a piece of test code to obtain its structured data.

% kubectl get ops
NAME    AGE
myops   45m

Brainless Get is over~

package main

import (
	"context"
	"fmt"
	v1 "github.com/octoboy233/ops-operator/pkg/client/clientset/versioned/typed/operation/v1"
	"github.com/octoboy233/ops-operator/pkg/kube"
	v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
	cli, _ := v1.NewForConfig(kube.InitConfig())
	r, _ := cli.Operations("default").Get(context.Background(), "myops", v12.GetOptions{})
	fmt.Println(r)
}

In addition, looking at the generated code directory, it also supports the extension of informer and lister other than Clientset.

So far, through the code generation tool, the method of monitoring and manipulating custom resources for the CRD in the operator is completed.

Guess you like

Origin blog.csdn.net/kingu_crimson/article/details/129382388