Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: slogger for zarf init #3168

Merged
merged 34 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
750443e
slog on deploy
AustinAbro321 Oct 29, 2024
0425beb
delete unused functions
AustinAbro321 Oct 29, 2024
f600cff
introduce use slog
AustinAbro321 Oct 29, 2024
76c5a2e
added todo
AustinAbro321 Oct 29, 2024
9b3056a
move context
AustinAbro321 Oct 29, 2024
bf29b07
add flag to enable logger
AustinAbro321 Oct 29, 2024
cea30a3
enable slogger
AustinAbro321 Oct 29, 2024
3fc8432
todo
AustinAbro321 Oct 29, 2024
9ec0f9c
refactor enabled
AustinAbro321 Oct 29, 2024
bc97cc2
merge
AustinAbro321 Oct 29, 2024
47d87f0
more logs
AustinAbro321 Oct 29, 2024
d04c773
logs
AustinAbro321 Oct 29, 2024
5f34a81
Merge branch 'main' into slog-package-deploy
AustinAbro321 Oct 29, 2024
13105b9
adding context
AustinAbro321 Oct 29, 2024
122f5f1
add logs for repo push
AustinAbro321 Oct 29, 2024
5f48403
adding logs
AustinAbro321 Oct 30, 2024
d36bb31
avoid merge conflicts
AustinAbro321 Oct 30, 2024
8543061
small style changes
AustinAbro321 Oct 30, 2024
a8bf4f3
merge
AustinAbro321 Oct 30, 2024
31ed212
make it more clear that we use helm
AustinAbro321 Oct 30, 2024
ef2a807
capitalizes and things I missed
AustinAbro321 Oct 31, 2024
ad895a6
fix performing spelling
AustinAbro321 Oct 31, 2024
035ee8d
Merge branch 'main' into slog-init
AustinAbro321 Oct 31, 2024
3802315
adding init messages
AustinAbro321 Oct 31, 2024
dedb950
failed to delete injector resources
AustinAbro321 Nov 1, 2024
b5c56ac
chagne text
AustinAbro321 Nov 1, 2024
e1a4af6
fix config print
AustinAbro321 Nov 1, 2024
978bdad
Merge branch 'main' into slog-init
AustinAbro321 Nov 1, 2024
b43ff55
instructions for zarf creds
AustinAbro321 Nov 4, 2024
a0e4e9a
reword
AustinAbro321 Nov 4, 2024
2260b94
pull Zarf init package automatically
AustinAbro321 Nov 4, 2024
2088d4e
change init logs and pull logs
AustinAbro321 Nov 4, 2024
42fb448
revert to prompt on init
AustinAbro321 Nov 4, 2024
e0c7bd6
improve info message
AustinAbro321 Nov 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/pkg/cluster/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ import (

"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/internal/healthchecks"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/transform"
"github.com/zarf-dev/zarf/src/pkg/utils"
)

