operator-sdk alpha config-gen

operator-sdk alpha config-gen

Generate configuration for controller-runtime based projects

Synopsis

config-gen programatically generates configuration for a controller-runtime based project using the project source code (golang) and a KubebuilderConfigGen resource file.

This is an alternative to expressing configuration as a static set of kustomize patches in the “config” directory.

config-gen may be used as a standalone command run against a file, as a kustomize transformer plugin, or as a configuration function (e.g. kpt).

config-gen uses the controller-tools generators to generate CRDs from the go source and then generates additional resources such as the namespace, controller-manager, webhooks, etc.

Following is an example KubebuilderConfigGen resource used by config-gen:

kubebuilderconfiggen.yaml

this resource describes how to generate configuration for a controller-runtime

based project

apiVersion: kubebuilder.sigs.k8s.io/v1alpha1 kind: KubebuilderConfigGen metadata: name: my-project-name spec: controllerManager: image: my-org-name/my-project-name:v0.1.0

If this file was at the project source root, config-gen could be used to emit configuration using:

kubebuilder alpha config-gen ./kubebuilderconfiggen.yaml

The KubebuilderConfigGen resource has the following fields:

apiVersion: kubebuilder.sigs.k8s.io/v1alpha1 kind: KubebuilderConfigGen

metadata: # name of the project. used in various resource names. # required name: project-name

  1. # namespace for the project
  2. # optional -- defaults to "${metadata.name}-system"
  3. namespace: project-namespace

spec: # configure how CRDs are generated crds: # path to go module source directory provided to controller-gen libraries # optional – defaults to ‘.’ sourceDirectory: ./relative/path

  1. # configure how the controller-manager is generated
  2. controllerManager:
  3. # image to run
  4. image: my-org/my-project:v0.1.0
  5. # if set, use component config for the controller-manager
  6. # optional
  7. componentConfig:
  8. # use component config
  9. enable: true
  10. # path to component config to put into a ConfigMap
  11. configFilepath: ./path/to/componentconfig.yaml
  12. # configure how metrics are exposed
  13. metrics:
  14. # disable the auth proxy required for scraping metrics
  15. # disable: false
  16. # generate prometheus ServiceMonitor resource
  17. enableServiceMonitor: true
  18. # configure how webhooks are generated
  19. # optional -- defaults to not generating webhook configuration
  20. webhooks:
  21. # enable will cause webhook config to be generated
  22. enable: true
  23. # configures crds which use conversion webhooks
  24. enableConversion:
  25. # key is the name of the CRD
  26. "bars.example.my.domain": true
  27. # configures where to get the certificate used for webhooks
  28. # discriminated union
  29. certificateSource:
  30. # type of certificate source
  31. # one of ["certManager", "dev", "manual"] -- defaults to "manual"
  32. # certManager: certmanager is used to manage certificates -- requires CertManager to be installed
  33. # dev: certificate is generated and wired into resources
  34. # manual: no certificate is generated or wired into resources
  35. type: "dev"
  36. # options for a dev certificate -- requires "dev" as the type
  37. devCertificate:
  38. duration: 1h
  1. operator-sdk alpha config-gen PROJECT_FILE [RESOURCE_PATCHES...] [flags]

Examples

  1. #
  2. # As command
  3. #
  4. # create the kubebuilderconfiggen.yaml at project root
  5. cat > kubebuilderconfiggen.yaml <<EOF
  6. apiVersion: kubebuilder.sigs.k8s.io/v1alpha1
  7. kind: KubebuilderConfigGen
  8. metadata:
  9. name: project
  10. spec:
  11. controllerManager
  12. image: org/project:v0.1.0
  13. EOF
  14. # run the config generator
  15. kubebuilder alpha config-gen kubebuilderconfiggen.yaml
  16. # run the config generator and apply
  17. kubebuilder alpha config-gen kubebuilderconfiggen.yaml | kubectl apply -f -
  18. # generate configuration from a file with patches
  19. kubebuilder alpha config-gen kubebuilderconfiggen.yaml patch1.yaml patch2.yaml
  20. #
  21. # As Kustomize plugin
  22. # this allows using config-gen with kustomize features such as patches, commonLabels,
  23. # commonAnnotations, resources, configMapGenerator and other transformer plugins.
  24. #
  25. # install the kustomize version used in the v3 plugin
  26. # set VERSION to install a different version
  27. curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/v${VERSION:-3.8.9}/hack/install_kustomize.sh" | bash -s -- "${VERSION:-3.8.9}"
  28. # install the command as a kustomize plugin
  29. kubebuilder alpha config-gen install-as-plugin
  30. # create the kustomization.yaml containing the KubebuilderConfigGen resource
  31. cat > kustomization.yaml <<EOF
  32. apiVersion: kustomize.config.k8s.io/v1beta1
  33. kind: Kustomization
  34. transformers:
  35. - |-
  36. apiVersion: kubebuilder.sigs.k8s.io/v1alpha1
  37. kind: KubebuilderConfigGen
  38. metadata:
  39. name: my-project
  40. spec:
  41. controllerManager:
  42. image: my-org/my-project:v0.1.0
  43. EOF
  44. # generate configuration from kustomize > v4.0.0
  45. kustomize build --enable-alpha-plugins .
  46. # generate configuration from kustomize <= v4.0.0
  47. kustomize build --enable_alpha_plugins .

Options

  1. -h, --help help for config-gen
  2. --stack print the stack trace on failure

Options inherited from parent commands

  1. --plugins strings plugin keys to be used for this subcommand execution
  2. --verbose Enable verbose logging

SEE ALSO

Last modified May 13, 2022: :sparkles: Upgrade kubebuilder from 3.3.0 to 3.4.1 (#5735) (2d0aae81)