Skip to content

Commit

Permalink
Add support for Fedora CoreOS snippets
Browse files Browse the repository at this point in the history
* Add support for Fedora CoreOS `snippets`
* Add a Fedora CoreOS example to the README
* Move example to examples for project consistency

Related: #22
  • Loading branch information
dghubble committed Mar 23, 2020
1 parent 6f22733 commit e27a7c8
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Notable changes between releases.

## Latest

* Add support for Fedora CoreOS `snippets` ([#58](https://github.com/poseidon/terraform-provider-ct/pull/58))
* Migrate to the Terraform Plugin SDK ([#56](https://github.com/poseidon/terraform-provider-ct/pull/56))
* Upgrade fcct from v0.1.0 to v0.4.0
* Upgrade fcct from v0.1.0 to v0.4.0 ([#57](https://github.com/poseidon/terraform-provider-ct/pull/57))

## v0.4.0

Expand Down
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@
Define a Container Linux Config (CLC) or Fedora CoreOS Config (FCC) in version control.

```yaml
# worker.yaml Container Linux Config
# Container Linux Config
---
passwd:
users:
- name: core
ssh_authorized_keys:
- ssh-key foo
```
```yaml
# Fedora CoreOS Config
---
variant: fcos
version: 1.0.0
passwd:
users:
- name: core
Expand All @@ -16,22 +29,25 @@ passwd:
Render the config with Terraform for machine consumption.
```hcl
# Define a data source
data "ct_config" "worker" {
content = file("worker.yaml")
pretty_print = false
strict = true
snippets = []
}

# Usage: Render the config as Ignition
resource "aws_instance" "worker" {
user_data = data.ct_config.worker.rendered
}
```

See the [Container Linux](example/container-linux.tf) or [Fedora CoreOS](example/fedora-coreos.tf) examples.
See the [Container Linux](examples/container-linux.tf) or [Fedora CoreOS](examples/fedora-coreos.tf) examples.

## Requirements

* Terraform v0.11+ [installed](https://www.terraform.io/downloads.html)
* Terraform v0.12+ [installed](https://www.terraform.io/downloads.html)

### Ignition Outputs

Expand Down
50 changes: 41 additions & 9 deletions ct/datasource_ct_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
clct "github.com/coreos/container-linux-config-transpiler/config"
fcct "github.com/coreos/fcct/config"
"github.com/coreos/fcct/config/common"

ignition "github.com/coreos/ignition/config/v2_2"
ignitionTypesV2_2 "github.com/coreos/ignition/config/v2_2/types"
ignitionTypes "github.com/coreos/ignition/config/v2_2/types"
ignitionV2 "github.com/coreos/ignition/v2/config/v3_0"
)

func dataSourceCTConfig() *schema.Resource {
Expand Down Expand Up @@ -83,7 +85,7 @@ func renderConfig(d *schema.ResourceData) (string, error) {
}

// Fedora CoreOS Config
ign, err := fccToIgnition([]byte(content), pretty, strict)
ign, err := fccToIgnition([]byte(content), pretty, strict, snippets)
if err == fcct.ErrNoVariant {
// consider as Container Linux Config
ign, err = renderCLC([]byte(content), platform, pretty, strict, snippets)
Expand All @@ -92,12 +94,42 @@ func renderConfig(d *schema.ResourceData) (string, error) {
}

// Translate Fedora CoreOS config to Ignition v3.X.Y
func fccToIgnition(data []byte, pretty, strict bool) ([]byte, error) {
ign, _, err := fcct.Translate(data, common.TranslateOptions{
func fccToIgnition(data []byte, pretty, strict bool, snippets []string) ([]byte, error) {
ignBytes, _, err := fcct.Translate(data, common.TranslateOptions{
Pretty: pretty,
Strict: strict,
})
return ign, err
// ErrNoVariant indicates data is a CLC, not an FCC
if err != nil {
return nil, err
}

ign, _, err := ignitionV2.Parse(ignBytes)
if err != nil {
return nil, err
}

for _, snippet := range snippets {
ignextBytes, _, err := fcct.Translate([]byte(snippet), common.TranslateOptions{
Pretty: pretty,
Strict: strict,
})
if err != nil {
// For FCC, require snippets be FCCs (don't fall-through to CLC)
if err == fcct.ErrNoVariant {
return []byte{}, fmt.Errorf("Fedora CoreOS snippets require `variant`: %v", err)
}
return nil, err
}
ignext, _, err := ignitionV2.Parse(ignextBytes)

ign = ignitionV2.Merge(ign, ignext)
}

if pretty {
return json.MarshalIndent(ign, "", " ")
}
return json.Marshal(ign)
}

// Translate Container Linux Config as Ignition JSON.
Expand All @@ -122,21 +154,21 @@ func renderCLC(data []byte, platform string, pretty, strict bool, snippets []str
}

// Parse Container Linux config and convert to Ignition v2.2.0 format.
func clcToIgnition(data []byte, platform string, strict bool) (ignitionTypesV2_2.Config, error) {
func clcToIgnition(data []byte, platform string, strict bool) (ignitionTypes.Config, error) {
// parse bytes into a Container Linux Config
clc, ast, report := clct.Parse([]byte(data))

if strict && len(report.Entries) > 0 {
return ignitionTypesV2_2.Config{}, fmt.Errorf("error strict parsing Container Linux Config: %v", report.String())
return ignitionTypes.Config{}, fmt.Errorf("error strict parsing Container Linux Config: %v", report.String())
}

if report.IsFatal() {
return ignitionTypesV2_2.Config{}, fmt.Errorf("error parsing Container Linux Config: %v", report.String())
return ignitionTypes.Config{}, fmt.Errorf("error parsing Container Linux Config: %v", report.String())
}
// convert Container Linux Config to an Ignition Config
ign, report := clct.Convert(clc, platform, ast)
if report.IsFatal() {
return ignitionTypesV2_2.Config{}, fmt.Errorf("error converting to Ignition: %v", report.String())
return ignitionTypes.Config{}, fmt.Errorf("error converting to Ignition: %v", report.String())
}
return ign, nil
}
70 changes: 70 additions & 0 deletions ct/datasource_ct_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ const snippetsExpected = `{
const fedoraCoreOSResource = `
data "ct_config" "fedora-coreos" {
pretty_print = true
strict = true
content = <<EOT
---
variant: fcos
Expand Down Expand Up @@ -235,6 +236,69 @@ const fedoraCoreOSExpected = `{
"systemd": {}
}`

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

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

const invalidResource = `
data "ct_config" "container-linux-strict" {
content = "foo"
Expand Down Expand Up @@ -271,6 +335,12 @@ func TestRender(t *testing.T) {
r.TestCheckResourceAttr("data.ct_config.fedora-coreos", "rendered", fedoraCoreOSExpected),
),
},
r.TestStep{
Config: fedoraCoreOSWithSnippets,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("data.ct_config.fedora-coreos-snippets", "rendered", fedoraCoreOSWithSnippetsExpected),
),
},
},
})
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions examples/content/fcc-snippet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variant: fcos
version: 1.0.0
storage:
files:
- path: /etc/zincati/config.d/90-disable-feature.toml
contents:
inline: |
[updates]
enabled = false
File renamed without changes.
4 changes: 4 additions & 0 deletions example/fedora-coreos.tf → examples/fedora-coreos.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ resource "local_file" "fedora-coreos-ign" {
data "ct_config" "fedora-coreos-config" {
content = data.template_file.fedora-coreos-worker.rendered
pretty_print = true

snippets = [
file("${path.module}/content/fcc-snippet.yaml"),
]
}

# Content (e.g. possibly templated)
Expand Down
2 changes: 1 addition & 1 deletion example/versions.tf → examples/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
local = "~> 1.2"
template = "~> 2.1"
ct = "~> 0.4.0"
ct = "~> 0.5.0"
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/coreos/container-linux-config-transpiler v0.9.0
github.com/coreos/fcct v0.4.0
github.com/coreos/ignition v0.28.0
github.com/coreos/ignition/v2 v2.1.1
github.com/hashicorp/terraform-plugin-sdk v1.8.0
go4.org v0.0.0-20200312051459-7028f7b4a332 // indirect
)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ github.com/coreos/ignition v0.24.0 h1:RGCRIVtEcOIhrG+pxrrPriqeemAnUu6fSwKGKJoEJ6
github.com/coreos/ignition v0.24.0/go.mod h1:WJQapxzEn9DE0ryxsGvm8QnBajm/XsS/PkrDqSpz+bA=
github.com/coreos/ignition v0.28.0 h1:wfX5JdoYrzH7pTJZlebeuwcb/NjzsEO6wgqs2OtVUPE=
github.com/coreos/ignition v0.28.0/go.mod h1:WJQapxzEn9DE0ryxsGvm8QnBajm/XsS/PkrDqSpz+bA=
github.com/coreos/ignition v0.35.0 h1:UFodoYq1mOPrbEjtxIsZbThcDyQwAI1owczRDqWmKkQ=
github.com/coreos/ignition/v2 v2.0.0 h1:2nIU6rQIh2bCSF0PNJKuNNa23L13G031CoxbI4GFojc=
github.com/coreos/ignition/v2 v2.0.0/go.mod h1:EJP9Gk/u21BPwWa5nT6yYbm5mO0u8JQAAzFqaxgH7Fg=
github.com/coreos/ignition/v2 v2.1.1 h1:siNZchKAWoLeV708X4QWG9chTYhssE93HBKkZx+JJO8=
Expand Down

0 comments on commit e27a7c8

Please sign in to comment.