【问题标题】:How to compare a value to a string with go templating如何使用 go 模板将值与字符串进行比较
【发布时间】:2020-03-30 20:54:01
【问题描述】:

我想遍历一个值文件以在该名称空间中/为该名称空间创建一个名称空间和一个网络策略,默认情况除外。我只想创建策略而不是默认命名空间,因为它默认存在。

值文件:

namespaces:
  - name: default
  - name: test1
  - name: test2

模板文件:

# Loop through the namespace names and create the namespaces
{{- range $namespaces := .Values.namespaces }}
{{- if ne "default" }}
apiVersion: v1
kind: Namespace
metadata:
  name: {{ $namespaces.name }}
---
{{- end }}
{{- end }}

# Loop through the namespace names and create a network policy for those namespace
{{- range $namespaces := .Values.namespaces }}                                                                                                                                                             
---                                                                                                                         
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: {{ $namespaces.name }}-networkpolicy
  namespace: {{ $namespaces.name }}
spec:
  podSelector: {}
  ingress:
    - from:
      - namespaceSelector:
          matchLabels:
            name: {{ $namespaces.name }}
---                                                                                                                         
{{- end }} 

我得到的错误是:

Error: UPGRADE FAILED: template: namespaces/templates/namespaces.yaml:3:7: executing "namespaces/templates/namespaces.yaml" at <ne>: wrong number of args for ne: want 2 got 1

这可能很简单,但没有看到。希望有人能帮忙。

【问题讨论】:

    标签: kubernetes-helm go-templates helmfile


    【解决方案1】:

    这对我有用:

    # Loop through the namespace names and create the namespaces
    {{- range $namespaces := .Values.namespaces }}
    {{- if ne $namespaces.name "default" }}
    apiVersion: v1
    kind: Namespace
    metadata:
      name: {{ $namespaces.name }}
    ---
    {{- end }}
    {{- end }}
    
    # Loop through the namespace names and create a network policy for those namespace
    {{- range $namespaces := .Values.namespaces }}                                                                                                                                                             
    ---                                                                                                                         
    kind: NetworkPolicy
    apiVersion: networking.k8s.io/v1
    metadata:
      name: {{ $namespaces.name }}-networkpolicy
      namespace: {{ $namespaces.name }}
    spec:
      podSelector: {}
      ingress:
        - from:
          - namespaceSelector:
              matchLabels:
                name: {{ $namespaces.name }}
    ---                                                                                                                         
    {{- end }} 
    

    【讨论】:

      猜你喜欢
      • 2013-12-12
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      相关资源
      最近更新 更多