Skip to content

Commit ff72d74

Browse files
committed
title: Add ipsec support
Signed-off-by: GreatLazyMan <[email protected]>
1 parent 6d4cdd2 commit ff72d74

File tree

20 files changed

+518
-85
lines changed

20 files changed

+518
-85
lines changed

cluster/images/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ ARG BINARY
44

55
RUN apk add --no-cache ca-certificates
66
RUN apk update && apk upgrade
7-
RUN apk add ip6tables iptables curl tcpdump busybox-extras
7+
RUN apk add ip6tables iptables ipset curl tcpdump busybox-extras
88

99
COPY ${BINARY} /bin/${BINARY}

deploy/crds/kosmos.io_clusters.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ spec:
5656
- ip
5757
- ip6
5858
type: object
59+
clusterpodCIDRs:
60+
items:
61+
type: string
62+
type: array
5963
cni:
6064
default: calico
6165
type: string
@@ -114,6 +118,8 @@ spec:
114118
useIPPool:
115119
default: false
116120
type: boolean
121+
useexternalapiserver:
122+
type: boolean
117123
type: object
118124
clusterTreeOptions:
119125
properties:

deploy/crds/kosmos.io_nodeconfigs.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ spec:
9292
- mac
9393
type: object
9494
type: array
95+
ipsetsavoidmasq:
96+
items:
97+
properties:
98+
cidr:
99+
type: string
100+
name:
101+
type: string
102+
required:
103+
- cidr
104+
- name
105+
type: object
106+
type: array
95107
iptables:
96108
items:
97109
properties:

pkg/apis/kosmos/v1alpha1/cluster_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ type ClusterLinkOptions struct {
9595
// NodeElasticIPMap presents mapping between nodename in kubernetes and elasticIP
9696
// +optional
9797
NodeElasticIPMap map[string]string `json:"nodeElasticIPMap,omitempty"`
98+
// +optional
99+
ClusterPodCIDRs []string `json:"clusterpodCIDRs,omitempty"`
100+
// +optional
101+
UseExternalApiserver bool `json:"useexternalapiserver,omitempty"`
98102
}
99103

