Skip to content

Commit 6d4cdd2

Browse files
committed
title: Add ipsec support
Description: Sync code, adapt to ACK Signed-off-by: GreatLazyMan <[email protected]>
1 parent 417ab54 commit 6d4cdd2

File tree

22 files changed

+797
-49
lines changed

22 files changed

+797
-49
lines changed

cmd/clusterlink/elector/app/elector.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ func run(ctx context.Context, opts *options.Options) error {
126126
err := elector.EnsureGateWayRole()
127127
if err != nil {
128128
klog.Errorf("set gateway role failure: %v, retry after 10 sec.", err)
129-
time.Sleep(10 * time.Second)
129+
time.Sleep(3 * time.Second)
130130
} else {
131-
klog.V(4).Info("ensure gateway role success, recheck after 60 sec.")
132-
time.Sleep(60 * time.Second)
131+
timeToRecheck := 3 * time.Second
132+
klog.V(4).Infof("ensure gateway role success, recheck after %d sec.", int(timeToRecheck))
133+
time.Sleep(timeToRecheck)
133134
}
134135
}
135136
}

deploy/crds/kosmos.io_clusternodes.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ spec:
4545
properties:
4646
clusterName:
4747
type: string
48+
elasticip:
49+
type: string
4850
interfaceName:
4951
type: string
5052
ip:
@@ -63,11 +65,13 @@ spec:
6365
type: array
6466
type: object
6567
status:
68+
properties:
69+
nodeStatus:
70+
type: string
6671
type: object
6772
required:
6873
- spec
6974
type: object
7075
served: true
7176
storage: true
72-
subresources:
73-
status: {}
77+
subresources: {}

deploy/crds/kosmos.io_clusters.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ spec:
105105
- nodeName
106106
type: object
107107
type: array
108+
nodeElasticIPMap:
109+
additionalProperties:
110+
type: string
111+
description: NodeElasticIPMap presents mapping between nodename
112+
in kubernetes and elasticIP
113+
type: object
108114
useIPPool:
109115
default: false
110116
type: boolean

deploy/crds/kosmos.io_nodeconfigs.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,52 @@ spec:
122122
- gw
123123
type: object
124124
type: array
125+
xfrmpolicies:
126+
items:
127+
properties:
128+
dir:
129+
type: integer
130+
leftip:
131+
type: string
132+
leftnet:
133+
type: string
134+
reqid:
135+
type: integer
136+
rightip:
137+
type: string
138+
rightnet:
139+
type: string
140+
required:
141+
- dir
142+
- leftip
143+
- leftnet
144+
- reqid
145+
- rightip
146+
- rightnet
147+
type: object
148+
type: array
149+
xfrmstates:
150+
items:
151+
properties:
152+
PSK:
153+
type: string
154+
leftip:
155+
type: string
156+
reqid:
157+
type: integer
158+
rightip:
159+
type: string
160+
spi:
161+
format: int32
162+
type: integer
163+
required:
164+
- PSK
165+
- leftip
166+
- reqid
167+
- rightip
168+
- spi
169+
type: object
170+
type: array
125171
type: object
126172
status:
127173
properties:

pkg/apis/kosmos/v1alpha1/cluster_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ type ClusterLinkOptions struct {
9191

9292
// +optional
9393
GlobalCIDRsMap map[string]string `json:"globalCIDRsMap,omitempty"`
94+
95+
// NodeElasticIPMap presents mapping between nodename in kubernetes and elasticIP
96+
// +optional
97+
NodeElasticIPMap map[string]string `json:"nodeElasticIPMap,omitempty"`
9498
}
9599

