kubernets code-generator

REF:

how-to-generate-client-codes-for-kubernetes-custom-resource-definitions-crd

 

Firstly we need to install golang.

1. the api spec looks like:

# Definition
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:  
  name: hellotypes.foo.com
spec:  
  group: foo.com  
  version: v1
  scope: Namespaced  
  names:    
    kind: HelloType   
    shortNames: ht
    plural: hellotypes
    singular: hellotype
# HelloType
---
apiVersion: foo.com/v1
kind: HelloType 
metadata: 
  name: superman-hello
spec:
  message: hello world

 

2. create the project workspace

mkdir -p $GOPATH/src/github.com/superman/demo/pkg/apis/foo/v1
cd $GOPATH/src/github.com/superman/demo/pkg/apis

3.  create foo/v1/types.go

cat foo/v1/types.go
package v1

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

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

// HelloType is a top-level type
type HelloType struct {
        metav1.TypeMeta `json:",inline"`
        // +optional
        metav1.ObjectMeta `json:"metadata,omitempty"`

        // +optional
        Status HelloTypeStatus `json:"status,omitempty"`
        // This is where you can define
        // your own custom spec
        Spec HelloSpec `json:"spec,omitempty"`
}

// custom spec
type HelloSpec struct {
        Message string `json:"message,omitempty"`
}

// custom status
type HelloTypeStatus struct {
        Name string
}

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

// no client needed for list as it's been created in above
type HelloTypeList struct {
        metav1.TypeMeta `json:",inline"`
        // +optional
        metav1.ListMeta `son:"metadata,omitempty"`

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

4. create foo/v1/doc.go 

cat foo/v1/doc.go
// +k8s:deepcopy-gen=package
// +k8s:defaulter-gen=TypeMeta
// +groupName=foo.com

package v1

5. ceate foo/v1/register.go

cat foo/v1/register.go
package v1

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

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

var (
        SchemeBuilder      runtime.SchemeBuilder
        localSchemeBuilder = &SchemeBuilder
        AddToScheme        = localSchemeBuilder.AddToScheme
)

func init() {
        // We only register manually written functions here. The registration of the
        // generated functions takes place in the generated files. The separation
        // makes the code compile even when the generated files are missing.
        localSchemeBuilder.Register(addKnownTypes)
}

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

// Adds the list of known types to the given scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
        scheme.AddKnownTypes(
                SchemeGroupVersion,
                &HelloType{},
                &HelloTypeList{},
        )

        scheme.AddKnownTypes(
                SchemeGroupVersion,
                &metav1.Status{},
        )

        metav1.AddToGroupVersion(
                scheme,
                SchemeGroupVersion,
        )

        return nil
}

 

cd d $GOPATH/src/github.com/superman/demo/pkg/apis
. ├── apis │   └── foo │   └── v1 │   ├── doc.go │   ├── register.go │   ├── types.go

 

5. gen code

go get k8s.io/code-generator
go get k8s.io/apimachinery
cd $GOPATH/src/k8s.io/code-generator
./generate-groups.sh all \
    "github.com/superman/demo/pkg/client" \
    "github.com/superman/demo/pkg/apis" \
    foo:v1

 

 
cd $GOPATH/src/github.com/superman/demo/pkg/
tree
.
├── apis
│   └── foo
│       └── v1
│           ├── doc.go
│           ├── register.go
│           ├── types.go
│           └── zz_generated.deepcopy.go
└── client
    ├── clientset
    │   └── versioned
    │       ├── clientset.go
    │       ├── doc.go
    │       ├── fake
    │       │   ├── clientset_generated.go
    │       │   ├── doc.go
    │       │   └── register.go
    │       ├── scheme
    │       │   ├── doc.go
    │       │   └── register.go
    │       └── typed
    │           └── foo
    │               └── v1
    │                   ├── doc.go
    │                   ├── fake
    │                   │   ├── doc.go
    │                   │   ├── fake_foo_client.go
    │                   │   └── fake_hellotype.go
    │                   ├── foo_client.go
    │                   ├── generated_expansion.go
    │                   └── hellotype.go
    ├── informers
    │   └── externalversions
    │       ├── factory.go
    │       ├── foo
    │       │   ├── interface.go
    │       │   └── v1
    │       │       ├── hellotype.go
    │       │       └── interface.go
    │       ├── generic.go
    │       └── internalinterfaces
    │           └── factory_interfaces.go
    └── listers
        └── foo
            └── v1
                ├── expansion_generated.go
                └── hellotype.go

 

 REF:

Kubernetes Deep Dive: Code Generation for CustomResources (翻译

Extending Kubernetes: Create Controllers for Core and Custom Resources (total example)

Writing Kubernetes Custom Controllers

crd-code-generation

 How to generate client codes for Kubernetes Custom Resource Definitions (CRD)

code-generator  git hub (各种概念定义)

官方的sample-controller项目

github上的crd-code-generation项目

Accessing Kubernetes CRDs from the client-go package (存手工代码,没有自动生成)

Pod client set的实现

deep copy 

github上client-go官方项目工程

sample-controller示例

kubernetes-crd-example

创建和管理CRD的client库位于:github.com/kubernetes/…

kubernetes-operator-tutorial

writing-your-first-kubernetes-operator

 search "kubernetes operator example" or "kubernetes operator tutorial" for operator  auto gen

posted @ 2019-03-31 00:01  lvmxh  阅读(700)  评论(0编辑  收藏  举报