From 565e52a376a7fcf90e90bac295737c76d36bf292 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Sat, 16 Apr 2022 10:05:02 +0800 Subject: [PATCH] Fix template yaml function --- cmd/fake-kubelet/node.initialization.tpl | 8 ++++---- controller.go | 9 +++++++-- deploy.yaml | 8 ++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/fake-kubelet/node.initialization.tpl b/cmd/fake-kubelet/node.initialization.tpl index 992bc32..da698b1 100644 --- a/cmd/fake-kubelet/node.initialization.tpl +++ b/cmd/fake-kubelet/node.initialization.tpl @@ -1,14 +1,14 @@ {{ with .status }} addresses: {{ with .addresses }} -{{ . | YAML }} +{{ YAML . 1 }} {{ else }} - address: {{ NodeIP }} type: InternalIP {{ end }} allocatable: {{ with .allocatable }} -{{ . | YAML }} +{{ YAML . 1 }} {{ else }} cpu: 1k memory: 1Ti @@ -16,7 +16,7 @@ allocatable: {{ end }} capacity: {{ with .capacity }} -{{ . | YAML }} +{{ YAML . 1 }} {{ else }} cpu: 1k memory: 1Ti @@ -24,7 +24,7 @@ capacity: {{ end }} daemonEndpoints: {{ with .daemonEndpoints }} -{{ . | YAML }} +{{ YAML . 1 }} {{ else }} kubeletEndpoint: Port: 0 diff --git a/controller.go b/controller.go index 0c6a7dc..8ebe41b 100644 --- a/controller.go +++ b/controller.go @@ -98,12 +98,17 @@ func NewController(conf Config) (*Controller, error) { "PodIP": func() string { return n.ipPool.Get() }, - "YAML": func(s interface{}) (string, error) { + "YAML": func(s interface{}, indent ...int) (string, error) { d, err := yaml.Marshal(s) if err != nil { return "", err } - return string(d), nil + + data := string(d) + if len(indent) == 1 && indent[0] > 0 { + data = strings.Replace("\n"+data, "\n", "\n"+strings.Repeat(" ", indent[0]), -1) + } + return data, nil }, } return n, nil diff --git a/deploy.yaml b/deploy.yaml index 866f2ab..51ba899 100644 --- a/deploy.yaml +++ b/deploy.yaml @@ -180,14 +180,14 @@ data: {{ with .status }} addresses: {{ with .addresses }} - {{ . | YAML }} + {{ YAML . 1 }} {{ else }} - address: {{ NodeIP }} type: InternalIP {{ end }} allocatable: {{ with .allocatable }} - {{ . | YAML }} + {{ YAML . 1 }} {{ else }} cpu: 1k memory: 1Ti @@ -195,7 +195,7 @@ data: {{ end }} capacity: {{ with .capacity }} - {{ . | YAML }} + {{ YAML . 1 }} {{ else }} cpu: 1k memory: 1Ti @@ -203,7 +203,7 @@ data: {{ end }} daemonEndpoints: {{ with .daemonEndpoints }} - {{ . | YAML }} + {{ YAML . 1 }} {{ else }} kubeletEndpoint: Port: 0