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 latest kustomize
  26. GO111MODULE=on go get sigs.k8s.io/kustomize/kustomize/v4
  27. # install the command as a kustomize plugin
  28. kubebuilder alpha config-gen install-as-plugin
  29. # create the kustomization.yaml containing the KubebuilderConfigGen resource
  30. cat > kustomization.yaml <<EOF
  31. apiVersion: kustomize.config.k8s.io/v1beta1
  32. kind: Kustomization
  33. transformers:
  34. - |-
  35. apiVersion: kubebuilder.sigs.k8s.io/v1alpha1
  36. kind: KubebuilderConfigGen
  37. metadata:
  38. name: my-project
  39. spec:
  40. controllerManager:
  41. image: my-org/my-project:v0.1.0
  42. EOF
  43. # generate configuration from kustomize > v4.0.0
  44. kustomize build --enable-alpha-plugins .
  45. # generate configuration from kustomize <= v4.0.0
  46. 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 July 15, 2021: Bump k8s dependencies to 1.21+ (#5057) (fb2a6577)