0

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

值文件:

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

这可能很简单,但没有看到它。希望有人可以提供帮助。

4

1 回答 1

0

这对我有用:

# 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 }} 
于 2020-03-30T21:04:39.070 回答