1

I tried to add a custom config for a Kubernetes operator with a kubebuilder ComponentConfig, but I received the bellow mentioned error:

ERROR setup unable to load the config file {"error": "could not decode file into runtime.Object"}

I set the value of configFile to config/manager/controller_manager_config.yaml, because by default it is an empty string:


    var configFile string
    flag.StringVar(&configFile, "config", "config/manager/controller_manager_config.yaml",
        "The controller will load its initial configuration from this file. "+
            "Omit this flag to use the default configuration values. "+
            "Command-line flags override configuration from this file.")

    var err error
    var options ctrl.Options
    ctrlConfig := v1alpha1.ProjectConfig{}

    if configFile != "" {
        options = ctrl.Options{Scheme: scheme}

        c := ctrl.ConfigFile().AtPath(configFile).OfKind(&ctrlConfig)
        options, err = options.AndFrom(c)

        if err != nil {
            setupLog.Error(err, "unable to load the config file")
            os.Exit(1)
        }
    }

I'm not pasting all the changes I did because I also tried to run the example from kubebuilder's repository with the same result.

When I try to load the options without the OfKind the options are loaded correctly:

c := ctrl.ConfigFile().AtPath(configFile) //.OfKind(&ctrlConfig)
options, err = options.AndFrom(c)

Is it a bug in one of the packages or is my configFile set improperly, because that's the only thing I changed in the example project? The full list of used packages is available here:

require (
    k8s.io/apimachinery v0.23.5 // for `kubebuilder alpha config-gen`
    sigs.k8s.io/controller-runtime v0.11.2
    sigs.k8s.io/controller-tools v0.8.0 // for `kubebuilder alpha config-gen`
    sigs.k8s.io/kustomize/kyaml v0.13.6 // for `kubebuilder alpha config-gen`
    sigs.k8s.io/yaml v1.3.0
)

projectconfig_types.go:

package v1alpha1

import (
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    cfg "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"
)

// +kubebuilder:object:root=true

// ProjectConfig is the Schema for the projectconfigs API
type ProjectConfig struct {
    metav1.TypeMeta `json:",inline"`

    // ControllerManagerConfigurationSpec returns the configurations for controllers
    cfg.ControllerManagerConfigurationSpec `json:",inline"`

    ClusterName string `json:"clusterName,omitempty"`
}

func init() {
    SchemeBuilder.Register(&ProjectConfig{})
}

Thanks.

Kubus
  • 677
  • 6
  • 18

3 Answers3

1

I expect you faced the same problem as me. If you follow https://book.kubebuilder.io/component-config-tutorial/define-custom-config.html then there is a bug in the documentation (or it is unclear). ApiVersion and Kind shouldn't be:

apiVersion: controller-runtime.sigs.k8s.io/v1alpha1
kind: ControllerManagerConfig

...but:

apiVersion: config.tutorial.kubebuilder.io/v2
kind: ProjectConfig

I wasted whole day to figure it out :/ (I feel like a moron ;) )

cisr
  • 34
  • 2
0

I had the same error. I had to dig deep into sigs.k8s.io.json.internal.golang.encoding.json.decode.unmarshal()

In my Go struct, i have defined a []string, but in the config manifest yaml, i created a "single string" comma-separated list of elements. So my manifest was invalid. Unfortunately, some Kubernetes component hides the real reason of the error.

mhmxs
  • 267
  • 1
  • 2
  • 13
-2

See that first of all you must init the project using the component-config option or do the changes to make it work with: https://book.kubebuilder.io/component-config-tutorial/api-changes.html

After that, if you want to pass other specs you need to build a custom type: https://book.kubebuilder.io/component-config-tutorial/custom-type.html

Please, ensure that you follow up and read the whole tutorial from the beginner before trying to use this feature.

Camila Macedo
  • 857
  • 7
  • 10
  • 1
    The provided component-config sample in the kubebuilder repository doesn't work with the patched args from the kustomization - "--config=controller_manager_config.yaml". We followed the whole tutorial of course and the component-config-turorial has already been initialized by a kubebuilder author not by us. – Kubus Jul 01 '22 at 08:47
  • I check the steps and your message again and I understand now the misunderstanding. The docs are missing let you know that because you create a new custom type you must update the default scaffold to use it instead, You are right – Camila Macedo Jul 08 '22 at 10:46
  • Hi @Camila Macedo I didn't want to be right. I wanted to be helpful. Thank you! – Kubus Jul 08 '22 at 21:02