OKD(Kubernetes)で動作しているリソースの定義を参照するには、以下のように「oc get」の結果をYAMLで出力していたのですが、
もうちょっと他に良さそうな方法があるようで。
$ oc get ... -o yaml
今回の環境は、こちら。
$ minishift version minishift v1.32.0+009893b $ oc version oc v3.11.0+0cbc58b kubernetes v1.11.0+d4cacc0 features: Basic-Auth GSSAPI Kerberos SPNEGO Server https://192.168.42.122:8443 kubernetes v1.11.0+d4cacc0
「oc get」に「--export」オプションを付与する
ちょっと、WildFlyをデプロイしてみましょう。
$ oc run wildfly --image jboss/wildfly deploymentconfig.apps.openshift.io/wildfly created $ oc expose dc wildfly --port 8080 service/wildfly exposed
まずは、「oc get」の結果をYAML出力しています。
$ oc get dc wildfly -o yaml apiVersion: apps.openshift.io/v1 kind: DeploymentConfig metadata: creationTimestamp: 2019-03-02T05:52:42Z generation: 1 labels: run: wildfly name: wildfly namespace: myproject resourceVersion: "18737" selfLink: /apis/apps.openshift.io/v1/namespaces/myproject/deploymentconfigs/wildfly uid: 64d52129-3caf-11e9-bebc-5254007890b0 spec: replicas: 1 revisionHistoryLimit: 10 selector: run: wildfly strategy: activeDeadlineSeconds: 21600 resources: {} rollingParams: intervalSeconds: 1 maxSurge: 25% maxUnavailable: 25% timeoutSeconds: 600 updatePeriodSeconds: 1 type: Rolling template: metadata: creationTimestamp: null labels: run: wildfly spec: containers: - image: jboss/wildfly imagePullPolicy: Always 〜省略〜 status: availableReplicas: 1 conditions: - lastTransitionTime: 2019-03-02T05:53:26Z lastUpdateTime: 2019-03-02T05:53:26Z message: Deployment config has minimum availability. status: "True" type: Available - lastTransitionTime: 2019-03-02T05:52:44Z lastUpdateTime: 2019-03-02T05:53:27Z message: replication controller "wildfly-1" successfully rolled out reason: NewReplicationControllerAvailable status: "True" type: Progressing details: causes: - type: ConfigChange message: config change latestVersion: 1 observedGeneration: 1 readyReplicas: 1 replicas: 1 unavailableReplicas: 0 updatedReplicas: 1
ここに、「--export」オプションを追加するとこのような結果になります。
$ oc get dc wildfly -o yaml --export apiVersion: apps.openshift.io/v1 kind: DeploymentConfig metadata: creationTimestamp: null generation: 1 labels: run: wildfly name: wildfly selfLink: /apis/apps.openshift.io/v1/namespaces/myproject/deploymentconfigs/wildfly spec: replicas: 1 revisionHistoryLimit: 10 selector: run: wildfly strategy: activeDeadlineSeconds: 21600 resources: {} rollingParams: intervalSeconds: 1 maxSurge: 25% maxUnavailable: 25% timeoutSeconds: 600 updatePeriodSeconds: 1 type: Rolling template: metadata: creationTimestamp: null labels: run: wildfly spec: containers: - image: jboss/wildfly imagePullPolicy: Always 〜省略〜 status: availableReplicas: 0 latestVersion: 0 observedGeneration: 0 replicas: 0 unavailableReplicas: 0 updatedReplicas: 0
「metadata」とか「status」とかが、少しすっきりしたと思います。
「--export」を付けると、環境に依存した情報を削除して表示してくれるようです。
API object specifications should be captured with oc get --export. This operation removes environment specific data from the object definitions (e.g., current namespace or assigned IP addresses), allowing them to be recreated in different environments (unlike oc get operations, which output an unfiltered state of the object).
同様のことは、「oc export」コマンドでも実行できます。
$ oc export dc wildfly
ただ、これは非推奨になっていて、「oc get ... --export」を使用した方が良さそうです。
$ oc export --help Command "export" is deprecated, use the oc get --export Export resources so they can be used elsewhere
「oc explain」でリソースに定義可能な情報を参照する
また、そもそもリソースに定義可能な情報ですが、いつもコマンドからリソースを作成して、それからYAMLに戻して確認
していたのですが、「oc explain」でなにを定義できるか確認できそうですね。
$ oc explain --help Documentation of resources. Possible resource types include: pods (po), services (svc), replicationcontrollers (rc), nodes (no), events (ev), componentstatuses (cs), limitranges (limits), persistentvolumes (pv), persistentvolumeclaims (pvc), resourcequotas (quota), namespaces (ns) or endpoints (ep).
確認。
$ oc explain service KIND: Service VERSION: v1 DESCRIPTION: Service is a named abstraction of software service (for example, mysql) consisting of local port (for example 3306) that the proxy listens on, and the selector that determines which pods will answer requests sent through the proxy. FIELDS: apiVersion <string> 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/api-conventions.md#resources kind <string> 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/api-conventions.md#types-kinds metadata <Object> Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata spec <Object> Spec defines the behavior of a service. https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status status <Object> Most recently observed status of the service. Populated by the system. Read-only. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
ネストした定義については、「.」で区切って次のフィールドを指定すればよいみたいです。
$ oc explain service.spec KIND: Service VERSION: v1 RESOURCE: spec <Object> DESCRIPTION: Spec defines the behavior of a service. https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status ServiceSpec describes the attributes that a user creates on a service. FIELDS: clusterIP <string> clusterIP is the IP address of the service and is usually assigned randomly by the master. If an address is specified manually and is not in use by others, it will be allocated to the service; otherwise, creation of the service will fail. This field can not be changed through updates. Valid values are "None", empty string (""), or a valid IP address. "None" can be specified for headless services when proxying is not required. Only applies to types ClusterIP, NodePort, and LoadBalancer. Ignored if type is ExternalName. More info: https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies externalIPs <[]string> externalIPs is a list of IP addresses for which nodes in the cluster will also accept traffic for this service. These IPs are not managed by Kubernetes. The user is responsible for ensuring that traffic arrives at a node with this IP. A common example is external load-balancers that are not part of the Kubernetes system. 〜省略〜
もしくは「--recursive」オプションを付与すると、フィールドの説明はなくなりますが、指定した階層以下の定義を一気に
出力してくれます。
$ oc explain service --recursive KIND: Service VERSION: v1 DESCRIPTION: Service is a named abstraction of software service (for example, mysql) consisting of local port (for example 3306) that the proxy listens on, and the selector that determines which pods will answer requests sent through the proxy. 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> initializers <Object> pending <[]Object> name <string> result <Object> apiVersion <string> code <integer> details <Object> causes <[]Object> field <string> message <string> reason <string> group <string> kind <string> name <string> retryAfterSeconds <integer> uid <string> 〜省略〜
途中から指定することもできます。
$ oc explain service.spec --recursive KIND: Service VERSION: v1 RESOURCE: spec <Object> DESCRIPTION: Spec defines the behavior of a service. https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status ServiceSpec describes the attributes that a user creates on a service. FIELDS: clusterIP <string> externalIPs <[]string> externalName <string> externalTrafficPolicy <string> healthCheckNodePort <integer> loadBalancerIP <string> loadBalancerSourceRanges <[]string> ports <[]Object> name <string> nodePort <integer> port <integer> protocol <string> targetPort <string> publishNotReadyAddresses <boolean> selector <map[string]string> sessionAffinity <string> sessionAffinityConfig <Object> clientIP <Object> timeoutSeconds <integer> type <string>
「--dry-run」と「--validation」
あと、YAMLの内容を適用する前に「--dry-run」と「--validate」も覚えておくと良いかもしれません。
「--dry-run」はサーバーに対して実際の処理はなげない、文字通りdry run。「--validate」は送信前にスキーマ定義の確認を
してくれます。
試しに、今のServiceの定義を取得します。
$ oc get svc wildfly -o yaml --export > service.yml
全然関係ない定義を追加してみましょう。「metadata」に「dummydata」という定義を書いてみました。
apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: run: wildfly name: wildfly dummydata: test selfLink: /api/v1/namespaces/myproject/services/wildfly
「--validate」をつけておくと、そんな定義はないと怒られます。
$ oc apply -f service.yml --dry-run --validate error: error validating "service.yml": error validating data: ValidationError(Service.metadata): unknown field "dummydata" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta; if you choose to ignore these errors, turn validation off with --validate=false
「--dry-run」だと、無視されるだけになります。
$ oc apply -f service.yml --dry-run service/wildfly configured (dry run)
また、例えば「metadata.name」を削除してみます。
apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: run: wildfly # name: wildfly selfLink: /api/v1/namespaces/myproject/services/wildfly
この場合は、そもそも必須項目がないということでエラーになります(「--validate」の有無関係なく)。
$ oc apply -f service.yml --dry-run --validate error: error when retrieving current configuration of: Resource: "/v1, Resource=services", GroupVersionKind: "/v1, Kind=Service" Name: "", Namespace: "myproject" Object: &{map["apiVersion":"v1" "kind":"Service" "metadata":map["creationTimestamp":<nil> "labels":map["run":"wildfly"] "selfLink":"/api/v1/namespaces/myproject/services/wildfly" "namespace":"myproject" "annotations":map["kubectl.kubernetes.io/last-applied-configuration":""]] "spec":map["type":"ClusterIP" "ports":[map["port":'\u1f90' "protocol":"TCP" "targetPort":'\u1f90']] "selector":map["run":"wildfly"] "sessionAffinity":"None"] "status":map["loadBalancer":map[]]]} from server for: "service.yml": resource name may not be empty
「metadata.name」を修正すれば、通るようになります。「--dry-run」なので、実際に適用されるわけではありませんが。
$ oc apply -f service.yml --dry-run --validate service/wildfly configured (dry run)
覚えておきましょう。