0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Kustomizeのpatchで配列内オブジェクトに新しいプロパティを生やす

Posted at

タイトルの意味

「配列内オブジェクトに新しいプロパティを生やす」とは、以下のようなことです。

生やす前
apiVersion: v1
kind: Pod
metadata:
  name: static-web
  labels:
    role: myrole
spec:
  containers:
  - name: web
    image: nginx
    ports:
    - name: web
      protocol: TCP
生やした後
apiVersion: v1
kind: Pod
metadata:
  name: static-web
  labels:
    role: myrole
spec:
  containers:
  - name: web
    image: nginx
    ports:
    - name: web
      containerPort: 80
      protocol: TCP

containerPort: 80が追加されています。

やり方

pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: static-web
  labels:
    role: myrole
spec:
  containers:
  - name: web
    image: nginx
    ports:
    - name: web
      protocol: TCP
kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- pod.yaml

patches:
- target:
    kind: Pod
  path: ./patches/pod.yaml

肝心のパッチは以下のようになります。

patches/pod.yaml
- op: add
  path: /spec/containers/0/ports/0/containerPort
  value:
    80

/spec/containers/0/ports/0/は、「specの中のcontainersの0番目の中のportsの0番目の中」という意味で、その中に存在しないcontainerPortを指定することで、valuesの値と共に追加されます。

結果

結果
apiVersion: v1
kind: Pod
metadata:
  labels:
    role: myrole
  name: static-web
spec:
  containers:
  - image: nginx
    name: web
    ports:
    - containerPort: 80
      name: web
      protocol: TCP

元のpod.yamlには存在しなかったcontainerPortプロパティが増えていますね!

参考

Kustomizeのop: addop: removeなどのパッチ操作はRFC6092で定義されているようです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?