Skip to content

Commit

Permalink
modify subnet and ip crds
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Dec 18, 2020
1 parent 8aa5d0a commit f1fe2b2
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 14 deletions.
33 changes: 25 additions & 8 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,12 @@ spec:
served: true
storage: true
additionalPrinterColumns:
- name: IP
- name: V4IP
type: string
jsonPath: .spec.ipAddress
jsonPath: .spec.v4IpAddress
- name: V6IP
type: string
jsonPath: .spec.v6IpAddress
- name: Mac
type: string
jsonPath: .spec.macAddress
Expand Down Expand Up @@ -270,6 +273,10 @@ spec:
type: string
ipAddress:
type: string
v4IpAddress:
type: string
v6IpAddress:
type: string
attachIps:
type: array
items:
Expand Down Expand Up @@ -327,22 +334,32 @@ spec:
- name: GatewayType
type: string
jsonPath: .spec.gatewayType
- name: Used
- name: V4Used
type: number
jsonPath: .status.v4usingIPs
- name: V4Available
type: number
jsonPath: .status.usingIPs
- name: Available
jsonPath: .status.v4availableIPs
- name: V6Used
type: number
jsonPath: .status.availableIPs
jsonPath: .status.v6usingIPs
- name: V6Available
type: number
jsonPath: .status.v6availableIPs
schema:
openAPIV3Schema:
type: object
properties:
status:
type: object
properties:
availableIPs:
v4availableIPs:
type: number
v4usingIPs:
type: number
v6availableIPs:
type: number
usingIPs:
v6usingIPs:
type: number
activateGateway:
type: string
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/kubeovn/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type IPSpec struct {
AttachSubnets []string `json:"attachSubnets"`
NodeName string `json:"nodeName"`
IPAddress string `json:"ipAddress"`
V4IPAddress string `json:"v4IpAddress"`
V6IPAddress string `json:"v6IpAddress"`
AttachIPs []string `json:"attachIps"`
MacAddress string `json:"macAddress"`
AttachMacs []string `json:"attachMacs"`
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ func (c *Controller) Run(stopCh <-chan struct{}) {
}

c.registerSubnetMetrics()
if err := c.initSyncCrdIPs(); err != nil {
klog.Errorf("failed to sync crd ips %v", err)
}
if err := c.initSyncCrdSubnets(); err != nil {
klog.Errorf("failed to sync crd subnets %v", err)
}

// start workers to do all the network operations
c.startWorkers(stopCh)
Expand Down
51 changes: 51 additions & 0 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

kubeovnv1 "github.com/alauda/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/alauda/kube-ovn/pkg/util"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
Expand Down Expand Up @@ -308,3 +310,52 @@ func (c *Controller) initDefaultVlan() error {
_, err = c.config.KubeOvnClient.KubeovnV1().Vlans().Create(context.Background(), &defaultVlan, v1.CreateOptions{})
return err
}

func (c *Controller) initSyncCrdIPs() error {
ips, err := c.config.KubeOvnClient.KubeovnV1().IPs().List(context.Background(), metav1.ListOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}

for _, ipCr := range ips.Items {
ip := ipCr
v4IP, v6IP := util.SplitStringIP(ip.Spec.IPAddress)
if ip.Spec.V4IPAddress == v4IP && ip.Spec.V6IPAddress == v6IP {
continue
}
ip.Spec.V4IPAddress = v4IP
ip.Spec.V6IPAddress = v6IP

_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), &ip, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("failed to sync crd ip %s, %v", ip.Spec.IPAddress, err)
return err
}
}
return nil
}

func (c *Controller) initSyncCrdSubnets() error {
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
for _, subnet := range subnets {
if util.CheckProtocol(subnet.Spec.CIDRBlock) == kubeovnv1.ProtocolDual {
err = calcDualSubnetStatusIP(subnet, c)
} else {
err = calcSubnetStatusIP(subnet, c)
}
if err != nil {
klog.Errorf("failed to calculate subnet %s used ip, %v", subnet.Name, err)
return err
}
}
return nil
}
5 changes: 5 additions & 0 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ func getNodeInternalIP(node *v1.Node) string {
}

