config: recognize Ignition configs and no-op

This commit is contained in:
Alex Crawford 2015-07-06 17:37:02 -07:00
parent f6647634f0
commit c6400f7751
5 changed files with 52 additions and 4 deletions

26
config/ignition.go Normal file
View File

@ -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)
}

View File

@ -40,6 +40,8 @@ func Validate(userdataBytes []byte) (Report, error) {
return Report{}, nil return Report{}, nil
case config.IsScript(string(userdataBytes)): case config.IsScript(string(userdataBytes)):
return Report{}, nil return Report{}, nil
case config.IsIgnitionConfig(string(userdataBytes)):
return Report{}, nil
case config.IsCloudConfig(string(userdataBytes)): case config.IsCloudConfig(string(userdataBytes)):
return validateCloudConfig(userdataBytes, Rules) return validateCloudConfig(userdataBytes, Rules)
default: default:

View File

@ -111,6 +111,16 @@ func TestValidate(t *testing.T) {
{ {
config: "#!/bin/bash\necho hey", 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 { for i, tt := range tests {

View File

@ -200,16 +200,20 @@ func main() {
var ccu *config.CloudConfig var ccu *config.CloudConfig
var script *config.Script var script *config.Script
if ud, err := initialize.ParseUserData(userdata); err != nil { switch ud, err := initialize.ParseUserData(userdata); err {
fmt.Printf("Failed to parse user-data: %v\nContinuing...\n", err) case initialize.ErrIgnitionConfig:
failure = true fmt.Printf("Detected an Ignition config. Exiting...")
} else { os.Exit(0)
case nil:
switch t := ud.(type) { switch t := ud.(type) {
case *config.CloudConfig: case *config.CloudConfig:
ccu = t ccu = t
case *config.Script: case *config.Script:
script = t 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") fmt.Println("Merging cloud-config from meta-data and user-data")

View File

@ -21,6 +21,10 @@ import (
"github.com/coreos/coreos-cloudinit/config" "github.com/coreos/coreos-cloudinit/config"
) )
var (
ErrIgnitionConfig = errors.New("not a config (found Ignition)")
)
func ParseUserData(contents string) (interface{}, error) { func ParseUserData(contents string) (interface{}, error) {
if len(contents) == 0 { if len(contents) == 0 {
return nil, nil return nil, nil
@ -33,6 +37,8 @@ func ParseUserData(contents string) (interface{}, error) {
case config.IsCloudConfig(contents): case config.IsCloudConfig(contents):
log.Printf("Parsing user-data as cloud-config") log.Printf("Parsing user-data as cloud-config")
return config.NewCloudConfig(contents) return config.NewCloudConfig(contents)
case config.IsIgnitionConfig(contents):
return nil, ErrIgnitionConfig
default: default:
return nil, errors.New("Unrecognized user-data format") return nil, errors.New("Unrecognized user-data format")
} }