kubebuilder实践笔记(3) - 修改CRD中的字段

这一篇是在上一篇《kubebuilder实践笔记(2) - 入门体验_湾区的候鸟的博客-CSDN博客》成功的基础上,再做一点点变化,给CRD加上几个自定义字段,使用kubebuilder重新生成代码,并部署到K8S中,修改资源对象,并观察自定义Controller里面的日志输出。

学习目标:

CRD加上几个自定义字段,使用kubebuilder重新构建,重新部署。

操作步骤:

第1步:修改guestbook_types.go文件,把GuestbookSpec与GuestbookStatus结构体中,改为下面5个字段。

注意:修改时,要那些注释带上,否则生成代码时会少了一些方法。

第2步:重新生成代码,运行make manifests。

注意:每次修改这个文件都要重新生成。

[root@workstation guestbook]# make manifests
/root/kubebuilder/projects/guestbook/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
[root@workstation guestbook]# 

第3步:把CRD安装到k8s集群中,执行:make install.

[root@workstation guestbook]# make install
/root/kubebuilder/projects/guestbook/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
/root/kubebuilder/projects/guestbook/bin/kustomize build config/crd | kubectl apply -f -
customresourcedefinition.apiextensions.k8s.io/guestbooks.webapp.my.domain created

安装完成后,可以看到k8s中的CRD已创建,里面已经有了对上面5个自定义字段的Schema: 

[root@master ~]# kubectl describe crd/guestbooks.webapp.my.domain
Name:         guestbooks.webapp.my.domain
Namespace:    
Labels:       <none>
Annotations:  controller-gen.kubebuilder.io/version: v0.8.0
API Version:  apiextensions.k8s.io/v1
Kind:         CustomResourceDefinition
Metadata:
  Creation Timestamp:  2022-04-01T00:51:08Z
  Generation:          1
  Managed Fields:
    API Version:  apiextensions.k8s.io/v1
    Fields Type:  FieldsV1
    fieldsV1:
      f:status:
        f:acceptedNames:
          f:kind:
          f:listKind:
          f:plural:
          f:singular:
        f:conditions:
          k:{"type":"Established"}:
            .:
            f:lastTransitionTime:
            f:message:
            f:reason:
            f:status:
            f:type:
          k:{"type":"NamesAccepted"}:
            .:
            f:lastTransitionTime:
            f:message:
            f:reason:
            f:status:
            f:type:
    Manager:      Go-http-client
    Operation:    Update
    Subresource:  status
    Time:         2022-04-01T00:51:08Z
    API Version:  apiextensions.k8s.io/v1
    Fields Type:  FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .:
          f:controller-gen.kubebuilder.io/version:
          f:kubectl.kubernetes.io/last-applied-configuration:
      f:spec:
        f:conversion:
          .:
          f:strategy:
        f:group:
        f:names:
          f:kind:
          f:listKind:
          f:plural:
          f:singular:
        f:scope:
        f:versions:
    Manager:         kubectl-client-side-apply
    Operation:       Update
    Time:            2022-04-01T00:51:08Z
  Resource Version:  743922
  UID:               9e84e11e-cd5a-4c2c-a295-5e1a5b38fcbb
Spec:
  Conversion:
    Strategy:  None
  Group:       webapp.my.domain
  Names:
    Kind:       Guestbook
    List Kind:  GuestbookList
    Plural:     guestbooks
    Singular:   guestbook
  Scope:        Cluster
  Versions:
    Name:  v1
    Schema:
      openAPIV3Schema:
        Description:  Guestbook is the Schema for the guestbooks API
        Properties:
          API Version:
            Description:  APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
            Type:         string
          Kind:
            Description:  Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
            Type:         string
          Metadata:
            Type:  object
          Spec:
            Description:  GuestbookSpec defines the desired state of Guestbook
            Properties:
              Alias:
                Enum:
                  Phone
                  Address
                  Name
                Type:  string
              Config Map Name:
                Description:  Name of the ConfigMap for GuestbookSpec's configuration
                Max Length:   15
                Min Length:   1
                Type:         string
              Size:
                Description:  Quantity of instances
                Format:       int32
                Maximum:      10
                Minimum:      1
                Type:         integer
            Required:
              configMapName
              size
            Type:  object
          Status:
            Description:  GuestbookStatus defines the observed state of Guestbook
            Properties:
              Active:
                Description:  PodName of the active Guestbook node.
                Type:         string
              Standby:
                Description:  PodNames of the standby Guestbook nodes.
                Items:
                  Type:  string
                Type:    array
            Required:
              active
              standby
            Type:  object
        Type:      object
    Served:        true
    Storage:       true
    Subresources:
      Status:
Status:
  Accepted Names:
    Kind:       Guestbook
    List Kind:  GuestbookList
    Plural:     guestbooks
    Singular:   guestbook
  Conditions:
    Last Transition Time:  2022-04-01T00:51:08Z
    Message:               no conflicts found
    Reason:                NoConflicts
    Status:                True
    Type:                  NamesAccepted
    Last Transition Time:  2022-04-01T00:51:08Z
    Message:               the initial names have been accepted
    Reason:                InitialNamesAccepted
    Status:                True
    Type:                  Established
  Stored Versions:
    v1
Events:  <none>
[root@master ~]#

第4步:运行controller,执行make run。

[root@workstation guestbook]# make run
/root/kubebuilder/projects/guestbook/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
/root/kubebuilder/projects/guestbook/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
go vet ./...
go run ./main.go
1.6487742857355735e+09	INFO	controller-runtime.metrics	Metrics server is starting to listen	{"addr": ":8080"}
1.6487742857360241e+09	INFO	setup	starting manager
1.6487742857365518e+09	INFO	Starting server	{"path": "/metrics", "kind": "metrics", "addr": "[::]:8080"}
1.6487742857366114e+09	INFO	Starting server	{"kind": "health probe", "addr": "[::]:8081"}
1.6487742857367413e+09	INFO	controller.guestbook	Starting EventSource	{"reconciler group": "webapp.my.domain", "reconciler kind": "Guestbook", "source": "kind source: *v1.Guestbook"}
1.6487742857367685e+09	INFO	controller.guestbook	Starting Controller	{"reconciler group": "webapp.my.domain", "reconciler kind": "Guestbook"}
1.6487742858375282e+09	INFO	controller.guestbook	Starting workers	{"reconciler group": "webapp.my.domain", "reconciler kind": "Guestbook", "worker count": 1}

第5步:打开另一个终端,准备一份自定义资源的yaml文件。 

第6步:应用到K8S中。

[root@workstation samples]# kubectl apply -f 2-cr.yaml 
guestbook.webapp.my.domain/guestbook-sample created
[root@workstation samples]#

第7步:修改自定资源对象。

第8步:回到第1个终端,观察controller输出的日志。

预期结果:

日志里有两段输出,第1段为第6步创建资源对象时的输出,第2段为第7步修改资源对象时的输出。

注:这里使用的controller是上一节改过的controller,所以才会有这种日志输出。

问题:

1)第3步中的kubectl describe crd/guestbooks.webapp.my.domain,看到的第一个字段,为枚举字段,这种字段的用法是什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值