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

Improve Generator Logging #2964

Merged
merged 29 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
146e683
Plumb new logging into gen-types
theunrepentantgeek Apr 20, 2023
f7dd2a4
Make log available to pipeline stages
theunrepentantgeek Apr 20, 2023
5326e7b
Update pipeline stages
theunrepentantgeek Apr 20, 2023
65ca935
Update Swagger parsing
theunrepentantgeek Apr 21, 2023
5baf342
Catch panics during stage execution
theunrepentantgeek May 11, 2023
822c0fa
Remove logging we don't need
theunrepentantgeek May 12, 2023
b8a6639
Fix deprecations
theunrepentantgeek May 12, 2023
380d395
Turn logs into panics
theunrepentantgeek May 12, 2023
86f0a1b
Generate logs during initial loading
theunrepentantgeek May 12, 2023
d3c8052
Code Gardening
theunrepentantgeek May 12, 2023
37151d7
Remove klog at entry points
theunrepentantgeek May 12, 2023
fd59e61
Add new packages and update dependencies
theunrepentantgeek Apr 20, 2023
d6f6a23
Update tests
theunrepentantgeek May 12, 2023
88b6850
Address lint issues
theunrepentantgeek May 12, 2023
f461998
Fix specifier
theunrepentantgeek May 12, 2023
a2b9d0f
Merge branch 'main' into improve/generator-logging
theunrepentantgeek May 14, 2023
4a1ea14
Merge branch 'main' into improve/generator-logging
theunrepentantgeek May 16, 2023
7bbd87c
Merge branch 'main' into improve/generator-logging
theunrepentantgeek May 17, 2023
8345517
Merge branch 'main' into improve/generator-logging
theunrepentantgeek May 18, 2023
49034cc
Merge branch 'main' into improve/generator-logging
theunrepentantgeek May 21, 2023
cabd956
Address PR feedback
theunrepentantgeek May 23, 2023
902fd2b
Fixup tests
theunrepentantgeek May 23, 2023
7b0742c
Merge branch 'main' into improve/generator-logging
theunrepentantgeek May 23, 2023
61e39f7
Merge branch 'improve/generator-logging' of https://github.com/Azure/…
theunrepentantgeek May 23, 2023
0e2eb0a
Merge branch 'main' into improve/generator-logging
theunrepentantgeek May 24, 2023
d99cc56
Fix merge
theunrepentantgeek May 24, 2023
33681c0
Panic if validations use unsupported float values
theunrepentantgeek May 25, 2023
9baba9d
Add --trace flag
theunrepentantgeek May 25, 2023
04d6e76
Add logging
theunrepentantgeek May 25, 2023
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
53 changes: 30 additions & 23 deletions v2/tools/generator/gen_kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"

