fix: change custom function ipInCIDR (#2639)

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
liujian 2025-06-26 11:19:38 +08:00 committed by GitHub
parent 9b36d38bc3
commit 9686e047be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 10 additions and 31 deletions

View File

@ -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: []

View File

@ -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" }}

View File

@ -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" }}

View File

@ -16,9 +16,9 @@
```
## ipInCIDR
获取IP范围(cidr)内特定下标的IP地址
获取IP范围(cidr)内的所有ip列表(数组)
```yaml
{{ .cidr_variable | ipInCIDR 1 }}
{{ .cidr_variable | ipInCIDR }}
```
## ipFamily

View File

@ -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.

View File

@ -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",