以下を apply すると15文字までしか使えないよって怒られるけど、何でこんなに短かいのか謎だったので調べてみたメモ。
---
apiVersion: v1
kind: Pod
metadata:
name: moo
labels:
name: moo
spec:
containers:
- name: moo
image: nginx
ports:
- containerPort: 80
name: long-container-port-name
% kubectl apply -f moo.yaml
The Pod "moo" is invalid: spec.containers[0].ports[0].name: Invalid value: "long-container-port-name": must be no more than 15 characters
リファレンスの ContainerPort v1 core によると以下のように「IANA_SVC_NAME
である事」と書いてある。
If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.
IANA_SVC_NAME
は Identifiers and Names in Kubernetes によると以下とある。つまり、この定義に従っているから15文字という話。
rfc6335 port name (IANA_SVC_NAME): An alphanumeric (a-z, and 0-9) string, with a maximum length of 15 characters, with the '-' character allowed anywhere except the first or the last character or adjacent to another '-' character, it must contain at least a (a-z) character.
RFC6335 はサービス名とポート番号の管理手続についての RFCで、
Internet Assigned Numbers Authority (IANA) Procedures for the Management of the Service Name and Transport Protocol Port Number Registry
「5.1. Service Name Syntax」に以下の記述があるので、IANA_SVC_NAME
はここで定義されている Service Name で合っていそう。
MUST be at least 1 character and no more than 15 characters long
RFC6335 についてもう少し補足すると、管理されているリストが Service Name and Transport Protocol Port Number Registry に書かれていて、例えば以下のようなものが定義されている。つまり、ポートを名前で表示させるための /etc/services
的なあれ。
ssh 22 tcp The Secure Shell (SSH) Protocol [RFC4251] Defined TXT keys: u=<username> p=<password>
API の型は pkg/apis/core/types.go で定義されているので blame して追っていくと、この コミット で「this must be a DNS_LABEL
」から「this must be a IANA_SVC_NAME
」に変わってる。その変更を加えた PR は To add validation for service ports when defined as string kubernetes/kubernetes#9736 で、対象の issue は Missing validation for Service.Spec.Ports.TargetPort kubernetes/kubernetes#9734。そこには以下と書いてあるけど、RFC6335 の Section 5.1 を選ぶ理由とかまでは書いてない。PR を見ても議論とかは特になさそう。
Some kind of validation with some syntax check should be introduced (for example https://tools.ietf.org/html/rfc6335#section-5.1)
また、その元となった issue の Missing validation for Service.Spec.TargetPort openshift/origin#3143 には以下と書いてあって、やっぱり選ぶ必要があってこれにした訳ではなさそう。
I've implemented almost this https://tools.ietf.org/html/rfc6335#section-5.1 (extended a little bit since I tested using my /etc/services) but well this is just to give an idea.
ちなみに、ports.name は 最初のコミット から存在していた (ここ)。思ったよりコメントが盛り上がてっない。
Service の ports.name は DNS_LABEL
で、Identifiers and Names in Kubernetes で以下とあるように63文字まで使える。
rfc1035/rfc1123 label (DNS_LABEL): An alphanumeric (a-z, and 0-9) string, with a maximum length of 63 characters, with the '-' character allowed anywhere except the first or last character, suitable for use as a hostname or segment in a domain name.
こっちの理由は明確で、DNS を使ったサービスディスカバリで SRV レコード として使うから。のはず。
結局、概念として似てるからこの定義を使いましたって事っぽくて、15文字にしないといけないシステム的な制約はないみたい。 もっと長くしてくれればいいのに。
というわけで、SENSYN Robotics Advent Calendar 2022 の21日目でした。