Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 119 additions & 2 deletions controller/machine/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,127 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var (
// ErrMissingName is the error returned when the WorfklowTemplate Name is not specified.
ErrMissingName = fmt.Errorf("name can't be empty")

// ErrMissingImageURL is the error returned when the WorfklowTemplate ImageURL is not specified.
ErrMissingImageURL = fmt.Errorf("imageURL can't be empty")
)

"github.com/tinkerbell/cluster-api-provider-tinkerbell/internal/templates"
const (
workflowTemplate = `
version: "0.1"
name: {{.Name}}
global_timeout: 6000
tasks:
- name: "{{.Name}}"
worker: "{{.DeviceTemplateName}}"
volumes:
- /dev:/dev
- /dev/console:/dev/console
- /lib/firmware:/lib/firmware:ro
actions:
- name: "stream-image"
image: quay.io/tinkerbell-actions/oci2disk:v1.0.0
timeout: 600
environment:
IMG_URL: {{.ImageURL}}
DEST_DISK: {{.DestDisk}}
COMPRESSED: true
- name: "add-tink-cloud-init-config"
image: quay.io/tinkerbell-actions/writefile:v1.0.0
timeout: 90
environment:
DEST_DISK: {{.DestPartition}}
FS_TYPE: ext4
DEST_PATH: /etc/cloud/cloud.cfg.d/10_tinkerbell.cfg
UID: 0
GID: 0
MODE: 0600
DIRMODE: 0700
CONTENTS: |
datasource:
Ec2:
metadata_urls: ["{{.MetadataURL}}"]
strict_id: false
system_info:
default_user:
name: tink
groups: [wheel, adm]
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
shell: /bin/bash
manage_etc_hosts: localhost
warnings:
dsid_missing_source: off
- name: "add-tink-cloud-init-ds-config"
image: quay.io/tinkerbell-actions/writefile:v1.0.0
timeout: 90
environment:
DEST_DISK: {{.DestPartition}}
FS_TYPE: ext4
DEST_PATH: /etc/cloud/ds-identify.cfg
UID: 0
GID: 0
MODE: 0600
DIRMODE: 0700
CONTENTS: |
datasource: Ec2
- name: "kexec-image"
image: ghcr.io/jacobweinstock/waitdaemon:0.1.2
timeout: 90
pid: host
environment:
BLOCK_DEVICE: {{.DestPartition}}
FS_TYPE: ext4
IMAGE: quay.io/tinkerbell-actions/kexec:v1.0.0
WAIT_SECONDS: 10
volumes:
- /var/run/docker.sock:/var/run/docker.sock
`
)

// WorkflowTemplate is a helper struct for rendering CAPT Template data.
type WorkflowTemplate struct {
Name string
MetadataURL string
ImageURL string
DestDisk string
DestPartition string
DeviceTemplateName string
}

// Render renders workflow template for a given machine including user-data.
func (wt *WorkflowTemplate) Render() (string, error) {
if wt.Name == "" {
return "", ErrMissingName
}

if wt.ImageURL == "" {
return "", ErrMissingImageURL
}

if wt.DeviceTemplateName == "" {
wt.DeviceTemplateName = "{{.device_1}}"
}

tpl, err := template.New("template").Parse(workflowTemplate)
if err != nil {
return "", fmt.Errorf("unable to parse template: %w", err)
}

buf := &bytes.Buffer{}

err = tpl.Execute(buf, wt)
if err != nil {
return "", fmt.Errorf("unable to execute template: %w", err)
}

return buf.String(), nil
}

func (scope *machineReconcileScope) templateExists() (bool, error) {
namespacedName := types.NamespacedName{
Name: scope.tinkerbellMachine.Name,
Expand Down Expand Up @@ -56,7 +173,7 @@ func (scope *machineReconcileScope) createTemplate(hw *tinkv1.Hardware) error {

metadataURL := fmt.Sprintf("http://%s:50061", metadataIP)

workflowTemplate := templates.WorkflowTemplate{
workflowTemplate := WorkflowTemplate{
Name: scope.tinkerbellMachine.Name,
MetadataURL: metadataURL,
ImageURL: imageURL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package templates_test
package machine_test

import (
"testing"

. "github.com/onsi/gomega" //nolint:revive // one day we will remove gomega
"sigs.k8s.io/yaml"

"github.com/tinkerbell/cluster-api-provider-tinkerbell/internal/templates"
"github.com/tinkerbell/cluster-api-provider-tinkerbell/controller/machine"
)

func validWorkflowTemplate() *templates.WorkflowTemplate {
return &templates.WorkflowTemplate{
func validWorkflowTemplate() *machine.WorkflowTemplate {
return &machine.WorkflowTemplate{
Name: "foo",
MetadataURL: "http://10.10.10.10",
ImageURL: "http://foo.bar.baz/do/it",
Expand All @@ -40,33 +40,33 @@ func Test_Cloud_config_template(t *testing.T) {
t.Parallel()

cases := map[string]struct {
mutateF func(*templates.WorkflowTemplate)
mutateF func(*machine.WorkflowTemplate)
expectError bool
expectedError error
validateF func(*testing.T, *templates.WorkflowTemplate, string)
validateF func(*testing.T, *machine.WorkflowTemplate, string)
}{
"requires_non_empty_ImageURL": {
mutateF: func(wt *templates.WorkflowTemplate) {
mutateF: func(wt *machine.WorkflowTemplate) {
wt.ImageURL = ""
},
expectError: true,
expectedError: templates.ErrMissingImageURL,
expectedError: machine.ErrMissingImageURL,
},

"requires_non_empty_Name": {
mutateF: func(wt *templates.WorkflowTemplate) {
mutateF: func(wt *machine.WorkflowTemplate) {
wt.Name = ""
},
expectError: true,
expectedError: templates.ErrMissingName,
expectedError: machine.ErrMissingName,
},

"renders_with_valid_config": {
mutateF: func(_ *templates.WorkflowTemplate) {},
mutateF: func(_ *machine.WorkflowTemplate) {},
},

"rendered_output_should_be_valid_YAML": {
validateF: func(t *testing.T, _ *templates.WorkflowTemplate, renderResult string) { //nolint:thelper
validateF: func(t *testing.T, _ *machine.WorkflowTemplate, renderResult string) { //nolint:thelper
g := NewWithT(t)
x := &map[string]interface{}{}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.33.1
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
github.com/spf13/pflag v1.0.5
github.com/tinkerbell/rufio v0.3.3
Expand Down Expand Up @@ -62,6 +61,7 @@ require (
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
Expand Down
146 changes: 0 additions & 146 deletions internal/templates/templates.go

This file was deleted.