Skip to content

Commit

Permalink
allocate eip to specified namespace
Browse files Browse the repository at this point in the history
Signed-off-by: cherryFloris <[email protected]>
  • Loading branch information
cherryFloris committed Aug 25, 2023
1 parent 1cf22d5 commit a334be4
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 270 deletions.
83 changes: 68 additions & 15 deletions api/v1alpha2/eip_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ type EipSpec struct {
Interface string `json:"interface,omitempty"`
Disable bool `json:"disable,omitempty"`
UsingKnownIPs bool `json:"usingKnownIPs,omitempty"`
// priority for automatically assigning addresses
Priority int `json:"priority,omitempty"`
// specify the namespace for the allocation by name
Namespaces []string `json:"namespaces,omitempty"`
// specify the namespace for allocation by selector
NamespaceSelector map[string]string `json:"namespaceSelector,omitempty"`
}

// EipStatus defines the observed state of EIP
Expand Down Expand Up @@ -179,45 +185,92 @@ func (e Eip) Contains(ip net.IP) bool {
cnet.IPToBigInt(cnet.IP{IP: ip}).Cmp(big.NewInt(0).Add(cnet.IPToBigInt(cnet.IP{IP: base}), big.NewInt(size-1))) <= 0
}

func (e Eip) IsDefault() bool {
return e.Annotations[constant.OpenELBEIPAnnotationDefaultPool] == "true"
}

func (e Eip) ValidateCreate() error {
_, _, err := e.GetSize()
if err != nil {
return err
}

eips := EipList{}
err = client.Client.List(context.Background(), &eips)
if err != nil {
if e.Spec.Protocol == constant.OpenELBProtocolLayer2 && e.Spec.Interface == "" {
return fmt.Errorf("field spec.interface should not be empty")
}
return e.validate(true)
}

func (e Eip) validate(overlap bool) error {
eips := &EipList{}
if err := client.Client.List(context.Background(), eips); err != nil {
return err
}
existDefaultEip := false

if overlap {
if err := e.validateOverlap(eips); err != nil {
return err
}
}

return e.validateDefault(eips)

}

func (e Eip) validateDefault(eips *EipList) error {
if eips == nil {
return nil
}

if !validate.HasOpenELBDefaultEipAnnotation(e.Annotations) {
return nil
}

for _, eip := range eips.Items {
if e.IsOverlap(eip) {
return fmt.Errorf("eip address overlap with %s", eip.Name)
if eip.Name == e.Name {
continue
}

if validate.HasOpenELBDefaultEipAnnotation(eip.Annotations) {
existDefaultEip = true
return fmt.Errorf("already exists a default EIP")
}
}

if e.Spec.Protocol == constant.OpenELBProtocolLayer2 {
if e.Spec.Interface == "" {
return fmt.Errorf("field spec.interface should not be empty")
}
return nil
}

func (e Eip) validateOverlap(eips *EipList) error {
if eips == nil {
return nil
}
if validate.HasOpenELBDefaultEipAnnotation(e.Annotations) && existDefaultEip {
return fmt.Errorf("already exists a default EIP")

for _, eip := range eips.Items {
if eip.Name == e.Name {
continue
}

if e.IsOverlap(eip) {
return fmt.Errorf("eip address overlap with %s", eip.Name)
}
}

return nil
}

func (e Eip) ValidateUpdate(old runtime.Object) error {
oldE := old.(*Eip)
if !reflect.DeepEqual(e.Annotations, oldE.Annotations) {
if err := e.validate(false); err != nil {
return err
}
}

if !reflect.DeepEqual(e.Spec, oldE.Spec) {
if e.Spec.Disable == oldE.Spec.Disable {
return fmt.Errorf("only allow modify field disable")
if e.Spec.Address != oldE.Spec.Address {
return fmt.Errorf("the address field is not allowed to be modified")
}
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions api/v1alpha2/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ var _ = Describe("Test eip types", func() {
},
Status: EipStatus{},
}
Expect(e.Contains(net.ParseIP(""))).Should(BeFalse())
Expect(e.Contains(net.ParseIP("192.168.0.100"))).Should(BeTrue())
Expect(e.Contains(net.ParseIP("192.168.0.200"))).Should(BeTrue())
Expect(e.Contains(net.ParseIP("192.168.0.150"))).Should(BeTrue())
Expand Down
14 changes: 13 additions & 1 deletion api/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions config/crd/bases/network.kubesphere.io_bgpconfs.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.12.1
name: bgpconfs.network.kubesphere.io
spec:
group: network.kubesphere.io
Expand Down Expand Up @@ -161,9 +159,3 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
11 changes: 2 additions & 9 deletions config/crd/bases/network.kubesphere.io_bgppeers.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.12.1
name: bgppeers.network.kubesphere.io
spec:
group: network.kubesphere.io
Expand Down Expand Up @@ -266,6 +264,7 @@ spec:
are ANDed.
type: object
type: object
x-kubernetes-map-type: atomic
timers:
properties:
config:
Expand Down Expand Up @@ -425,9 +424,3 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
23 changes: 14 additions & 9 deletions config/crd/bases/network.kubesphere.io_eips.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.12.1
name: eips.network.kubesphere.io
spec:
group: network.kubesphere.io
Expand Down Expand Up @@ -113,6 +111,19 @@ spec:
type: boolean
interface:
type: string
namespaceSelector:
additionalProperties:
type: string
description: specify the namespace for allocation by selector
type: object
namespaces:
description: specify the namespace for the allocation by name
items:
type: string
type: array
priority:
description: priority for automatically assigning addresses
type: integer
protocol:
enum:
- bgp
Expand Down Expand Up @@ -151,9 +162,3 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
8 changes: 8 additions & 0 deletions config/crd/bases/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ rules:
- create
- patch
- update
- apiGroups:
- ""
resources:
- namespaces
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
49 changes: 25 additions & 24 deletions deploy/openelb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.12.1
name: bgpconfs.network.kubesphere.io
spec:
group: network.kubesphere.io
Expand Down Expand Up @@ -164,19 +163,12 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.12.1
name: bgppeers.network.kubesphere.io
spec:
group: network.kubesphere.io
Expand Down Expand Up @@ -437,6 +429,7 @@ spec:
are ANDed.
type: object
type: object
x-kubernetes-map-type: atomic
timers:
properties:
config:
Expand Down Expand Up @@ -596,19 +589,12 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.12.1
name: eips.network.kubesphere.io
spec:
group: network.kubesphere.io
Expand Down Expand Up @@ -716,6 +702,19 @@ spec:
type: boolean
interface:
type: string
namespaceSelector:
additionalProperties:
type: string
description: specify the namespace for allocation by selector
type: object
namespaces:
description: specify the namespace for the allocation by name
items:
type: string
type: array
priority:
description: priority for automatically assigning addresses
type: integer
protocol:
enum:
- bgp
Expand Down Expand Up @@ -754,12 +753,6 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
---
apiVersion: v1
kind: ServiceAccount
Expand Down Expand Up @@ -929,6 +922,14 @@ rules:
- create
- patch
- update
- apiGroups:
- ""
resources:
- namespaces
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
Loading

0 comments on commit a334be4

Please sign in to comment.