I am building a k8s operator using operator-sdk, which manages custom resource called Module:

type Module struct {
    metav1.TypeMeta   `json:",inline"`
    metav1.ObjectMeta `json:"metadata,omitempty"`
    Spec   ModuleSpec   `json:"spec,omitempty"`
    Status ModuleStatus `json:"status,omitempty"`
}

type ModuleSpec struct {
    App string `json:"app"`
    WorkflowSettings WorkflowSettings `json:"workflowSettings,omitempty"`
    WorkerSettings WorkerSettings `json:"workerSettings"`
}

type WorkflowSettings struct {
    RunAfter []RunAfterConfig `json:"runAfter"`
    InputMapping Mapping `json:"inputMapping,omitempty"`
    OutputMapping Mapping `json:"outputMapping,omitempty"`
    FieldsProjection []string `json:"fieldsProjection,omitempty"`
}

ModuleSpec has an optional field WorkflowSettings. WorkflowSettings in turn has required field RunAfter and butch of other optional fields. When I apply a custom resource instance where ModuleSpec.WorkflowSettings field is not present (it's optional an optional field) as a yaml file - everything is ok. But then in operator's reconciliation loop I'am trying to update some of the metadata fields of this CR instance (ObjectMeta) and got validation error from kube api:

"Module.module.dev.module \"test-module\" is invalid: [spec.workflowSettings.runAfter: Required value, <nil>: Invalid value: \"null\": some validation rules were not checked because the object was invalid; correct the existing errors to complete validation]"

It seems to me when golang struct Module is initialized, every not filled field has default values. API client tries to apply it with all this default values to the cluster and we got validation error: spec.workflowSettings is now present but spec.workflowSettings.runAfter is empty.

Please help me to resolve this issue.

0

There are 0 best solutions below