"github.com/Azure/azure-service-operator/v2/tools/generator/internal/kustomization"
)
Expand All @@ -34,22 +31,28 @@ func NewGenKustomizeCommand() (*cobra.Command, error) {
const bases = "bases"
const patches = "patches"

log := CreateLogger()

// We have an expectation that the folder structure is: .../config/crd/generated/bases and .../config/crd/generated/patches
basesPath := filepath.Join(crdPath, bases)
patchesPath := filepath.Join(crdPath, patches)

destination := filepath.Join(crdPath, "kustomization.yaml")

klog.V(3).Infof("Scanning %q for resources", basesPath)
log.Info(
"Scanning for resources",
"basePath", basesPath)

files, err := ioutil.ReadDir(basesPath)
files, err := os.ReadDir(basesPath)
if err != nil {
return logAndExtractStack(fmt.Sprintf("Unable to scan folder %q", basesPath), err)
log.Error(err, "Unable to scan folder", "folder", basesPath)
return err
}

err = os.MkdirAll(patchesPath, os.ModePerm)
if err != nil {
return logAndExtractStack(fmt.Sprintf("Unable to create output folder %s", patchesPath), err)
log.Error(err, "Unable to create output folder", "folder", patchesPath)
return err
}

var errs []error
Expand All @@ -60,7 +63,9 @@ func NewGenKustomizeCommand() (*cobra.Command, error) {
continue
}

klog.V(3).Infof("Found resource file %s", f.Name())
log.V(1).Info(
"Found resource file",
"file", f.Name())

patchFile := "webhook-conversion-" + f.Name()
var def *kustomization.ResourceDefinition
Expand All @@ -70,7 +75,9 @@ func NewGenKustomizeCommand() (*cobra.Command, error) {
continue
}

klog.V(4).Infof("Resource is %q", def.Name())
log.V(1).Info(
"Loaded Resource",
"name", def.Name())

patch := kustomization.NewConversionPatchFile(def.Name())
err = patch.Save(filepath.Join(patchesPath, patchFile))
Expand All @@ -84,17 +91,28 @@ func NewGenKustomizeCommand() (*cobra.Command, error) {
}

if len(errs) > 0 {
return logAndExtractStack("Error creating conversion patches", kerrors.NewAggregate(errs))
err = kerrors.NewAggregate(errs)
log.Error(
err,
"Error creating conversion patches")
return err
}

if len(result.Resources) == 0 {
err = errors.Errorf("no files found in %q", basesPath)
return logAndExtractStack("No CRD files found", err)
log.Error(
err,
"No CRD files found")
return err
}

err = result.Save(destination)
if err != nil {
return logAndExtractStack("Error generating "+destination, err)
log.Error(
err,
"Error generating",
"destination", destination)
return err
}

return nil
Expand All @@ -103,14 +121,3 @@ func NewGenKustomizeCommand() (*cobra.Command, error) {

return cmd, nil
}

func logAndExtractStack(str string, err error) error {
klog.Errorf("%s:\n%s\n", str, err)
if tr, ok := findDeepestTrace(err); ok {
matthchr marked this conversation as resolved.
Show resolved Hide resolved
for _, fr := range tr {
klog.Errorf("%n (%s:%d)", fr, fr, fr)
}
}

return err
}
22 changes: 14 additions & 8 deletions v2/tools/generator/gen_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/Azure/azure-service-operator/v2/tools/generator/internal/codegen"
)
Expand All @@ -32,32 +31,39 @@ func NewGenTypesCommand() (*cobra.Command, error) {
configFile := args[0]
ctx := cmd.Context()

cg, err := codegen.NewCodeGeneratorFromConfigFile(configFile)
log := CreateLogger()

cg, err := codegen.NewCodeGeneratorFromConfigFile(configFile, log)
if err != nil {
klog.Errorf("Error creating code generator: %s\n", err)
log.Error(err, "Error creating code generator")
return err
}

if debugMode != nil && *debugMode != "" {
var tmpDir string
tmpDir, err = ioutil.TempDir("", createDebugPrefix(*debugMode))
if err != nil {
klog.Errorf("Error creating temporary directory: %s\n", err)
log.Error(err, "Error creating temporary directory")
return err
}

klog.V(0).Infof("Debug output will be written to the folder %s\n", tmpDir)
log.Info(
"Debug output will be written",
"folder", tmpDir)
cg.UseDebugMode(*debugMode, tmpDir)
defer func() {
// Write the debug folder again so the user doesn't have to scroll back
klog.V(0).Infof("Debug output is available in folder %s\n", tmpDir)
log.Info(
"Debug output available",
"folder", tmpDir)
}()
}

err = cg.Generate(ctx)
err = cg.Generate(ctx, log)

if err != nil {
return logAndExtractStack("Error during code generation", err)
log.Error(err, "Error generating code generation")
return err
}

return nil
Expand Down
47 changes: 25 additions & 22 deletions v2/tools/generator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,50 @@ replace github.com/Azure/azure-service-operator/v2 => ../../
replace github.com/xeipuuv/gojsonschema => github.com/devigned/gojsonschema v1.2.1-0.20191231010529-c593123f1e5d

require (
github.com/Azure/azure-service-operator/v2 v2.0.0-00010101000000-000000000000
github.com/Azure/azure-service-operator/v2 v2.0.0
github.com/bmatcuk/doublestar v1.3.4
github.com/dave/dst v0.26.2
github.com/dave/dst v0.27.2
github.com/devigned/tab v0.1.1
github.com/go-openapi/jsonpointer v0.19.5
github.com/go-openapi/spec v0.20.4
github.com/go-logr/logr v1.2.4
github.com/go-logr/zerologr v1.2.3
github.com/go-openapi/jsonpointer v0.19.6
github.com/go-openapi/spec v0.20.9
github.com/gobuffalo/flect v1.0.2
github.com/google/go-cmp v0.5.9
github.com/hbollon/go-edlib v1.6.0
github.com/kr/pretty v0.3.0
github.com/kr/pretty v0.3.1
github.com/kylelemons/godebug v1.1.0
github.com/leanovate/gopter v0.2.9
github.com/onsi/gomega v1.20.1
github.com/onsi/gomega v1.27.6
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.29.1
github.com/sebdah/goldie/v2 v2.5.3
github.com/spf13/cobra v1.6.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd
golang.org/x/mod v0.8.0
golang.org/x/net v0.9.0
golang.org/x/sync v0.1.0
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
golang.org/x/mod v0.10.0
golang.org/x/net v0.10.0
golang.org/x/sync v0.2.0
golang.org/x/text v0.9.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.25.2
k8s.io/klog/v2 v2.80.1
k8s.io/apimachinery v0.27.1
)

require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/tools v0.9.1 // indirect
)
Loading