Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support wildcard match for IPPool #3262

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 23 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,24 @@ spec:

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

> 对于以下指定使用 SpiderIPPool 规则的优先级,请参考 [IP 候选池规则](./../concepts/ipam-des-zh_CN.md#获取候选池)
该功能有: `使用 Pod Annotation 指定使用 IP 池``使用 Namespace 注解指定池``使用 CNI 配置文件指定池``为 SpiderIPPool 设置集群默认级别`一共4种使用方案。

> 对于这4种指定使用 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 +83,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 +96,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 +119,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 +138,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
37 changes: 22 additions & 15 deletions docs/usage/spider-ippool.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ 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).
This feature owns 4 usage options including: `Use Pod Annotation to Specify IP Pool`, `Use Namespace Annotation to Specify IP Pool`, `Use CNI Configuration File to Specify IP Pool` and `Set Cluster Default Level for SpiderIPPool`.

> For the priority rules when specifying the SpiderIPPool, refer to the [Candidate Pool Acquisition](../concepts/ipam-des.md#candidate-pool-acquisition).
> Additionally, 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 +71,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 +83,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 +96,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 +119,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 +138,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
2 changes: 1 addition & 1 deletion docs/usage/spider-subnet-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ subnet-7 4 10.7.0.0/16 0 10

以下的示例 Yaml 中, 会创建 2 个副本的 Deployment 应用 ,其中:

- `ipam.spidernet.io/subnet`:用于指定 Spiderpool 的子网,Spiderpool 会自动在该子网中随机选择一些 IP 来创建固定 IP 池,与本应用绑定,实现 IP 固定的效果。在本示例中该注解会为 Pod 创建 1 个对应子网的固定 IP 池。
- `ipam.spidernet.io/subnet`:用于指定 Spiderpool 的子网,Spiderpool 会自动在该子网中随机选择一些 IP 来创建固定 IP 池,与本应用绑定,实现 IP 固定的效果。在本示例中该注解会为 Pod 创建 1 个对应子网的固定 IP 池。(注意:不支持通配符的形式。)

- `ipam.spidernet.io/ippool-ip-number`:用于指定创建 IP 池 中 的 IP 数量。该 annotation 的写法支持两种方式:一种是数字的方式指定 IP 池的固定数量,例如 `ipam.spidernet.io/ippool-ip-number:1`;另一种方式是使用加号和数字指定 IP 池的相对数量,例如`ipam.spidernet.io/ippool-ip-number:+1`,即表示 IP 池中的数量会自动实时保持在应用的副本数的基础上多 1 个 IP,以解决应用在弹性扩缩容的时有临时的 IP 可用。

Expand Down
2 changes: 1 addition & 1 deletion docs/usage/spider-subnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ subnet-7 4 10.7.0.0/16 0 10

The following YAML example creates two replicas of a Deployment application:

- `ipam.spidernet.io/subnet`: specifies the Spiderpool subnet. Spiderpool automatically selects IP addresses from this subnet to create a fixed IP pool associated with the application, ensuring fixed IP assignment.
- `ipam.spidernet.io/subnet`: specifies the Spiderpool subnet. Spiderpool automatically selects IP addresses from this subnet to create a fixed IP pool associated with the application, ensuring fixed IP assignment. (Notice: this feature don't support wildcard.)

- `ipam.spidernet.io/ippool-ip-number`: specifies the number of IP addresses in the IP pool. This annotation can be written in two ways: specifying a fixed quantity using a numeric value, such as `ipam.spidernet.io/ippool-ip-number:1`, or specifying a relative quantity using a plus and a number, such as `ipam.spidernet.io/ippool-ip-number:+1`. The latter means that the IP pool will dynamically maintain an additional IP address based on the number of replicas, ensuring temporary IPs are available during elastic scaling.

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
Loading