// StartInjection initializes a Zarf injection into the cluster.
func (c *Cluster) StartInjection(ctx context.Context, tmpDir, imagesDir string, injectorSeedSrcs []string) error {
l := logger.From(ctx)
start := time.Now()
// Stop any previous running injection before starting.
err := c.StopInjection(ctx)
if err != nil {
Expand All @@ -42,6 +45,7 @@ func (c *Cluster) StartInjection(ctx context.Context, tmpDir, imagesDir string,

spinner := message.NewProgressSpinner("Attempting to bootstrap the seed image into the cluster")
defer spinner.Stop()
l.Info("creating Zarf injector resources")

resReq := corev1.ResourceRequirements{
Requests: corev1.ResourceList{
Expand Down Expand Up @@ -123,11 +127,15 @@ func (c *Cluster) StartInjection(ctx context.Context, tmpDir, imagesDir string,
}

spinner.Success()
l.Debug("done with injection", "duration", time.Since(start))
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// StopInjection handles cleanup once the seed registry is up.
func (c *Cluster) StopInjection(ctx context.Context) error {
start := time.Now()
l := logger.From(ctx)
l.Debug("deleting injector resources")
err := c.Clientset.CoreV1().Pods(ZarfNamespaceName).Delete(ctx, "injector", metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return err
Expand Down Expand Up @@ -183,6 +191,7 @@ func (c *Cluster) StopInjection(ctx context.Context) error {
if err != nil {
return err
}
l.Debug("done deleting injector resources", "duration", time.Since(start))
return nil
}

Expand Down
13 changes: 11 additions & 2 deletions src/pkg/cluster/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ const (

// InitZarfState initializes the Zarf state with the given temporary directory and init configs.
func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitOptions) error {
l := logger.From(ctx)
spinner := message.NewProgressSpinner("Gathering cluster state information")
defer spinner.Stop()

// Attempt to load an existing state prior to init.
// NOTE: We are ignoring the error here because we don't really expect a state to exist yet.
spinner.Updatef("Checking cluster for existing Zarf deployment")
l.Debug("checking cluster for existing Zarf deployment")
state, err := c.LoadZarfState(ctx)
if err != nil && !kerrors.IsNotFound(err) {
return fmt.Errorf("failed to check for existing state: %w", err)
Expand All @@ -52,7 +54,7 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO
if state == nil {
state = &types.ZarfState{}
spinner.Updatef("New cluster, no prior Zarf deployments found")

l.Debug("new cluster, no prior Zarf deployments found")
if initOptions.ApplianceMode {
// If the K3s component is being deployed, skip distro detection.
state.Distro = DistroIsK3s
Expand All @@ -75,6 +77,7 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO

if state.Distro != DistroIsUnknown {
spinner.Updatef("Detected K8s distro %s", state.Distro)
l.Debug("Detected K8s distro", "name", state.Distro)
}

// Setup zarf agent PKI
Expand All @@ -90,7 +93,8 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO
}
// Mark existing namespaces as ignored for the zarf agent to prevent mutating resources we don't own.
for _, namespace := range namespaceList.Items {
spinner.Updatef("Marking existing namespace %s as ignored by Zarf Agent", namespace.Name)
l.Debug("marking namespace as ignored by Zarf Agent", "name", namespace.Name)

if namespace.Labels == nil {
// Ensure label map exists to avoid nil panic
namespace.Labels = make(map[string]string)
Expand All @@ -106,6 +110,7 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO

// Try to create the zarf namespace.
spinner.Updatef("Creating the Zarf namespace")
l.Debug("creating the Zarf namespace")
zarfNamespace := NewZarfManagedNamespace(ZarfNamespaceName)
err = func() error {
_, err := c.Clientset.CoreV1().Namespaces().Create(ctx, zarfNamespace, metav1.CreateOptions{})
Expand Down Expand Up @@ -154,17 +159,21 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO
initOptions.ArtifactServer.FillInEmptyValues()
state.ArtifactServer = initOptions.ArtifactServer
} else {
// TODO (@austinabro321) validate immediately in `zarf init` if these are set and not equal and error out if so
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
if helpers.IsNotZeroAndNotEqual(initOptions.GitServer, state.GitServer) {
message.Warn("Detected a change in Git Server init options on a re-init. Ignoring... To update run:")
message.ZarfCommand("tools update-creds git")
l.Warn("ignoring change in git sever init options on re-init, to update run `zarf tools update-creds git`")
}
if helpers.IsNotZeroAndNotEqual(initOptions.RegistryInfo, state.RegistryInfo) {
message.Warn("Detected a change in Image Registry init options on a re-init. Ignoring... To update run:")
message.ZarfCommand("tools update-creds registry")
l.Warn("ignoring change to registry init options on re-init, to update run `zarf tools update-creds registry`")
}
if helpers.IsNotZeroAndNotEqual(initOptions.ArtifactServer, state.ArtifactServer) {
message.Warn("Detected a change in Artifact Server init options on a re-init. Ignoring... To update run:")
message.ZarfCommand("tools update-creds artifact")
l.Warn("ignoring change to registry init options on re-init, to update run `zarf tools update-creds registry`")
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ var (
Error = Level(slog.LevelError) // 8
)

// String returns the string representation of the Level.
func (l Level) String() string {
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
switch l {
case Debug:
return "debug"
case Info:
return "info"
case Warn:
return "warn"
case Error:
return "error"
default:
return "unknown"
}
}

// validLevels is a set that provides an ergonomic way to check if a level is a member of the set.
var validLevels = map[Level]bool{
Debug: true,
Expand Down Expand Up @@ -99,6 +115,18 @@ var (
DestinationNone Destination = io.Discard
)

// can't define method on Destination type
func destinationString(d Destination) string {
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
switch {
case d == DestinationDefault:
return "os.Stderr"
case d == DestinationNone:
return "io.Discard"
default:
return "unknown"
}
}

// Config is configuration for a logger.
type Config struct {
// Level sets the log level. An empty value corresponds to Info aka 0.
Expand All @@ -107,6 +135,15 @@ type Config struct {
Destination
}

// LogValue of config
func (c Config) LogValue() slog.Value {
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
return slog.GroupValue(
slog.String("level", c.Level.String()),
slog.Any("format", c.Format),
slog.Any("Destination", destinationString(c.Destination)),
)
}

// ConfigDefault returns a Config with defaults like Text formatting at Info level writing to Stderr.
func ConfigDefault() Config {
return Config{
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (p *Packager) deployInitComponent(ctx context.Context, component v1alpha1.Z
// Do cleanup for when we inject the seed registry during initialization
if isSeedRegistry {
if err := p.cluster.StopInjection(ctx); err != nil {
return nil, fmt.Errorf("unable to seed the Zarf Registry: %w", err)
return nil, fmt.Errorf("failed to delete injector resources: %w", err)
}
}

Expand Down
Loading