Skip to content

Commit

Permalink
daemon: avoid gc in used res
Browse files Browse the repository at this point in the history
  • Loading branch information
l1b0k committed Nov 16, 2023
1 parent 8d2c396 commit 8e37028
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
13 changes: 10 additions & 3 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (n *networkService) AllocIP(ctx context.Context, r *rpc.AllocIPRequest) (*r
}()

// 0. Get pod Info
podinfo, err := n.k8s.GetPod(r.K8SPodNamespace, r.K8SPodName)
podinfo, err := n.k8s.GetPod(r.K8SPodNamespace, r.K8SPodName, true)
if err != nil {
return nil, errors.Wrapf(err, "error get pod info for: %+v", r)
}
Expand Down Expand Up @@ -510,7 +510,7 @@ func (n *networkService) ReleaseIP(ctx context.Context, r *rpc.ReleaseIPRequest)
}()

// 0. Get pod Info
podinfo, err := n.k8s.GetPod(r.K8SPodNamespace, r.K8SPodName)
podinfo, err := n.k8s.GetPod(r.K8SPodNamespace, r.K8SPodName, true)
if err != nil {
return nil, errors.Wrapf(err, "error get pod info for: %+v", r)
}
Expand Down Expand Up @@ -580,7 +580,7 @@ func (n *networkService) ReleaseIP(ctx context.Context, r *rpc.ReleaseIPRequest)
func (n *networkService) GetIPInfo(ctx context.Context, r *rpc.GetInfoRequest) (*rpc.GetInfoReply, error) {
serviceLog.Debugf("GetIPInfo request: %+v", r)
// 0. Get pod Info
podinfo, err := n.k8s.GetPod(r.K8SPodNamespace, r.K8SPodName)
podinfo, err := n.k8s.GetPod(r.K8SPodNamespace, r.K8SPodName, true)
if err != nil {
return nil, errors.Wrapf(err, "error get pod info for: %+v", r)
}
Expand Down Expand Up @@ -820,6 +820,13 @@ func (n *networkService) startGarbageCollectionLoop() {
resRelate := resRelateObj.(types.PodResources)
_, podExist := podKeyMap[podInfoKey(resRelate.PodInfo.Namespace, resRelate.PodInfo.Name)]
if !podExist {
// check kbe-api again
_, err := n.k8s.GetPod(resRelate.PodInfo.Namespace, resRelate.PodInfo.Name, false)
if !k8sErr.IsNotFound(err) {
continue
}
serviceLog.Infof("found pod %s not exist, will cleanup related resource", podInfoKey(resRelate.PodInfo.Namespace, resRelate.PodInfo.Name))

if resRelate.PodInfo.IPStickTime != 0 {
// delay resource garbage collection for sticky ip
resRelate.PodInfo.IPStickTime = 0
Expand Down
22 changes: 12 additions & 10 deletions daemon/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const (
// Kubernetes operation set
type Kubernetes interface {
GetLocalPods() ([]*types.PodInfo, error)
GetPod(namespace, name string) (*types.PodInfo, error)
GetPod(namespace, name string, cache bool) (*types.PodInfo, error)
GetServiceCIDR() *types.IPNetSet
GetNodeCidr() *types.IPNetSet
SetNodeAllocatablePod(count int) error
Expand Down Expand Up @@ -205,7 +205,7 @@ func (k *k8s) setSvcCIDR(svcCidr *types.IPNetSet) error {
}

func (k *k8s) PatchEipInfo(info *types.PodInfo) error {
pod, err := getPod(context.Background(), k.client, info.Namespace, info.Name)
pod, err := getPod(context.Background(), k.client, info.Namespace, info.Name, true)
if err != nil || pod == nil {
return err
}
Expand All @@ -220,7 +220,7 @@ func (k *k8s) PatchEipInfo(info *types.PodInfo) error {
}

func (k *k8s) PatchPodIPInfo(info *types.PodInfo, ips string) error {
pod, err := getPod(context.Background(), k.client, info.Namespace, info.Name)
pod, err := getPod(context.Background(), k.client, info.Namespace, info.Name, true)
if err != nil || pod == nil {
return err
}
Expand Down Expand Up @@ -690,8 +690,8 @@ func deserialize(data []byte) (interface{}, error) {
return item, nil
}

func (k *k8s) GetPod(namespace, name string) (*types.PodInfo, error) {
pod, err := getPod(context.Background(), k.client, namespace, name)
func (k *k8s) GetPod(namespace, name string, cache bool) (*types.PodInfo, error) {
pod, err := getPod(context.Background(), k.client, namespace, name, cache)
if err != nil {
if apierrors.IsNotFound(err) {
key := podInfoKey(namespace, name)
Expand Down Expand Up @@ -819,7 +819,7 @@ func (k *k8s) RecordNodeEvent(eventType, reason, message string) {
}

func (k *k8s) RecordPodEvent(podName, podNamespace, eventType, reason, message string) error {
pod, err := getPod(context.Background(), k.client, podNamespace, podName)
pod, err := getPod(context.Background(), k.client, podNamespace, podName, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -869,11 +869,13 @@ func getNode(ctx context.Context, c client.Client, nodeName string) (*corev1.Nod
return obj, err
}

func getPod(ctx context.Context, c client.Client, namespace, name string) (*corev1.Pod, error) {
func getPod(ctx context.Context, c client.Client, namespace, name string, cache bool) (*corev1.Pod, error) {
opt := &metav1.GetOptions{}
if cache {
opt.ResourceVersion = "0"
}
obj := &corev1.Pod{}
err := c.Get(ctx, k8stypes.NamespacedName{Namespace: namespace, Name: name}, obj, &client.GetOptions{Raw: &metav1.GetOptions{
ResourceVersion: "0",
}})
err := c.Get(ctx, k8stypes.NamespacedName{Namespace: namespace, Name: name}, obj, &client.GetOptions{Raw: opt})
return obj, err
}

Expand Down

0 comments on commit 8e37028

Please sign in to comment.