Skip to content

Commit

Permalink
replace global logrus instance usage with local logrus instance
Browse files Browse the repository at this point in the history
Signed-off-by: Jose R. Gonzalez <[email protected]>
  • Loading branch information
komish committed Nov 22, 2022
1 parent 89bc38e commit 3560a24
Show file tree
Hide file tree
Showing 40 changed files with 278 additions and 214 deletions.
10 changes: 5 additions & 5 deletions certification/pyxis/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io"
"net/http"

log "github.com/sirupsen/logrus"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
)

// certificationInputBuilder facilitates the building of CertificationInput for
Expand Down Expand Up @@ -68,7 +68,7 @@ func (b *certificationInputBuilder) Finalize() (*CertificationInput, error) {
// Errors are logged, but will not halt execution.
func (b *certificationInputBuilder) WithCertImage(r io.Reader) *certificationInputBuilder {
if err := b.storeCertImage(r); err != nil {
log.Error(fmt.Errorf("cert image could not be stored: %v", err))
log.L().Error(fmt.Errorf("cert image could not be stored: %v", err))
}

return b
Expand All @@ -78,7 +78,7 @@ func (b *certificationInputBuilder) WithCertImage(r io.Reader) *certificationInp
// Errors are logged, but will not halt execution.
func (b *certificationInputBuilder) WithPreflightResults(r io.Reader) *certificationInputBuilder {
if err := b.storePreflightResults(r); err != nil {
log.Error(fmt.Errorf("preflight results could not be stored: %v", err))
log.L().Error(fmt.Errorf("preflight results could not be stored: %v", err))
}

return b
Expand All @@ -88,7 +88,7 @@ func (b *certificationInputBuilder) WithPreflightResults(r io.Reader) *certifica
// Errors are logged, but will not halt execution.
func (b *certificationInputBuilder) WithRPMManifest(r io.Reader) *certificationInputBuilder {
if err := b.storeRPMManifest(r); err != nil {
log.Error(fmt.Errorf("rpm manifest could not be stored: %v", err))
log.L().Error(fmt.Errorf("rpm manifest could not be stored: %v", err))
}

return b
Expand All @@ -102,7 +102,7 @@ func (b *certificationInputBuilder) WithRPMManifest(r io.Reader) *certificationI
func (b *certificationInputBuilder) WithArtifact(r io.Reader, filename string) *certificationInputBuilder {
bts, err := io.ReadAll(r)
if err != nil {
log.Error(fmt.Errorf("artifact could not be stored: %s: %v", filename, err))
log.L().Error(fmt.Errorf("artifact could not be stored: %s: %v", filename, err))
return b
}

Expand Down
17 changes: 9 additions & 8 deletions certification/pyxis/pyxis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"net/http"

"github.com/shurcooL/graphql"
log "github.com/sirupsen/logrus"

"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
)

const (
Expand Down Expand Up @@ -54,7 +55,7 @@ func (p *pyxisClient) createImage(ctx context.Context, certImage *CertImage) (*C
return nil, err
}

log.Debugf("URL is: %s", req.URL)
log.L().Debugf("URL is: %s", req.URL)

resp, err := p.Client.Do(req)
if err != nil {
Expand Down Expand Up @@ -94,7 +95,7 @@ func (p *pyxisClient) getImage(ctx context.Context, dockerImageDigest string) (*
return nil, fmt.Errorf("could not create new request: %w", err)
}

log.Debugf("URL is: %s", req.URL)
log.L().Debugf("URL is: %s", req.URL)

resp, err := p.Client.Do(req)
if err != nil {
Expand Down Expand Up @@ -241,7 +242,7 @@ func (p *pyxisClient) createRPMManifest(ctx context.Context, rpmManifest *RPMMan
return nil, fmt.Errorf("could not create new request: %w", err)
}

log.Debugf("URL is: %s", req.URL)
log.L().Debugf("URL is: %s", req.URL)

resp, err := p.Client.Do(req)
if err != nil {
Expand Down Expand Up @@ -280,7 +281,7 @@ func (p *pyxisClient) getRPMManifest(ctx context.Context, imageID string) (*RPMM
return nil, fmt.Errorf("could not create new request: %w", err)
}

log.Debugf("URL is: %s", req.URL)
log.L().Debugf("URL is: %s", req.URL)

resp, err := p.Client.Do(req)
if err != nil {
Expand Down Expand Up @@ -314,7 +315,7 @@ func (p *pyxisClient) GetProject(ctx context.Context) (*CertProject, error) {
if err != nil {
return nil, fmt.Errorf("could not create new request: %v", err)
}
log.Debugf("URL is: %s", req.URL)
log.L().Debugf("URL is: %s", req.URL)

resp, err := p.Client.Do(req)
if err != nil {
Expand Down Expand Up @@ -366,7 +367,7 @@ func (p *pyxisClient) updateProject(ctx context.Context, certProject *CertProjec
return nil, fmt.Errorf("could not create new request: %w", err)
}

log.Debugf("URL is: %s", req.URL)
log.L().Debugf("URL is: %s", req.URL)

resp, err := p.Client.Do(req)
if err != nil {
Expand Down Expand Up @@ -442,7 +443,7 @@ func (p *pyxisClient) createArtifact(ctx context.Context, artifact *Artifact) (*
return nil, fmt.Errorf("could not create new request: %w", err)
}

log.Debugf("URL is: %s", req.URL)
log.L().Debugf("URL is: %s", req.URL)

resp, err := p.Client.Do(req)
if err != nil {
Expand Down
24 changes: 13 additions & 11 deletions certification/pyxis/pyxis_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (

. "github.com/onsi/ginkgo/v2/dsl/core"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"

"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
)

func TestPyxis(t *testing.T) {
Expand All @@ -20,8 +22,8 @@ func TestPyxis(t *testing.T) {
}

func init() {
log.SetFormatter(&log.TextFormatter{})
log.SetLevel(log.TraceLevel)
log.L().SetFormatter(&logrus.TextFormatter{})
log.L().SetLevel(logrus.TraceLevel)
}

type localRoundTripper struct {
Expand Down Expand Up @@ -57,7 +59,7 @@ type (
// to dedupe. Acknowledged that it is a bit fragile. -bpc

func (p *pyxisProjectHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
log.Trace("In the Project ServeHTTP")
log.L().Trace("In the Project ServeHTTP")
response.Header().Set("Content-Type", "application/json")
if request.Body != nil {
defer request.Body.Close()
Expand All @@ -83,13 +85,13 @@ func (p *pyxisProjectHandler) ServeHTTP(response http.ResponseWriter, request *h
}

func (p *pyxisImageHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
log.Trace("In the Image ServeHTTP")
log.L().Trace("In the Image ServeHTTP")
response.Header().Set("Content-Type", "application/json")
if request.Body != nil {
defer request.Body.Close()
}
responseString := `{"_id":"blah","certified":false,"deleted":false,"image_id":"123456789abc"}`
log.Tracef("Method: %s", request.Method)
log.L().Tracef("Method: %s", request.Method)
switch {
case request.Method == http.MethodPost && strings.Contains(request.Header["X-Api-Key"][0], "my-update-image"):
response.WriteHeader(http.StatusConflict)
Expand All @@ -106,7 +108,7 @@ func (p *pyxisImageHandler) ServeHTTP(response http.ResponseWriter, request *htt
case request.Method == http.MethodPost && request.Header["X-Api-Key"][0] == "my-bad-image-api-token":
response.WriteHeader(http.StatusConflict)
case request.Method == http.MethodGet && request.Header["X-Api-Key"][0] == "my-bad-401-image-api-token":
log.Trace("get with 401")
log.L().Trace("get with 401")
response.WriteHeader(http.StatusUnauthorized)
case request.Header["X-Api-Key"][0] == "my-bad-image-api-token":
response.WriteHeader(http.StatusUnauthorized)
Expand All @@ -122,7 +124,7 @@ func (p *pyxisImageHandler) ServeHTTP(response http.ResponseWriter, request *htt
}

func (p *pyxisRPMManifestHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
log.Trace("In the RPM Manifest ServeHTTP")
log.L().Trace("In the RPM Manifest ServeHTTP")
response.Header().Set("Content-Type", "application/json")
if request.Body != nil {
defer request.Body.Close()
Expand All @@ -145,7 +147,7 @@ func (p *pyxisRPMManifestHandler) ServeHTTP(response http.ResponseWriter, reques
}

func (p *pyxisTestResultsHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
log.Trace("In the Results ServeHTTP")
log.L().Trace("In the Results ServeHTTP")
response.Header().Set("Content-Type", "application/json")
if request.Body != nil {
defer request.Body.Close()
Expand All @@ -159,7 +161,7 @@ func (p *pyxisTestResultsHandler) ServeHTTP(response http.ResponseWriter, reques
}

func (p *pyxisGraphqlLayerHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
log.Trace("In the graphql Layers ServeHTTP")
log.L().Trace("In the graphql Layers ServeHTTP")
response.Header().Set("Content-Type", "application/json")
if request.Body != nil {
defer request.Body.Close()
Expand Down Expand Up @@ -189,7 +191,7 @@ func (p *pyxisGraphqlLayerHandler) ServeHTTP(response http.ResponseWriter, reque
}

func (p *pyxisGraphqlFindImagesHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
log.Trace("In the graphql FindImages ServeHTTP")
log.L().Trace("In the graphql FindImages ServeHTTP")
response.Header().Set("Content-Type", "application/json")
if request.Body != nil {
defer request.Body.Close()
Expand Down
6 changes: 3 additions & 3 deletions certification/runtime/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"strings"

"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/authn"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"

"github.com/google/go-containerregistry/pkg/crane"
cranev1 "github.com/google/go-containerregistry/pkg/v1"
log "github.com/sirupsen/logrus"
)

// images maps the images use by preflight with their purpose.
Expand Down Expand Up @@ -41,7 +41,7 @@ func imageList(ctx context.Context) []string {
base := strings.Split(image, ":")[0]
digest, err := crane.Digest(image, options...)
if err != nil {
log.Error(fmt.Errorf("could not retrieve image digest: %w", err))
log.L().Error(fmt.Errorf("could not retrieve image digest: %w", err))
// Skip this entry
continue
}
Expand All @@ -63,7 +63,7 @@ func Assets(ctx context.Context) AssetData {
// returned, otherwise, the default is returned.
func ScorecardImage(userProvidedScorecardImage string) string {
if userProvidedScorecardImage != "" {
log.Debugf("Using %s as the scorecard test image", userProvidedScorecardImage)
log.L().Debugf("Using %s as the scorecard test image", userProvidedScorecardImage)
return userProvidedScorecardImage
}
return images["scorecard"]
Expand Down
4 changes: 2 additions & 2 deletions cmd/preflight/cmd/check_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/redhat-openshift-ecosystem/openshift-preflight/container"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/cli"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/lib"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
"github.com/redhat-openshift-ecosystem/openshift-preflight/version"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down Expand Up @@ -67,7 +67,7 @@ func checkContainerCmd() *cobra.Command {

// checkContainerRunE executes checkContainer using the user args to inform the execution.
func checkContainerRunE(cmd *cobra.Command, args []string) error {
log.Info("certification library version ", version.Version.String())
log.L().Info("certification library version ", version.Version.String())
ctx := cmd.Context()
containerImage := args[0]

Expand Down
4 changes: 2 additions & 2 deletions cmd/preflight/cmd/check_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/runtime"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/cli"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/lib"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
"github.com/redhat-openshift-ecosystem/openshift-preflight/operator"
"github.com/redhat-openshift-ecosystem/openshift-preflight/version"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -72,7 +72,7 @@ func ensureIndexImageConfigIsSet() error {

// checkOperatorRunE executes checkOperator using the user args to inform the execution.
func checkOperatorRunE(cmd *cobra.Command, args []string) error {
log.Info("certification library version ", version.Version.String())
log.L().Info("certification library version ", version.Version.String())
ctx := cmd.Context()
operatorImage := args[0]

Expand Down
18 changes: 11 additions & 7 deletions cmd/preflight/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"io"
"os"

"github.com/sirupsen/logrus"

"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/artifacts"
"github.com/redhat-openshift-ecosystem/openshift-preflight/version"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
)

var configFileUsed bool
Expand Down Expand Up @@ -80,21 +83,22 @@ func initConfig() {

// preRunConfig is used by cobra.PreRun in all non-root commands to load all necessary configurations
func preRunConfig(cmd *cobra.Command, args []string) {
l := log.L()
// set up logging
logname := viper.GetString("logfile")
logFile, err := os.OpenFile(logname, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err == nil {
mw := io.MultiWriter(os.Stderr, logFile)
log.SetOutput(mw)
l.SetOutput(mw)
} else {
log.Debug("Failed to log to file, using default stderr")
l.Debug("Failed to log to file, using default stderr")
}
if ll, err := log.ParseLevel(viper.GetString("loglevel")); err == nil {
log.SetLevel(ll)
if ll, err := logrus.ParseLevel(viper.GetString("loglevel")); err == nil {
l.SetLevel(ll)
}

log.SetFormatter(&log.TextFormatter{})
l.SetFormatter(&logrus.TextFormatter{})
if !configFileUsed {
log.Debug("config file not found, proceeding without it")
l.Debug("config file not found, proceeding without it")
}
}
5 changes: 3 additions & 2 deletions internal/authn/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/docker/cli/cli/config/types"
craneauthn "github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
log "github.com/sirupsen/logrus"

"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
)

type preflightKeychain struct {
Expand Down Expand Up @@ -49,7 +50,7 @@ func PreflightKeychain(opts ...PreflightKeychainOption) craneauthn.Keychain {
// If the file cannot be found or read, that constitutes an error.
// Can return os.IsNotExist.
func (k *preflightKeychain) Resolve(target craneauthn.Resource) (craneauthn.Authenticator, error) {
log.Trace("entering preflight keychain Resolve")
log.L().Trace("entering preflight keychain Resolve")

if k.dockercfg == "" {
// No file specified. No auth expected
Expand Down
8 changes: 5 additions & 3 deletions internal/authn/keychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (

craneauthn "github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"

"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
)

var (
Expand Down Expand Up @@ -201,6 +203,6 @@ func TestVariousPaths(t *testing.T) {
}

func init() {
log.SetFormatter(&log.TextFormatter{})
log.SetLevel(log.TraceLevel)
log.L().SetFormatter(&logrus.TextFormatter{})
log.L().SetLevel(logrus.TraceLevel)
}
10 changes: 6 additions & 4 deletions internal/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"github.com/operator-framework/api/pkg/manifests"
"github.com/operator-framework/api/pkg/validation"
olmvalidation "github.com/redhat-openshift-ecosystem/ocp-olm-catalog-validator/pkg/validation"
log "github.com/sirupsen/logrus"

rbacv1 "k8s.io/api/rbac/v1"
"sigs.k8s.io/yaml"

"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
)

// This table signifies what the NEXT release of OpenShift will
Expand All @@ -30,8 +32,8 @@ var ocpToKubeVersion = map[string]string{
const latestReleasedVersion = "4.11"

func Validate(ctx context.Context, imagePath string) (*Report, error) {
log.Trace("reading annotations file from the bundle")
log.Debug("image extraction directory is ", imagePath)
log.L().Trace("reading annotations file from the bundle")
log.L().Debug("image extraction directory is ", imagePath)

bundle, err := manifests.GetBundleFromDir(imagePath)
if err != nil {
Expand Down Expand Up @@ -65,7 +67,7 @@ func Validate(ctx context.Context, imagePath string) (*Report, error) {
return nil, fmt.Errorf("%v", err)
}
if k8sVer, found := ocpToKubeVersion[targetVersion]; found {
log.Debugf("OpenShift %s detected in annotations. Running with additional checks enabled.", targetVersion)
log.L().Debugf("OpenShift %s detected in annotations. Running with additional checks enabled.", targetVersion)
optionalValues = make(map[string]string)
optionalValues["k8s-version"] = k8sVer
}
Expand Down
Loading

0 comments on commit 3560a24

Please sign in to comment.