Skip to content

Commit

Permalink
Merge pull request #381 from jacobweinstock/file-reorg
Browse files Browse the repository at this point in the history
Move templates pkg to machine:

## Description

<!--- Please describe what this PR is going to change -->
The templates package was missed in the original file reorg PR. The functionality didn't need to be its own package.

> [!NOTE] 
> No functionality has been changed.

## Why is this needed

<!--- Link to issue you have raised -->

Fixes: #

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->


## How are existing users impacted? What migration steps/scripts do we need?

<!--- Fixes a bug, unblocks installation, removes a component of the stack etc -->
<!--- Requires a DB migration script, etc. -->


## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [ ] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
jacobweinstock authored Jun 29, 2024
2 parents 99d924a + 9ea5b79 commit 9202ef8
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 161 deletions.
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.

0 comments on commit 9202ef8

Please sign in to comment.