zoukankan      html  css  js  c++  java
  • Kubernetes-CRD-Demo

    创建工程

    利用go module创建模块

    root@ubuntu:~/go_learn/proj1# go mod init  demo-controller
    go: creating new go.mod: module demo-controller
    root@ubuntu:~/go_learn/proj1# ls
    go.mod
    root@ubuntu:~/go_learn/proj1# mkdir  demo-controller
    root@ubuntu:~/go_learn/proj1# 

    编写pkg/api

    code-generator要求工程的目录结构满足一定的规则

    • 目录结构必须为pkg/apis/xxx/yyy/
    • 包含如下文件
      1. doc.go
      2. register.go
      3. types.go

     hack

    这部分内容,我们直接从kubernetes官方示例(sample-controller)中拷贝即可

    执行脚本创建pkg/generated

    在执行脚本前,我们需要先下载一个依赖的脚本,就是update-codegen.sh中提到的generate-groups.sh

    root@ubuntu:~/go_learn/proj1/demo-controller/hack# ls
    boilerplate.go.txt  custom-boilerplate.go.txt  tools.go  update-codegen.sh  verify-codegen.sh
    root@ubuntu:~/go_learn/proj1/demo-controller/hack# cd ../../
    root@ubuntu:~/go_learn/proj1# ls
    demo-controller  go.mod
    root@ubuntu:~/go_learn/proj1# cd $GOPATH
    root@ubuntu:/opt/gopath# mkdir -p src/k8s.io
    root@ubuntu:/opt/gopath# cd src/k8s.io
    root@ubuntu:/opt/gopath/src/k8s.io# git clone https://github.com/kubernetes/code-generator.git
    Cloning into 'code-generator'...
    
    remote: Enumerating objects: 10133, done.
    remote: Counting objects: 100% (511/511), done.
    remote: Compressing objects: 100% (182/182), done.
    remote: Total 10133 (delta 340), reused 483 (delta 325), pack-reused 9622
    Receiving objects: 100% (10133/10133), 9.28 MiB | 9.40 MiB/s, done.
    Resolving deltas: 100% (5440/5440), done.
    root@ubuntu:/opt/gopath/src/k8s.io# 
    root@ubuntu:/opt/gopath/src/k8s.io# cd ~/go_learn/proj1/
    root@ubuntu:~/go_learn/proj1# ls
    demo-controller  go.mod
    root@ubuntu:~/go_learn/proj1# cd ~/go_learn/proj1/demo-controller/
    root@ubuntu:~/go_learn/proj1/demo-controller# ls
    hack  pkg
    root@ubuntu:~/go_learn/proj1/demo-controller# bash hack/update-codegen.sh
    go: downloading k8s.io/gengo v0.0.0-20201214224949-b6c5ce23f027
    go: downloading golang.org/x/tools v0.1.2
    go: downloading golang.org/x/mod v0.4.2
    go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
    Generating deepcopy funcs
    Generating clientset for democontroller:v1 at demo-controller/pkg/generated/clientset
    Generating listers for democontroller:v1 at demo-controller/pkg/generated/listers
    Generating informers for democontroller:v1 at demo-controller/pkg/generated/informers
    root@ubuntu:~/go_learn/proj1/demo-controller# 
    root@ubuntu:~/go_learn/proj1/demo-controller# ls pkg/
    apis
    root@ubuntu:~/go_learn/proj1/demo-controller# ls pkg/apis/
    democontroller
    root@ubuntu:~/go_learn/proj1/demo-controller# ls pkg/apis/democontroller/
    v1
    root@ubuntu:~/go_learn/proj1/demo-controller# ls pkg/apis/democontroller/v1/
    doc.go  register.go  types.go
    root@ubuntu:~/go_learn/proj1/demo-controller# ls
    hack  pkg
    root@ubuntu:~/go_learn/proj1/demo-controller# cd ..
    root@ubuntu:~/go_learn/proj1# ls
    demo-controller  go.mod
    root@ubuntu:~/go_learn/proj1# ls demo-controller/pkg/generated/listers
    ls: cannot access 'demo-controller/pkg/generated/listers': No such file or directory
    root@ubuntu:~/go_learn/proj1# ls demo-controller/pkg/generated/ 
    ls: cannot access 'demo-controller/pkg/generated/': No such file or directory   --什么都没有
    root@ubuntu:~/go_learn/proj1# ls demo-controller/pkg/
    apis
    root@ubuntu:~/go_learn/proj1# ls demo-controller/pkg/apis/
    democontroller
    root@ubuntu:~/go_learn/proj1# 

    原来是go mod init demo-controller使用了错误的目录

    root@ubuntu:~/go_learn/proj1/demo-controller# go mod init  demo-controller
    go: creating new go.mod: module demo-controller
    root@ubuntu:~/go_learn/proj1/demo-controller# ls
    go.mod  hack  pkg
    root@ubuntu:~/go_learn/proj1/demo-controller#  bash hack/update-codegen.sh
    Generating deepcopy funcs
    Generating clientset for democontroller:v1 at demo-controller/pkg/generated/clientset
    Generating listers for democontroller:v1 at demo-controller/pkg/generated/listers
    Generating informers for democontroller:v1 at demo-controller/pkg/generated/informers
    root@ubuntu:~/go_learn/proj1/demo-controller# ls demo-controller/pkg/generated
    ls: cannot access 'demo-controller/pkg/generated': No such file or directory
    root@ubuntu:~/go_learn/proj1/demo-controller# ls demo-controller/pkg/
    ls: cannot access 'demo-controller/pkg/': No such file or directory
    root@ubuntu:~/go_learn/proj1/demo-controller# ls
    go.mod  go.sum  hack  pkg
    root@ubuntu:~/go_learn/proj1/demo-controller# ls pkg/
    apis  generated
    root@ubuntu:~/go_learn/proj1/demo-controller# 

    root@ubuntu:~/go_learn/proj1/demo-controller# cat hack/update-codegen.sh
    #!/usr/bin/env bash

    # Copyright 2017 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.

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

    SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    CODEGEN_PKG=${GOPATH}/src/k8s.io/code-generator

    # generate the code with:
    # --output-base because this script should also be able to run inside the vendor dir of
    # k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
    # instead of the $GOPATH directly. For normal projects this can be dropped.
    bash "${CODEGEN_PKG}"/generate-groups.sh "deepcopy,client,informer,lister"
    demo-controller/pkg/generated demo-controller/pkg/apis
    democontroller:v1
    # --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.."
    # --go-header-file "${SCRIPT_ROOT}"/hack/boilerplate.go.txt

    # To use your own boilerplate text append:
    # --go-header-file "${SCRIPT_ROOT}"/hack/custom-boilerplate.go.txt

    更改

    Kubernetes-CRD-Demo

    good code-generator使用

    code-generator使用

  • 相关阅读:
    上传文件大小问题
    中检测到有潜在危险的 Request.Form 值
    Session
    C#浏览器中在线操作文档
    数据导入
    文件上传(插件版)和下载
    Leetcode练习(Python):递归类:面试题10- I. 斐波那契数列:写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项。斐波那契数列的定义如下: F(0) = 0,   F(1) = 1 F(N) = F(N
    Leetcode练习(Python):递归类:面试题07. 重建二叉树:输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
    Leetcode练习(Python):递归类:面试题 16.11. 跳水板:你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter,长度较长的木板长度为longer。你必须正好使用k块木板。编写一个方法,生成跳水板所有可能的长度。 返回的长度需要从小到大排列。
    Leetcode练习(Python):递归类:面试题 08.06. 汉诺塔问题:在经典汉诺塔问题中,有 3 根柱子及 N 个不同大小的穿孔圆盘,盘子可以滑入任意一根柱子。一开始,所有盘子自上而下按升序依次套在第一根柱子上(即每一个盘子只能放在更大的盘子上面)。
  • 原文地址:https://www.cnblogs.com/dream397/p/14977387.html
Copyright © 2011-2022 走看看