Skip to content

Commit

Permalink
chore: add logger to packager2 Mirror (#3152)
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <kit@defenseunicorns.com>
  • Loading branch information
mkcp authored Oct 29, 2024
1 parent 4d78738 commit 96c5afa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/internal/packager2/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"

"github.com/zarf-dev/zarf/src/config"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/zarf-dev/zarf/src/internal/gitea"
"github.com/zarf-dev/zarf/src/internal/packager2/layout"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager/filters"
"github.com/zarf-dev/zarf/src/pkg/transform"
Expand Down Expand Up @@ -56,8 +56,7 @@ func Mirror(ctx context.Context, opt MirrorOptions) error {
}

func pushImagesToRegistry(ctx context.Context, c *cluster.Cluster, pkgLayout *layout.PackageLayout, filter filters.ComponentFilterStrategy, regInfo types.RegistryInfo, noImgChecksum bool, retries int) error {
logs.Warn.SetOutput(&message.DebugWriter{})
logs.Progress.SetOutput(&message.DebugWriter{})
l := logger.From(ctx)

components, err := filter.Apply(pkgLayout.Pkg)
if err != nil {
Expand Down Expand Up @@ -124,6 +123,7 @@ func pushImagesToRegistry(ctx context.Context, c *cluster.Cluster, pkgLayout *la
names = append(names, offlineName)
for _, name := range names {
message.Infof("Pushing image %s", name)
l.Info("pushing image", "name", name)
err = crane.Push(img, name, pushOptions...)
if err != nil {
return err
Expand Down Expand Up @@ -168,6 +168,7 @@ func pushImagesToRegistry(ctx context.Context, c *cluster.Cluster, pkgLayout *la
}

func pushReposToRepository(ctx context.Context, c *cluster.Cluster, pkgLayout *layout.PackageLayout, filter filters.ComponentFilterStrategy, gitInfo types.GitServerInfo, retries int) error {
l := logger.From(ctx)
components, err := filter.Apply(pkgLayout.Pkg)
if err != nil {
return err
Expand All @@ -190,6 +191,7 @@ func pushReposToRepository(ctx context.Context, c *cluster.Cluster, pkgLayout *l
err = retry.Do(func() error {
if !dns.IsServiceURL(gitInfo.Address) {
message.Infof("Pushing repository %s to server %s", repoURL, gitInfo.Address)
l.Info("pushing repository to server", "repo", repoURL, "server", gitInfo.Address)
err = repository.Push(ctx, gitInfo.Address, gitInfo.PushUsername, gitInfo.PushPassword)
if err != nil {
return err
Expand Down Expand Up @@ -219,6 +221,7 @@ func pushReposToRepository(ctx context.Context, c *cluster.Cluster, pkgLayout *l
}
return tunnel.Wrap(func() error {
message.Infof("Pushing repository %s to server %s", repoURL, tunnel.HTTPEndpoint())
l.Info("pushing repository to server", "repo", repoURL, "server", tunnel.HTTPEndpoint())
err = repository.Push(ctx, tunnel.HTTPEndpoint(), gitInfo.PushUsername, gitInfo.PushPassword)
if err != nil {
return err
Expand Down
18 changes: 18 additions & 0 deletions src/pkg/cluster/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/avast/retry-go/v4"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/types"
)
Expand Down Expand Up @@ -221,6 +222,10 @@ func (c *Cluster) checkForZarfConnectLabel(ctx context.Context, name string) (Tu
zt.urlSuffix = svc.Annotations[ZarfConnectAnnotationURL]

message.Debugf("tunnel connection match: %s/%s on port %d", svc.Namespace, svc.Name, zt.RemotePort)
logger.From(ctx).Debug("tunnel connection match",
"namespace", svc.Namespace,
"name", svc.Name,
"remotePort", zt.RemotePort)
} else {
return zt, fmt.Errorf("no matching services found for %s", name)
}
Expand Down Expand Up @@ -393,6 +398,7 @@ func (tunnel *Tunnel) Close() {
// establish opens a tunnel to a kubernetes resource, as specified by the provided tunnel struct.
func (tunnel *Tunnel) establish(ctx context.Context) (string, error) {
var err error
l := logger.From(ctx)

// Track this locally as we may need to retry if the tunnel fails.
localPort := tunnel.localPort
Expand All @@ -405,11 +411,13 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) {
// is available for selection again.
if localPort == 0 {
message.Debugf("Requested local port is 0. Selecting an open port on host system")
l.Debug("requested local port is 0. Selecting an open port on host system")
localPort, err = helpers.GetAvailablePort()
if err != nil {
return "", fmt.Errorf("unable to find an available port: %w", err)
}
message.Debugf("Selected port %d", localPort)
l.Debug("selected port", "port", localPort)
globalMutex.Lock()
defer globalMutex.Unlock()
}
Expand All @@ -422,13 +430,21 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) {
tunnel.namespace,
)
message.Debugf(msg)
l.Debug("opening tunnel",
"localPort", localPort,
"remotePort", tunnel.remotePort,
"resourceType", tunnel.resourceType,
"resourceName", tunnel.resourceName,
"namespace", tunnel.namespace,
)

// Find the pod to port forward to
podName, err := tunnel.getAttachablePodForResource(ctx)
if err != nil {
return "", fmt.Errorf("unable to find pod attached to given resource: %w", err)
}
message.Debugf("Selected pod %s to open port forward to", podName)
l.Debug("selected pod to open port forward to", "name", podName)

// Build url to the port forward endpoint.
// Example: http://localhost:8080/api/v1/namespaces/helm/pods/tiller-deploy-9itlq/portforward.
Expand All @@ -442,6 +458,7 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) {
URL()

message.Debugf("Using URL %s to create portforward", portForwardCreateURL)
l.Debug("using URL to create portforward", "url", portForwardCreateURL)

// Construct the spdy client required by the client-go portforward library.
transport, upgrader, err := spdy.RoundTripperFor(tunnel.restConfig)
Expand Down Expand Up @@ -477,6 +494,7 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) {
tunnel.errChan = errChan

message.Debugf("Creating port forwarding tunnel at %s", url)
l.Debug("creating port forwarding tunnel", "url", url)
return url, nil
}
}
Expand Down

0 comments on commit 96c5afa

Please sign in to comment.