Skip to content

Commit 5c74ebd

Browse files
committed
Include function descriptions
1 parent fc198c6 commit 5c74ebd

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

phpfpm/pod_discovery.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ import (
1515

1616
const uriTemplate string = "tcp://%s:%s/status"
1717

18+
// customWatcher is a custom implementation of the cache.Watcher interface,
19+
// designed to watch Kubernetes pods based on specific label selectors and namespace.
1820
type customWatcher struct {
1921
clientset *kubernetes.Clientset
2022
labelSelector string
2123
namespace string
2224
}
2325

24-
// newWatcher creates a new instance of customWatcher.
26+
// newWatcher creates and returns a new instance of customWatcher.
27+
// It is used to initialize the watcher with a Kubernetes clientset, a namespace, and a label selector for filtering the pods to be monitored.
2528
func newWatcher(clientset *kubernetes.Clientset, namespace string, podLabels string) cache.Watcher {
2629
return &customWatcher{
2730
clientset: clientset,
@@ -30,7 +33,11 @@ func newWatcher(clientset *kubernetes.Clientset, namespace string, podLabels str
3033
}
3134
}
3235

33-
// Watch starts a new watch session for Pods
36+
// Watch initiates a new watch session for Pods by establishing a connection to the Kubernetes API.
37+
// This function is used as part of the NewRetryWatcher setup, which ensures a resilient connection.
38+
// If the connection to the API is interrupted, the NewRetryWatcher will automatically attempt to re-establish it,
39+
// providing continuous monitoring of pod events. This approach is ideal for maintaining reliable event streaming,
40+
// especially in cases of network instability or API server disruptions.
3441
func (c *customWatcher) Watch(options metav1.ListOptions) (apiWatch.Interface, error) {
3542
options.LabelSelector = c.labelSelector
3643
ns := c.namespace
@@ -57,7 +64,7 @@ func k8sGetClient() (*kubernetes.Clientset, error) {
5764
return clientset, nil
5865
}
5966

60-
// listPods returns the initial list of pods
67+
// listPods retrieves the initial list of pods that match the specified label criteria and namespace.
6168
func listPods(clientset *kubernetes.Clientset, namespace string, podLabels string) (*v1.PodList, error) {
6269
if namespace == "" {
6370
namespace = metav1.NamespaceAll
@@ -69,10 +76,12 @@ func listPods(clientset *kubernetes.Clientset, namespace string, podLabels strin
6976
return podList, nil
7077
}
7178

72-
// initializePodEnlisting lists all Pods that match the criteria and initializes their state in PoolManager.
79+
// initializePodEnlisting retrieves all pods matching the specified criteria and appends their URIs to the PoolManager's PodPhases.
80+
// This function is invoked prior to starting the NewRetryWatcher to capture the initial state of existing pods
81+
// and to obtain the ResourceVersion required for initializing the NewRetryWatcher.
7382
func (pm *PoolManager) initialPodEnlisting(exporter *Exporter, podList *v1.PodList, port string) (string, error) {
7483

75-
log.Infof("Found %d pods during initial list", len(podList.Items))
84+
log.Infof("Found %d pod(s) during initial list", len(podList.Items))
7685
for _, pod := range podList.Items {
7786
podName := pod.Name
7887
currentPhase := pod.Status.Phase
@@ -84,20 +93,20 @@ func (pm *PoolManager) initialPodEnlisting(exporter *Exporter, podList *v1.PodLi
8493
return podList.ResourceVersion, nil
8594
}
8695

87-
// handlePodRunning handles actions for a pod that is in the Running phase.
96+
// handlePodRunning is used when a pod is in the Running phase and needs to be appended into the pool manager's PodPhases.
8897
func (pm *PoolManager) handlePodRunning(exporter *Exporter, pod *v1.Pod, uri string) {
8998
ip := pod.Status.PodIP
9099
podName := pod.Name
91100
if ip != "" {
92-
log.Infof("Handling Running pod %s with IP %s", podName, ip)
101+
log.Infof("New running pod detected %s with IP %s", podName, ip)
93102
pm.Add(uri)
94103
exporter.UpdatePoolManager(*pm)
95104
} else {
96-
log.Debugf("Pod %s is Running but has no IP assigned", podName)
105+
log.Debugf("Pod %s is in Running state but has no IP assigned", podName)
97106
}
98107
}
99108

100-
// podAdded processes a newly added pod.
109+
// processPodAdded handles the addition of a newly created pod to the cluster by appending its URI to the pool manager.
101110
func (pm *PoolManager) processPodAdded(exporter *Exporter, pod *v1.Pod, uri string) {
102111
pm.PodPhases[pod.Name] = pod.Status.Phase
103112

@@ -106,7 +115,9 @@ func (pm *PoolManager) processPodAdded(exporter *Exporter, pod *v1.Pod, uri stri
106115
}
107116
}
108117

109-
// podModified processes modifications to an existing pod.
118+
// processPodModified handles events triggered by pod modifications, including when a new pod is added to the cluster.
119+
// To be included in the pool manager, the pod must be in the "Running" phase. The function checks the pod's current phase
120+
// and, if it is running, calls handlePodRunning to append the pod to the pool manager's PodPhases.
110121
func (pm *PoolManager) processPodModified(exporter *Exporter, pod *v1.Pod, uri string) {
111122
lastPhase, exists := pm.PodPhases[pod.Name]
112123

@@ -117,7 +128,7 @@ func (pm *PoolManager) processPodModified(exporter *Exporter, pod *v1.Pod, uri s
117128
pm.PodPhases[pod.Name] = pod.Status.Phase
118129
}
119130

120-
// podDeleted processes the deletion of a pod.
131+
// processPodDeleted handles the removal of a pods URI from the pool manager's PodPhases.
121132
func (pm *PoolManager) processPodDeleted(exporter *Exporter, pod *v1.Pod, uri string) {
122133
ip := pod.Status.PodIP
123134

@@ -127,7 +138,9 @@ func (pm *PoolManager) processPodDeleted(exporter *Exporter, pod *v1.Pod, uri st
127138
delete(pm.PodPhases, pod.Name)
128139
}
129140

130-
// DiscoverPods finds pods with the specified annotation in the given namespace.
141+
// DiscoverPods begins by listing the pods that match the specified labels within the given namespace.
142+
// It then starts a watch session in a separate goroutine.
143+
// The list operation is performed first to retrieve the initial ResourceVersion, which is required to initialize a NewRetryWatcher.
131144
func (pm *PoolManager) DiscoverPods(exporter *Exporter, namespace string, podLabels string, port string) error {
132145
// Get the Kubernetes client
133146
clientset, err := k8sGetClient()
@@ -147,15 +160,19 @@ func (pm *PoolManager) DiscoverPods(exporter *Exporter, namespace string, podLab
147160
return nil
148161
}
149162

150-
// watchPodEvents watches for pod events and processes them.
163+
// watchPodEvents monitors pod events and processes them accordingly:
164+
// - For "added" events, the new pod's URI is appended to the pool manager.
165+
// - For "modified" events, it verifies if the pod is in the running state before appending its URI to the pool manager.
166+
// - For "deleted" events, the pod's URI is removed from the pool manager's PodPhases.
167+
// Note: There is an unresolved issue with timeout errors when a pod is deleted, which requires further investigation and handling.
151168
func (pm *PoolManager) watchPodEvents(exporter *Exporter, watcher cache.Watcher, resourceVersion string, port string) {
152169
retryWatcher, err := watch.NewRetryWatcher(resourceVersion, watcher)
153170
if err != nil {
154-
log.Errorf("Failed to create RetryWatcher: %v", err)
171+
log.Errorf("Failed to create Retry Watcher: %v", err)
155172
return
156173
}
157174
defer retryWatcher.Stop()
158-
log.Info("RetryWatcher initialized successfully")
175+
log.Info("Retry Watcher initialized successfully")
159176

160177
for event := range retryWatcher.ResultChan() {
161178
pod, ok := event.Object.(*v1.Pod)

0 commit comments

Comments
 (0)