Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ linters:
- all
- '-ST1003' # waiting for package name will be fixed (underscores)
- '-QF1008' # not need to fix; we understand how to call nested structs
- '-SA1019' # remove when fix logger
revive:
rules:
- name: var-naming # waiting for package name will be fixed (incorrect naming)
Expand Down
20 changes: 12 additions & 8 deletions cmd/dmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ package main
import (
"context"
"errors"
"log/slog"
"os"
"runtime"
"runtime/pprof"

"github.com/fatih/color"

"github.com/deckhouse/deckhouse/pkg/log"

"github.com/deckhouse/dmt/internal/flags"
"github.com/deckhouse/dmt/internal/fsutils"
"github.com/deckhouse/dmt/internal/logger"
"github.com/deckhouse/dmt/internal/manager"
"github.com/deckhouse/dmt/internal/metrics"
"github.com/deckhouse/dmt/internal/version"
Expand All @@ -40,17 +42,17 @@ func main() {

func runLint(dir string) error {
if flags.PprofFile != "" {
logger.InfoF("Profiling enabled. Profile file: %s", flags.PprofFile)
log.Info("Profiling enabled", slog.String("file", flags.PprofFile))
defer func() {
pproFile, err := fsutils.ExpandDir(flags.PprofFile)
if err != nil {
logger.ErrorF("could not get current working directory: %s", err)
log.Error("could not get current working directory", log.Err(err))
return
}
logger.InfoF("Writing memory profile to %s", pproFile)
log.Info("Writing memory profile", slog.String("file", pproFile))
f, err := os.Create(pproFile)
if err != nil {
logger.ErrorF("could not create memory profile: %s", err)
log.Error("could not create memory profile", log.Err(err))
return
}
defer f.Close()
Expand All @@ -59,17 +61,19 @@ func runLint(dir string) error {
// Alternatively, use Lookup("heap") for a profile
// that has inuse_space as the default index.
if err := pprof.Lookup("allocs").WriteTo(f, 0); err != nil {
logger.ErrorF("could not write memory profile: %s", err)
log.Error("could not write memory profile", log.Err(err))
return
}
}()
}
// enable color output for Github actions, do not remove it
color.NoColor = false
logger.InfoF("DMT version: %s, Commit: %s, Date: %s", version.Version, version.Commit, version.Date)
log.Info("DMT version", slog.String("version", version.Version), slog.String("commit", version.Commit), slog.String("date", version.Date))

cfg, err := config.NewDefaultRootConfig(dir)
logger.CheckErr(err)
if err != nil {
log.Fatal("default root config", log.Err(err)) //nolint:gocritic
}

// init metrics storage, should be done before running manager
metrics.GetClient(dir)
Expand Down
17 changes: 12 additions & 5 deletions cmd/dmt/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"errors"
"fmt"
"log/slog"
"os"
"regexp"
"strings"
Expand All @@ -28,10 +29,11 @@ import (
"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/deckhouse/deckhouse/pkg/log"

"github.com/deckhouse/dmt/internal/bootstrap"
"github.com/deckhouse/dmt/internal/flags"
"github.com/deckhouse/dmt/internal/fsutils"
"github.com/deckhouse/dmt/internal/logger"
"github.com/deckhouse/dmt/internal/version"
)

Expand All @@ -49,7 +51,12 @@ func execute() {
PersistentPreRun: func(_ *cobra.Command, _ []string) {
// TODO: move to separate package
flags.Version = version.Version
logger.InitLogger(os.Stdout, flags.LogLevel)
lvl := log.LogLevelFromStr(flags.LogLevel)
logger := log.NewLogger(
log.WithLevel(slog.Level(lvl)),
log.WithHandlerType(log.TextHandlerType),
)
log.SetDefault(logger)
},
}

Expand Down Expand Up @@ -170,15 +177,15 @@ func runLintMultiple(dirs []string) error {
for _, dir := range dirs {
expandedDir, err := fsutils.ExpandDir(dir)
if err != nil {
logger.ErrorF("Error expanding directory %s: %v", dir, err)
log.Error("Error expanding directory", slog.String("dir", dir), log.Err(err))
continue
}

logger.InfoF("Processing directory: %s", expandedDir)
log.Info("Processing directory", slog.String("directory", expandedDir))

// Run lint for this directory as a separate execution
if err := runLint(expandedDir); err != nil {
logger.ErrorF("Error processing directory %s: %v", expandedDir, err)
log.Error("Error processing directory", slog.String("directory", expandedDir), log.Err(err))
hasErrors = true
// Continue processing other directories even if one fails
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/Masterminds/semver/v3 v3.3.1
github.com/Masterminds/sprig/v3 v3.3.0
github.com/bmatcuk/doublestar v1.3.4
github.com/deckhouse/deckhouse/pkg/log v0.1.1
github.com/fatih/color v1.16.0
github.com/flant/addon-operator v1.5.0
github.com/go-openapi/spec v0.21.0
Expand Down Expand Up @@ -50,6 +51,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 // indirect
github.com/DataDog/gostackparse v0.7.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/Code-Hex/go-generics-cache v1.5.1 h1:6vhZGc5M7Y/YD8cIUcY8kcuQLB4cHR7U+0KMqAA0KcU=
github.com/Code-Hex/go-generics-cache v1.5.1/go.mod h1:qxcC9kRVrct9rHeiYpFWSoW1vxyillCVzX13KZG8dl4=
github.com/DataDog/gostackparse v0.7.0 h1:i7dLkXHvYzHV308hnkvVGDL3BR4FWl7IsXNPz/IGQh4=
github.com/DataDog/gostackparse v0.7.0/go.mod h1:lTfqcJKqS9KnXQGnyQMCugq3u1FP6UZMfWR0aitKFMM=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4=
Expand Down Expand Up @@ -63,6 +65,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/deckhouse/pkg/log v0.1.1 h1:qHpMyj9coob+VfP3Qv+eIBa0pXFLn1FFRJztyryILB0=
github.com/deckhouse/deckhouse/pkg/log v0.1.1/go.mod h1:pbAxTSDcPmwyl3wwKDcEB3qdxHnRxqTV+J0K+sha8bw=
github.com/dennwc/varint v1.0.0 h1:kGNFFSSw8ToIy3obO/kKr8U9GZYUAxQEVuix4zfDWzE=
github.com/dennwc/varint v1.0.0/go.mod h1:hnItb35rvZvJrbTALZtY/iQfDs48JKRG1RPpgziApxA=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
Expand Down
22 changes: 12 additions & 10 deletions internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
Expand All @@ -30,8 +31,9 @@ import (
"github.com/iancoleman/strcase"
"gopkg.in/yaml.v3"

"github.com/deckhouse/deckhouse/pkg/log"

"github.com/deckhouse/dmt/internal/fsutils"
"github.com/deckhouse/dmt/internal/logger"
)

const (
Expand Down Expand Up @@ -61,7 +63,7 @@ type BootstrapConfig struct {

// RunBootstrap initializes a new module with the given configuration
func RunBootstrap(config BootstrapConfig) error {
logger.InfoF("Bootstrap type: %s", config.RepositoryType)
log.Info("Bootstrap type", slog.String("type", config.RepositoryType))

// if config.Directory does not exist, create it
if _, err := os.Stat(config.Directory); os.IsNotExist(err) {
Expand All @@ -75,7 +77,7 @@ func RunBootstrap(config BootstrapConfig) error {
return fmt.Errorf("failed to get absolute directory path: %w", err)
}

logger.InfoF("Using directory: %s", absDirectory)
log.Info("Using directory", slog.String("directory", absDirectory))

// Check if directory is empty
if err := checkDirectoryEmpty(absDirectory); err != nil {
Expand Down Expand Up @@ -114,7 +116,7 @@ func RunBootstrap(config BootstrapConfig) error {
}
}

logger.InfoF("Bootstrap completed successfully")
log.Info("Bootstrap completed successfully")

return nil
}
Expand Down Expand Up @@ -145,7 +147,7 @@ func checkDirectoryEmpty(absDirectory string) error {
return fmt.Errorf("directory is not empty. Please run bootstrap in an empty directory")
}

logger.DebugF("Directory is empty, proceeding with bootstrap")
log.Debug("Directory is empty, proceeding with bootstrap")

return nil
}
Expand Down Expand Up @@ -266,7 +268,7 @@ func downloadAndExtractTemplate(repositoryURL, directory string) error {

// downloadFile downloads a file from URL to local path with timeout
func downloadFile(url, targetPath string) error {
logger.InfoF("Downloading template from: %s", url)
log.Info("Downloading template", slog.String("url", url))

ctx, cancel := context.WithTimeout(context.Background(), downloadTimeout)
defer cancel()
Expand Down Expand Up @@ -299,13 +301,13 @@ func downloadFile(url, targetPath string) error {
return fmt.Errorf("failed to write file: %w", err)
}

logger.InfoF("Template downloaded successfully")
log.Info("Template downloaded successfully")
return nil
}

// extractZip extracts a zip file to the specified directory
func extractZip(zipPath, extractDir string) error {
logger.DebugF("Extracting template archive")
log.Debug("Extracting template archive")

reader, err := zip.OpenReader(zipPath)
if err != nil {
Expand Down Expand Up @@ -380,7 +382,7 @@ func extractZip(zipPath, extractDir string) error {
}
}

logger.DebugF("Template extracted successfully")
log.Debug("Template extracted successfully")
return nil
}

Expand Down Expand Up @@ -418,7 +420,7 @@ func moveExtractedContent(tempDir, directory string) error {
}
}

logger.DebugF("Template files moved to current directory")
log.Debug("Template files moved to current directory")
return nil
}

Expand Down
11 changes: 6 additions & 5 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package engine
import (
"errors"
"fmt"
"log/slog"
"path"
"path/filepath"
"regexp"
Expand All @@ -29,7 +30,7 @@ import (
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"

"github.com/deckhouse/dmt/internal/logger"
"github.com/deckhouse/deckhouse/pkg/log"
)

// Engine is an implementation of the Helm rendering implementation for templates.
Expand Down Expand Up @@ -177,15 +178,15 @@ func (e Engine) initFuncMap(t *template.Template) {
if val == nil {
if e.LintMode {
// Don't fail on missing required values when linting
logger.WarnF("[WARNING] Missing required value: %s", warn)
log.Warn("[WARNING] Missing required value", slog.String("value", warn))
return "", nil
}
return val, errors.New(warnWrap(warn))
} else if _, ok := val.(string); ok {
if val == "" {
if e.LintMode {
// Don't fail on missing required values when linting
logger.ErrorF("[ERROR] Missing required value: %s", warn)
log.Error("[ERROR] Missing required value", slog.String("value", warn))
return "", nil
}
return val, errors.New(warnWrap(warn))
Expand All @@ -198,7 +199,7 @@ func (e Engine) initFuncMap(t *template.Template) {
funcMap["fail"] = func(msg string) (string, error) {
if e.LintMode {
// Don't fail when linting
logger.WarnF("[WARNING] Fail: %s", msg)
log.Warn("[WARNING] Fail", slog.String("message", msg))
return "", nil
}
return "", errors.New(warnWrap(msg))
Expand Down Expand Up @@ -287,7 +288,7 @@ func (e Engine) render(tpls map[string]renderable) (rendered map[string]string,
}

if e.LintMode && isRecoverableNilError {
logger.ErrorF("[LINT] Template %s encountered a nil pointer access during execution: %v. Using partially rendered output.", filename, executeErr)
log.Error("[LINT] Template encountered a nil pointer access during execution. Using partially rendered output", slog.String("template", filename), slog.Any("error", executeErr))
rendered[filename] = getRenderedContent(&buf)
} else {
// For other errors, or if not in LintMode, this is a hard error.
Expand Down
10 changes: 5 additions & 5 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package flags
import (
"github.com/spf13/pflag"

"github.com/deckhouse/dmt/internal/logger"
"github.com/deckhouse/deckhouse/pkg/log"
)

const (
Expand Down Expand Up @@ -72,27 +72,27 @@ func InitLintFlagSet() *pflag.FlagSet {
lint.BoolVarP(&HideWarnings, "hide-warnings", "", false, "hide warnings")
err := lint.MarkHidden("hide-warnings")
if err != nil {
logger.ErrorF("mark hidden flag 'hide-warnings' is failed")
log.Error("mark hidden flag 'hide-warnings' is failed")
}

// make path absolute
lint.BoolVarP(&AbsPath, "abs-path", "", false, "make paths absolute")
err = lint.MarkHidden("abs-path")
if err != nil {
logger.ErrorF("mark hidden flag 'abs-path' is failed")
log.Error("mark hidden flag 'abs-path' is failed")
}

// show ignored errors
lint.BoolVarP(&ShowIgnored, "show-ignored", "", false, "show ignored errors")
err = lint.MarkHidden("show-ignored")
if err != nil {
logger.ErrorF("mark hidden flag 'show-ignored' is failed")
log.Error("mark hidden flag 'show-ignored' is failed")
}

lint.BoolVarP(&ShowDocumentation, "doc", "", false, "show documentation links")
err = lint.MarkHidden("doc")
if err != nil {
logger.ErrorF("mark hidden flag 'doc' is failed")
log.Error("mark hidden flag 'doc' is failed")
}

return lint
Expand Down
Loading