96100
type ClusterTreeOptions struct {

pkg/apis/kosmos/v1alpha1/clusternode_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
// +genclient
1010
// +genclient:nonNamespaced
11-
// +kubebuilder:subresource:status
1211
// +kubebuilder:resource:scope="Cluster"
1312
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
1413
// +kubebuilder:printcolumn:name="ROLES",type=string,JSONPath=`.spec.roles`
@@ -33,6 +32,8 @@ type ClusterNodeSpec struct {
3332
// +optional
3433
IP string `json:"ip,omitempty"`
3534
// +optional
35+
ElasticIP string `json:"elasticip,omitempty"`
36+
// +optional
3637
IP6 string `json:"ip6,omitempty"`
3738
// +optional
3839
Roles []Role `json:"roles,omitempty"`
@@ -41,6 +42,8 @@ type ClusterNodeSpec struct {
4142
}
4243

4344
type ClusterNodeStatus struct {
45+
// +optional
46+
NodeStatus string `json:"nodeStatus,omitempty"`
4447
}
4548

4649
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

pkg/apis/kosmos/v1alpha1/constants.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ type DeviceType string
2828
const (
2929
VxlanDevice DeviceType = "vxlan"
3030
)
31+
32+
const (
33+
DefaultPSK string = "bfd6224354977084568832b811226b3d6cff6685"
34+
DefaultPSKPreStr = "WelcometoKosmos"
35+
DefaultReqID int = 336
36+
)
37+
38+
type IPSECDirection int
39+
40+
const (
41+
IPSECIn IPSECDirection = 0
42+
IPSECOut IPSECDirection = 1
43+
IPSECFwd IPSECDirection = 2
44+
)

pkg/apis/kosmos/v1alpha1/nodeconfig_types.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ 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"`
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"`
2931
}
3032

3133
type NodeConfigStatus struct {
@@ -101,6 +103,40 @@ func (a *Arp) Compare(v Arp) bool {
101103
a.Dev == v.Dev
102104
}
103105

106+
type XfrmPolicy struct {
107+
LeftIP string `json:"leftip"`
108+
LeftNet string `json:"leftnet"`
109+
RightIP string `json:"rightip"`
110+
RightNet string `json:"rightnet"`
111+
ReqID int `json:"reqid"`
112+
Dir int `json:"dir"`
113+
}
114+
115+
func (a *XfrmPolicy) Compare(v XfrmPolicy) bool {
116+
return a.LeftIP == v.LeftIP &&
117+
a.LeftNet == v.LeftNet &&
118+
a.RightNet == v.RightNet &&
119+
a.RightIP == v.RightIP &&
120+
a.ReqID == v.ReqID &&
121+
a.Dir == v.Dir
122+
}
123+
124+
type XfrmState struct {
125+
LeftIP string `json:"leftip"`
126+
RightIP string `json:"rightip"`
127+
ReqID int `json:"reqid"`
128+
SPI uint32 `json:"spi"`
129+
PSK string `json:"PSK"`
130+
}
131+
132+
func (a *XfrmState) Compare(v XfrmState) bool {
133+
return a.LeftIP == v.LeftIP &&
134+
a.RightIP == v.RightIP &&
135+
a.ReqID == v.ReqID &&
136+
a.PSK == v.PSK &&
137+
a.SPI == v.SPI
138+
}
139+
104140
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
105141

106142
type NodeConfigList struct {

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ func (e *NetworkManager) Diff(oldConfig, newConfig *clusterlinkv1alpha1.NodeConf
112112
createConfig.Routes = createRecord
113113
isSame = false
114114
}
115+
// ipsec:
116+
if flag, deleteRecord, createRecord := compareFunc(oldConfig.XfrmPolicies, newConfig.XfrmPolicies, func(a, b clusterlinkv1alpha1.XfrmPolicy) bool {
117+
return a.Compare(b)
118+
}); !flag {
119+
deleteConfig.XfrmPolicies = deleteRecord
120+
createConfig.XfrmPolicies = createRecord
121+
isSame = false
122+
}
123+
if flag, deleteRecord, createRecord := compareFunc(oldConfig.XfrmStates, newConfig.XfrmStates, func(a, b clusterlinkv1alpha1.XfrmState) bool {
124+
return a.Compare(b)
125+
}); !flag {
126+
deleteConfig.XfrmStates = deleteRecord
127+
createConfig.XfrmStates = createRecord
128+
isSame = false
129+
}
115130
// iptables:
116131
if flag, deleteRecord, createRecord := compareFunc(oldConfig.Iptables, newConfig.Iptables, func(a, b clusterlinkv1alpha1.Iptables) bool {
117132
return a.Compare(b)
@@ -188,6 +203,18 @@ func (e *NetworkManager) WriteSys(configDiff *ConfigDiff) error {
188203
errs = errors.Wrap(err, fmt.Sprint(errs))
189204
}
190205
}
206+
if config.XfrmPolicies != nil {
207+
if err := e.NetworkInterface.DeleteXfrmPolicies(config.XfrmPolicies); err != nil {
208+
klog.Warning(err)
209+
errs = errors.Wrap(err, fmt.Sprint(errs))
210+
}
211+
}
212+
if config.XfrmStates != nil {
213+
if err := e.NetworkInterface.DeleteXfrmStates(config.XfrmStates); err != nil {
214+
klog.Warning(err)
215+
errs = errors.Wrap(err, fmt.Sprint(errs))
216+
}
217+
}
191218
}
192219

193220
if configDiff.createConfig != nil {
@@ -223,6 +250,18 @@ func (e *NetworkManager) WriteSys(configDiff *ConfigDiff) error {
223250
errs = errors.Wrap(err, fmt.Sprint(errs))
224251
}
225252
}
253+
if config.XfrmPolicies != nil {
254+
if err := e.NetworkInterface.AddXfrmPolicies(config.XfrmPolicies); err != nil {
255+
klog.Warning(err)
256+
errs = errors.Wrap(err, fmt.Sprint(errs))
257+
}
258+
}
259+
if config.XfrmStates != nil {
260+
if err := e.NetworkInterface.AddXfrmStates(config.XfrmStates); err != nil {
261+
klog.Warning(err)
262+
errs = errors.Wrap(err, fmt.Sprint(errs))
263+
}
264+
}
226265
}
227266

228267
return errs
@@ -254,11 +293,13 @@ func (e *NetworkManager) UpdateFromChecker() NodeConfigSyncStatus {
254293
}
255294

256295
func printNodeConfig(data *clusterlinkv1alpha1.NodeConfigSpec) {
257-
klog.Infof("device: ", data.Devices)
258-
klog.Infof("Arps: ", data.Arps)
259-
klog.Infof("Fdbs: ", data.Fdbs)
260-
klog.Infof("Iptables: ", data.Iptables)
261-
klog.Infof("Routes: ", data.Routes)
296+
klog.Infof("device: %v", data.Devices)
297+
klog.Infof("Arps: %v", data.Arps)
298+
klog.Infof("Fdbs: %v", data.Fdbs)
299+
klog.Infof("Iptables: %v", data.Iptables)
300+
klog.Infof("Routes: %v", data.Routes)
301+
klog.Infof("XfrmPolicys: %v", data.XfrmPolicies)
302+
klog.Infof("XfrmStates: %v", data.XfrmStates)
262303
}
263304

264305
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,10 +339,6 @@ func (c *Controller) Reconcile(key utils.QueueKey) error {
339339
}
340340

341341
klog.Infof("start reconcile cluster %s", cluster.Name)
342-
if 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-
}
346342
for ipPool := range c.globalExtIPPoolSet {
347343
if ipPool.cluster == cluster.Name {
348344
delete(c.globalExtIPPoolSet, ipPool)
@@ -375,6 +371,10 @@ func (c *Controller) Reconcile(key utils.QueueKey) error {
375371
c.globalExtIPPoolSet[extIPPool] = struct{}{}
376372
}
377373
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)

0 commit comments

Comments
 (0)