From 3183c43f190b74531ddbbf1fc8effafac41ef19d Mon Sep 17 00:00:00 2001 From: Icarus9913 Date: Tue, 31 Oct 2023 16:06:40 +0800 Subject: [PATCH] fix multusName usage bug with wrong net-attach-def namespace Signed-off-by: Icarus9913 --- cmd/spiderpool-agent/cmd/config.go | 4 ++ cmd/spiderpool-agent/cmd/daemon.go | 1 + docs/usage/spider-affinity-zh_CN.md | 71 ++++++++++++++++++++++++- docs/usage/spider-affinity.md | 80 +++++++++++++++++++++++++++++ pkg/ipam/allocate.go | 14 ++++- pkg/ipam/config.go | 1 + 6 files changed, 168 insertions(+), 3 deletions(-) diff --git a/cmd/spiderpool-agent/cmd/config.go b/cmd/spiderpool-agent/cmd/config.go index 3e07a0765f..3741e85f5d 100644 --- a/cmd/spiderpool-agent/cmd/config.go +++ b/cmd/spiderpool-agent/cmd/config.go @@ -53,6 +53,8 @@ var envInfo = []envConf{ {"SPIDERPOOL_LOG_LEVEL", logutils.LogInfoLevelStr, true, &agentContext.Cfg.LogLevel, nil, nil}, {"SPIDERPOOL_ENABLED_METRIC", "false", false, nil, &agentContext.Cfg.EnableMetric, nil}, {"SPIDERPOOL_ENABLED_DEBUG_METRIC", "false", false, nil, &agentContext.Cfg.EnableDebugLevelMetric, nil}, + {"SPIDERPOOL_POD_NAMESPACE", "", true, &agentContext.Cfg.AgentPodNamespace, nil, nil}, + {"SPIDERPOOL_POD_NAME", "", true, &agentContext.Cfg.AgentPodName, nil, nil}, {"SPIDERPOOL_HEALTH_PORT", "5710", true, &agentContext.Cfg.HttpPort, nil, nil}, {"SPIDERPOOL_METRIC_HTTP_PORT", "5711", true, &agentContext.Cfg.MetricHttpPort, nil, nil}, {"SPIDERPOOL_GOPS_LISTEN_PORT", "5712", false, &agentContext.Cfg.GopsListenPort, nil, nil}, @@ -78,6 +80,8 @@ type Config struct { LogLevel string EnableMetric bool EnableDebugLevelMetric bool + AgentPodNamespace string + AgentPodName string HttpPort string MetricHttpPort string diff --git a/cmd/spiderpool-agent/cmd/daemon.go b/cmd/spiderpool-agent/cmd/daemon.go index 5fd801568c..fba68af09b 100644 --- a/cmd/spiderpool-agent/cmd/daemon.go +++ b/cmd/spiderpool-agent/cmd/daemon.go @@ -139,6 +139,7 @@ func DaemonMain() { EnableKubevirtStaticIP: agentContext.Cfg.EnableKubevirtStaticIP, OperationRetries: agentContext.Cfg.WaitSubnetPoolMaxRetries, OperationGapDuration: time.Duration(agentContext.Cfg.WaitSubnetPoolTime) * time.Second, + AgentNamespace: agentContext.Cfg.AgentPodNamespace, } if len(agentContext.Cfg.MultusClusterNetwork) != 0 { ipamConfig.MultusClusterNetwork = pointer.String(agentContext.Cfg.MultusClusterNetwork) diff --git a/docs/usage/spider-affinity-zh_CN.md b/docs/usage/spider-affinity-zh_CN.md index 14ab7da879..26eafbc97e 100644 --- a/docs/usage/spider-affinity-zh_CN.md +++ b/docs/usage/spider-affinity-zh_CN.md @@ -10,7 +10,7 @@ SpiderIPPool 资源代表 IP 地址的集合,一个 Subnet 中的不同 IP 地 在 [SpiderIPPool CRD](./../reference/crd-spiderippool.md) 里,我们有定义很多的字段来搭配亲和性使用,如: -- `spec.podAffinity` 字段可控制该池是否可被 Pod 使用 +- `spec.podAffinity` 字段可控制该池是否可被 Pod 使用。 - `spec.namespaceName` 和 `spec.namespaceAffinity` 字段会校验是否与 Pod 的Namespace相匹配,若不匹配则不可使用。(`namespaceName` 优先级高于 `namespaceAffinity`) - `spec.nodeName` 和 `spec.nodeAffinity` 字段会校验是否与 Pod 所在的节点相匹配,若不匹配则不可使用。(`nodeName` 优先级高于 `nodeAffinity`) - `multusName` 字段会判断当前网卡是否与 multus 的 net-attach-def 资源使用的 CNI 配置相匹配,若不匹配则不可使用。 @@ -495,6 +495,75 @@ NAMESPACE NAME READY STATUS REST test-ns2 test-other-ns-56cc9b7d95-hx4b5 0/1 ContainerCreating 0 6m3s node2 ``` +## 网卡配置亲和性 + +当为应用创建多网卡时候,我们可以为**集群级别缺省池**指定 multus 的 net-attach-def 实例亲和性。该方法相比于通过注解 `ipam.spidernet.io/ippools` 显式指定网卡与 IPPool 资源的绑定关系更为简单。 + +首先为 IPPool 资源配置好各类属性,其中: + +- `spec.default` 字段设置为 `true`, 以此减少为应用打上 `ipam.spidernet.io/ippool` 或 `ipam.spidernet.io/ippools` 注解,让体验更为简单。 + +- `spec.multusName` 字段配置该 IPPool 对应的 multus 网卡配置。(若您未指定对应 multus 的 net-attach-def 实例的 namespace,我们会默认视属于 spiderpool 安装时的命名空间) + +```yaml +apiVersion: spiderpool.spidernet.io/v2beta1 +kind: SpiderIPPool +metadata: + name: test-ippool-eth0 +spec: + default: true + subnet: 10.6.0.0/16 + ips: + - 10.6.168.151-10.6.168.160 + multusName: + - default/macvlan-vlan0-eth0 +--- +apiVersion: spiderpool.spidernet.io/v2beta1 +kind: SpiderIPPool +metadata: + name: test-ippool-eth1 +spec: + default: true + subnet: 10.7.0.0/16 + ips: + - 10.7.168.151-10.7.168.160 + multusName: + - kube-system/macvlan-vlan0-eth1 +``` + +创建多网卡的应用。我们只需以下的示例 Yaml 中, 会创建有两张网卡的 Deployment 应用 ,其中: + +- `v1.multus-cni.io/default-network`:为创建的应用选择默认网卡配置信息。(若不指定该注解而直接使用 multus 集群默认网卡配置信息,请在 helm 安装 spiderpool 时通过参数指定默认网卡配置信息 `--set multus.multusCNI.defaultCniCRName=default/macvlan-vlan0-eth0`) + +- `k8s.v1.cni.cncf.io/networks`:为创建的应用选择额外网卡的配置信息。 + +```bash +cat < node2 ``` + +## Multus affinity + +When creating multiple network interfaces for an application, we can specify the affinity of multus net-attach-def instance for the **cluster-level default pool**. This way is simpler compared to explicitly specifying the binding relationship between network interfaces and IPPool resources through the `ipam.spidernet.io/ippools` annotation. + +First, configure various properties for the IPPool resource, including: + +- Set the `spec.default` field to `true` to simplify the experience by reducing the need to annotate the application with `ipam.spidernet.io/ippool` or `ipam.spidernet.io/ippools`. + +- Configure the `spec.multusName` field to specify the multus net-attach-def instance. (If you do not specify the namespace of the corresponding multus net-attach-def instance, we will default to the namespace where Spiderpool is installed.) + +```yaml +apiVersion: spiderpool.spidernet.io/v2beta1 +kind: SpiderIPPool +metadata: + name: test-ippool-eth0 +spec: + default: true + subnet: 10.6.0.0/16 + ips: + - 10.6.168.151-10.6.168.160 + multusName: + - default/macvlan-vlan0-eth0 +--- +apiVersion: spiderpool.spidernet.io/v2beta1 +kind: SpiderIPPool +metadata: + name: test-ippool-eth1 +spec: + default: true + subnet: 10.7.0.0/16 + ips: + - 10.7.168.151-10.7.168.160 + multusName: + - kube-system/macvlan-vlan0-eth1 +``` + +Create an application with multiple network interfaces, you can use the following example YAML: + +- `v1.multus-cni.io/default-network`: Choose the default network configuration for the created application. (If you don't specify this annotation and directly use the clusterNetwork configuration of the multus, please specify the default network configuration during the installation of Spiderpool via Helm using the parameter `--set multus.multusCNI.defaultCniCRName=default/macvlan-vlan0-eth0`). + +- `k8s.v1.cni.cncf.io/networks`: Selects the additional network configuration for the created application. + +```bash +cat <