kubernetes-helm - 如何使用 go 模板将值与字符串进行比较
问题描述
我想遍历一个值文件以在该命名空间中/为该命名空间创建一个命名空间和一个网络策略,默认情况除外。我只想创建策略而不是默认命名空间,因为它默认存在。
值文件:
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
这可能很简单,但没有看到它。希望有人可以提供帮助。
解决方案
这对我有用:
# 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 }}
推荐阅读
- gradle - 您如何在 gradle 中并行运行 ScalaTest 测试套件?
- java - 如何使用迭代器访问类的这些元素?
- google-cloud-dataflow - 在语句中使用聚合时 Beam SQL 不起作用:“无法计划执行”
- python - setuptools:load_entry_point 上的 ImportError
- wordpress - 不理解这个 robots.txt
- ibm-mq - 如何在没有计时器的情况下重新连接到 IBM MQ
- java - 使用eclipse构建rhapsody API兼容代码
- parsing - ANTLR4 语法在输入时没有可行的替代方案
- symfony - symfony easyadmin 自定义表单生成器
- php - 我只想在我写的时候显示选项标签:select2