diff --git a/builtin/core/playbooks/vars/create_cluster_kubernetes.yaml b/builtin/core/playbooks/vars/create_cluster_kubernetes.yaml index 32cfe109..60053a04 100644 --- a/builtin/core/playbooks/vars/create_cluster_kubernetes.yaml +++ b/builtin/core/playbooks/vars/create_cluster_kubernetes.yaml @@ -33,7 +33,7 @@ kubernetes: repository: kubesphere/k8s-dns-node-cache tag: 1.22.20 dns_service_ip: >- - {{ .kubernetes.networking.service_cidr | ipInCIDR 2 }} + {{ index (.kubernetes.networking.service_cidr | ipInCIDR) 2 }} apiserver: port: 6443 certSANs: [] diff --git a/builtin/core/roles/kubernetes/init-kubernetes/templates/kubeadm/kubeadm-init.v1beta2 b/builtin/core/roles/kubernetes/init-kubernetes/templates/kubeadm/kubeadm-init.v1beta2 index 70c3cae6..d23ae337 100644 --- a/builtin/core/roles/kubernetes/init-kubernetes/templates/kubeadm/kubeadm-init.v1beta2 +++ b/builtin/core/roles/kubernetes/init-kubernetes/templates/kubeadm/kubeadm-init.v1beta2 @@ -70,7 +70,7 @@ apiServer: - kubernetes.default.svc - kubernetes.default.svc.{{ .kubernetes.cluster_name }} - kubernetes.default.svc.{{ .kubernetes.cluster_name }}.{{ .kubernetes.networking.dns_domain }} - - {{ .kubernetes.networking.service_cidr | ipInCIDR 0 }} + - {{ index (.kubernetes.networking.service_cidr | ipInCIDR) 0 }} - {{ .kubernetes.control_plane_endpoint.host }} {{- range .groups.k8s_cluster | default list }} - {{ index $.hostvars . "hostname" }} diff --git a/builtin/core/roles/kubernetes/init-kubernetes/templates/kubeadm/kubeadm-init.v1beta3 b/builtin/core/roles/kubernetes/init-kubernetes/templates/kubeadm/kubeadm-init.v1beta3 index 73c144f2..2352dcf3 100644 --- a/builtin/core/roles/kubernetes/init-kubernetes/templates/kubeadm/kubeadm-init.v1beta3 +++ b/builtin/core/roles/kubernetes/init-kubernetes/templates/kubeadm/kubeadm-init.v1beta3 @@ -69,7 +69,7 @@ apiServer: - kubernetes.default.svc - kubernetes.default.svc.{{ .kubernetes.cluster_name }} - kubernetes.default.svc.{{ .kubernetes.cluster_name }}.{{ .kubernetes.networking.dns_domain }} - - {{ .kubernetes.networking.service_cidr | ipInCIDR 0 }} + - {{ index (.kubernetes.networking.service_cidr | ipInCIDR) 0 }} - {{ .kubernetes.control_plane_endpoint.host }} {{- range .groups.k8s_cluster | default list }} - {{ index $.hostvars . "hostname" }} diff --git a/docs/zh/101-syntax.md b/docs/zh/101-syntax.md index adcd0a24..fe91f70e 100644 --- a/docs/zh/101-syntax.md +++ b/docs/zh/101-syntax.md @@ -16,9 +16,9 @@ ``` ## ipInCIDR -获取IP范围(cidr)内特定下标的IP地址 +获取IP范围(cidr)内的所有ip列表(数组) ```yaml -{{ .cidr_variable | ipInCIDR 1 }} +{{ .cidr_variable | ipInCIDR }} ``` ## ipFamily diff --git a/pkg/converter/internal/functions.go b/pkg/converter/internal/functions.go index 18df3ee0..f8554235 100644 --- a/pkg/converter/internal/functions.go +++ b/pkg/converter/internal/functions.go @@ -54,20 +54,15 @@ func fromYAML(v string) (any, error) { return output, err } -// ipInCIDR get the IP of a specific location within the cidr range -func ipInCIDR(index int, cidr string) (string, error) { +// ipInCIDR takes a comma-separated list of CIDR strings, parses each one to extract IPs using parseIP, +// and returns a combined slice of all IPs found. Returns an error only if parseIP fails (not shown here). +func ipInCIDR(cidr string) ([]string, error) { var ips = make([]string, 0) for _, s := range strings.Split(cidr, ",") { ips = append(ips, parseIP(s)...) } - if index < 0 { - index = max(len(ips)+index, 0) - } - index = max(index, 0) - index = min(index, len(ips)-1) - - return ips[index], nil + return ips, nil } // ipFamily returns the IP family (IPv4 or IPv6) of the given IP address or IP cidr. diff --git a/pkg/converter/tmpl/template_test.go b/pkg/converter/tmpl/template_test.go index 440d6821..a7d0ff5c 100644 --- a/pkg/converter/tmpl/template_test.go +++ b/pkg/converter/tmpl/template_test.go @@ -664,28 +664,12 @@ a2: // ======= ipInCIDR ======= { name: "ipInCIDR true-1", - input: "{{ ipInCIDR 0 .foo }}", + input: "{{ index (.foo | ipInCIDR) 0 }}", variable: map[string]any{ "foo": "10.233.0.0/18", }, excepted: "10.233.0.1", }, - { - name: "ipInCIDR true-2", - input: "{{ .foo | ipInCIDR 0 }}", - variable: map[string]any{ - "foo": "10.233.0.0/18", - }, - excepted: "10.233.0.1", - }, - { - name: "ipInCIDR true-3", - input: "{{ ipInCIDR -1 .foo }}", - variable: map[string]any{ - "foo": "10.233.0.0/18", - }, - excepted: "10.233.63.254", - }, // ======= ipFamily ======= { name: "ipFamily for ip address",