4

I am trying to use a constant in skaffold, and to access it in skaffold profile:

example export SOME_IP=199.99.99.99 && skaffold run -p dev

skaffold.yaml

...
deploy:
  helm:
    flags:
      global:
      - "--debug" 
    releases:
    - name: ***
      chartPath: ***
      imageStrategy:
        helm:
          explicitRegistry: true
      createNamespace: true
      namespace: "***"
      setValueTemplates:
        SKAFFOLD_SOME_IP: "{{.SOME_IP}}"

and in dev.yaml profile I need somehow to access it,
something like:
{{ .Template.SKAFFOLD_SOME_IP }} and it should be rendered as 199.99.99.99

I tried to use skaffold envTemplate and setValueTemplates fields, but could not get success, and could not find any example on web

Binzari Catalin
  • 172
  • 1
  • 8
  • Hi Binzari Catalin! As I understand, you want to use that variable in the kubernetes helm template? – mozello Mar 24 '22 at 13:12

2 Answers2

1

Basically found a solution which I truly don't like, but it works:

in dev profile: values.dev.yaml I added a placeholder

  _anchors_:
    - &_IPAddr_01 "<IPAddr_01_TAG>" # will be replaced with SOME_IP

The <IPAddr_01_TAG> will be replaced with const SOME_IP which will become 199.99.99.99 at the skaffold run

Now to run skaffold I will do:

export SOME_IP=199.99.99.99
sed -i "s/<IPAddr_01_TAG>/$SOME_IP/g" values/values.dev.yaml
skaffold run -p dev

so after the above sed, in dev profile: values.dev.yaml, we will see the SOME_IP const instead of placeholder

  _anchors_:
    - &_IPAddr_01 "199.99.99.99"
Binzari Catalin
  • 172
  • 1
  • 8
0

To use the SKAFFOLD_SOME_IP variable that you have set in your skaffold.yaml you can write the chart template for Kubernetes Deployment like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}
  labels:
    app: {{ .Chart.Name }}
spec:
  selector:
    matchLabels:
      app: {{ .Chart.Name }}
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: {{ .Values.image }}
        env:
        - name: SKAFFOLD_SOME_IP
          value: "{{ .Values.SKAFFOLD_SOME_IP }}"

This will create an environment variable SKAFFOLD_SOME_IP for Kubernetes pods. And you can access it using 'go', for example, like this:

os.Getenv("SKAFFOLD_SOME_IP")
mozello
  • 1,083
  • 3
  • 8