Merge pull request #356 from crawford/ignition
config: recognize Ignition configs and no-op
This commit is contained in:
		
							
								
								
									
										26
									
								
								config/ignition.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								config/ignition.go
									
									
									
									
									
										Normal 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) | ||||
| } | ||||
| @@ -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: | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
| @@ -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") | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user