教程办法

源码位置:${GOPATH}/src/k8s-crd

k8s.io/code-generator位置:${GOPATH}/src/k8s.io

按照code-generator的提示操作的,报错了

[root@node-01 code-generator]# ./generate-groups.sh all demo/pkg/client \
> demo/pkg/apis \
> foo:v1
Generating deepcopy funcs
F0319 09:01:37.153577   20541 main.go:82] Error: Failed making a parser: unable to add directory "demo/pkg/apis/foo/v1": unable to import "demo/pkg/apis/foo/v1": go/build: importGo demo/pkg/apis/foo/v1: exit status 1
can't load package: package demo/pkg/apis/foo/v1: malformed module path "demo/pkg/apis/foo": missing dot in first path element

不知道什么原因。

笨办法

找到k8s.io/code-generator代码的运行脚本:generate-groups.sh

脚本里面其实是根据参数的不同执行了不同的代码,遇是把需要运行的代码编译成二进制。

看脚本是需要以下程序

deepcopy-gen
client-gen
lister-gen
informer-gen

我把这4个 程序全部编译好了放在了${GOPATH}/src下。

[root@node-01 src]# pwd
/usr/local/gopath/src
[root@node-01 src]# ls
client-gen  deepcopy-gen  demo  github.com  informer-gen  k8s.io  lister-gen

--help查看参数

可以看到这4个程序都需要哪些参数

[root@node-01 src]# ./client-gen --help
Usage of ./client-gen:
      --add_dir_header                     If true, adds the file directory to the header
      --alsologtostderr                    log to standard error as well as files
      --build-tag string                   A Go build tag to use to identify files generated by this command. Should be unique. (default "ignore_autogenerated")
      --clientset-api-path string          the value of default API HTTP path, starting with / and without trailing /. (default "/apis")
  -n, --clientset-name string              the name of the generated clientset package. (default "internalclientset")
      --clientset-only                     when set, client-gen only generates the clientset shell, without generating the individual typed clients
      --fake-clientset                     when set, client-gen will generate the fake clientset that can be used in tests (default true)
  -h, --go-header-file string              File containing boilerplate header text. The string YEAR will be replaced with the current 4-digit year. (default "/usr/local/gopath/src/k8s.io/code-generator/hack/boilerplate.go.txt")
      --included-types-overrides strings   list of group/version/type for which client should be generated. By default, client is generated for all types which have genclient in types.go. This overrides that. For each groupVersion in this list, only the types mentioned here will be included. The default check of genclient will be used for other group versions. (default [])
      --input strings                      group/versions that client-gen will generate clients for. At most one version per group is allowed. Specified in the format "group1/version1,group2/version2...". (default [])
      --input-base string                  base path to look for the api group. (default "k8s.io/kubernetes/pkg/apis")
  -i, --input-dirs strings                 Comma-separated list of import paths to get input types from.
      --log_backtrace_at traceLocation     when logging hits line file:N, emit a stack trace (default :0)
      --log_dir string                     If non-empty, write log files in this directory
      --log_file string                    If non-empty, use this log file
      --log_file_max_size uint             Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
      --logtostderr                        log to standard error instead of files (default true)
  -o, --output-base string                 Output base; defaults to $GOPATH/src/ or ./ if $GOPATH is not set. (default "/usr/local/gopath/src")
  -O, --output-file-base string            Base name (without .go suffix) for output files.
  -p, --output-package string              Base package path. (default "k8s.io/kubernetes/pkg/client/clientset_generated/")
      --plural-exceptions strings          list of comma separated plural exception definitions in Type:PluralizedType form (default [Endpoints:Endpoints])
      --skip_headers                       If true, avoid header prefixes in the log messages
      --skip_log_headers                   If true, avoid headers when opening log files
      --stderrthreshold severity           logs at or above this threshold go to stderr (default 2)
  -v, --v Level                            number for the log level verbosity
      --verify-only                        If true, only verify existing output, do not write anything.
      --vmodule moduleSpec                 comma-separated list of pattern=N settings for file-filtered logging
pflag: help requested

生成客户端代码

以demo项目为例

// 用deepcopy-gen生成
./deepcopy-gen --input-dirs demo/pkg/apis/foo/v1 --output-package demo/pkg/client

执行完后,进入demo/pkg/apis/foo/v1,可以看到已经生成了deepcopy_generated.go,符合要求了。
再以相同的方法执行client-gen, informer-gen, lister-gen

[root@node-01 src]# ./client-gen --input-dirs demo/pkg/apis/foo/v1 --output-package demo/pkg/client
[root@node-01 src]# ./informer-gen --input-dirs demo/pkg/apis/foo/v1 --output-package demo/pkg/client
[root@node-01 src]# ./lister-gen --input-dirs demo/pkg/apis/foo/v1 --output-package demo/pkg/client

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