Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Butane Config v1.4.0 support #100

Merged
merged 1 commit into from
Jul 16, 2021
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ passwd:
# Butane config
---
variant: fcos
version: 1.3.0
version: 1.4.0
passwd:
users:
- name: core
Expand Down Expand Up @@ -80,6 +80,7 @@ Container Linux Configs render a fixed Ignition version, depending on the `terra

| terraform-provider-ct | CLC to Ignition | Butane to Ignition |
|-----------------------|---------------------|--------------------|
| 0.9.x | Renders 2.3.0 | Butane (1.0, 1.1, 1.2, 1.3, 1.4) -> Ignition (3.0, 3.1, 3.2, 3.2, 3.3)
| 0.8.x | Renders 2.3.0 | Butane (1.0, 1.1, 1.2, 1.3) -> Ignition (3.0, 3.1, 3.2, 3.2)
| 0.7.x | Renders 2.3.0 | Butane (1.0, 1.1, 1.2) -> Ignition (3.0, 3.1, 3.2) |
| 0.6.x | Renders 2.3.0 | Butane (1.0, 1.1) -> Ignition (3.0, 3.1) |
Expand Down
41 changes: 38 additions & 3 deletions ct/datasource_ct_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
ignition31Types "github.com/coreos/ignition/v2/config/v3_1/types"
ignition32 "github.com/coreos/ignition/v2/config/v3_2"
ignition32Types "github.com/coreos/ignition/v2/config/v3_2/types"
ignition33 "github.com/coreos/ignition/v2/config/v3_3"
ignition33Types "github.com/coreos/ignition/v2/config/v3_3/types"
)

func dataSourceCTConfig() *schema.Resource {
Expand Down Expand Up @@ -130,14 +132,24 @@ func fccToIgnition(data []byte, pretty, strict bool, snippets []string) ([]byte,
// Upstream might later handle: https://github.com/coreos/butane/issues/118
// Note: This means snippets version must match the main config version.
func mergeFCCSnippets(ignBytes []byte, pretty, strict bool, snippets []string) ([]byte, error) {
ign, _, err := ignition32.Parse(ignBytes)
ign33, _, err := ignition33.Parse(ignBytes)
if err == nil {
// FCC config v1.4.0
ign33, err = mergeFCC14(ign33, snippets, pretty, strict)
if err != nil {
return nil, fmt.Errorf("FCC v1.4.0 merge error: %v", err)
}
return marshalJSON(ign33, pretty)
}

ign32, _, err := ignition32.Parse(ignBytes)
if err == nil {
// FCC config v1.2.0
ign, err = mergeFCC12(ign, snippets, pretty, strict)
ign32, err = mergeFCC12(ign32, snippets, pretty, strict)
if err != nil {
return nil, fmt.Errorf("FCC v1.2.0 merge error: %v", err)
}
return marshalJSON(ign, pretty)
return marshalJSON(ign32, pretty)
}

ign31, _, err := ignition31.Parse(ignBytes)
Expand All @@ -163,6 +175,29 @@ func mergeFCCSnippets(ignBytes []byte, pretty, strict bool, snippets []string) (
return marshalJSON(ign30, pretty)
}

// merge FCC v1.4.0 snippets
func mergeFCC14(ign ignition33Types.Config, snippets []string, pretty, strict bool) (ignition33Types.Config, error) {
for _, snippet := range snippets {
ignextBytes, _, err := butane.TranslateBytes([]byte(snippet), common.TranslateBytesOptions{
Pretty: pretty,
Strict: strict,
})
if err != nil {
// For FCC, require snippets be FCCs (don't fall-through to CLC)
if err == common.ErrNoVariant {
return ign, fmt.Errorf("Fedora CoreOS snippets require `variant`: %v", err)
}
return ign, fmt.Errorf("snippet v1.4.0 translate error: %v", err)
}
ignext, _, err := ignition33.Parse(ignextBytes)
if err != nil {
return ign, fmt.Errorf("snippet parse error: %v, expect v1.4.0", err)
}
ign = ignition33.Merge(ign, ignext)
}
return ign, nil
}

// merge FCC v1.2.0 snippets
func mergeFCC12(ign ignition32Types.Config, snippets []string, pretty, strict bool) (ignition32Types.Config, error) {
for _, snippet := range snippets {
Expand Down
165 changes: 165 additions & 0 deletions ct/datasource_ct_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,171 @@ func TestContainerLinuxConfig(t *testing.T) {

// Fedora CoreOS

const fedoraCoreOSV14Resource = `
data "ct_config" "fedora-coreos" {
pretty_print = true
strict = true
content = <<EOT
---
variant: fcos
version: 1.4.0
storage:
luks:
- name: data
device: /dev/vdb
passwd:
users:
- name: core
ssh_authorized_keys:
- key
EOT
}
`

const fedoraCoreOSV14Expected = `{
"ignition": {
"version": "3.3.0"
},
"passwd": {
"users": [
{
"name": "core",
"sshAuthorizedKeys": [
"key"
]
}
]
},
"storage": {
"luks": [
{
"device": "/dev/vdb",
"name": "data"
}
]
}
}`

const fedoraCoreOSV14WithSnippets = `
data "ct_config" "fedora-coreos-snippets" {
pretty_print = true
strict = true
content = <<EOT
---
variant: fcos
version: 1.4.0
passwd:
users:
- name: core
ssh_authorized_keys:
- key
EOT
snippets = [
<<EOT
---
variant: fcos
version: 1.4.0
systemd:
units:
- name: docker.service
enabled: true
EOT
]
}
`

const fedoraCoreOSV14WithSnippetsExpected = `{
"ignition": {
"config": {
"replace": {
"verification": {}
}
},
"proxy": {},
"security": {
"tls": {}
},
"timeouts": {},
"version": "3.3.0"
},
"kernelArguments": {},
"passwd": {
"users": [
{
"name": "core",
"sshAuthorizedKeys": [
"key"
]
}
]
},
"storage": {},
"systemd": {
"units": [
{
"enabled": true,
"name": "docker.service"
}
]
}
}`

const fedoraCoreOSV14WithSnippetsPrettyFalse = `
data "ct_config" "fedora-coreos-snippets" {
pretty_print = false
strict = true
content = <<EOT
---
variant: fcos
version: 1.4.0
passwd:
users:
- name: core
ssh_authorized_keys:
- key
EOT
snippets = [
<<EOT
---
variant: fcos
version: 1.4.0
systemd:
units:
- name: docker.service
enabled: true
EOT
]
}
`

const fedoraCoreOSV14WithSnippetsPrettyFalseExpected = `{"ignition":{"config":{"replace":{"verification":{}}},"proxy":{},"security":{"tls":{}},"timeouts":{},"version":"3.3.0"},"kernelArguments":{},"passwd":{"users":[{"name":"core","sshAuthorizedKeys":["key"]}]},"storage":{},"systemd":{"units":[{"enabled":true,"name":"docker.service"}]}}`

func TestFedoraCoreOSConfigV14(t *testing.T) {
r.UnitTest(t, r.TestCase{
Providers: testProviders,
Steps: []r.TestStep{
r.TestStep{
Config: fedoraCoreOSV14Resource,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("data.ct_config.fedora-coreos", "rendered", fedoraCoreOSV14Expected),
),
},
r.TestStep{
Config: fedoraCoreOSV14WithSnippets,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("data.ct_config.fedora-coreos-snippets", "rendered", fedoraCoreOSV14WithSnippetsExpected),
),
},
r.TestStep{
Config: fedoraCoreOSV14WithSnippetsPrettyFalse,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("data.ct_config.fedora-coreos-snippets", "rendered", fedoraCoreOSV14WithSnippetsPrettyFalseExpected),
),
},
},
})
}

const fedoraCoreOSV13Resource = `
data "ct_config" "fedora-coreos" {
pretty_print = true
Expand Down