-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
…#4421) * add annotation cni.spidernet.io/network-resource-inject Co-authored-by: Cyclinder Kuo <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Calico with multi underlay NIC | ||
|
||
[**English**](./multi-underlay-nic.md) | **简体中文** | ||
|
||
## 基于 Webhook 自动为 Pod 附加多张 Underlay 网卡 | ||
|
||
本文集群节点网卡: `ens192` 所在子网为 `10.6.0.0/16`,`ens193` 所在子网为 `10.7.0.0/16`,以此创建 SpiderIPPool: | ||
|
||
```shell | ||
$ cat <<EOF | kubectl apply -f - | ||
apiVersion: spiderpool.spidernet.io/v2beta1 | ||
kind: SpiderIPPool | ||
metadata: | ||
name: macvlan-ens192 | ||
spec: | ||
disable: false | ||
gateway: 10.6.0.1 | ||
subnet: 10.6.0.0/16 | ||
ips: | ||
- 10.6.212.100-10.6.212.200 | ||
--- | ||
apiVersion: spiderpool.spidernet.io/v2beta1 | ||
kind: SpiderIPPool | ||
metadata: | ||
name: macvlan-ens193 | ||
spec: | ||
disable: false | ||
gateway: 10.7.0.1 | ||
subnet: 10.7.0.0/16 | ||
ips: | ||
- 10.7.212.100-10.7.212.200 | ||
--- | ||
apiVersion: spiderpool.spidernet.io/v2beta1 | ||
kind: SpiderMultusConfig | ||
metadata: | ||
name: macvlan-ens192 | ||
namespace: spiderpool | ||
annotations: | ||
cni.spidernet.io/network-resource-inject: multi-network | ||
spec: | ||
cniType: macvlan | ||
macvlan: | ||
master: | ||
- ens192 | ||
ippools: | ||
ipv4: | ||
- macvlan-ens192 | ||
vlanID: 0 | ||
--- | ||
apiVersion: spiderpool.spidernet.io/v2beta1 | ||
kind: SpiderMultusConfig | ||
metadata: | ||
name: macvlan-ens193 | ||
namespace: spiderpool | ||
annotations: | ||
cni.spidernet.io/network-resource-inject: multi-network | ||
spec: | ||
cniType: macvlan | ||
macvlan: | ||
master: | ||
- ens193 | ||
ippools: | ||
ipv4: | ||
- macvlan-ens193 | ||
vlanID: 0 | ||
EOF | ||
``` | ||
## 创建测试应用 | ||
1. 为应用也添加相同注解: | ||
```yaml | ||
... | ||
spec: | ||
template: | ||
metadata: | ||
annotations: | ||
cni.spidernet.io/network-resource-inject: multi-network | ||
``` | ||
> 注意:使用 webhook 自动注入网络资源功能时,不能为应用添加其他网络配置注解(如 `k8s.v1.cni.cncf.io/networks` 和 `ipam.spidernet.io ippools`等),否则会影响资源自动注入功能。 | ||
2. 当 Pod 被创建后,可观测到 Pod 被自动注入了网卡 annotation | ||
```yaml | ||
... | ||
spec: | ||
template: | ||
metadata: | ||
annotations: | ||
k8s.v1.cni.cncf.io/networks: |- | ||
[{"name":"macvlan-ens192","namespace":"spiderpool"}, | ||
{"name":"macvlan-ens193","namespace":"spiderpool"}] | ||
.... | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Calico with multi underlay NIC | ||
|
||
**English** | [**简体中文**](./multi-underlay-nic-zh_CN.md) | ||
|
||
## Auto attach multiple underlay NICs to Pod based on Webhook | ||
|
||
The subnet for the interface `ens192` on the cluster nodes here is `10.6.0.0/16`. The subnet for the interface `ens193` on the cluster nodes here is `10.7.0.0/16`. Create SpiderIPPools using these subnets: | ||
|
||
```shell | ||
$ cat <<EOF | kubectl apply -f - | ||
apiVersion: spiderpool.spidernet.io/v2beta1 | ||
kind: SpiderIPPool | ||
metadata: | ||
name: macvlan-ens192 | ||
spec: | ||
disable: false | ||
gateway: 10.6.0.1 | ||
subnet: 10.6.0.0/16 | ||
ips: | ||
- 10.6.212.100-10.6.212.200 | ||
--- | ||
apiVersion: spiderpool.spidernet.io/v2beta1 | ||
kind: SpiderIPPool | ||
metadata: | ||
name: macvlan-ens193 | ||
spec: | ||
disable: false | ||
gateway: 10.7.0.1 | ||
subnet: 10.7.0.0/16 | ||
ips: | ||
- 10.7.212.100-10.7.212.200 | ||
--- | ||
apiVersion: spiderpool.spidernet.io/v2beta1 | ||
kind: SpiderMultusConfig | ||
metadata: | ||
name: macvlan-ens192 | ||
namespace: spiderpool | ||
annotations: | ||
cni.spidernet.io/network-resource-inject: multi-network | ||
spec: | ||
cniType: macvlan | ||
macvlan: | ||
master: | ||
- ens192 | ||
ippools: | ||
ipv4: | ||
- macvlan-ens192 | ||
vlanID: 0 | ||
--- | ||
apiVersion: spiderpool.spidernet.io/v2beta1 | ||
kind: SpiderMultusConfig | ||
metadata: | ||
name: macvlan-ens193 | ||
namespace: spiderpool | ||
annotations: | ||
cni.spidernet.io/network-resource-inject: multi-network | ||
spec: | ||
cniType: macvlan | ||
macvlan: | ||
master: | ||
- ens193 | ||
ippools: | ||
ipv4: | ||
- macvlan-ens193 | ||
vlanID: 0 | ||
EOF | ||
``` | ||
## Create an application | ||
1. Add the same annotation to the application: | ||
```yaml | ||
... | ||
spec: | ||
template: | ||
metadata: | ||
annotations: | ||
cni.spidernet.io/network-resource-inject: multi-network | ||
``` | ||
> Note: When using the webhook automatic injection of network resources feature, do not add other network configuration annotations (such as `k8s.v1.cni.cncf.io/networks` and `ipam.spidernet.io/ippools`) to the application, as it will affect the automatic injection of resources. | ||
2. Once the Pod is created, you can observe that the Pod has been automatically injected with network card annotations. | ||
```yaml | ||
... | ||
spec: | ||
template: | ||
metadata: | ||
annotations: | ||
k8s.v1.cni.cncf.io/networks: |- | ||
[{"name":"macvlan-ens192","namespace":"spiderpool"}, | ||
{"name":"macvlan-ens193","namespace":"spiderpool"}] | ||
.... | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2022 Authors of spidernet-io | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package coordinatormanager | ||
|
||
import ( | ||
"encoding/json" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
var _ = Describe("Coordinator Manager", Label("coordinatorinformer", "informer_test"), Serial, func() { | ||
DescribeTable("should extract CIDRs correctly", | ||
func(testName, cmStr string, expectedPodCIDR, expectedServiceCIDR []string, expectError bool) { | ||
var cm corev1.ConfigMap | ||
err := json.Unmarshal([]byte(cmStr), &cm) | ||
Expect(err).NotTo(HaveOccurred(), "Failed to unmarshal configMap: %v\n", err) | ||
|
||
podCIDR, serviceCIDR, err := ExtractK8sCIDRFromKubeadmConfigMap(&cm) | ||
Check failure on line 20 in pkg/coordinatormanager/coordinator_informer_test.go
|
||
|
||
if expectError { | ||
Expect(err).To(HaveOccurred(), "Expected an error but got none") | ||
} else { | ||
Expect(err).NotTo(HaveOccurred(), "Did not expect an error but got one: %v", err) | ||
} | ||
|
||
Expect(podCIDR).To(Equal(expectedPodCIDR), "Pod CIDR does not match") | ||
Expect(serviceCIDR).To(Equal(expectedServiceCIDR), "Service CIDR does not match") | ||
}, | ||
Entry("ClusterConfiguration", | ||
"ClusterConfiguration", | ||
clusterConfigurationJson, | ||
[]string{"192.168.165.0/24"}, | ||
[]string{"245.100.128.0/18"}, | ||
false, | ||
), | ||
Entry("No ClusterConfiguration", | ||
"No ClusterConfiguration", | ||
noClusterConfigurationJson, | ||
nil, | ||
nil, | ||
true, | ||
), | ||
Entry("No CIDR", | ||
"No CIDR", | ||
noCIDRJson, | ||
nil, | ||
nil, | ||
false, | ||
), | ||
) | ||
|
||
}) |