Skip to content

Commit

Permalink
modify init ipam by ip crd only for sts pod (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Apr 18, 2022
1 parent 3a5ead6 commit aa75651
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ spec:
type: string
containerID:
type: string
podType:
type: string
scope: Cluster
names:
plural: ips
Expand Down
2 changes: 2 additions & 0 deletions dist/images/update/1.7-1.8.3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ spec:
type: string
containerID:
type: string
podType:
type: string
scope: Cluster
names:
plural: ips
Expand Down
2 changes: 2 additions & 0 deletions dist/images/update/1.8.3-1.9.1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ spec:
type: string
containerID:
type: string
podType:
type: string
scope: Cluster
names:
plural: ips
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/kubeovn/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type IPSpec struct {
MacAddress string `json:"macAddress"`
AttachMacs []string `json:"attachMacs"`
ContainerID string `json:"containerID"`
PodType string `json:"podType"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,12 @@ func (c *Controller) InitIPAM() error {
ip := pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, podNet.ProviderName)]
mac := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)]
subnet := pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, podNet.ProviderName)]
podType := getPodType(pod)
_, _, _, err := c.ipam.GetStaticAddress(key, portName, ip, mac, subnet, false)
if err != nil {
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, podNet.ProviderName)], err)
} else {
err = c.createOrUpdateCrdIPs(podName, ip, mac, subnet, pod.Namespace, pod.Spec.NodeName, podNet.ProviderName)
err = c.createOrUpdateCrdIPs(podName, ip, mac, subnet, pod.Namespace, pod.Spec.NodeName, podNet.ProviderName, podType)
if err != nil {
klog.Errorf("failed to create/update ips CR %s.%s with ip address %s: %v", podName, pod.Namespace, ip, err)
}
Expand All @@ -354,6 +355,10 @@ func (c *Controller) InitIPAM() error {
return err
}
for _, ip := range ips {
if ip.Spec.PodType != "StatefulSet" {
continue
}

var ipamKey string
if ip.Spec.Namespace != "" {
ipamKey = fmt.Sprintf("%s/%s", ip.Spec.Namespace, ip.Spec.PodName)
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (c *Controller) handleAddNode(key string) error {
return err
}

if err := c.createOrUpdateCrdIPs("", ipStr, mac, c.config.NodeSwitch, "", node.Name, ""); err != nil {
if err := c.createOrUpdateCrdIPs("", ipStr, mac, c.config.NodeSwitch, "", node.Name, "", ""); err != nil {
klog.Errorf("failed to create or update IPs node-%s: %v", key, err)
return err
}
Expand Down Expand Up @@ -600,7 +600,7 @@ func (c *Controller) handleUpdateNode(key string) error {
return nil
}

func (c *Controller) createOrUpdateCrdIPs(podName, ip, mac, subnetName, ns, nodeName, providerName string) error {
func (c *Controller) createOrUpdateCrdIPs(podName, ip, mac, subnetName, ns, nodeName, providerName, podType string) error {
var key, ipName string
if subnetName == c.config.NodeSwitch {
key = nodeName
Expand Down Expand Up @@ -634,6 +634,7 @@ func (c *Controller) createOrUpdateCrdIPs(podName, ip, mac, subnetName, ns, node
AttachIPs: []string{},
AttachMacs: []string{},
AttachSubnets: []string{},
PodType: podType,
},
}, metav1.CreateOptions{})
if err != nil {
Expand Down Expand Up @@ -667,6 +668,7 @@ func (c *Controller) createOrUpdateCrdIPs(podName, ip, mac, subnetName, ns, node
newIpCr.Spec.AttachIPs = []string{}
newIpCr.Spec.AttachMacs = []string{}
newIpCr.Spec.AttachSubnets = []string{}
newIpCr.Spec.PodType = podType
_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), newIpCr, metav1.UpdateOptions{})
if err != nil {
errMsg := fmt.Errorf("failed to update ips cr %s: %v", newIpCr.Name, err)
Expand Down
11 changes: 10 additions & 1 deletion pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,9 @@ func (c *Controller) handleAddPod(key string) error {
return err
}

podType := getPodType(pod)
podName := c.getNameByPod(pod)
if err := c.createOrUpdateCrdIPs(podName, ipStr, mac, subnet.Name, pod.Namespace, pod.Spec.NodeName, podNet.ProviderName); err != nil {
if err := c.createOrUpdateCrdIPs(podName, ipStr, mac, subnet.Name, pod.Namespace, pod.Spec.NodeName, podNet.ProviderName, podType); err != nil {
klog.Errorf("failed to create IP %s.%s: %v", podName, pod.Namespace, err)
}

Expand Down Expand Up @@ -1538,3 +1539,11 @@ func (c *Controller) getNsAvailableSubnets(pod *v1.Pod) ([]*kubeovnNet, error) {

return result, nil
}

func getPodType(pod *v1.Pod) string {
var podType string
if ok, _ := isStatefulSetPod(pod); ok {
podType = "StatefulSet"
}
return podType
}
2 changes: 2 additions & 0 deletions yamls/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
type: string
containerID:
type: string
podType:
type: string
scope: Cluster
names:
plural: ips
Expand Down

0 comments on commit aa75651

Please sign in to comment.