func (c *Controller) createOrUpdateCrdIPs(key, ip, mac string) error {
v4IP, v6IP := util.SplitStringIP(ip)
ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), fmt.Sprintf("node-%s", key), metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
Expand All @@ -364,6 +365,8 @@ func (c *Controller) createOrUpdateCrdIPs(key, ip, mac string) error {
Subnet: c.config.NodeSwitch,
NodeName: key,
IPAddress: ip,
V4IPAddress: v4IP,
V6IPAddress: v6IP,
MacAddress: mac,
AttachIPs: []string{},
AttachMacs: []string{},
Expand Down Expand Up @@ -393,6 +396,8 @@ func (c *Controller) createOrUpdateCrdIPs(key, ip, mac string) error {
ipCr.Spec.Subnet = c.config.NodeSwitch
ipCr.Spec.NodeName = key
ipCr.Spec.IPAddress = ip
ipCr.Spec.V4IPAddress = v4IP
ipCr.Spec.V6IPAddress = v6IP
ipCr.Spec.MacAddress = mac
ipCr.Spec.ContainerID = ""
_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), ipCr, metav1.UpdateOptions{})
Expand Down
3 changes: 3 additions & 0 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
}

func (csh cniServerHandler) createOrUpdateIPCr(podRequest request.CniRequest, subnet, ip, macAddr string) error {
v4IP, v6IP := util.SplitStringIP(ip)
ipCr, err := csh.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), fmt.Sprintf("%s.%s", podRequest.PodName, podRequest.PodNamespace), metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
Expand All @@ -136,6 +137,8 @@ func (csh cniServerHandler) createOrUpdateIPCr(podRequest request.CniRequest, su
Subnet: subnet,
NodeName: csh.Config.NodeName,
IPAddress: ip,
V4IPAddress: v4IP,
V6IPAddress: v6IP,
MacAddress: macAddr,
ContainerID: podRequest.ContainerID,
AttachIPs: []string{},
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,17 @@ func GetIpWithoutMask(ipStr string) string {
}
return strings.Join(ips, ",")
}

func SplitStringIP(ipStr string) (string, string) {
var v4IP, v6IP string
if CheckProtocol(ipStr) == kubeovnv1.ProtocolDual {
v4IP = strings.Split(ipStr, ",")[0]
v6IP = strings.Split(ipStr, ",")[1]
} else if CheckProtocol(ipStr) == kubeovnv1.ProtocolIPv4 {
v4IP = ipStr
} else if CheckProtocol(ipStr) == kubeovnv1.ProtocolIPv6 {
v6IP = ipStr
}

return v4IP, v6IP
}
2 changes: 1 addition & 1 deletion test/e2e/ip/static_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var _ = Describe("[IP Allocation]", func() {
time.Sleep(1 * time.Second)
ip, err := f.OvnClientSet.KubeovnV1().IPs().Get(context.Background(), fmt.Sprintf("%s.%s", name, namespace), metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(ip.Spec.IPAddress).To(Equal("12.10.0.10"))
Expect(ip.Spec.V4IPAddress).To(Equal("12.10.0.10"))
Expect(ip.Spec.MacAddress).To(Equal("00:00:00:53:6B:B6"))
})

Expand Down
10 changes: 5 additions & 5 deletions test/e2e/subnet/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ var _ = Describe("[Subnet]", func() {

By("validate status")
Expect(subnet.Status.ActivateGateway).To(BeEmpty())
Expect(subnet.Status.AvailableIPs).To(Equal(float64(65533)))
Expect(subnet.Status.UsingIPs).To(BeZero())
Expect(subnet.Status.V4AvailableIPs).To(Equal(float64(65533)))
Expect(subnet.Status.V4UsingIPs).To(BeZero())

pods, err := f.KubeClientSet.CoreV1().Pods("kube-system").List(context.Background(), metav1.ListOptions{LabelSelector: "app=ovs"})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -234,7 +234,7 @@ var _ = Describe("[Subnet]", func() {

s, err = f.OvnClientSet.KubeovnV1().Subnets().Get(context.Background(), name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(s.Status.AvailableIPs).To(Equal(float64(0)))
Expect(s.Status.V4AvailableIPs).To(Equal(float64(0)))
})

It("small cidr", func() {
Expand All @@ -257,7 +257,7 @@ var _ = Describe("[Subnet]", func() {

s, err = f.OvnClientSet.KubeovnV1().Subnets().Get(context.Background(), name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(s.Status.AvailableIPs).To(Equal(float64(1)))
Expect(s.Status.V4AvailableIPs).To(Equal(float64(1)))
})

It("with excludeips", func() {
Expand All @@ -280,7 +280,7 @@ var _ = Describe("[Subnet]", func() {

s, err = f.OvnClientSet.KubeovnV1().Subnets().Get(context.Background(), name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(s.Status.AvailableIPs).To(Equal(float64(244)))
Expect(s.Status.V4AvailableIPs).To(Equal(float64(244)))
})
})
})

0 comments on commit f1fe2b2

Please sign in to comment.