Skip to content

Commit

Permalink
✨ Reduce the number of updateNodePodProbes calls (#1430)
Browse files Browse the repository at this point in the history
Signed-off-by: 刘硕 <liushuo@zetyun.com>
Co-authored-by: 刘硕 <liushuo@zetyun.com>
  • Loading branch information
ls-2018 and 刘硕 authored Oct 17, 2023
1 parent 85cece5 commit b9484d6
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions pkg/controller/podprobemarker/pod_probe_marker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,16 @@ func (r *ReconcilePodProbeMarker) syncPodProbeMarker(ns, name string) error {
}
klog.V(3).Infof("add PodProbeMarker(%s/%s) finalizer success", ppm.Namespace, ppm.Name)
}

groupByNode := make(map[string][]*corev1.Pod)
for i, pod := range pods {
groupByNode[pod.Spec.NodeName] = append(groupByNode[pod.Spec.NodeName], pods[i])
}

// add podProbe in NodePodProbe
for _, pod := range pods {
for nodeName := range groupByNode {
// add podProbe to NodePodProbe.Spec
if err = r.updateNodePodProbe(ppm, pod); err != nil {
if err = r.updateNodePodProbes(ppm, nodeName, groupByNode[nodeName]); err != nil {
return err
}
}
Expand Down Expand Up @@ -206,43 +212,45 @@ func (r *ReconcilePodProbeMarker) handlerPodProbeMarkerFinalizer(ppm *appsv1alph
return nil
}

func (r *ReconcilePodProbeMarker) updateNodePodProbe(ppm *appsv1alpha1.PodProbeMarker, pod *corev1.Pod) error {
func (r *ReconcilePodProbeMarker) updateNodePodProbes(ppm *appsv1alpha1.PodProbeMarker, nodeName string, pods []*corev1.Pod) error {
npp := &appsv1alpha1.NodePodProbe{}
err := r.Get(context.TODO(), client.ObjectKey{Name: pod.Spec.NodeName}, npp)
err := r.Get(context.TODO(), client.ObjectKey{Name: nodeName}, npp)
if err != nil {
if errors.IsNotFound(err) {
klog.Warningf("PodProbeMarker ppm(%s/%s) NodePodProbe(%s) is Not Found", ppm.Namespace, ppm.Name, npp.Name)
return nil
}
// Error reading the object - requeue the request.
klog.Errorf("PodProbeMarker ppm(%s/%s) get NodePodProbe(%s) failed: %s", ppm.Namespace, ppm.Name, pod.Spec.NodeName, err.Error())
klog.Errorf("PodProbeMarker ppm(%s/%s) get NodePodProbe(%s) failed: %s", ppm.Namespace, ppm.Name, nodeName, err.Error())
return err
}

oldSpec := npp.Spec.DeepCopy()
exist := false
for i := range npp.Spec.PodProbes {
podProbe := &npp.Spec.PodProbes[i]
if podProbe.Name == pod.Name && podProbe.Namespace == pod.Namespace && podProbe.UID == string(pod.UID) {
exist = true
for _, pod := range pods {
exist := false
for i := range npp.Spec.PodProbes {
podProbe := &npp.Spec.PodProbes[i]
if podProbe.Name == pod.Name && podProbe.Namespace == pod.Namespace && podProbe.UID == string(pod.UID) {
exist = true
for j := range ppm.Spec.Probes {
probe := ppm.Spec.Probes[j]
setPodContainerProbes(podProbe, probe, ppm.Name)
}
break
}
}
if !exist {
podProbe := appsv1alpha1.PodProbe{Name: pod.Name, Namespace: pod.Namespace, UID: string(pod.UID)}
for j := range ppm.Spec.Probes {
probe := ppm.Spec.Probes[j]
setPodContainerProbes(podProbe, probe, ppm.Name)
podProbe.Probes = append(podProbe.Probes, appsv1alpha1.ContainerProbe{
Name: fmt.Sprintf("%s#%s", ppm.Name, probe.Name),
ContainerName: probe.ContainerName,
Probe: probe.Probe,
})
}
break
}
}
if !exist {
podProbe := appsv1alpha1.PodProbe{Name: pod.Name, Namespace: pod.Namespace, UID: string(pod.UID)}
for j := range ppm.Spec.Probes {
probe := ppm.Spec.Probes[j]
podProbe.Probes = append(podProbe.Probes, appsv1alpha1.ContainerProbe{
Name: fmt.Sprintf("%s#%s", ppm.Name, probe.Name),
ContainerName: probe.ContainerName,
Probe: probe.Probe,
})
npp.Spec.PodProbes = append(npp.Spec.PodProbes, podProbe)
}
npp.Spec.PodProbes = append(npp.Spec.PodProbes, podProbe)
}

if reflect.DeepEqual(npp.Spec, oldSpec) {
Expand Down

0 comments on commit b9484d6

Please sign in to comment.