Skip to content

Commit

Permalink
Use go templating
Browse files Browse the repository at this point in the history
Signed-off-by: Ondra Machacek <omachace@redhat.com>
  • Loading branch information
machacekondra committed Oct 16, 2024
1 parent e32c0cd commit 32b79e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
10 changes: 5 additions & 5 deletions data/config.ign.template → data/ignition.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ passwd:
- name: core
password_hash: "$y$j9T$hUUbW8zoB.Qcmpwm4/RuK1$FMtuDAxNLp3sEa2PnGiJdXr8uYbvUNPlVDXpcJim529"
ssh_authorized_keys:
- @SSH_KEY@
- {{.SshKey}}

storage:
links:
Expand Down Expand Up @@ -53,11 +53,11 @@ storage:
data-dir: /agent/data
www-dir: /app/www
log-level: debug
source-id: @CONFIG_ID@
source-id: {{.SourceId}}
update-interval: 5s
planner-service:
service:
server: @CONFIG_SERVER@
server: {{.PlannerService}}
mode: 0644
user:
name: core
Expand Down Expand Up @@ -109,7 +109,7 @@ storage:
Wants=planner-setup.service

[Container]
Image=@MIGRATION_PLANNER_AGENT_IMAGE@
Image={{.MigrationPlannerAgentImage}}
ContainerName=planner-agent
AutoUpdate=registry
Exec= -config /agent/config.yaml
Expand Down Expand Up @@ -146,7 +146,7 @@ storage:
Wants=planner-agent-opa.service

[Container]
Image=@MIGRATION_PLANNER_COLLECTOR_IMAGE@
Image={{.MigrationPlannerCollectorImage}}
ContainerName=migration-planner-collector
AutoUpdate=registry
Exec=/vol/data/credentials.json /vol/data/inventory.json
Expand Down
36 changes: 26 additions & 10 deletions internal/image/ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package image

import (
"archive/tar"
"bytes"
"fmt"
"io"
"os"
"strings"
"text/template"
"time"

"github.com/coreos/butane/config"
Expand All @@ -26,6 +27,15 @@ type Ova struct {
SshKey string
}

// IgnitionData defines modifiable fields in ignition config
type IgnitionData struct {
SourceId string
SshKey string
PlannerService string
MigrationPlannerCollectorImage string
MigrationPlannerAgentImage string
}

type Image interface {
Generate() (io.Reader, error)
}
Expand Down Expand Up @@ -112,18 +122,24 @@ func writeOvf(tw *tar.Writer) error {
}

func (o *Ova) generateIgnition() (string, error) {
butaneTemplate, err := os.ReadFile("data/config.ign.template")
if err != nil {
return "", fmt.Errorf("error reading ignition template file: %w", err)
ignData := IgnitionData{
SourceId: o.Id.String(),
SshKey: o.SshKey,
PlannerService: util.GetEnv("CONFIG_SERVER", "http://127.0.0.1:7443"),
MigrationPlannerCollectorImage: util.GetEnv("MIGRATION_PLANNER_COLLECTOR_IMAGE", "quay.io/kubev2v/migration-planner-collector"),
MigrationPlannerAgentImage: util.GetEnv("MIGRATION_PLANNER_AGENT_IMAGE", "quay.io/kubev2v/migration-planner-agent"),
}

butaneContent := strings.Replace(string(butaneTemplate), "@CONFIG_ID@", o.Id.String(), -1)
butaneContent = strings.Replace(butaneContent, "@SSH_KEY@", o.SshKey, -1)
butaneContent = strings.Replace(butaneContent, "@CONFIG_SERVER@", util.GetEnv("CONFIG_SERVER", "http://127.0.0.1:7443"), -1)
butaneContent = strings.Replace(butaneContent, "@MIGRATION_PLANNER_COLLECTOR_IMAGE@", util.GetEnv("MIGRATION_PLANNER_COLLECTOR_IMAGE", "quay.io/kubev2v/migration-planner-collector"), -1)
butaneContent = strings.Replace(butaneContent, "@MIGRATION_PLANNER_AGENT_IMAGE@", util.GetEnv("MIGRATION_PLANNER_AGENT_IMAGE", "quay.io/kubev2v/migration-planner-agent"), -1)
var buf bytes.Buffer
t, err := template.New("ignition.template").ParseFiles("data/ignition.template")
if err != nil {
return "", fmt.Errorf("error reading the ignition template: %w", err)
}
if err := t.Execute(&buf, ignData); err != nil {
return "", fmt.Errorf("error parsing the ignition template: %w", err)
}

dataOut, _, err := config.TranslateBytes([]byte(butaneContent), common.TranslateBytesOptions{})
dataOut, _, err := config.TranslateBytes(buf.Bytes(), common.TranslateBytesOptions{})
if err != nil {
return "", fmt.Errorf("error translating config: %w", err)
}
Expand Down

0 comments on commit 32b79e5

Please sign in to comment.