Skip to content

Commit

Permalink
add wildcard to match the IPPools
Browse files Browse the repository at this point in the history
Signed-off-by: Icarus9913 <icaruswu66@qq.com>
  • Loading branch information
Icarus9913 committed Apr 3, 2024
1 parent 3ae7bf0 commit 7731102
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 62 deletions.
12 changes: 10 additions & 2 deletions cmd/spiderpool-agent/cmd/crd_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
controllerruntimelog "sigs.k8s.io/controller-runtime/pkg/log"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

"github.com/spidernet-io/spiderpool/pkg/constant"
spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1"
)

Expand Down Expand Up @@ -46,14 +47,21 @@ func newCRDManager() (ctrl.Manager, error) {
return nil, err
}

if err := mgr.GetFieldIndexer().IndexField(agentContext.InnerCtx, &spiderpoolv2beta1.SpiderIPPool{}, "spec.default", func(raw client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(agentContext.InnerCtx, &spiderpoolv2beta1.SpiderIPPool{}, constant.SpecDefaultField, func(raw client.Object) []string {
ipPool := raw.(*spiderpoolv2beta1.SpiderIPPool)
return []string{strconv.FormatBool(*ipPool.Spec.Default)}
}); err != nil {
return nil, err
}

if err := mgr.GetFieldIndexer().IndexField(agentContext.InnerCtx, &spiderpoolv2beta1.SpiderReservedIP{}, "spec.ipVersion", func(raw client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(agentContext.InnerCtx, &spiderpoolv2beta1.SpiderIPPool{}, constant.SpecIPVersionField, func(raw client.Object) []string {
ipPool := raw.(*spiderpoolv2beta1.SpiderIPPool)
return []string{strconv.FormatInt(*ipPool.Spec.IPVersion, 10)}
}); err != nil {
return nil, err
}

if err := mgr.GetFieldIndexer().IndexField(agentContext.InnerCtx, &spiderpoolv2beta1.SpiderReservedIP{}, constant.SpecIPVersionField, func(raw client.Object) []string {
reservedIP := raw.(*spiderpoolv2beta1.SpiderReservedIP)
return []string{strconv.FormatInt(*reservedIP.Spec.IPVersion, 10)}
}); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/spiderpool-controller/cmd/crd_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func newCRDManager() (ctrl.Manager, error) {
return nil, err
}

if err := mgr.GetFieldIndexer().IndexField(controllerContext.InnerCtx, &spiderpoolv2beta1.SpiderIPPool{}, "spec.default", func(raw client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(controllerContext.InnerCtx, &spiderpoolv2beta1.SpiderIPPool{}, constant.SpecDefaultField, func(raw client.Object) []string {
ipPool := raw.(*spiderpoolv2beta1.SpiderIPPool)
return []string{strconv.FormatBool(*ipPool.Spec.Default)}
}); err != nil {
return nil, err
}

if err := mgr.GetFieldIndexer().IndexField(controllerContext.InnerCtx, &spiderpoolv2beta1.SpiderReservedIP{}, "spec.ipVersion", func(raw client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(controllerContext.InnerCtx, &spiderpoolv2beta1.SpiderReservedIP{}, constant.SpecIPVersionField, func(raw client.Object) []string {
reservedIP := raw.(*spiderpoolv2beta1.SpiderReservedIP)
return []string{strconv.FormatInt(*reservedIP.Spec.IPVersion, 10)}
}); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/coordinator.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ spec:

## Automatically get the CIDR of a clustered Service

Kubernetes 1.29 starts to support configuring the CIDR of a clustered Service as a ServiceCIDR resource, for more information refer to [KEP 1880](https://github.com/kubernetes/enhancements/blob/master/keps/ sig-network/1880-multiple-service-cidrs/README.md). If your cluster supports ServiceCIDR, the Spiderpool-controller component automatically listens for changes to the ServiceCIDR resource and automatically updates the Service subnet information it reads into the Status of the Spidercoordinator.
Kubernetes 1.29 starts to support configuring the CIDR of a clustered Service as a ServiceCIDR resource, for more information refer to [KEP 1880](https://github.com/kubernetes/enhancements/blob/master/keps/sig-network/1880-multiple-service-cidrs/README.md). If your cluster supports ServiceCIDR, the Spiderpool-controller component automatically listens for changes to the ServiceCIDR resource and automatically updates the Service subnet information it reads into the Status of the Spidercoordinator.

```shell
~# kubectl get servicecidr kubernetes -o yaml
Expand Down
37 changes: 21 additions & 16 deletions docs/usage/spider-ippool-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,22 @@ spec:

### 指定 IPPool 为应用分配 IP 地址

> 对于以下指定使用 SpiderIPPool 规则的优先级,请参考 [IP 候选池规则](./../concepts/ipam-des-zh_CN.md#获取候选池)
> 对于以下指定使用 SpiderIPPool 规则的优先级,请参考 [IP 候选池规则](./../concepts/ipam-des-zh_CN.md#获取候选池)
> 以下指定 IP 池的方式(Pod Annotation, Namespace 注解, CNI 配置文件),还可使用通配符 `*`, `?``[]` 来匹配期望的 IP 池。如: ipam.spidernet.io/ippool: '{"ipv4": ["demo-v4-ippool1", "backup-ipv4*"]}'。
>
> 1. '*': 匹配零个或多个字符。例如,"ab" 可以匹配 "ab"、"abc"、"abcd"等等。
> 2. '?': 匹配一个单独的字符。例如,"a?c" 可以匹配 "abc"、"adc"、"axc"等等。
> 3. '[]': 匹配指定范围内的一个字符。您可以在方括号内指定字符的选择,或者使用连字符指定字符范围。例如,"[abc]" 可以匹配 "a"、"b"、"c"中的任意一个字符。
#### 使用 Pod Annotation 指定使用 IP 池

我们可借助注解 `ipam.spidernet.io/ippool``ipam.spidernet.io/ippools` 标记在 Pod 的 Annotation上来指定 Pod 使用哪些 IP 池, 注解 `ipam.spidernet.io/ippools` 多用于多网卡指定。此外我们可以指定多个 IP 池以供备选,当某个池的 IP 被用完后,可继续从你指定的其他池中分配地址。
可借助注解 `ipam.spidernet.io/ippool``ipam.spidernet.io/ippools` 标记在 Pod 的 Annotation上来指定 Pod 使用哪些 IP 池, 注解 `ipam.spidernet.io/ippools` 多用于多网卡指定。此外还可以指定多个 IP 池以供备选,当某个池的 IP 被用完后,可继续从你指定的其他池中分配地址。

```yaml
ipam.spidernet.io/ippool: |-
{
"ipv4": ["demo-v4-ippool1", "backup-ipv4-ippool"],
"ipv6": ["demo-v6-ippool1", "backup-ipv6-ippool"]
"ipv4": ["demo-v4-ippool1", "backup-ipv4-ippool", "wildcard-v4?"],
"ipv6": ["demo-v6-ippool1", "backup-ipv6-ippool", "wildcard-v6*"]
}
```

Expand All @@ -76,11 +81,11 @@ ipam.spidernet.io/ippool: |-
```yaml
ipam.spidernet.io/ippools: |-
[{
"ipv4": ["demo-v4-ippool1"],
"ipv6": ["demo-v6-ippool1"],
"ipv4": ["demo-v4-ippool1", "wildcard-v4-ippool[123]"],
"ipv6": ["demo-v6-ippool1", "wildcard-v6-ippool[123]"]
},{
"ipv4": ["demo-v4-ippool2"],
"ipv6": ["demo-v6-ippool2"],
"ipv4": ["demo-v4-ippool2", "wildcard-v4-ippool[456]"],
"ipv6": ["demo-v6-ippool2", "wildcard-v6-ippool[456]"],
"cleangateway": true
}]
```
Expand All @@ -89,13 +94,13 @@ ipam.spidernet.io/ippools: |-
ipam.spidernet.io/ippools: |-
[{
"interface": "eth0",
"ipv4": ["demo-v4-ippool1"],
"ipv6": ["demo-v6-ippool1"],
"ipv4": ["demo-v4-ippool1", "wildcard-v4-ippool[123]"],
"ipv6": ["demo-v6-ippool1", "wildcard-v6-ippool[123]"],
"cleangateway": true
},{
"interface": "net1",
"ipv4": ["demo-v4-ippool2"],
"ipv6": ["demo-v6-ippool2"],
"ipv4": ["demo-v4-ippool2", "wildcard-v4-ippool[456]"],
"ipv6": ["demo-v6-ippool2", "wildcard-v6-ippool[456]"],
"cleangateway": false
}]
```
Expand All @@ -112,8 +117,8 @@ apiVersion: v1
kind: Namespace
metadata:
annotations:
ipam.spidernet.io/default-ipv4-ippool: '["ns-v4-ippool1","ns-v4-ippool2"]'
ipam.spidernet.io/default-ipv6-ippool: '["ns-v6-ippool1","ns-v6-ippool2"]'
ipam.spidernet.io/default-ipv4-ippool: '["ns-v4-ippool1", "ns-v4-ippool2", "wildcard-v4*"]'
ipam.spidernet.io/default-ipv6-ippool: '["ns-v6-ippool1", "ns-v6-ippool2", "wildcard-v6?"]'
name: kube-system
...
```
Expand All @@ -131,8 +136,8 @@ metadata:
"master": "eth0",
"ipam": {
"type": "spiderpool",
"default_ipv4_ippool":["default-v4-ippool","backup-ipv4-ippool"],
"default_ipv6_ippool":["default-v6-ippool","backup-ipv6-ippool"]
"default_ipv4_ippool":["default-v4-ippool", "backup-ipv4-ippool", "wildcard-v4-ippool[123]"],
"default_ipv6_ippool":["default-v6-ippool", "backup-ipv6-ippool", "wildcard-v6-ippool[456]"]
}
}
```
Expand Down
35 changes: 20 additions & 15 deletions docs/usage/spider-ippool.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ spec:

### Specify IPPool to Allocate IP Addresses to Applications

> For the priority rules when specifying the SpiderIPPool, refer to the [Candidate Pool Acquisition](../concepts/ipam-des.md#candidate-pool-acquisition).
> For the priority rules when specifying the SpiderIPPool, refer to the [Candidate Pool Acquisition](../concepts/ipam-des.md#candidate-pool-acquisition).
> With the following ways of specifying IPPools(Pod Annotation, Namespace Annotation, CNI configuration file), you can also use wildcards '*', '?' and '[]' to match the desired IPPools. For example: ipam.spidernet.io/ippool: '{"ipv4": ["demo-v4-ippool1", "backup-ipv4*"]}'
>
> 1. '*': Matches zero or more characters. For example, "ab" can match "ab", "abc", "abcd", and so on.
> 2. '?': Matches a single character. For example, "a?c" can match "abc", "adc", "axc", and so on.
> 3. '[]': Matches a specified range of characters. You can specify the choices of characters inside the brackets, or use a hyphen to specify a character range. For example, "[abc]" can match any one of the characters "a", "b", or "c".
#### Use Pod Annotation to Specify IP Pool

Expand All @@ -64,8 +69,8 @@ You can use annotations like `ipam.spidernet.io/ippool` or `ipam.spidernet.io/ip
```yaml
ipam.spidernet.io/ippool: |-
{
"ipv4": ["demo-v4-ippool1", "backup-ipv4-ippool"],
"ipv6": ["demo-v6-ippool1", "backup-ipv6-ippool"]
"ipv4": ["demo-v4-ippool1", "backup-ipv4-ippool", "wildcard-v4?"],
"ipv6": ["demo-v6-ippool1", "backup-ipv6-ippool", "wildcard-v6*"]
}
```

Expand All @@ -76,11 +81,11 @@ When using the annotation `ipam.spidernet.io/ippools` for specifying multiple ne
```yaml
ipam.spidernet.io/ippools: |-
[{
"ipv4": ["demo-v4-ippool1"],
"ipv6": ["demo-v6-ippool1"],
"ipv4": ["demo-v4-ippool1", "wildcard-v4-ippool[123]"],
"ipv6": ["demo-v6-ippool1", "wildcard-v6-ippool[123]"]
},{
"ipv4": ["demo-v4-ippool2"],
"ipv6": ["demo-v6-ippool2"],
"ipv4": ["demo-v4-ippool2", "wildcard-v4-ippool[456]"],
"ipv6": ["demo-v6-ippool2", "wildcard-v6-ippool[456]"],
"cleangateway": true
}]
```
Expand All @@ -89,13 +94,13 @@ ipam.spidernet.io/ippools: |-
ipam.spidernet.io/ippools: |-
[{
"interface": "eth0",
"ipv4": ["demo-v4-ippool1"],
"ipv6": ["demo-v6-ippool1"],
"ipv4": ["demo-v4-ippool1", "wildcard-v4-ippool[123]"],
"ipv6": ["demo-v6-ippool1", "wildcard-v6-ippool[123]"],
"cleangateway": true
},{
"interface": "net1",
"ipv4": ["demo-v4-ippool2"],
"ipv6": ["demo-v6-ippool2"],
"ipv4": ["demo-v4-ippool2", "wildcard-v4-ippool[456]"],
"ipv6": ["demo-v6-ippool2", "wildcard-v6-ippool[456]"],
"cleangateway": false
}]
```
Expand All @@ -112,8 +117,8 @@ apiVersion: v1
kind: Namespace
metadata:
annotations:
ipam.spidernet.io/default-ipv4-ippool: '["ns-v4-ippool1","ns-v4-ippool2"]'
ipam.spidernet.io/default-ipv6-ippool: '["ns-v6-ippool1","ns-v6-ippool2"]'
ipam.spidernet.io/default-ipv4-ippool: '["ns-v4-ippool1", "ns-v4-ippool2", "wildcard-v4*"]'
ipam.spidernet.io/default-ipv6-ippool: '["ns-v6-ippool1", "ns-v6-ippool2", "wildcard-v6?"]'
name: kube-system
...
```
Expand All @@ -131,8 +136,8 @@ You can specify the default IPv4 and IPv6 pools for an application in the CNI co
"master": "eth0",
"ipam": {
"type": "spiderpool",
"default_ipv4_ippool":["default-v4-ippool","backup-ipv4-ippool"],
"default_ipv6_ippool":["default-v6-ippool","backup-ipv6-ippool"]
"default_ipv4_ippool":["default-v4-ippool", "backup-ipv4-ippool", "wildcard-v4-ippool[123]"],
"default_ipv6_ippool":["default-v6-ippool", "backup-ipv6-ippool", "wildcard-v6-ippool[456]"]
}
}
```
Expand Down
11 changes: 11 additions & 0 deletions pkg/constant/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,14 @@ const (
)

const WebhookMutateRoute = "/webhook-health-check"

// CRD field
const (
SpecIPVersionField = "spec.ipVersion"
SpecDefaultField = "spec.default"
)

const (
Str4 = "4"
Str6 = "6"
)
Loading

0 comments on commit 7731102

Please sign in to comment.