Skip to content

Commit

Permalink
refactor: remove unused message functions and verbose logging (#2712)
Browse files Browse the repository at this point in the history
Co-authored-by: Austin Abro <37223396+AustinAbro321@users.noreply.github.com>
  • Loading branch information
phillebaba and AustinAbro321 authored Jul 10, 2024
1 parent d05c409 commit 2ef1216
Show file tree
Hide file tree
Showing 18 changed files with 17 additions and 74 deletions.
7 changes: 6 additions & 1 deletion src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/types"
"github.com/mholt/archiver/v3"
"github.com/pterm/pterm"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -110,7 +112,10 @@ var devTransformGitLinksCmd = &cobra.Command{
processedText := transform.MutateGitURLsInText(message.Warnf, pkgConfig.InitOpts.GitServer.Address, text, pkgConfig.InitOpts.GitServer.PushUsername)

// Print the differences
message.PrintDiff(text, processedText)
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(text, processedText, true)
diffs = dmp.DiffCleanupSemantic(diffs)
pterm.Println(dmp.DiffPrettyText(diffs))

// Ask the user before this destructive action
confirm := false
Expand Down
1 change: 0 additions & 1 deletion src/internal/agent/hooks/argocd-application.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ type ApplicationSource struct {

// NewApplicationMutationHook creates a new instance of the ArgoCD Application mutation hook.
func NewApplicationMutationHook(ctx context.Context, cluster *cluster.Cluster) operations.Hook {
message.Debug("hooks.NewApplicationMutationHook()")
return operations.Hook{
Create: func(r *v1.AdmissionRequest) (*operations.Result, error) {
return mutateApplication(ctx, r, cluster)
Expand Down
1 change: 0 additions & 1 deletion src/internal/agent/hooks/argocd-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type RepoCreds struct {

// NewRepositorySecretMutationHook creates a new instance of the ArgoCD repository secret mutation hook.
func NewRepositorySecretMutationHook(ctx context.Context, cluster *cluster.Cluster) operations.Hook {
message.Debug("hooks.NewRepositoryMutationHook()")
return operations.Hook{
Create: func(r *v1.AdmissionRequest) (*operations.Result, error) {
return mutateRepositorySecret(ctx, r, cluster)
Expand Down
1 change: 0 additions & 1 deletion src/internal/agent/hooks/flux-gitrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const AgentErrTransformGitURL = "unable to transform the git url"

// NewGitRepositoryMutationHook creates a new instance of the git repo mutation hook.
func NewGitRepositoryMutationHook(ctx context.Context, cluster *cluster.Cluster) operations.Hook {
message.Debug("hooks.NewGitRepositoryMutationHook()")
return operations.Hook{
Create: func(r *v1.AdmissionRequest) (*operations.Result, error) {
return mutateGitRepo(ctx, r, cluster)
Expand Down
1 change: 0 additions & 1 deletion src/internal/agent/hooks/flux-helmrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

// NewHelmRepositoryMutationHook creates a new instance of the helm repo mutation hook.
func NewHelmRepositoryMutationHook(ctx context.Context, cluster *cluster.Cluster) operations.Hook {
message.Debug("hooks.NewHelmRepositoryMutationHook()")
return operations.Hook{
Create: func(r *v1.AdmissionRequest) (*operations.Result, error) {
return mutateHelmRepo(ctx, r, cluster)
Expand Down
1 change: 0 additions & 1 deletion src/internal/agent/hooks/flux-ocirepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

// NewOCIRepositoryMutationHook creates a new instance of the oci repo mutation hook.
func NewOCIRepositoryMutationHook(ctx context.Context, cluster *cluster.Cluster) operations.Hook {
message.Debug("hooks.NewOCIRepositoryMutationHook()")
return operations.Hook{
Create: func(r *v1.AdmissionRequest) (*operations.Result, error) {
return mutateOCIRepo(ctx, r, cluster)
Expand Down
4 changes: 0 additions & 4 deletions src/internal/agent/hooks/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

// NewPodMutationHook creates a new instance of pods mutation hook.
func NewPodMutationHook(ctx context.Context, cluster *cluster.Cluster) operations.Hook {
message.Debug("hooks.NewMutationHook()")
return operations.Hook{
Create: func(r *v1.AdmissionRequest) (*operations.Result, error) {
return mutatePod(ctx, r, cluster)
Expand All @@ -34,7 +33,6 @@ func NewPodMutationHook(ctx context.Context, cluster *cluster.Cluster) operation
}

func parsePod(object []byte) (*corev1.Pod, error) {
message.Debugf("pods.parsePod(%s)", string(object))
var pod corev1.Pod
if err := json.Unmarshal(object, &pod); err != nil {
return nil, err
Expand All @@ -43,8 +41,6 @@ func parsePod(object []byte) (*corev1.Pod, error) {
}

func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (*operations.Result, error) {
message.Debugf("hooks.mutatePod()(*v1.AdmissionRequest) - %#v , %s/%s: %#v", r.Kind, r.Namespace, r.Name, r.Operation)

pod, err := parsePod(r.Object.Raw)
if err != nil {
return nil, fmt.Errorf(lang.AgentErrParsePod, err)
Expand Down
3 changes: 0 additions & 3 deletions src/internal/agent/http/admission/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ func NewHandler() *Handler {

// Serve returns an http.HandlerFunc for an admission webhook.
func (h *Handler) Serve(hook operations.Hook) http.HandlerFunc {
message.Debugf("http.Serve(%#v)", hook)
return func(w http.ResponseWriter, r *http.Request) {
message.Debugf("http.Serve()(writer, %#v)", r.URL)

w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPost {
http.Error(w, lang.AgentErrInvalidMethod, http.StatusMethodNotAllowed)
Expand Down
2 changes: 0 additions & 2 deletions src/internal/agent/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const (

// StartWebhook launches the Zarf agent mutating webhook in the cluster.
func StartWebhook(ctx context.Context) error {
message.Debug("agent.StartWebhook()")
srv, err := agentHttp.NewAdmissionServer(ctx, httpPort)
if err != nil {
return err
Expand All @@ -39,7 +38,6 @@ func StartWebhook(ctx context.Context) error {

// StartHTTPProxy launches the zarf agent proxy in the cluster.
func StartHTTPProxy(ctx context.Context) error {
message.Debug("agent.StartHttpProxy()")
return startServer(ctx, agentHttp.NewProxyServer(httpPort))
}

Expand Down
7 changes: 6 additions & 1 deletion src/pkg/cluster/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cluster

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -46,8 +47,12 @@ func (c *Cluster) HandleDataInjection(ctx context.Context, wg *sync.WaitGroup, d

// Pod filter to ensure we only use the current deployment's pods
podFilterByInitContainer := func(pod corev1.Pod) bool {
b, err := json.Marshal(pod)
if err != nil {
return false
}
// Look everywhere in the pod for a matching data injection marker
return strings.Contains(message.JSONValue(pod), config.GetDataInjectionMarker())
return strings.Contains(string(b), config.GetDataInjectionMarker())
}

// Get the OS shell to execute commands in
Expand Down
2 changes: 0 additions & 2 deletions src/pkg/cluster/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ func (c *Cluster) GenerateRegistryPullCreds(ctx context.Context, namespace, name

// GenerateGitPullCreds generates a secret containing the git credentials.
func (c *Cluster) GenerateGitPullCreds(namespace, name string, gitServerInfo types.GitServerInfo) *corev1.Secret {
message.Debugf("k8s.GenerateGitPullCreds(%s, %s, gitServerInfo)", namespace, name)

gitServerSecret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Expand Down
6 changes: 5 additions & 1 deletion src/pkg/cluster/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ func (c *Cluster) debugPrintZarfState(state *types.ZarfState) {
// this is a shallow copy, nested pointers WILL NOT be copied
oldState := *state
sanitized := c.sanitizeZarfState(&oldState)
message.Debugf("ZarfState - %s", message.JSONValue(sanitized))
b, err := json.MarshalIndent(sanitized, "", " ")
if err != nil {
return
}
message.Debugf("ZarfState - %s", string(b))
}

// SaveZarfState takes a given state and persists it to the Zarf/zarf-state secret.
Expand Down
2 changes: 0 additions & 2 deletions src/pkg/message/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (

// PrintConnectStringTable prints a table of connect strings.
func PrintConnectStringTable(connectStrings types.ConnectStrings) {
Debugf("message.PrintConnectStringTable(%#v)", connectStrings)

if len(connectStrings) > 0 {
connectData := [][]string{}
// Loop over each connectStrings and convert to a string matrix
Expand Down
36 changes: 0 additions & 36 deletions src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
package message

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
Expand All @@ -17,7 +15,6 @@ import (
"github.com/defenseunicorns/zarf/src/config"
"github.com/fatih/color"
"github.com/pterm/pterm"
"github.com/sergi/go-diff/diffmatchpatch"
)

// LogLevel is the level of logging to display.
Expand Down Expand Up @@ -97,11 +94,6 @@ func SetLogLevel(lvl LogLevel) {
}
}

// GetLogLevel returns the current log level.
func GetLogLevel() LogLevel {
return logLevel
}

// DisableColor disables color in output
func DisableColor() {
pterm.DisableColor()
Expand Down Expand Up @@ -129,14 +121,6 @@ func Debugf(format string, a ...any) {
debugPrinter(2, message)
}

// ErrorWebf prints an error message and returns a web response.
func ErrorWebf(err any, w http.ResponseWriter, format string, a ...any) {
debugPrinter(2, err)
message := fmt.Sprintf(format, a...)
Warn(message)
http.Error(w, message, http.StatusInternalServerError)
}

// Warn prints a warning message.
func Warn(message string) {
Warnf("%s", message)
Expand Down Expand Up @@ -242,15 +226,6 @@ func HorizontalRule() {
pterm.Println(RuleLine)
}

// JSONValue prints any value as JSON.
func JSONValue(value any) string {
bytes, err := json.MarshalIndent(value, "", " ")
if err != nil {
debugPrinter(2, fmt.Sprintf("ERROR marshalling json: %s", err.Error()))
}
return string(bytes)
}

// Paragraph formats text into a paragraph matching the TermWidth
func Paragraph(format string, a ...any) string {
return Paragraphn(TermWidth, format, a...)
Expand All @@ -269,17 +244,6 @@ func Paragraphn(n int, format string, a ...any) string {
return strings.Join(formattedLines, "\n")
}

// PrintDiff prints the differences between a and b with a as original and b as new
func PrintDiff(textA, textB string) {
dmp := diffmatchpatch.New()

diffs := dmp.DiffMain(textA, textB, true)

diffs = dmp.DiffCleanupSemantic(diffs)

pterm.Println(dmp.DiffPrettyText(diffs))
}

// Table prints a padded table containing the specified header and data
func Table(header []string, data [][]string) {
pterm.Println()
Expand Down
2 changes: 0 additions & 2 deletions src/pkg/utils/cosign.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ func Sget(ctx context.Context, image, key string, out io.Writer) error {
// Remove the custom protocol header from the url
image = strings.TrimPrefix(image, helpers.SGETURLPrefix)

message.Debugf("utils.Sget: image=%s, key=%s", image, key)

spinner := message.NewProgressSpinner("Loading signed file %s", image)
defer spinner.Stop()

Expand Down
2 changes: 0 additions & 2 deletions src/pkg/utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func MakeTempDir(basePath string) (string, error) {

// GetFinalExecutablePath returns the absolute path to the current executable, following any symlinks along the way.
func GetFinalExecutablePath() (string, error) {
message.Debug("utils.GetExecutablePath()")

binaryPath, err := os.Executable()
if err != nil {
return "", err
Expand Down
2 changes: 0 additions & 2 deletions src/pkg/utils/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ func AddRootHint(hints map[string]string, rootKey string, hintText string) map[s

// ReadYaml reads a yaml file and unmarshals it into a given config.
func ReadYaml(path string, destConfig any) error {
message.Debugf("Reading YAML at %s", path)

file, err := os.ReadFile(path)
if err != nil {
return err
Expand Down
11 changes: 0 additions & 11 deletions src/test/e2e/05_tarball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -98,10 +97,6 @@ func TestReproducibleTarballs(t *testing.T) {
err = utils.ReadYaml(filepath.Join(unpack1, layout.ZarfYAML), &pkg1)
require.NoError(t, err)

b, err := os.ReadFile(filepath.Join(unpack1, layout.Checksums))
require.NoError(t, err)
checksums1 := string(b)

e2e.CleanFiles(unpack1, tb)

stdOut, stdErr, err = e2e.Zarf("package", "create", createPath, "--confirm", "--output", tmp)
Expand All @@ -114,11 +109,5 @@ func TestReproducibleTarballs(t *testing.T) {
err = utils.ReadYaml(filepath.Join(unpack2, layout.ZarfYAML), &pkg2)
require.NoError(t, err)

b, err = os.ReadFile(filepath.Join(unpack2, layout.Checksums))
require.NoError(t, err)
checksums2 := string(b)

message.PrintDiff(checksums1, checksums2)

require.Equal(t, pkg1.Metadata.AggregateChecksum, pkg2.Metadata.AggregateChecksum)
}

0 comments on commit 2ef1216

Please sign in to comment.