Skip to content

Commit

Permalink
chore: add logger to packager2 Mirror (zarf-dev#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 and Jneville0815 committed Dec 12, 2024
1 parent 1b273aa commit be7858b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 8 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 @@ -58,8 +58,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 @@ -126,6 +125,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 @@ -170,6 +170,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, forcePushRepos bool) error {
l := logger.From(ctx)
components, err := filter.Apply(pkgLayout.Pkg)
if err != nil {
return err
Expand All @@ -192,7 +193,9 @@ 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, forcePushRepos)

if err != nil {
return err
}
Expand Down Expand Up @@ -221,7 +224,9 @@ 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, forcePushRepos)

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 be7858b

Please sign in to comment.