兩條命令助你成為優(yōu)秀的 YAML 工程師
我們在編寫 Kubernetes 資源清單的時候可能會經(jīng)常會忘記要創(chuàng)建的資源名稱,即使知道了可能也不記得該資源對象有哪些屬性可以使用了,特別是對于那些名稱很長的資源或者屬性,即使死記硬背下來隔一段時間又會忘記了。

比如現(xiàn)在我們要創(chuàng)建一個 validating 的 admission webhook,我們就需要定義一個 ValidatingWebhookConfiguration 的資源對象,但是可能我們不記得它的全名了。這個時候我們可以使用 kubectl api-resources 命令來找到我們需要的 API 資源。找到了正確的資源名稱之后,就需要了解如何編寫正確的 YAML 資源清單文件了,但是 Kubernetes 中資源對象實在是太多了,而且每一個資源對象中配置屬性也是非常多的,我們不可能都能忘記記住,這個時候我們也可以借助 kubectl explain 命令來找到完整的結(jié)構(gòu),這對于我們編寫 YAML 資源清單文件非常有幫助。
kubectl api-resources 命令
kubectl api-resources 命令可以打印所有已經(jīng)注冊的 API 資源,如下所示:
Kubectl api-resourcesNAME SHORTNAMES APIGROUP NAMESPACED KINDbindings true Bindingcomponentstatuses cs false ComponentStatusconfigmaps cm true ConfigMapendpoints ep true Endpointsevents ev true Eventlimitranges limits true LimitRangenamespaces ns false Namespacenodes no false Node......
其中也會包含上面提到的 ValidatingWebhookConfiguration 資源:
mutatingwebhookconfigurations admissionregistration.k8s.io false MutatingWebhookConfigurationvalidatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfigurationcustomresourcedefinitions crd,crds apiextensions.k8s.io false CustomResourceDefinitionapiservices apiregistration.k8s.io false APIService
由于 Kubernetes 中已經(jīng)注冊的資源對象非常多,所以如果我們知道我們要查找的資源名稱包含一些關(guān)鍵詞的話,可以用 grep 來過濾:
$ kubectl api-resources |grep validatingvalidatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration
這樣就可以更精確的搜索到需要使用的資源名稱了,比如我們這里就是 ValidatingWebhookConfiguration ,現(xiàn)在知道了資源對象的名稱,然后可以使用 kubectl explain 命令來查找資源對象的屬性。
kubectl explain 命令
kubectl explain 命令可以將資源對象的詳細屬性都展示出來,比如我們現(xiàn)在不知道如何去編寫 ValidatingWebhookConfiguration ,這個時候我們可以通過命令 kubectl explain ValidatingWebhookConfiguration 來獲取詳細的信息:
$ kubectl explain ValidatingWebhookConfigurationKIND: ValidatingWebhookConfigurationVERSION: admissionregistration.k8s.io/v1DESCRIPTION:ValidatingWebhookConfiguration describes the configuration of and admissionwebhook that accept or reject and object without changing it.FIELDS:apiVersion <string>APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadataStandard object metadata; More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.webhooks <[]Object>Webhooks is a list of webhooks and the affected resources and operations.
這個命令會輸出頂層的屬性,我們只需要明白 表示字符串,表示對象, []表示數(shù)組即可,對象在 YAML 文件中就需要縮進,數(shù)組就需要通過添加一個破折號來表示一個 Item,對于對象和對象數(shù)組我們不知道里面有什么屬性的,我們還可以繼續(xù)在后面查看,如下所示:
$ kubectl explain ValidatingWebhookConfiguration.metadataKIND: ValidatingWebhookConfigurationVERSION: admissionregistration.k8s.io/v1RESOURCE: metadata <Object>DESCRIPTION:Standard object metadata; More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.ObjectMeta is metadata that all persisted resources must have, whichincludes all objects users must create.FIELDS:annotations <map[string]string>Annotations is an unstructured key value map stored with a resource thatmay be set by external tools to store and retrieve arbitrary metadata. Theyare not queryable and should be preserved when modifying objects. Moreinfo: http://kubernetes.io/docs/user-guide/annotations......
上面輸出的屬性就是屬于 metadata 這個 key 下面對應的對象了,有的時候如果覺得這樣一層一層的去查看比較麻煩,我們還可以傳入一個 --recursive 參數(shù)來獲取所有的屬性:
$ kubectl explain validatingwebhookconfiguration --recursiveKIND: ValidatingWebhookConfigurationVERSION: admissionregistration.k8s.io/v1DESCRIPTION:ValidatingWebhookConfiguration describes the configuration of and admissionwebhook that accept or reject and object without changing it.FIELDS:apiVersion <string>kind <string>metadata <Object>annotations <map[string]string>clusterName <string>creationTimestamp <string>deletionGracePeriodSeconds <integer>deletionTimestamp <string>finalizers <[]string>generateName <string>generation <integer>labels <map[string]string>managedFields <[]Object>apiVersion <string>fieldsType <string>fieldsV1 <map[string]>manager <string>operation <string>time <string>name <string>namespace <string>ownerReferences <[]Object>apiVersion <string>blockOwnerDeletion <boolean>controller <boolean>kind <string>name <string>uid <string>resourceVersion <string>selfLink <string>uid <string>webhooks <[]Object>admissionReviewVersions <[]string>clientConfig <Object>caBundle <string>service <Object>name <string>namespace <string>path <string>port <integer>url <string>failurePolicy <string>matchPolicy <string>name <string>namespaceSelector <Object>matchExpressions <[]Object>key <string>operator <string>values <[]string>matchLabels <map[string]string>objectSelector <Object>matchExpressions <[]Object>key <string>operator <string>values <[]string>matchLabels <map[string]string>rules <[]Object>apiGroups <[]string>apiVersions <[]string>operations <[]string>resources <[]string>scope <string>sideEffects <string>timeoutSeconds <integer>
這個命令就可以將資源對象的完整屬性列出來,而且縮進格式和 YAML 文件基本上是一致的,這樣對于我們?nèi)ゾ帉戀Y源清單文件就更加友好了。
使用 kubectl api-resources 和 kubectl explain 這兩個命令可以為我們節(jié)省編寫資源清單文件的大量時間。
