mirror of
https://github.com/kubernetes-sigs/kind.git
synced 2025-12-01 07:26:05 +07:00
Merge pull request #2911 from BenTheElder/safeyaml
switch to safetext/yamltemplate for yaml templating
This commit is contained in:
1
go.mod
1
go.mod
@@ -6,6 +6,7 @@ require (
|
||||
github.com/BurntSushi/toml v1.0.0
|
||||
github.com/alessio/shellescape v1.4.1
|
||||
github.com/evanphx/json-patch/v5 v5.6.0
|
||||
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2
|
||||
github.com/mattn/go-isatty v0.0.14
|
||||
github.com/pelletier/go-toml v1.9.4
|
||||
github.com/pkg/errors v0.9.1
|
||||
|
||||
10
go.sum
10
go.sum
@@ -7,11 +7,18 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
|
||||
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
|
||||
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 h1:SJ+NtwL6QaZ21U+IrK7d0gGgpjGGvd2kz+FzTHVzdqI=
|
||||
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2/go.mod h1:Tv1PlzqC9t8wNnpPdctvtSUOPUUg4SHeE6vR1Ir2hmg=
|
||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
|
||||
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
@@ -24,8 +31,9 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
|
||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
@@ -20,7 +20,8 @@ package installcni
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/google/safetext/yamltemplate"
|
||||
|
||||
"sigs.k8s.io/kind/pkg/errors"
|
||||
"sigs.k8s.io/kind/pkg/internal/apis/config"
|
||||
@@ -69,7 +70,7 @@ func (a *action) Execute(ctx *actions.ActionContext) error {
|
||||
// their own, or use the default. The internal templating mechanism is
|
||||
// not intended for external usage and is unstable.
|
||||
if strings.Contains(manifest, "would you kindly template this file") {
|
||||
t, err := template.New("cni-manifest").Parse(manifest)
|
||||
t, err := yamltemplate.New("cni-manifest").Parse(manifest)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to parse CNI manifest template")
|
||||
}
|
||||
|
||||
@@ -21,7 +21,8 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/google/safetext/yamltemplate"
|
||||
|
||||
"sigs.k8s.io/kind/pkg/errors"
|
||||
|
||||
@@ -94,8 +95,8 @@ type DerivedConfigData struct {
|
||||
AdvertiseAddress string
|
||||
// DockerStableTag is automatically derived from KubernetesVersion
|
||||
DockerStableTag string
|
||||
// SortedFeatureGateKeys allows us to iterate FeatureGates deterministically
|
||||
SortedFeatureGateKeys []string
|
||||
// SortedFeatureGates allows us to iterate FeatureGates deterministically
|
||||
SortedFeatureGates []FeatureGate
|
||||
// FeatureGatesString is of the form `Foo=true,Baz=false`
|
||||
FeatureGatesString string
|
||||
// RuntimeConfigString is of the form `Foo=true,Baz=false`
|
||||
@@ -108,6 +109,11 @@ type DerivedConfigData struct {
|
||||
CgroupDriver string
|
||||
}
|
||||
|
||||
type FeatureGate struct {
|
||||
Name string
|
||||
Value bool
|
||||
}
|
||||
|
||||
// Derive automatically derives DockerStableTag if not specified
|
||||
func (c *ConfigData) Derive() {
|
||||
// default cgroup driver
|
||||
@@ -130,13 +136,17 @@ func (c *ConfigData) Derive() {
|
||||
featureGateKeys = append(featureGateKeys, k)
|
||||
}
|
||||
sort.Strings(featureGateKeys)
|
||||
c.SortedFeatureGateKeys = featureGateKeys
|
||||
|
||||
// create a sorted key=value,... string of FeatureGates
|
||||
var featureGates []string
|
||||
c.SortedFeatureGates = make([]FeatureGate, 0, len(c.FeatureGates))
|
||||
featureGates := make([]string, 0, len(c.FeatureGates))
|
||||
for _, k := range featureGateKeys {
|
||||
v := c.FeatureGates[k]
|
||||
featureGates = append(featureGates, fmt.Sprintf("%s=%t", k, v))
|
||||
c.SortedFeatureGates = append(c.SortedFeatureGates, FeatureGate{
|
||||
Name: k,
|
||||
Value: v,
|
||||
})
|
||||
}
|
||||
c.FeatureGatesString = strings.Join(featureGates, ",")
|
||||
|
||||
@@ -173,7 +183,7 @@ kubernetesVersion: {{.KubernetesVersion}}
|
||||
clusterName: "{{.ClusterName}}"
|
||||
{{ if .KubeadmFeatureGates}}featureGates:
|
||||
{{ range $key, $value := .KubeadmFeatureGates }}
|
||||
"{{ $key }}": {{ $value }}
|
||||
"{{ (StructuralData $key) }}": {{ $value }}
|
||||
{{end}}{{end}}
|
||||
controlPlaneEndpoint: "{{ .ControlPlaneEndpoint }}"
|
||||
# on docker for mac we have to expose the api server via port forward,
|
||||
@@ -272,8 +282,8 @@ evictionHard:
|
||||
nodefs.inodesFree: "0%"
|
||||
imagefs.available: "0%"
|
||||
{{if .FeatureGates}}featureGates:
|
||||
{{ range $key := .SortedFeatureGateKeys }}
|
||||
"{{ $key }}": {{ index $.FeatureGates $key }}
|
||||
{{ range $index, $gate := .SortedFeatureGates }}
|
||||
"{{ (StructuralData $gate.Name) }}": {{ $gate.Value }}
|
||||
{{end}}{{end}}
|
||||
{{if ne .KubeProxyMode "None"}}
|
||||
---
|
||||
@@ -283,8 +293,8 @@ metadata:
|
||||
name: config
|
||||
mode: "{{ .KubeProxyMode }}"
|
||||
{{if .FeatureGates}}featureGates:
|
||||
{{ range $key := .SortedFeatureGateKeys }}
|
||||
"{{ $key }}": {{ index $.FeatureGates $key }}
|
||||
{{ range $index, $gate := .SortedFeatureGates }}
|
||||
"{{ (StructuralData $gate.Name) }}": {{ $gate.Value }}
|
||||
{{end}}{{end}}
|
||||
iptables:
|
||||
minSyncPeriod: 1s
|
||||
@@ -310,7 +320,7 @@ kubernetesVersion: {{.KubernetesVersion}}
|
||||
clusterName: "{{.ClusterName}}"
|
||||
{{ if .KubeadmFeatureGates}}featureGates:
|
||||
{{ range $key, $value := .KubeadmFeatureGates }}
|
||||
"{{ $key }}": {{ $value }}
|
||||
"{{ (StructuralData $key) }}": {{ $value }}
|
||||
{{end}}{{end}}
|
||||
controlPlaneEndpoint: "{{ .ControlPlaneEndpoint }}"
|
||||
# on docker for mac we have to expose the api server via port forward,
|
||||
@@ -409,8 +419,8 @@ evictionHard:
|
||||
nodefs.inodesFree: "0%"
|
||||
imagefs.available: "0%"
|
||||
{{if .FeatureGates}}featureGates:
|
||||
{{ range $key := .SortedFeatureGateKeys }}
|
||||
"{{ $key }}": {{ index $.FeatureGates $key }}
|
||||
{{ range $index, $gate := .SortedFeatureGates }}
|
||||
"{{ (StructuralData $gate.Name) }}": {{ $gate.Value }}
|
||||
{{end}}{{end}}
|
||||
{{if .DisableLocalStorageCapacityIsolation}}localStorageCapacityIsolation: false{{end}}
|
||||
{{if ne .KubeProxyMode "None"}}
|
||||
@@ -421,8 +431,8 @@ metadata:
|
||||
name: config
|
||||
mode: "{{ .KubeProxyMode }}"
|
||||
{{if .FeatureGates}}featureGates:
|
||||
{{ range $key := .SortedFeatureGateKeys }}
|
||||
"{{ $key }}": {{ index $.FeatureGates $key }}
|
||||
{{ range $index, $gate := .SortedFeatureGates }}
|
||||
"{{ (StructuralData $gate.Name) }}": {{ $gate.Value }}
|
||||
{{end}}{{end}}
|
||||
iptables:
|
||||
minSyncPeriod: 1s
|
||||
@@ -476,7 +486,7 @@ func Config(data ConfigData) (config string, err error) {
|
||||
templateSource = ConfigTemplateBetaV2
|
||||
}
|
||||
|
||||
t, err := template.New("kubeadm-config").Parse(templateSource)
|
||||
t, err := yamltemplate.New("kubeadm-config").Parse(templateSource)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to parse config template")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user