在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):jinghzhu/KubernetesCRD开源软件地址(OpenSource Url):https://github.com/jinghzhu/KubernetesCRD开源编程语言(OpenSource Language):Go 97.2%开源软件介绍(OpenSource Introduction):Kubernetes Custom Resource DefinitionThis repository is an example of how to create/list/update/delete Kubernetes Custom Resource Definition. If you also want to learn how to develop your own Operator, please visit: The CRD example I write here is like the ReplicaSet. You can use my CRD with my Operators mentioned before to run following scenarios:
Environment
Dependency PackageThe CRD is mainly developed in repository apiextensions-apiserver which depends on client-go and apimachinery. This code is based on Kubernetes v1.18.12:
Step-by-step InstructionsDefine CRD ObjectWe need firstly create the struct of CRD. The CRD object structure has these components:
// pkg/crd/jinghzhu/v1/types.go
// Jinghzhu is the CRD. Use this command to generate deepcopy for it:
type Jinghzhu struct {
metav1.TypeMeta `json:",inline"`
// Standard object's metadata.
metav1.ObjectMeta `json:"metadata"`
// Specification of the desired behavior of Jinghzhu.
Spec JinghzhuSpec `json:"spec"`
// Observed status of Jinghzhu.
Status JinghzhuStatus `json:"status"`
}
// JinghzhuSpec is a desired state description of Jinghzhu.
type JinghzhuSpec struct {
// Desired is the desired Pod number.
Desired int `json:"desired"`
// Current is the number of Pod currently running.
Current int `json:"current"`
// PodList is the name list of current Pods.
PodList []string `json:"podList"`
}
// JinghzhuStatus describes the lifecycle status of Jinghzhu.
type JinghzhuStatus struct {
State string `json:"state"`
Message string `json:"message"`
} We need to leverage the automatically code generated script to create the deep copy methods for CRD object. The result can be found at:
You can get code-generator from GitHub. In my example, I run following command to generate that file: $ ./k8s.io/code-generator/generate-groups.sh all github.com/jinghzhu/KubernetesCRD/pkg/crd/jinghzhu/v1/apis github.com/jinghzhu/KubernetesCRD/pkg/crd "jinghzhu:v1" Please note that the code-generator requires annotation to work as expected. If you carefully read my code, you can find the annotations at:
// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +resource:path=jinghzhu
...
// +k8s:deepcopy-gen=true
...
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +resource:path=jinghzhu
// +k8s:deepcopy-gen=package,register
// +k8s:defaulter-gen=TypeMeta
// +k8s:openapi-gen=true
// Package v1 is the v1 version of the API.
// +groupName=jinghzhu.io For more details about annotations for code-generator, please read Kubernetes documents or Google it.
Register CRDThe CRD name (
const (
// Kind is normally the CamelCased singular type. The resource manifest uses this.
Kind string = "Jinghzhu"
// GroupVersion is the version.
GroupVersion string = "v1"
// Plural is the plural name used in /apis/<group>/<version>/<plural>
Plural string = "jinghzhus"
// Singular is used as an alias on kubectl for display.
Singular string = "jinghzhu"
// CRDName is the CRD name for Jinghzhu.
CRDName string = Plural + "." + crdjinghzhu.GroupName
// ShortName is the short alias for the CRD.
ShortName string = "jh"
) The method, Meanwhile, I also leverage OpenAPI v3 to perform validation check. You can find related logic at Validation: &apiextensionsv1beta1.CustomResourceValidation{
OpenAPIV3Schema: &apiextensionsv1beta1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextensionsv1beta1.JSONSchemaProps{
"spec": {
Type: "object",
Properties: map[string]apiextensionsv1beta1.JSONSchemaProps{
"desired": {Type: "integer", Format: "int"},
"current": {Type: "integer", Format: "int"},
"podList": {
Type: "array",
Items: &apiextensionsv1beta1.JSONSchemaPropsOrArray{
Schema: &apiextensionsv1beta1.JSONSchemaProps{Type: "string"},
},
},
},
Required: []string{"desired"},
},
},
},
}, CRD ClientAfter creating CRD, we can access via CLI. For easily usage, we hope it can also be accessed via API. So I develop some methods to wrapper some codes for CRD Create, Update, Delete, Get, and List. You can view them at Main Logic to Use CRDHere, I'll go through the main code (
ResultIf everything goes well, you should see logs like: $ go run cmd/crd/main.go
CRD Jinghzhu is created
CREATED: Name = jinghzhu-example-f7wgv
Resource Version = 2343534
Desired = 1
Current = 0
PodList =
State = Pending
Message = Created but not processed yet
Processed jinghzhu-example-f7wgv
LIST: &v1.JinghzhuList{TypeMeta:v1.TypeMeta{Kind:"", APIVersion:""}, ListMeta:v1.ListMeta{SelfLink:"/apis/jinghzhu.io/v1/namespaces/crd/jinghzhus", ResourceVersion:"2343539", Continue:"", RemainingItemCount:(*int64)(nil)}, Items:[]v1.Jinghzhu{v1.Jinghzhu{TypeMeta:v1.TypeMeta{Kind:"Jinghzhu", APIVersion:"jinghzhu.io/v1"}, ObjectMeta:v1.ObjectMeta{Name:"jinghzhu-example-f7wgv", GenerateName:"jinghzhu-example-", Namespace:"crd", SelfLink:"/apis/jinghzhu.io/v1/namespaces/crd/jinghzhus/jinghzhu-example-f7wgv", UID:"c9a14049-e255-45be-99f7-033a5ea87787", ResourceVersion:"2343534", Generation:1, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:63743288791, loc:(*time.Location)(0x1c802e0)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string(nil), Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry{v1.ManagedFieldsEntry{Manager:"main", Operation:"Update", APIVersion:"jinghzhu.io/v1", Time:(*v1.Time)(0xc00039d1c0), FieldsType:"FieldsV1", FieldsV1:(*v1.FieldsV1)(0xc00039d1a0)}}}, Spec:v1.JinghzhuSpec{Desired:1, Current:0, PodList:[]string{}}, Status:v1.JinghzhuStatus{State:"Pending", Message:"Created but not processed yet"}}}} Now, let's check CRD. $ kubectl get crd
NAME CREATED AT
jinghzhus.jinghzhu.io 2020-12-11T13:06:25Z $ kubectl describe crd jinghzhus.jinghzhu.com
Name: jinghzhus.jinghzhu.io
Namespace:
Labels: <none>
Annotations: <none>
API Version: apiextensions.k8s.io/v1
Kind: CustomResourceDefinition
Metadata:
Creation Timestamp: 2020-12-11T13:06:25Z
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:shortNames:
f:singular:
f:conditions:
Manager: kube-apiserver
Operation: Update
Time: 2020-12-11T13:06:25Z
API Version: apiextensions.k8s.io/v1beta1
Fields Type: FieldsV1
fieldsV1:
f:spec:
f:conversion:
.:
f:strategy:
f:group:
f:names:
f:kind:
f:listKind:
f:plural:
f:shortNames:
f:singular:
f:preserveUnknownFields:
f:scope:
f:validation:
.:
f:openAPIV3Schema:
.:
f:properties:
.:
f:spec:
.:
f:properties:
.:
f:current:
.:
f:format:
f:type:
f:desired:
.:
f:format:
f:type:
f:podList:
.:
f:items:
f:type:
f:required:
f:type:
f:type:
f:version:
f:versions:
f:status:
f:storedVersions:
Manager: main
Operation: Update
Time: 2020-12-11T13:06:25Z
Resource Version: 2343519
Self Link: /apis/apiextensions.k8s.io/v1/customresourcedefinitions/jinghzhus.jinghzhu.io
UID: 1afe3057-4398-45f7-9200-90b879d52f6a
Spec:
Conversion:
Strategy: None
Group: jinghzhu.io
Names:
Kind: Jinghzhu
List Kind: JinghzhuList
Plural: jinghzhus
Short Names:
jh
Singular: jinghzhu
Preserve Unknown Fields: true
Scope: Namespaced
Versions:
Name: v1
Schema:
openAPIV3Schema:
Properties:
Spec:
Properties:
Current:
Format: int
Type: integer
Desired:
Format: int
Type: integer
Pod List:
Items:
Type: string
Type: array
Required:
desired
Type: object
Type: object
Served: true
Storage: true
Status:
Accepted Names:
Kind: Jinghzhu
List Kind: JinghzhuList
Plural: jinghzhus
Short Names:
jh
Singular: jinghzhu
Conditions:
Last Transition Time: 2020-12-11T13:06:25Z
Message: no conflicts found
Reason: NoConflicts
Status: True
Type: NamesAccepted
Last Transition Time: 2020-12-11T13:06:25Z
Message: the initial names have been accepted
Reason: InitialNamesAccepted
Status: True
Type: Established
Stored Versions:
v1
Events: <none> $ kubectl proxy
Starting to serve on 127.0.0.1:8001
$ curl -i 127.0.0.1:8001/apis/jinghzhu.io
HTTP/1.1 200 OK
Cache-Control: no-cache, private
Content-Length: 253
Content-Type: application/json
Date: Fri, 11 Dec 2020 13:11:34 GMT
{
"kind": "APIGroup",
"apiVersion": "v1",
"name": "jinghzhu.io",
"versions": [
{
"groupVersion": "jinghzhu.io/v1",
"version": "v1"
}
],
"preferredVersion": {
"groupVersion": "jinghzhu.io/v1",
"version": "v1"
}
} $ kubectl -n crd get jh
NAME AGE
jinghzhu-example-f7wgv 6m20s
$ kubectl -n crd describe jh jinghzhu-example-f7wgv
Name: jinghzhu-example-f7wgv
Namespace: crd
Labels: <none>
Annotations: <none>
API Version: jinghzhu.io/v1
Kind: Jinghzhu
Metadata:
Creation Timestamp: 2020-12-11T13:06:31Z
Generate Name: jinghzhu-example-
Generation: 1
Managed Fields:
API Version: jinghzhu.io/v1
Fields Type: FieldsV1
fieldsV1:
f:metadata:
f:generateName:
f:spec:
.:
f:current:
f:desired:
f:podList:
f:status:
.:
f:message:
f:state:
Manager: main
Operation: Update
Time: 2020-12-11T13:06:31Z
Resource Version: 2343534
Self Link: /apis/jinghzhu.io/v1/namespaces/crd/jinghzhus/jinghzhu-example-f7wgv
UID: c9a14049-e255-45be-99f7-033a5ea87787
Spec:
Current: 0
Desired: 1
Pod List:
Status:
Message: Created but not processed yet
State: Pending
Events: <none> |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论