Skip to content

Commit

Permalink
feat: upgrade module to Go 1.22
Browse files Browse the repository at this point in the history
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
  • Loading branch information
smlx committed Feb 26, 2024
1 parent 8d26401 commit 1c1840b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 11 additions & 12 deletions internal/k8s/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1c1840b

Please sign in to comment.