Search code examples
kubernetes-operatorkubebuilder

Kubebuilder project with a component config - could not decode file into runtime.Object


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.


Solution

  • 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 ;) )