diff --git a/pkg/asset/ignition/BUILD.bazel b/pkg/asset/ignition/BUILD.bazel index d3691413867..778b4edbf78 100644 --- a/pkg/asset/ignition/BUILD.bazel +++ b/pkg/asset/ignition/BUILD.bazel @@ -14,7 +14,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/asset:go_default_library", - "//pkg/asset/ignition/templates:go_default_library", + "//pkg/asset/ignition/content:go_default_library", "//pkg/asset/installconfig:go_default_library", "//pkg/asset/kubeconfig:go_default_library", "//pkg/asset/tls:go_default_library", @@ -37,7 +37,7 @@ go_test( embed = [":go_default_library"], deps = [ "//pkg/asset:go_default_library", - "//pkg/asset/ignition/templates:go_default_library", + "//pkg/asset/ignition/content:go_default_library", "//vendor/github.com/stretchr/testify/assert:go_default_library", "//vendor/github.com/vincent-petithory/dataurl:go_default_library", ], diff --git a/pkg/asset/ignition/bootstrap.go b/pkg/asset/ignition/bootstrap.go index 0db038d9477..db80e5d4c7f 100644 --- a/pkg/asset/ignition/bootstrap.go +++ b/pkg/asset/ignition/bootstrap.go @@ -13,7 +13,7 @@ import ( ignition "github.com/coreos/ignition/config/v2_2/types" "github.com/openshift/installer/pkg/asset" - "github.com/openshift/installer/pkg/asset/ignition/templates" + "github.com/openshift/installer/pkg/asset/ignition/content" "github.com/openshift/installer/pkg/asset/installconfig" "github.com/openshift/installer/pkg/asset/kubeconfig" "github.com/openshift/installer/pkg/asset/tls" @@ -143,9 +143,9 @@ func (a *bootstrap) Generate(dependencies map[asset.Asset]*asset.State) (*asset. config.Systemd.Units = append( config.Systemd.Units, - ignition.Unit{Name: "bootkube.service", Contents: templates.BootkubeSystemdContents}, - ignition.Unit{Name: "tectonic.service", Contents: templates.TectonicSystemdContents, Enabled: util.BoolToPtr(true)}, - ignition.Unit{Name: "kubelet.service", Contents: string(applyTemplateData(templates.KubeletSystemdContents, templateData)), Enabled: util.BoolToPtr(true)}, + ignition.Unit{Name: "bootkube.service", Contents: content.BootkubeSystemdContents}, + ignition.Unit{Name: "tectonic.service", Contents: content.TectonicSystemdContents, Enabled: util.BoolToPtr(true)}, + ignition.Unit{Name: "kubelet.service", Contents: applyTemplateData(content.KubeletSystemdTemplate, templateData), Enabled: util.BoolToPtr(true)}, ) config.Passwd.Users = append( @@ -220,7 +220,7 @@ func (a *bootstrap) addBootkubeFiles(config *ignition.Config, dependencies map[a config.Storage.Files, fileFromAsset("/opt/tectonic/auth/kubeconfig", 0400, dependencies[a.kubeconfig], 0), fileFromAsset("/opt/tectonic/auth/kubeconfig-kubelet", 0400, dependencies[a.kubeconfigKubelet], 0), - fileFromBytes("/opt/tectonic/bootkube.sh", 0555, applyTemplateData(templates.BootkubeShFileContents, templateData)), + fileFromString("/opt/tectonic/bootkube.sh", 0555, applyTemplateData(content.BootkubeShFileTemplate, templateData)), ) } @@ -228,7 +228,7 @@ func (a *bootstrap) addTectonicFiles(config *ignition.Config, dependencies map[a // TODO (staebler) - missing manifests from tectonic module config.Storage.Files = append( config.Storage.Files, - fileFromBytes("/opt/tectonic/tectonic.sh", 0555, applyTemplateData(templates.TectonicShFileContents, templateData)), + fileFromString("/opt/tectonic/tectonic.sh", 0555, content.TectonicShFileContents), ) } @@ -281,14 +281,10 @@ func getCloudProviderConfig(installConfig *types.InstallConfig) string { return "" } -func applyTemplateData(templateString string, templateData interface{}) []byte { - t, err := template.New("").Parse(templateString) - if err != nil { - panic(err) - } +func applyTemplateData(template *template.Template, templateData interface{}) string { buf := &bytes.Buffer{} - if err := t.Execute(buf, &templateData); err != nil { + if err := template.Execute(buf, templateData); err != nil { panic(err) } - return buf.Bytes() + return buf.String() } diff --git a/pkg/asset/ignition/bootstrap_test.go b/pkg/asset/ignition/bootstrap_test.go index fa28fae46d3..75c03e461a8 100644 --- a/pkg/asset/ignition/bootstrap_test.go +++ b/pkg/asset/ignition/bootstrap_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/openshift/installer/pkg/asset" - "github.com/openshift/installer/pkg/asset/ignition/templates" + "github.com/openshift/installer/pkg/asset/ignition/content" ) // TestBootstrapGenerate tests generating the bootstrap asset. @@ -245,11 +245,11 @@ machines: bootstrapState.Contents[0].Data, systemdUnitAssertion{ name: "bootkube.service", - contents: templates.BootkubeSystemdContents, + contents: content.BootkubeSystemdContents, }, systemdUnitAssertion{ name: "tectonic.service", - contents: templates.TectonicSystemdContents, + contents: content.TectonicSystemdContents, }, systemdUnitAssertion{ name: "kubelet.service", diff --git a/pkg/asset/ignition/templates/BUILD.bazel b/pkg/asset/ignition/content/BUILD.bazel similarity index 95% rename from pkg/asset/ignition/templates/BUILD.bazel rename to pkg/asset/ignition/content/BUILD.bazel index eec25a1dfdc..7fd8c130e9f 100644 --- a/pkg/asset/ignition/templates/BUILD.bazel +++ b/pkg/asset/ignition/content/BUILD.bazel @@ -8,6 +8,6 @@ go_library( "kubelet.go", "tectonic.go", ], - importpath = "github.com/openshift/installer/pkg/asset/ignition/templates", + importpath = "github.com/openshift/installer/pkg/asset/ignition/content", visibility = ["//visibility:public"], ) diff --git a/pkg/asset/ignition/templates/bootkube.go b/pkg/asset/ignition/content/bootkube.go similarity index 94% rename from pkg/asset/ignition/templates/bootkube.go rename to pkg/asset/ignition/content/bootkube.go index 2307fd44501..e38b1b2b2ae 100644 --- a/pkg/asset/ignition/templates/bootkube.go +++ b/pkg/asset/ignition/content/bootkube.go @@ -1,4 +1,8 @@ -package templates +package content + +import ( + "text/template" +) const ( // BootkubeSystemdContents is a service for running bootkube on the bootstrap @@ -15,10 +19,12 @@ ExecStart=/opt/tectonic/bootkube.sh Restart=on-failure RestartSec=5s` +) - // BootkubeShFileContents is a script file for running bootkube on the +var ( + // BootkubeShFileTemplate is a script file for running bootkube on the // bootstrap nodes. - BootkubeShFileContents = `#!/usr/bin/env bash + BootkubeShFileTemplate = template.Must(template.New("bootkube.sh").Parse(`#!/usr/bin/env bash set -e mkdir --parents /etc/kubernetes/manifests/ @@ -137,5 +143,5 @@ podman run \ --network=host \ --entrypoint=/bootkube \ "{{.BootkubeImage}}" \ - start --asset-dir=/assets` + start --asset-dir=/assets`)) ) diff --git a/pkg/asset/ignition/content/doc.go b/pkg/asset/ignition/content/doc.go new file mode 100644 index 00000000000..caaafb25a8f --- /dev/null +++ b/pkg/asset/ignition/content/doc.go @@ -0,0 +1,3 @@ +// Package content contains the contents of files and systemd units to be added +// to Ignition configs. +package content diff --git a/pkg/asset/ignition/templates/kubelet.go b/pkg/asset/ignition/content/kubelet.go similarity index 84% rename from pkg/asset/ignition/templates/kubelet.go rename to pkg/asset/ignition/content/kubelet.go index 6f9d72d784f..d665bbaada8 100644 --- a/pkg/asset/ignition/templates/kubelet.go +++ b/pkg/asset/ignition/content/kubelet.go @@ -1,9 +1,13 @@ -package templates +package content -const ( - // KubeletSystemdContents is a service for running the kubelet on the +import ( + "text/template" +) + +var ( + // KubeletSystemdTemplate is a service for running the kubelet on the // bootstrap nodes. - KubeletSystemdContents = `[Unit] + KubeletSystemdTemplate = template.Must(template.New("kubelet.service").Parse(`[Unit] Description=Kubernetes Kubelet Wants=rpc-statd.service @@ -39,5 +43,5 @@ Restart=always RestartSec=10 [Install] -WantedBy=multi-user.target` +WantedBy=multi-user.target`)) ) diff --git a/pkg/asset/ignition/templates/tectonic.go b/pkg/asset/ignition/content/tectonic.go similarity index 99% rename from pkg/asset/ignition/templates/tectonic.go rename to pkg/asset/ignition/content/tectonic.go index 9c0b86fc867..a56ff21a2a5 100644 --- a/pkg/asset/ignition/templates/tectonic.go +++ b/pkg/asset/ignition/content/tectonic.go @@ -1,4 +1,4 @@ -package templates +package content const ( // TectonicSystemdContents is a service that runs tectonic on the masters. diff --git a/pkg/asset/ignition/node.go b/pkg/asset/ignition/node.go index f14611d0f43..b15511ecb9a 100644 --- a/pkg/asset/ignition/node.go +++ b/pkg/asset/ignition/node.go @@ -25,6 +25,11 @@ func fileFromAsset(path string, mode int, assetState *asset.State, contentIndex return fileFromBytes(path, mode, assetState.Contents[contentIndex].Data) } +// fileFromString creates an ignition-config file with the given contents. +func fileFromString(path string, mode int, contents string) ignition.File { + return fileFromBytes(path, mode, []byte(contents)) +} + // fileFromAsset creates an ignition-config file with the given contents. func fileFromBytes(path string, mode int, contents []byte) ignition.File { return ignition.File{ diff --git a/pkg/asset/ignition/templates/doc.go b/pkg/asset/ignition/templates/doc.go deleted file mode 100644 index 9df1cc1a733..00000000000 --- a/pkg/asset/ignition/templates/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package templates contains consts for the contents of files and systemd units -// to add to ignition configs. -package templates