k8s自定义资源类型代码自动生成

用以下命令生成代码:

./generate-groups.sh all "github.com/openshift-evangelist/crd-code-generation/pkg/client" "github.com/openshift-evangelist/crd-code-generation/pkg/apis" "ingressgroup:v1"

第一个报错

生成代码报错:

Generating deepcopy funcs
F0910 19:18:35.552948   12153 main.go:82] Error: Failed making a parser: unable to add directory "github.com/openshift-evangelist/crd-code-generation/pkg/client": unable to import "github.com/asdfsx/getkubeconfig/pkg/apis/example/v1": cannot find package "github.com/openshift-evangelist/crd-code-generation/pkg/client" in any of:
        D:/Program Files/Go/go103/src/github.com/openshift-evangelist/crd-code-generation/pkg/client (from $GOROOT)
        D:/SoftwareAndProgram/program/Go/Development/src/github.com/openshift-evangelist/crd-code-generation/pkg/client (from $GOPATH)

这个问题可以参考issue

image.png

可以参考这个文章:
https://medium.com/@trstringer/create-kubernetes-controllers-for-core-and-custom-resources-62fc35ad64a3

由于该链接国内访问比较困难,故转载到了这里:
https://www.jianshu.com/p/dcfe6eac4152
总而言之,就是目录有问题。比如:

./generate-groups.sh all k8s.io/sample-controller/pkg/client k8s.io/sample-controller/pkg/apis samplecontroller:v1alpha1

那么k8s.io/sample-controller/pkg/apis目录下就需要有一个samplecontroller/v1alpha1的文件夹,最终生成的文件会被放到k8s.io/sample-controller/pkg/client目录下。

[root@10 code-generator]# ./generate-groups.sh all k8s.io/sample-controller/pkg/client k8s.io/sample-controller/pkg/apis samplecontroller:v1alpha1
Generating deepcopy funcs
Generating clientset for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/client/clientset
Generating listers for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/client/listers
Generating informers for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/client/informers
[root@10 code-generator]# ll $GOPATH/src/k8s.io/sample-controller/pkg/
总用量 0
drwxr-xr-x 3 root root 30 4月  16 21:42 apis
drwxr-xr-x 5 root root 55 4月  16 21:58 client
[root@10 code-generator]# ll $GOPATH/src/k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1/
总用量 8
-rw-r--r-- 1 root root 1462 4月  16 21:42 types.go
-rw-r--r-- 1 root root 3272 4月  16 21:57 zz_generated.deepcopy.go
[root@10 code-generator]# ll $GOPATH/src/k8s.io/sample-controller/pkg/client
总用量 0
drwxr-xr-x 3 root root 23 4月  16 21:57 clientset
drwxr-xr-x 3 root root 30 4月  16 21:58 informers
drwxr-xr-x 3 root root 30 4月  16 21:58 listers

第二个报错:

Generating deepcopy funcs
F1104 02:57:44.419529      35 main.go:82] 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 not an interface, but: ""
goroutine 1 [running]:

这个报错是因为k8s.io/apimachinery这个包目录结构不对,放到vendor目录下找不到,必须放到$GOPATH下的src/k8s.io/apimachinery,具体参考issue

image.png

我解决了这个问题 。这不起作用,除非k8s.io/apimachinery在GOPATH中,如果它只是在vendor目录下,那么deepcopy无法找到它。至少,这需要在某处记录。如果在vendor目录下也能正常工作,那将会很棒。

目录结构如下:
$GOPATH/src


image.png

$GOPATH/src/github.com/


image.png

第三个报错

image.png
Generating deepcopy funcs
F0221 09:54:08.335328   26316 main.go:82] Error: Failed executing generator: som                                          e packages had errors:
errors in package "github.com/openshift-evangelist/crd-code-generation/pkg/apis/                                          ingressgroup/v1":
unable to format file "D:\\SoftwareAndProgram\\program\\Go\\Development\\src\\gi                                          thub.com\\openshift-evangelist\\crd-code-generation\\pkg\\apis\\ingressgroup\\v1                                          \\zz_generated.deepcopy.go" (The filename, directory name, or volume label synta                                          x is incorrect.).

windows上执行报这个错,需要在linux上执行generate-groups.sh 脚本。

最终生成如下:

image.png
[root@master-192 code-generator]# dos2unix generate-groups.sh 
dos2unix: converting file generate-groups.sh to Unix format ...
[root@master-192 code-generator]# ./generate-groups.sh all "github.com/openshift-evangelist/crd-code-generation/pkg/client" "github.com/openshift-evangelist/crd-code-generation/pkg/apis" "ingressgroup:v1"
Generating deepcopy funcs
Generating clientset for ingressgroup:v1 at github.com/openshift-evangelist/crd-code-generation/pkg/client/clientset
Generating listers for ingressgroup:v1 at github.com/openshift-evangelist/crd-code-generation/pkg/client/listers
Generating informers for ingressgroup:v1 at github.com/openshift-evangelist/crd-code-generation/pkg/client/informers

最终生成目录结构如下:

[root@master-192 crd-code-generation]# pwd
/root/Work/programmer/go/gopath/src/github.com/openshift-evangelist/crd-code-generation
[root@master-192 crd-code-generation]# tree
.
└── pkg
    ├── apis
    │   └── ingressgroup
    │       ├── register.go
    │       └── 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
        │           └── ingressgroup
        │               └── v1
        │                   ├── doc.go
        │                   ├── fake
        │                   │   ├── doc.go
        │                   │   ├── fake_ingressgroup_client.go
        │                   │   └── fake_ingressgroup.go
        │                   ├── generated_expansion.go
        │                   ├── ingressgroup_client.go
        │                   └── ingressgroup.go
        ├── informers
        │   └── externalversions
        │       ├── factory.go
        │       ├── generic.go
        │       ├── ingressgroup
        │       │   ├── interface.go
        │       │   └── v1
        │       │       ├── ingressgroup.go
        │       │       └── interface.go
        │       └── internalinterfaces
        │           └── factory_interfaces.go
        └── listers
            └── ingressgroup
                └── v1
                    ├── expansion_generated.go
                    └── ingressgroup.go

你可能感兴趣的:(k8s自定义资源类型代码自动生成)