mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-24 04:07:02 +00:00
fix: default to injecting missing defaults
This commit is contained in:
parent
46e6b6518e
commit
160be0f96a
2 changed files with 23 additions and 12 deletions
|
|
@ -44,8 +44,8 @@ func loadManifestAndSchema(manifestPath, schemaPath string) (map[string]interfac
|
||||||
return manifest, schema, nil
|
return manifest, schema, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// New function to inject defaults recursively
|
// New function to inject defaults recursively and return the injected defaults in a list form
|
||||||
func injectDefaultsRecursively(schema map[string]interface{}, manifest map[string]interface{}) {
|
func injectDefaultsRecursively(schema map[string]interface{}, manifest map[string]interface{}, injectedDefaults *[]string) {
|
||||||
properties, propertiesExist := schema["properties"].(map[string]interface{})
|
properties, propertiesExist := schema["properties"].(map[string]interface{})
|
||||||
if !propertiesExist {
|
if !propertiesExist {
|
||||||
return
|
return
|
||||||
|
|
@ -57,7 +57,7 @@ func injectDefaultsRecursively(schema map[string]interface{}, manifest map[strin
|
||||||
if ok {
|
if ok {
|
||||||
if defaultValue, hasDefault := subSchemaMap["default"]; hasDefault {
|
if defaultValue, hasDefault := subSchemaMap["default"]; hasDefault {
|
||||||
manifest[key] = defaultValue
|
manifest[key] = defaultValue
|
||||||
fmt.Printf("Injected default for %s: %v\\n", key, defaultValue)
|
*injectedDefaults = append(*injectedDefaults, fmt.Sprintf("%s: %v", key, defaultValue))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -65,14 +65,14 @@ func injectDefaultsRecursively(schema map[string]interface{}, manifest map[strin
|
||||||
if subSchemaType, typeExists := subSchemaMap["type"].(string); typeExists {
|
if subSchemaType, typeExists := subSchemaMap["type"].(string); typeExists {
|
||||||
if subSchemaType == "object" {
|
if subSchemaType == "object" {
|
||||||
if nestedManifest, isMap := manifest[key].(map[string]interface{}); isMap {
|
if nestedManifest, isMap := manifest[key].(map[string]interface{}); isMap {
|
||||||
injectDefaultsRecursively(subSchemaMap, nestedManifest)
|
injectDefaultsRecursively(subSchemaMap, nestedManifest, injectedDefaults)
|
||||||
}
|
}
|
||||||
} else if subSchemaType == "array" {
|
} else if subSchemaType == "array" {
|
||||||
if arrayItems, hasItems := subSchemaMap["items"].(map[string]interface{}); hasItems {
|
if arrayItems, hasItems := subSchemaMap["items"].(map[string]interface{}); hasItems {
|
||||||
if manifestArray, isArray := manifest[key].([]interface{}); isArray {
|
if manifestArray, isArray := manifest[key].([]interface{}); isArray {
|
||||||
for _, item := range manifestArray {
|
for _, item := range manifestArray {
|
||||||
if itemMap, isMap := item.(map[string]interface{}); isMap {
|
if itemMap, isMap := item.(map[string]interface{}); isMap {
|
||||||
injectDefaultsRecursively(arrayItems, itemMap)
|
injectDefaultsRecursively(arrayItems, itemMap, injectedDefaults)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -95,7 +95,7 @@ func processResults(cancel context.CancelFunc, o output.Output, validationResult
|
||||||
}
|
}
|
||||||
if o != nil {
|
if o != nil {
|
||||||
if err := o.Write(res); err != nil {
|
if err := o.Write(res); err != nil {
|
||||||
fmt.Fprint(os.Stderr, "failed writing log\\n")
|
fmt.Fprint(os.Stderr, "failed writing log\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !success && exitOnError {
|
if !success && exitOnError {
|
||||||
|
|
@ -171,17 +171,28 @@ func kubeconform(cfg config.Config) int {
|
||||||
if cfg.InjectMissingDefaults {
|
if cfg.InjectMissingDefaults {
|
||||||
manifest, schema, err := loadManifestAndSchema(cfg.Files[0], cfg.SchemaLocations[0])
|
manifest, schema, err := loadManifestAndSchema(cfg.Files[0], cfg.SchemaLocations[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error loading manifest or schema: %s\\n", err)
|
fmt.Fprintf(os.Stderr, "error loading manifest or schema: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List to store injected defaults
|
||||||
|
var injectedDefaults []string
|
||||||
|
|
||||||
// Inject defaults into the manifest
|
// Inject defaults into the manifest
|
||||||
injectDefaultsRecursively(schema, manifest)
|
injectDefaultsRecursively(schema, manifest, &injectedDefaults)
|
||||||
|
|
||||||
|
// Display injected defaults in list form if verbose flag is enabled
|
||||||
|
if cfg.Verbose && len(injectedDefaults) > 0 {
|
||||||
|
fmt.Println("Injected Defaults:")
|
||||||
|
for _, def := range injectedDefaults {
|
||||||
|
fmt.Printf(" - %s\n", def)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Convert the modified manifest back to YAML for validation
|
// Convert the modified manifest back to YAML for validation
|
||||||
updatedManifest, err := yaml.Marshal(manifest)
|
updatedManifest, err := yaml.Marshal(manifest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error converting updated manifest to YAML: %s\\n", err)
|
fmt.Fprintf(os.Stderr, "error converting updated manifest to YAML: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -268,7 +279,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed parsing command line: %s\\n", err.Error())
|
fmt.Fprintf(os.Stderr, "failed parsing command line: %s\n", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ func (kv k8sVersionValue) MarshalText() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kv *k8sVersionValue) UnmarshalText(v []byte) error {
|
func (kv *k8sVersionValue) UnmarshalText(v []byte) error {
|
||||||
if ok, _ := regexp.MatchString(`^(master|\d+\.\d+\.\d+)$`, string(v)); ok != true {
|
if ok, _ := regexp.MatchString(`^(master|\d+\.\d+\.\d+)$`, string(v)); !ok {
|
||||||
return fmt.Errorf("%v is not a valid version. Valid values are \"master\" (default) or full version x.y.z (e.g. \"1.27.2\")", string(v))
|
return fmt.Errorf("%v is not a valid version. Valid values are \"master\" (default) or full version x.y.z (e.g. \"1.27.2\")", string(v))
|
||||||
}
|
}
|
||||||
*kv = k8sVersionValue(v)
|
*kv = k8sVersionValue(v)
|
||||||
|
|
@ -103,7 +103,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
|
||||||
flags.BoolVar(&c.Version, "v", false, "show version information")
|
flags.BoolVar(&c.Version, "v", false, "show version information")
|
||||||
|
|
||||||
// New flag added for injecting missing defaults
|
// New flag added for injecting missing defaults
|
||||||
flags.BoolVar(&c.InjectMissingDefaults, "inject-missing-defaults", false, "Inject missing required fields with defaults from the schema")
|
flags.BoolVar(&c.InjectMissingDefaults, "inject-missing-defaults", true, "Inject missing required fields with defaults from the schema")
|
||||||
|
|
||||||
flags.Usage = func() {
|
flags.Usage = func() {
|
||||||
fmt.Fprintf(&buf, "Usage: %s [OPTION]... [FILE OR FOLDER]...\n", progName)
|
fmt.Fprintf(&buf, "Usage: %s [OPTION]... [FILE OR FOLDER]...\n", progName)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue