From 823c2988e9634afeb69a5fec3f5cc854236b3948 Mon Sep 17 00:00:00 2001 From: Yu Qi Zhang Date: Wed, 8 Apr 2020 11:33:33 -0400 Subject: [PATCH] controller: do not error on empty Ignition configs With the introduction of rawExtension for Ignition config objects via https://github.com/openshift/machine-config-operator/pull/996, we also now use ignition.Parse directly to process the validity of ignition configs. This will cause machineconfigs without a Ignition section to fail, which we don't want. Also modify some error messages for clarity. Signed-off-by: Yu Qi Zhang --- pkg/controller/common/helpers.go | 41 +++++++++++++------ .../container-runtime-config/helpers.go | 2 +- pkg/controller/kubelet-config/helpers.go | 2 +- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/pkg/controller/common/helpers.go b/pkg/controller/common/helpers.go index 75de24d3c5..3455ad1ec9 100644 --- a/pkg/controller/common/helpers.go +++ b/pkg/controller/common/helpers.go @@ -28,10 +28,16 @@ func MergeMachineConfigs(configs []*mcfgv1.MachineConfig, osImageURL string) (*m var fips bool var kernelType string + var outIgn igntypes.Config - outIgn, report, err := ign.Parse(configs[0].Spec.Config.Raw) - if err != nil { - return nil, errors.Errorf("parsing Ignition config failed with error: %v\nReport: %v", err, report) + if configs[0].Spec.Config.Raw == nil { + outIgn = igntypes.Config{} + } else { + parsedIgn, report, err := ign.Parse(configs[0].Spec.Config.Raw) + if err != nil { + return nil, errors.Errorf("parsing Ignition config failed with error: %v\nReport: %v", err, report) + } + outIgn = parsedIgn } for idx := 1; idx < len(configs); idx++ { @@ -39,9 +45,16 @@ func MergeMachineConfigs(configs []*mcfgv1.MachineConfig, osImageURL string) (*m if configs[idx].Spec.FIPS { fips = true } - appendIgn, report, err := ign.Parse(configs[idx].Spec.Config.Raw) - if err != nil { - return nil, errors.Errorf("parsing appendix Ignition config failed with error: %v\nReport: %v", err, report) + + var appendIgn igntypes.Config + if configs[idx].Spec.Config.Raw == nil { + appendIgn = igntypes.Config{} + } else { + parsedIgn, report, err := ign.Parse(configs[idx].Spec.Config.Raw) + if err != nil { + return nil, errors.Errorf("parsing appendix Ignition config failed with error: %v\nReport: %v", err, report) + } + appendIgn = parsedIgn } outIgn = ign.Append(outIgn, appendIgn) } @@ -121,12 +134,16 @@ func ValidateMachineConfig(cfg mcfgv1.MachineConfigSpec) error { if !(cfg.KernelType == "" || cfg.KernelType == KernelTypeDefault || cfg.KernelType == KernelTypeRealtime) { return errors.Errorf("kernelType=%s is invalid", cfg.KernelType) } - ignCfg, report, err := ign.Parse(cfg.Config.Raw) - if err != nil { - return errors.Errorf("parsing Ignition config failed with error: %v\nReport: %v", err, report) - } - if err := ValidateIgnition(ignCfg); err != nil { - return err + + if cfg.Config.Raw != nil { + ignCfg, report, err := ign.Parse(cfg.Config.Raw) + if err != nil { + return errors.Errorf("parsing Ignition config failed with error: %v\nReport: %v", err, report) + } + if err := ValidateIgnition(ignCfg); err != nil { + return err + } } + return nil } diff --git a/pkg/controller/container-runtime-config/helpers.go b/pkg/controller/container-runtime-config/helpers.go index 899f8b459e..4c29ebbb25 100644 --- a/pkg/controller/container-runtime-config/helpers.go +++ b/pkg/controller/container-runtime-config/helpers.go @@ -138,7 +138,7 @@ func findRegistriesConfig(mc *mcfgv1.MachineConfig) (*igntypes.File, error) { func findPolicyJSON(mc *mcfgv1.MachineConfig) (*igntypes.File, error) { ignCfg, report, err := ign.Parse(mc.Spec.Config.Raw) if err != nil { - return nil, fmt.Errorf("parsing Ignition config failed with error: %v\nReport: %v", err, report) + return nil, fmt.Errorf("parsing Policy JSON Ignition config failed with error: %v\nReport: %v", err, report) } for _, c := range ignCfg.Storage.Files { if c.Path == policyConfigPath { diff --git a/pkg/controller/kubelet-config/helpers.go b/pkg/controller/kubelet-config/helpers.go index 0efe0c0f24..ccb5c4494c 100644 --- a/pkg/controller/kubelet-config/helpers.go +++ b/pkg/controller/kubelet-config/helpers.go @@ -52,7 +52,7 @@ func createNewDefaultFeatureGate() *osev1.FeatureGate { func findKubeletConfig(mc *mcfgv1.MachineConfig) (*igntypes.File, error) { ignCfg, report, err := ign.Parse(mc.Spec.Config.Raw) if err != nil { - return nil, fmt.Errorf("parsing Ignition config failed with error: %v\nReport: %v", err, report) + return nil, fmt.Errorf("parsing Kubelet Ignition config failed with error: %v\nReport: %v", err, report) } for _, c := range ignCfg.Storage.Files { if c.Path == "/etc/kubernetes/kubelet.conf" {