Kpt で Kubernetes の Manifest を編集して apply する

kubernetes

Kpt は Kubernetes の Manifest を編集する Google の OSS。kubectl に組み込まれている kustomize と比較して、ベースの Manifest からパッチを当てるのではなく、テンプレートや Functions によって WYSIWYG に Manifest を編集するアプローチを取っている。

kustomizeでkubernetesのmanifestを環境ごとに生成する - sambaiz-net

CLI をインストールする。

$ brew tap GoogleContainerTools/kpt https://github.com/GoogleContainerTools/kpt.git
$ brew install kpt
$ kpt version
1.0.0-beta.49

kpt pkg get で git リポジトリからパッケージを取ってくる。

$ kpt pkg get https://github.com/GoogleContainerTools/kpt/package-examples/[email protected]
$ ls nginx
Kptfile         deployment.yaml svc.yaml

設定ファイル Kptfile には upstream などの情報が記述されている。

$ cat Kptfile 
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
  name: nginx
upstream:
  type: git
  git:
    repo: https://github.com/GoogleContainerTools/kpt
    directory: /package-examples/nginx
    ref: v0.9
  updateStrategy: resource-merge
upstreamLock:
  type: git
  git:
    repo: https://github.com/GoogleContainerTools/kpt
    directory: /package-examples/nginx
    ref: package-examples/nginx/v0.9
    commit: b9ea0bca019dafa9f9f91fd428385597c708518c
info:
  emails:
    - [email protected]
  description: This is an example nginx package.
pipeline:
  validators:
    - image: gcr.io/kpt-fn/kubeval:v0.3
      configMap:
        strict: "true"

kpt pkg update で upstream の変更が 3-way merge される。その際 kpt-merge コメントと共に annotation が自動で追加される

$ cat svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-nginx-svc
  annotations:
    aaaa: bbbb
spec:
  type: LoadBalancer
  selector:
    app: my-nginx
  ports:
    - protocol: TCP
      port: 8080

$ kpt pkg update
$ git diff svc.yaml
...
 apiVersion: v1
 kind: Service
-metadata:
+metadata: # kpt-merge: /my-nginx-svc
   name: my-nginx-svc
   labels:
     app: my-nginx
   annotations:
     aaaa: bbbb
+    internal.kpt.dev/upstream-identifier: '|Service|default|my-nginx-svc'
 spec:
   type: LoadBalancer
   selector:

kpt fn eval で Functions を実行し、kpt fn render で Kptfile の pipeline を実行できる。

$ kpt fn eval --image gcr.io/kpt-fn/search-replace:v0.1 -- by-path='spec.**.app' put-value=my-nginx
$ git diff deployment.yaml 
...
@@ -22,11 +21,11 @@ spec:
   replicas: 4
   selector:
     matchLabels:
-      app: nginx
+      app: my-nginx
   template:
     metadata:
       labels:
-        app: nginx
+        app: my-nginx
     spec:
       containers:
         - name: nginx

kpt live install-resource-group で管理に用いる ResourceGroup CRD をクラスタにインストールする。kpt live init でパッケージを初期化し kpt live apply で apply する。

$ kpt live install-resource-group
$ kpt live init
$ cat resourcegroup.yaml 
apiVersion: kpt.dev/v1alpha1
kind: ResourceGroup
metadata:
  name: inventory-48465506
  namespace: default
  labels:
    cli-utils.sigs.k8s.io/inventory-id: 3128ef464fed9e269153f7801d6dbcac677e712a-1730792231618652000

$ kpt live apply
inventory update started
inventory update finished
apply phase started
service/my-nginx-svc apply successful
deployment.apps/my-nginx-update apply successful
apply phase finished
reconcile phase started
service/my-nginx-svc reconcile successful
deployment.apps/my-nginx-update reconcile pending
deployment.apps/my-nginx-update reconcile successful
reconcile phase finished
inventory update started
inventory update finished
apply result: 2 attempted, 2 successful, 0 skipped, 0 failed
reconcile result: 2 attempted, 2 successful, 0 skipped, 0 failed, 0 timed out