From 1c1840bcb473310f2c7ab83539ea7852ae459100 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Fri, 23 Feb 2024 22:59:18 +0800 Subject: [PATCH] feat: upgrade module to Go 1.22 Go 1.22 brings changes to the way loop iteration works with inner closures. So take advantage of this feature to remove redundancy. https://go.dev/blog/loopvar-preview --- go.mod | 2 +- internal/k8s/logs.go | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index be54e8ab..b640b35e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/uselagoon/ssh-portal -go 1.21 +go 1.22 require ( github.com/MicahParks/keyfunc/v2 v2.1.0 diff --git a/internal/k8s/logs.go b/internal/k8s/logs.go index 1b20ae23..6081e797 100644 --- a/internal/k8s/logs.go +++ b/internal/k8s/logs.go @@ -89,12 +89,9 @@ func (c *Client) readLogs(ctx context.Context, requestID string, if err != nil { return fmt.Errorf("couldn't stream logs: %v", err) } - // copy loop vars so they can be referenced in the closure - cName := cStatus.Name - cID := cStatus.ContainerID egSend.Go(func() error { - defer c.logStreamIDs.Delete(cID) - linewiseCopy(ctx, fmt.Sprintf("[pod/%s/%s]", p.Name, cName), logs, + defer c.logStreamIDs.Delete(cStatus.ContainerID) + linewiseCopy(ctx, fmt.Sprintf("[pod/%s/%s]", p.Name, cStatus.Name), logs, logStream) // When a pod is terminating, the k8s API sometimes sends an event // showing a healthy pod _after_ an existing logStream for the same pod @@ -159,12 +156,15 @@ func (c *Client) newPodInformer(ctx context.Context, return nil, fmt.Errorf("couldn't get deployment: %v", err) } // configure the informer factory, filtering on deployment selector labels - factory := informers.NewSharedInformerFactoryWithOptions(c.clientset, - time.Hour, informers.WithNamespace(namespace), + factory := informers.NewSharedInformerFactoryWithOptions( + c.clientset, + time.Hour, + informers.WithNamespace(namespace), informers.WithTweakListOptions(func(opts *metav1.ListOptions) { - opts.LabelSelector = labels.SelectorFromSet( - d.Spec.Selector.MatchLabels).String() - })) + opts.LabelSelector = + labels.SelectorFromSet(d.Spec.Selector.MatchLabels).String() + }), + ) // construct the informer podInformer := factory.Core().V1().Pods().Informer() _, err = podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ @@ -273,8 +273,7 @@ func (c *Client) Logs(ctx context.Context, if len(pods.Items) == 0 { return fmt.Errorf("no pods for deployment %s", deployment) } - for i := range pods.Items { - pod := pods.Items[i] // copy loop var so it can be referenced in the closure + for _, pod := range pods.Items { egSend.Go(func() error { readLogsErr := c.readLogs(childCtx, requestID, &egSend, &pod, container, follow, tailLines, logs)