Skip to content

Commit

Permalink
Merge pull request #259 from crawford/content
Browse files Browse the repository at this point in the history
asset/ignition: clean up templates
  • Loading branch information
openshift-merge-robot authored Sep 17, 2018
2 parents 2afc1f0 + 7ab6636 commit 00cb2f1
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 32 deletions.
4 changes: 2 additions & 2 deletions pkg/asset/ignition/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
],
Expand Down
22 changes: 9 additions & 13 deletions pkg/asset/ignition/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -220,15 +220,15 @@ 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)),
)
}

func (a *bootstrap) addTectonicFiles(config *ignition.Config, dependencies map[asset.Asset]*asset.State, templateData *bootstrapTemplateData) {
// 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),
)
}

Expand Down Expand Up @@ -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()
}
6 changes: 3 additions & 3 deletions pkg/asset/ignition/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package templates
package content

import (
"text/template"
)

const (
// BootkubeSystemdContents is a service for running bootkube on the bootstrap
Expand All @@ -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/
Expand Down Expand Up @@ -137,5 +143,5 @@ podman run \
--network=host \
--entrypoint=/bootkube \
"{{.BootkubeImage}}" \
start --asset-dir=/assets`
start --asset-dir=/assets`))
)
3 changes: 3 additions & 0 deletions pkg/asset/ignition/content/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package content contains the contents of files and systemd units to be added
// to Ignition configs.
package content
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,5 +43,5 @@ Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target`
WantedBy=multi-user.target`))
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package templates
package content

const (
// TectonicSystemdContents is a service that runs tectonic on the masters.
Expand Down
5 changes: 5 additions & 0 deletions pkg/asset/ignition/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
3 changes: 0 additions & 3 deletions pkg/asset/ignition/templates/doc.go

This file was deleted.

0 comments on commit 00cb2f1

Please sign in to comment.