How Can I Automatically Detect the Kubernetes Distribution?

I want to be able to detect the Kubernetes distribution where the application is being deployed to so I can automatically only show Config fields that apply to the distribution. I also want to be able to only run specific preflight checks, and deploy cluster specific resources.

Replicated exposes this value via the Distribution Static Context. As he documentation states, using this you may get one of the following values:

  • microk8s
  • dockerDesktop
  • eks
  • gke
  • digitalOcean
  • openShift
  • kurl
  • aks
  • ibm
  • minikube
  • rke2
  • k3s

Below is an example of using this to display a label when the application is being deployed on GKE

apiVersion: kots.io/v1beta1
kind: Config
metadata:
  name: example-config
spec:
  groups:
    - name: example_settings
      title: My Example Config
      description: Configuration to serve as an example for creating your own. See [https://kots.io/reference/v1beta1/config/](https://kots.io/reference/v1beta1/config/) for configuration docs. In this case, we provide example fields for configuring an Ingress object.
      items:
      - name: gke-description
        type: label
        title: "You are Deploying to GKE"
        when: 'repl{{ eq Distribution "gke" }}'
      - name: kurl-description
        type: label
        title: "You are Deploying to kURL"
        when: 'repl{{ eq Distribution "kurl" }}'
      - name: eks-description
        type: label
        title: "You are Deploying to EKS"
        when: 'repl{{ eq Distribution "eks" }}'

When Deploying to a GKE Cluster, only the correct label is displayed:

You could also use this to determine which workload to deploy. For example, you may want to deploy different ingress based on the cloud provider.

Here is an example of a YAML that would be deployed only if the application is being deployed to GKE:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gke-nginx
  labels:
    app: example
    component: nginx
  annotations:
    kots.io/when: 'repl{{ eq Distribution "gke"}}'
spec:
  selector:
    matchLabels:
      app: example
      component: nginx
  template:
    metadata:
      labels:
        app: example
        component: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          envFrom:
          - configMapRef:
              name: example-configmap
          resources:
            limits:
              memory: '256Mi'
              cpu: '500m'
            requests:
              memory: '32Mi'
              cpu: '100m'

In this case you would have a YAML definition for each cloud provider.

For Preflight Checks and Support Bundles, use the exclude shared property . In this case, we need to test for the opposite, so you could try templating like:

 exclude: '{{repl not (eq Distribution "gke") }}'

The above line would ensure that the collector or analyzer would only run when the Distribution is ā€˜gkeā€™.

For an AWS only collector or analyzer, it would look like this:

exclude: '{{repl not (eq Distribution "aws") }}'

TIP

If you want to see what the actual value of the ā€˜Distributionā€™ Static Context, add a Static Data Collector like this one:

    - data:
        name: static/dist.txt
        data: '{{repl Distribution}}'
1 Like

How would you use it to change a ā€œdefaultā€ value?

apiVersion: kots.io/v1beta1
kind: Config
metadata:
  name: bpaas-config
spec:
  groups:
    - name: security_context_group
      title: Security Context
      items:
        - name: security_context
          type: select_one
          title: Type
          help_text: Would you like to use the default security context or provide your own?
          default: security_context_default
          items:
            - name: security_context_default
              title: Default
            - name: security_context_enforced
              title: Enforced
        - name: security_context_runasgroup
          title: runAsGroup
          when: '{{repl ConfigOptionEquals "security_context" "security_context_enforced"}}'
          type: text
          required: true
        - name: security_context_fsgroup
          title: fsGroup
          when: '{{repl ConfigOptionEquals "security_context" "security_context_enforced"}}'
          type: text
          required: true
        - name: security_context_runasuser
          title: runAsUser
          when: '{{repl ConfigOptionEquals "security_context" "security_context_enforced"}}'
          type: text
          required: true

I would like to have the default be security_context_enforced when on OpenShift, and on security_context_default when on anything else.

I canā€™t seem to get any of the ā€œif/elseā€ options to work with Distribution

have you tried to sprig ternary?

Here is an example of using if/else with the {{repl templating

But since Distribution can not be used with ConfigOptionEquals, yours would need to be something like:

value: '{{repl if (eq Distribution "openshift")}}OpenShiftValue{{repl else}}ElseValue{{repl end}}

In the above I am using eq two compare to values and get true/false as a result