Kubernetes CRD Extensions

Explains extension points for deep CRD integrations within Spinnaker.

Spinnaker Extension Points for Custom Resource Definitions

At Google, we’ve built extension points for deep CRD integrations within Spinnaker.

This work has allowed us to support the following features within Spinnaker:

  • Custom models for representing CRDs as spinnakerKinds
  • Deploying CRDs with custom Spinnaker artifact types
  • Custom Kubernetes API versions
  • Custom Spinnaker naming strategies
  • Per-account, custom Spinnaker UIs that can run alongside the existing Kubernetes UIs

This guide is for developers who want to duplicate this functionality for their CRDs. It also exists as an explanation of certain code paths within Spinnaker which include hooks with no current corresponding open-source implementations.

Developers who want to implement these features will have to build their own layered version of Clouddriver - see Adam Jorden’s blog post - and should be familiar with the Kubernetes provider and writing code for Clouddriver.

Custom Handlers

The central extension point is the KubernetesHandler class. A subclass of KubernetesHandler - e.g., KubernetesReplicaSetHandler - defines the relationship between Spinnaker and your Kubernetes kind.

For example, if you wanted to build a Spinnaker integration for your CRD of kind MyCRDKind, you would start with the following handler:

  1. @Component
  2. public class MyCRDHandler extends KubernetesHandler {
  3. public MyCRDHandler() {
  4. // Hook point for registering a custom `ArtifactReplacer`
  5. // for your CRD. During a deploy operation,
  6. // if an artifact of the type specified in the replacer is present,
  7. // the artifact will be inserted into the manifest using the
  8. // strategy described in the replacer.
  9. }
  10. @Override
  11. public KubernetesKind kind() {
  12. return KubernetesKind.from("MyCRDKind");
  13. }
  14. @Override
  15. public boolean versioned() {
  16. // If the CRD resource should be versioned - i.e., assigned a sequence
  17. // v001, v002, etc.
  18. return false;
  19. }
  20. @Override
  21. public SpinnakerKind spinnakerKind() {
  22. // The Spinnaker kind that the resource will be represented as in Spinnaker's API and UI.
  23. return SpinnakerKind.SERVER_GROUPS;
  24. }
  25. @Override
  26. public Status status(KubernetesManifest manifest) {
  27. // Includes logic for determining whether your CRD manifest is stable.
  28. // A deploy manifest operation, for example, will block until this
  29. // method returns a stable status.
  30. }
  31. @Override
  32. public KubernetesV2CachingAgentFactory cachingAgentFactory() {
  33. return KubernetesCoreCachingAgent::new;
  34. }
  35. }

Custom Spinnaker Resource Models

You may want to change how their CRD is represented in Spinnaker’s API. By default, a CRD of spinnakerKind serverGroups will be represented with the model class KubernetesV2ServerGroup .

You may want to override this representation, for example, if you want to define how your server group’s region is resolved.

To override the default model class, MyCRDHandler should implement ServerGroupHandler (or ServerGroupManagerHandler if your resource is of spinnakerKind serverGroupManagers). You will be responsible for translating raw Spinnaker cache data into a subclass of KubernetesServerGroup.

Custom Kubernetes API Versions

If you want Spinnaker to support custom Kubernetes API versions, subclass KubernetesApiVersion.

For example,

  1. public class MyApiVersion extends KubernetesApiVersion {
  2. public static MY_BETA_API_VERSION = new MyApiVersion("myApiVersion/v1beta");
  3. public MyApiVersion(String name) {
  4. // Base class maintains state.
  5. super(name);
  6. }
  7. }

Custom Spinnaker Naming Strategies

To use a custom naming strategy for your CRD, implement NamingStrategy . For example,

  1. @Component
  2. public class MyManifestNamer implements NamingStrategy<KubernetesManifest> {
  3. @Override
  4. public String getName() {
  5. return "myManifestNamingStrategy";
  6. }
  7. @Override
  8. public void applyMoniker(KubernetesManifest manifest, Moniker moniker) {
  9. // Strategy for applying a Spinnaker `Moniker` to a Kubernetes
  10. // manifest prior to deployment.
  11. }
  12. @Override
  13. public Moniker deriveMoniker(KubernetesManifest manifest) {
  14. // Strategy for deriving a Spinnaker `Moniker` from a Kubernetes
  15. // manifest.
  16. }
  17. }

This naming strategy can be referenced in a Kubernetes account config. For example:

  1. kubernetes:
  2. accounts:
  3. - name: my-kubernetes-account
  4. namingStrategy: myManifestNamingStrategy

Be careful - this naming strategy will be applied to all manifests manipulated by this account.

Last modified May 13, 2021: docs(migrate): add remaining Extending Spinnaker pages (3835a79)