掌舵范围不超出全球范围 [英] Helm range without leaving global scope

查看:51
本文介绍了掌舵范围不超出全球范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历实例列表,并为每个实例创建1个有状态集.但是,在范围之内,我便将自己限制在该循环的范围内.我需要在状态集中访问一些全局值.

I need to loop through a list of instances and create 1 stateful set for every instance. However, inside range I then limit myself to the scope of that loop. I need to access some global values in my statefulset.

我已经通过将我需要的所有全局对象放在一个env变量中来解决了问题,但是...这似乎很棘手.

I've solved it by just putting all global objects I need in an env variable but... this very seems hacky.

在仍然可以引用全局对象的同时循环遍历范围的正确方法是什么?

What is the correct way to loop through ranges while still being able to reference global objects?

我的循环示例

{{- $values := .Values -}}
{{- $release := .Release -}}

{{- range .Values.nodes }}

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $release.Name }} <-- Global Scope
  labels:
    .
    .
    .    
        env:
          - name: IP_ADDRESS
            value: {{ .ip_address }} <-- From range scope
    .
    .
    .
{{- end }}

值示例

# Global
image:
  repository: ..ecr.....

# Instances
nodes:

  - node1:
    name: node-1
    iP: 1.1.1.1
  - node2:
    name: node-2
    iP: 1.1.1.1

推荐答案

在输入循环块时,使用.会丢失全局上下文.您可以使用 $.来访问全局上下文.

When entering a loop block you lose your global context when using .. You can access the global context by using $. instead.

头盔文档-

有一个始终是全局变量-$-此变量将始终指向根上下文.当您在一个范围中循环并且需要知道图表的发行名称时,这可能非常有用.

there is one variable that is always global - $ - this variable will always point to the root context. This can be very useful when you are looping in a range and need to know the chart's release name.

在您的示例中,使用它的方式类似于:

In your example, using this would look something like:

{{- range .Values.nodes }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $.Release.Name }}
  labels:
    .
    .
    .    
        env:
          - name: IP_ADDRESS
            value: {{ .ip_address }}
    .
    .
    .
{{- end }}

这篇关于掌舵范围不超出全球范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