k8s client-gen

client-gen

When we want to operate the kubernetes resources, like the crds that we defined and the related objects we created. We should use some tools to operate these resource.

For now, most of coder use "code-generator" to generate some related function to help us to operate the objects, like "deepcopy-gen","informer-gen","lister-gen" and so on.

Step by step to create a clientset for your crd

1. Create one repo

For example, I create one repo and I have already put all my necessary components there. The details about how to create a repo please see the mannual of github

2. Define our client

In the repo, we can use go.mod to manage our project, and we should install some dependency repo:

 go mod init <your-project-name>
 go get k8s.io/apimachinery
 go get k8s.io/client-go
 go get k8s.io/code-generator

and then we should create some important directories. for my project:

mkdir -p pkg/apis/nodehealth/v1alpha1 
cd pkg/apis/nodehealth/v1alpha1 

in /v1alpha1 we put some crd definition files. we create file

doc.go

in doc.go

// +k8s:deepcopy-gen=package
// +groupName=compute.company.com

package v1alpha1

create file types.go

package v1alpha1

import (
	"time"

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

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

// NodeHealth is a specification for a NodeHealth resource
type NodeHealth struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   NodeHealthSpec   `json:"spec"`
	Status NodeHealthStatus `json:"status"`
}

type Ping struct {
	PingFrequency *int32 `json:"pingFrequency"`
	PingTimes     *int32 `json:"pingTime"`
}

// NodeHealthSpec is the spec for a NodeHealth resource
type NodeHealthSpec struct {
	Replicas     *int32 `json:"replicas"`
	NodeName     string `json:"nodeName"`
	Cluster      string `json:"cluster"`
	CheckSetting Ping   `json:"ping"`
}

type NodeStatus struct {
	LastHealthStatus       string    `json:"lastHealthStatus"`
	LastCheckTimeStamp     time.Time `json:"lastCheckTimeStamp"`
	LastHealthyTimeStamp   time.Time `json:"lastHealthyTimeStamp"`
	LastUnHealthyTimeStamp time.Time `json:"lastUnHealthyTimeStamp"`
}

// NodeHealthStatus is the status for a NodeHealth resource
type NodeHealthStatus struct {
	AvailableReplicas int32        `json:"availableReplicas"`
	HealthStatus      []NodeStatus `json:"healthStatus"`
}

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

// NodeHealthList is a list of NodeHealth resources
type NodeHealthList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata"`

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

3. create a vendor to store the code-generator codes

  1. we should create a vendor dir
go mod vendor
  1. in the root of the project, make all the code in vendor readable
chmod -R 777 vendor

4. create code-gen scripts:

mkdir hack && cd hack

create tool.go

// +build tools

/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// This package imports things required by build scripts, to force `go mod` to see them as dependencies
package tools

import _ "k8s.io/code-generator"

and create code-gen script: "update-codegen.sh"

#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

package_path="/Users/yuanmh/Desktop/code/k8s-crds-clientsets"


../vendor/k8s.io/code-generator/generate-groups.sh "deepcopy,client,informer,lister" \
../pkg/client \
../pkg/apis \
  nodehealth:v1alpha1 \
  --go-header-file ../hack/boilerplate.go.txt

code-generator hase some commands options. If you want to know more about it, you can go to the doc. To be honest, the doc sucks!!! I cannot get any useful information there. I have encountered many problems, and found the way by myself.

5. execute the script to create lister, informer and deepcopy funcs

cd hack
./update-codegen.sh    
Generating deepcopy funcs
Generating clientset for nodehealth:v1alpha1 at ../pkg/client/clientset
Generating listers for nodehealth:v1alpha1 at ../pkg/client/listers
Generating informers for nodehealth:v1alpha1 at ../pkg/client/informers

We can see that the funcs have been created succesfully in the directories.

OK, that's all about generate crd's clients, next we will discover how to use these clients, informers, listers to operate our crds.

https://blog.openshift.com/kubernetes-deep-dive-code-generation-customresources/
git: https://github.com/kubernetes/code-generator

posted on 2021-08-04 21:59  程序袁先森  阅读(992)  评论(0编辑  收藏  举报