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 8b5ce74 commit c9dfa5b
Show file tree
Hide file tree
Showing 6 changed files with 25 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 @@ -404,6 +404,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 @@ -307,11 +307,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, ip, mac, subnet)
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 @@ -330,6 +331,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 @@ -295,7 +295,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 @@ -540,7 +540,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 @@ -574,6 +574,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 @@ -607,6 +608,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 @@ -486,8 +486,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 @@ -1377,3 +1378,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 c9dfa5b

Please sign in to comment.