100104
type ClusterTreeOptions struct {

pkg/apis/kosmos/v1alpha1/nodeconfig_types.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ type NodeConfig struct {
2121
}
2222

2323
type NodeConfigSpec struct {
24-
Devices []Device `json:"devices,omitempty"`
25-
Routes []Route `json:"routes,omitempty"`
26-
Iptables []Iptables `json:"iptables,omitempty"`
27-
Fdbs []Fdb `json:"fdbs,omitempty"`
28-
Arps []Arp `json:"arps,omitempty"`
29-
XfrmPolicies []XfrmPolicy `json:"xfrmpolicies,omitempty"`
30-
XfrmStates []XfrmState `json:"xfrmstates,omitempty"`
24+
Devices []Device `json:"devices,omitempty"`
25+
Routes []Route `json:"routes,omitempty"`
26+
Iptables []Iptables `json:"iptables,omitempty"`
27+
Fdbs []Fdb `json:"fdbs,omitempty"`
28+
Arps []Arp `json:"arps,omitempty"`
29+
XfrmPolicies []XfrmPolicy `json:"xfrmpolicies,omitempty"`
30+
XfrmStates []XfrmState `json:"xfrmstates,omitempty"`
31+
IPsetsAvoidMasqs []IPset `json:"ipsetsavoidmasq,omitempty"`
3132
}
3233

3334
type NodeConfigStatus struct {
@@ -137,6 +138,16 @@ func (a *XfrmState) Compare(v XfrmState) bool {
137138
a.SPI == v.SPI
138139
}
139140

141+
type IPset struct {
142+
CIDR string `json:"cidr"`
143+
Name string `json:"name"`
144+
}
145+
146+
func (a *IPset) Compare(v IPset) bool {
147+
return a.CIDR == v.CIDR &&
148+
a.Name == v.Name
149+
}
150+
140151
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
141152

142153
type NodeConfigList struct {

pkg/clusterlink/agent-manager/network-manager/network_manager.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ func (e *NetworkManager) Diff(oldConfig, newConfig *clusterlinkv1alpha1.NodeConf
127127
createConfig.XfrmStates = createRecord
128128
isSame = false
129129
}
130+
//ipset
131+
if flag, deleteRecord, createRecord := compareFunc(oldConfig.IPsetsAvoidMasqs, newConfig.IPsetsAvoidMasqs, func(a, b clusterlinkv1alpha1.IPset) bool {
132+
return a.Compare(b)
133+
}); !flag {
134+
deleteConfig.IPsetsAvoidMasqs = deleteRecord
135+
createConfig.IPsetsAvoidMasqs = createRecord
136+
isSame = false
137+
}
130138
// iptables:
131139
if flag, deleteRecord, createRecord := compareFunc(oldConfig.Iptables, newConfig.Iptables, func(a, b clusterlinkv1alpha1.Iptables) bool {
132140
return a.Compare(b)
@@ -215,6 +223,12 @@ func (e *NetworkManager) WriteSys(configDiff *ConfigDiff) error {
215223
errs = errors.Wrap(err, fmt.Sprint(errs))
216224
}
217225
}
226+
if config.IPsetsAvoidMasqs != nil {
227+
if err := e.NetworkInterface.DeleteIPsetsAvoidMasq(config.IPsetsAvoidMasqs); err != nil {
228+
klog.Warning(err)
229+
errs = errors.Wrap(err, fmt.Sprint(errs))
230+
}
231+
}
218232
}
219233

220234
if configDiff.createConfig != nil {
@@ -262,6 +276,12 @@ func (e *NetworkManager) WriteSys(configDiff *ConfigDiff) error {
262276
errs = errors.Wrap(err, fmt.Sprint(errs))
263277
}
264278
}
279+
if config.IPsetsAvoidMasqs != nil {
280+
if err := e.NetworkInterface.AddIPsetsAvoidMasq(config.IPsetsAvoidMasqs); err != nil {
281+
klog.Warning(err)
282+
errs = errors.Wrap(err, fmt.Sprint(errs))
283+
}
284+
}
265285
}
266286

267287
return errs
@@ -300,6 +320,7 @@ func printNodeConfig(data *clusterlinkv1alpha1.NodeConfigSpec) {
300320
klog.Infof("Routes: %v", data.Routes)
301321
klog.Infof("XfrmPolicys: %v", data.XfrmPolicies)
302322
klog.Infof("XfrmStates: %v", data.XfrmStates)
323+
klog.Infof("IPsetsAvoidMasqs: %v", data.IPsetsAvoidMasqs)
303324
}
304325

305326
func (e *NetworkManager) UpdateSync() NodeConfigSyncStatus {

pkg/clusterlink/controllers/calicoippool/calicoippool_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ func (c *Controller) Reconcile(key utils.QueueKey) error {
339339
}
340340

341341
klog.Infof("start reconcile cluster %s", cluster.Name)
342+
if cluster.Name == c.clusterName && cluster.Spec.ClusterLinkOptions.CNI != utils.CNITypeCalico {
343+
klog.Infof("cluster %s cni type is %s skip reconcile", cluster.Name, cluster.Spec.ClusterLinkOptions.CNI)
344+
return nil
345+
}
342346
for ipPool := range c.globalExtIPPoolSet {
343347
if ipPool.cluster == cluster.Name {
344348
delete(c.globalExtIPPoolSet, ipPool)
@@ -371,10 +375,6 @@ func (c *Controller) Reconcile(key utils.QueueKey) error {
371375
c.globalExtIPPoolSet[extIPPool] = struct{}{}
372376
}
373377
klog.Infof("now has %d globalIPPools", len(c.globalExtIPPoolSet))
374-
if cluster.Spec.ClusterLinkOptions.CNI != utils.CNITypeCalico {
375-
klog.Infof("cluster %s cni type is %s skip reconcile", cluster.Name, cluster.Spec.ClusterLinkOptions.CNI)
376-
return nil
377-
}
378378
if c.iPPoolClient == nil {
379379
if cluster.Name == c.clusterName {
380380
ipPoolClient, err := c.createIPPoolClient(cluster)

pkg/clusterlink/controllers/cluster/cluster_controller.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
// KubeFlannelNetworkConfig
4343
const (
4444
FlannelCNI = "flannel"
45+
GlobalRouterCNI = "globalrouter"
4546
KubeFlannelConfigMap = "kube-flannel-cfg"
4647
KubeFlannelNetworkConf = "net-conf.json"
4748
KubeFlannelIPPool = "Network"
@@ -122,16 +123,23 @@ func (c *Controller) Start(ctx context.Context) error {
122123
factory := informers.NewSharedInformerFactory(c.kubeClient, 0)
123124
informer := factory.Core().V1().Pods().Informer()
124125
c.podLister = factory.Core().V1().Pods().Lister()
125-
podFilterFunc := func(pod *corev1.Pod) bool {
126-
//TODO 确认这个写法是否正确
127-
return pod.Labels["component"] == "kube-apiserver"
128-
}
129126

130127
cluster, err := c.clusterLinkClient.KosmosV1alpha1().Clusters().Get(ctx, c.clusterName, metav1.GetOptions{})
131128
if err != nil {
132129
klog.Errorf("can not find local cluster %s, err: %v", c.clusterName, err)
133130
return err
134131
}
132+
var podFilterFunc func(pod *corev1.Pod) bool
133+
if cluster.Spec.ClusterLinkOptions.UseExternalApiserver {
134+
podFilterFunc = func(pod *corev1.Pod) bool {
135+
return pod.Labels["k8s-app"] == "kube-proxy" || pod.Labels["app"] == "clusterlink-controller-manager"
136+
}
137+
} else {
138+
podFilterFunc = func(pod *corev1.Pod) bool {
139+
//TODO 确认这个写法是否正确
140+
return pod.Labels["component"] == "kube-apiserver"
141+
}
142+
}
135143
_, err = informer.AddEventHandler(cache.FilteringResourceEventHandler{
136144
FilterFunc: func(obj interface{}) bool {
137145
pod := obj.(*corev1.Pod)
@@ -157,6 +165,15 @@ func (c *Controller) Start(ctx context.Context) error {
157165
klog.Errorf("cluster %s initCalicoInformer err: %v", err)
158166
return err
159167
}
168+
} else if cluster.Spec.ClusterLinkOptions.CNI == GlobalRouterCNI {
169+
c.setClusterPodCIDRFun = func(cluster *clusterlinkv1alpha1.Cluster) error {
170+
if len(cluster.Spec.ClusterLinkOptions.ClusterPodCIDRs) == 0 {
171+
klog.Errorf("Please define ClusterPodCIDRs for cni %s", GlobalRouterCNI)
172+
return fmt.Errorf("clusterpodcidrs is not defined for cni %s", GlobalRouterCNI)
173+
}
174+
cluster.Status.ClusterLinkStatus.PodCIDRs = cluster.Spec.ClusterLinkOptions.ClusterPodCIDRs
175+
return nil
176+
}
160177
} else {
161178
isEtcd := CheckIsEtcd(cluster)
162179
if !isEtcd {
@@ -188,6 +205,7 @@ func (c *Controller) Reconcile(key utils.QueueKey) error {
188205
klog.Error("invalid key")
189206
return fmt.Errorf("invalid key")
190207
}
208+
klog.Info("cluster controller start reconcile")
191209
namespacedName := types.NamespacedName{
192210
Name: clusterWideKey.Name,
193211
Namespace: clusterWideKey.Namespace,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package handlers
2+
3+
import (
4+
"k8s.io/klog"
5+
6+
"github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1"
7+
"github.com/kosmos.io/kosmos/pkg/clusterlink/controllers/cluster"
8+
"github.com/kosmos.io/kosmos/pkg/clusterlink/network"
9+
)
10+
11+
type CNISupport struct {
12+
Next
13+
}
14+
15+
func (h *CNISupport) Do(c *Context) (err error) {
16+
flannelClusters, otherClusters := c.Filter.GetClusterByCNI([]string{cluster.GlobalRouterCNI, cluster.FlannelCNI})
17+
allClustes := append(flannelClusters, otherClusters...)
18+
for _, flanflannelCluster := range flannelClusters {
19+
var targetIPset []v1alpha1.IPset
20+
for _, otherClusters := range allClustes {
21+
if otherClusters.Name != flanflannelCluster.Name {
22+
for _, cidr := range otherClusters.Status.ClusterLinkStatus.PodCIDRs {
23+
targetIPset = append(targetIPset, v1alpha1.IPset{
24+
Name: network.KosmosIPsetVoidMasq,
25+
CIDR: cidr,
26+
})
27+
}
28+
}
29+
}
30+
klog.Infof("flannel cluster name: %s, ipset: %v", flanflannelCluster.Name, targetIPset)
31+
targetNodes := c.Filter.GetAllNodesByClusterName(flanflannelCluster.Name)
32+
for _, node := range targetNodes {
33+
c.Results[node.Name].IPsetsAvoidMasq = targetIPset
34+
}
35+
}
36+
37+
return nil
38+
}

pkg/clusterlink/network-manager/handlers/nodeconfig.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212

1313
// NodeConfig network configuration of the node
1414
type NodeConfig struct {
15-
Devices []v1alpha1.Device `json:"devices,omitempty"`
16-
Routes []v1alpha1.Route `json:"routes,omitempty"`
17-
Iptables []v1alpha1.Iptables `json:"iptables,omitempty"`
18-
Fdbs []v1alpha1.Fdb `json:"fdbs,omitempty"`
19-
Arps []v1alpha1.Arp `json:"arps,omitempty"`
20-
XfrmPolicies []v1alpha1.XfrmPolicy `json:"xfrmpolicies,omitempty"`
21-
XfrmStates []v1alpha1.XfrmState `json:"xfrmstates,omitempty"`
15+
Devices []v1alpha1.Device `json:"devices,omitempty"`
16+
Routes []v1alpha1.Route `json:"routes,omitempty"`
17+
Iptables []v1alpha1.Iptables `json:"iptables,omitempty"`
18+
Fdbs []v1alpha1.Fdb `json:"fdbs,omitempty"`
19+
Arps []v1alpha1.Arp `json:"arps,omitempty"`
20+
XfrmPolicies []v1alpha1.XfrmPolicy `json:"xfrmpolicies,omitempty"`
21+
XfrmStates []v1alpha1.XfrmState `json:"xfrmstates,omitempty"`
22+
IPsetsAvoidMasq []v1alpha1.IPset `json:"ipsetsavoidmasq,omitempty"`
2223
}
2324

2425
func (c *NodeConfig) ToString() string {
@@ -35,13 +36,14 @@ func (c *NodeConfig) ToJson() ([]byte, error) {
3536

3637
func (c *NodeConfig) ConvertToNodeConfigSpec() v1alpha1.NodeConfigSpec {
3738
return v1alpha1.NodeConfigSpec{
38-
Devices: c.Devices,
39-
Routes: c.Routes,
40-
Iptables: c.Iptables,
41-
Fdbs: c.Fdbs,
42-
Arps: c.Arps,
43-
XfrmStates: c.XfrmStates,
44-
XfrmPolicies: c.XfrmPolicies,
39+
Devices: c.Devices,
40+
Routes: c.Routes,
41+
Iptables: c.Iptables,
42+
Fdbs: c.Fdbs,
43+
Arps: c.Arps,
44+
XfrmStates: c.XfrmStates,
45+
XfrmPolicies: c.XfrmPolicies,
46+
IPsetsAvoidMasqs: c.IPsetsAvoidMasq,
4547
}
4648
}
4749

0 commit comments

Comments
 (0)