From c6400f77518455c31e302c1977758c81f4911596 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Mon, 6 Jul 2015 17:37:02 -0700 Subject: [PATCH] config: recognize Ignition configs and no-op --- config/ignition.go | 26 ++++++++++++++++++++++++++ config/validate/validate.go | 2 ++ config/validate/validate_test.go | 10 ++++++++++ coreos-cloudinit.go | 12 ++++++++---- initialize/user_data.go | 6 ++++++ 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 config/ignition.go diff --git a/config/ignition.go b/config/ignition.go new file mode 100644 index 0000000..413a636 --- /dev/null +++ b/config/ignition.go @@ -0,0 +1,26 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package config + +import ( + "encoding/json" +) + +func IsIgnitionConfig(userdata string) bool { + var cfg struct { + Version *int `json:"ignitionVersion" yaml:"ignition_version"` + } + return (json.Unmarshal([]byte(userdata), &cfg) == nil && cfg.Version != nil) +} diff --git a/config/validate/validate.go b/config/validate/validate.go index e1b7a9f..d8bd1e5 100644 --- a/config/validate/validate.go +++ b/config/validate/validate.go @@ -40,6 +40,8 @@ func Validate(userdataBytes []byte) (Report, error) { return Report{}, nil case config.IsScript(string(userdataBytes)): return Report{}, nil + case config.IsIgnitionConfig(string(userdataBytes)): + return Report{}, nil case config.IsCloudConfig(string(userdataBytes)): return validateCloudConfig(userdataBytes, Rules) default: diff --git a/config/validate/validate_test.go b/config/validate/validate_test.go index dee79ad..0bb3a39 100644 --- a/config/validate/validate_test.go +++ b/config/validate/validate_test.go @@ -111,6 +111,16 @@ func TestValidate(t *testing.T) { { config: "#!/bin/bash\necho hey", }, + { + config: "{}", + report: Report{entries: []Entry{{entryError, `must be "#cloud-config" or begin with "#!"`, 1}}}, + }, + { + config: `{"ignitionVersion":0}`, + }, + { + config: `{"ignitionVersion":1}`, + }, } for i, tt := range tests { diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index e29ff7e..ac98107 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -200,16 +200,20 @@ func main() { var ccu *config.CloudConfig var script *config.Script - if ud, err := initialize.ParseUserData(userdata); err != nil { - fmt.Printf("Failed to parse user-data: %v\nContinuing...\n", err) - failure = true - } else { + switch ud, err := initialize.ParseUserData(userdata); err { + case initialize.ErrIgnitionConfig: + fmt.Printf("Detected an Ignition config. Exiting...") + os.Exit(0) + case nil: switch t := ud.(type) { case *config.CloudConfig: ccu = t case *config.Script: script = t } + default: + fmt.Printf("Failed to parse user-data: %v\nContinuing...\n", err) + failure = true } fmt.Println("Merging cloud-config from meta-data and user-data") diff --git a/initialize/user_data.go b/initialize/user_data.go index 170efaa..c728d64 100644 --- a/initialize/user_data.go +++ b/initialize/user_data.go @@ -21,6 +21,10 @@ import ( "github.com/coreos/coreos-cloudinit/config" ) +var ( + ErrIgnitionConfig = errors.New("not a config (found Ignition)") +) + func ParseUserData(contents string) (interface{}, error) { if len(contents) == 0 { return nil, nil @@ -33,6 +37,8 @@ func ParseUserData(contents string) (interface{}, error) { case config.IsCloudConfig(contents): log.Printf("Parsing user-data as cloud-config") return config.NewCloudConfig(contents) + case config.IsIgnitionConfig(contents): + return nil, ErrIgnitionConfig default: return nil, errors.New("Unrecognized user-data format") }