mirror of
https://github.com/outbackdingo/terraform-provider-ct.git
synced 2026-01-27 10:20:38 +00:00
Add support for Fedora CoreOS snippets
* Add support for Fedora CoreOS `snippets` * Add a Fedora CoreOS example to the README * Move example to examples for project consistency Related: https://github.com/poseidon/terraform-provider-ct/pull/22
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
22
README.md
22
README.md
@@ -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
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 {
|
||||
@@ -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)
|
||||
@@ -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.
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -194,6 +194,7 @@ const snippetsExpected = `{
|
||||
const fedoraCoreOSResource = `
|
||||
data "ct_config" "fedora-coreos" {
|
||||
pretty_print = true
|
||||
strict = true
|
||||
content = <<EOT
|
||||
---
|
||||
variant: fcos
|
||||
@@ -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"
|
||||
@@ -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),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
10
examples/content/fcc-snippet.yaml
Normal file
10
examples/content/fcc-snippet.yaml
Normal file
@@ -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
|
||||
|
||||
@@ -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)
|
||||
@@ -4,7 +4,7 @@ terraform {
|
||||
required_providers {
|
||||
local = "~> 1.2"
|
||||
template = "~> 2.1"
|
||||
ct = "~> 0.4.0"
|
||||
ct = "~> 0.5.0"
|
||||
}
|
||||
}
|
||||
|
||||
1
go.mod
1
go.mod
@@ -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
|
||||
)
|
||||
|
||||
1
go.sum
1
go.sum
@@ -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=
|
||||
|
||||
Reference in New Issue
Block a user