Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Use context.WithTimeout() to prevent binary from waiting forever
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Rodriguez committed Oct 25, 2023
1 parent 8933517 commit 1ff0438
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"os"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -26,13 +27,16 @@ const (
)

func main() {
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
defer cancel()

clientset, err := newK8sClient()
if err != nil {
klog.Errorf("failed to create Kubernetes clientset: %v", err)
os.Exit(1)
}

ecrURL, err := getECRURL(clientset)
ecrURL, err := getECRURL(ctx, clientset)
if err != nil {
klog.Errorf("failed to get ECR URL from zarf-state secret: %v", err)
os.Exit(1)
Expand All @@ -44,7 +48,7 @@ func main() {
os.Exit(1)
}

err = updateZarfManagedImageSecrets(clientset, ecrURL, authToken)
err = updateZarfManagedImageSecrets(ctx, clientset, ecrURL, authToken)
if err != nil {
klog.Errorf("failed to update ECR image pull credentials: %v", err)
os.Exit(1)
Expand All @@ -65,8 +69,8 @@ func newK8sClient() (*kubernetes.Clientset, error) {
return clientset, nil
}

func getECRURL(clientset *kubernetes.Clientset) (string, error) {
secret, err := clientset.CoreV1().Secrets(zarfNamespace).Get(context.TODO(), zarfStateSecret, metav1.GetOptions{})
func getECRURL(ctx context.Context, clientset *kubernetes.Clientset) (string, error) {
secret, err := clientset.CoreV1().Secrets(zarfNamespace).Get(ctx, zarfStateSecret, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get secret '%s' in namespace '%s': %w", zarfStateSecret, zarfNamespace, err)
}
Expand Down Expand Up @@ -106,14 +110,14 @@ func fetchECRToken() (string, error) {
return *authOutput.AuthorizationData[0].AuthorizationToken, nil
}

func updateZarfManagedImageSecrets(clientset *kubernetes.Clientset, ecrURL string, authToken string) error {
namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
func updateZarfManagedImageSecrets(ctx context.Context, clientset *kubernetes.Clientset, ecrURL string, authToken string) error {
namespaces, err := clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("error listing namespaces: %w", err)
}

for _, namespace := range namespaces.Items {
registrySecret, err := clientset.CoreV1().Secrets(namespace.Name).Get(context.TODO(), zarfImagePullSecret, metav1.GetOptions{})
registrySecret, err := clientset.CoreV1().Secrets(namespace.Name).Get(ctx, zarfImagePullSecret, metav1.GetOptions{})
if err != nil {
continue
}
Expand All @@ -138,7 +142,7 @@ func updateZarfManagedImageSecrets(clientset *kubernetes.Clientset, ecrURL strin

registrySecret.Data[".dockerconfigjson"] = dockerConfigData

updatedRegistrySecret, err := clientset.CoreV1().Secrets(namespace.Name).Update(context.TODO(), registrySecret, metav1.UpdateOptions{})
updatedRegistrySecret, err := clientset.CoreV1().Secrets(namespace.Name).Update(ctx, registrySecret, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("failed to update secret '%s' in namespace '%s': %w", updatedRegistrySecret.Name, namespace.Name, err)
}
Expand Down

0 comments on commit 1ff0438

Please sign in to comment.