Skip to content

Commit

Permalink
log: remove logrus and migrate all logs to slog
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontesi committed Dec 7, 2023
1 parent 5ba0990 commit 3bb9620
Show file tree
Hide file tree
Showing 84 changed files with 980 additions and 1,098 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
bin/
catalog.json
claim.json
tnf-execution.log
cnf-certification-test/tnf-execution.log
cnf-certification-test/cnf-test-log
cnf-certification-test/cnf-certsuite.log
.idea
vendor
*.test
Expand Down
26 changes: 16 additions & 10 deletions cmd/tnf/claim/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"encoding/json"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/test-network-function/cnf-certification-test/internal/log"
"github.com/test-network-function/cnf-certification-test/pkg/junit"
"github.com/test-network-function/test-network-function-claim/pkg/claim"
)
Expand All @@ -34,41 +34,46 @@ func claimUpdate(_ *cobra.Command, _ []string) error {
fileUpdated := false
dat, err := os.ReadFile(*claimFileTextPtr)
if err != nil {
log.Fatalf("Error reading claim file :%v", err)
log.Error("Error reading claim file :%v", err)
os.Exit(1)
}
claimRoot := readClaim(&dat)
junitMap := claimRoot.Claim.RawResults
items, err := os.ReadDir(*reportFilesTextPtr)
if err != nil {
log.Fatalf("Error reading directory: %v", err)
log.Error("Error reading directory: %v", err)
os.Exit(1)
}
for _, item := range items {
fileName := item.Name()
extension := filepath.Ext(fileName)
reportKeyName := fileName[0 : len(fileName)-len(extension)]
if _, ok := junitMap[reportKeyName]; ok {
log.Printf("Skipping: %s already exists in supplied `%s` claim file", reportKeyName, *claimFileTextPtr)
log.Info("Skipping: %s already exists in supplied `%s` claim file", reportKeyName, *claimFileTextPtr)
} else {
junitMap[reportKeyName], err = junit.ExportJUnitAsMap(fmt.Sprintf("%s/%s", *reportFilesTextPtr, item.Name()))
if err != nil {
log.Fatalf("Error reading JUnit XML file into JSON: %v", err)
log.Error("Error reading JUnit XML file into JSON: %v", err)
os.Exit(1)
}
fileUpdated = true
}
}
claimRoot.Claim.RawResults = junitMap
payload, err := json.MarshalIndent(claimRoot, "", " ")
if err != nil {
log.Fatalf("Failed to generate the claim: %v", err)
log.Error("Failed to generate the claim: %v", err)
os.Exit(1)
}
err = os.WriteFile(*claimFileTextPtr, payload, claimFilePermissions)
if err != nil {
log.Fatalf("Error writing claim data:\n%s", string(payload))
log.Error("Error writing claim data:\n%s", string(payload))
os.Exit(1)
}
if fileUpdated {
log.Printf("Claim file `%s` updated\n", *claimFileTextPtr)
log.Info("Claim file `%s` updated\n", *claimFileTextPtr)
} else {
log.Printf("No changes were applied to `%s`\n", *claimFileTextPtr)
log.Info("No changes were applied to `%s`\n", *claimFileTextPtr)
}
return nil
}
Expand All @@ -77,7 +82,8 @@ func readClaim(contents *[]byte) *claim.Root {
var claimRoot claim.Root
err := json.Unmarshal(*contents, &claimRoot)
if err != nil {
log.Fatalf("Error reading claim constents file into type: %v", err)
log.Error("Error reading claim constents file into type: %v", err)
os.Exit(1)
}
return &claimRoot
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/tnf/claim/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/test-network-function/cnf-certification-test/cmd/tnf/claim/compare/configurations"
"github.com/test-network-function/cnf-certification-test/cmd/tnf/claim/compare/nodes"
"github.com/test-network-function/cnf-certification-test/cmd/tnf/claim/compare/testcases"
"github.com/test-network-function/cnf-certification-test/cmd/tnf/claim/compare/versions"
"github.com/test-network-function/cnf-certification-test/cmd/tnf/pkg/claim"
"github.com/test-network-function/cnf-certification-test/internal/log"
)

const longHelp = `Compares sections of both claim files and the differences are shown in a table per section.
Expand Down Expand Up @@ -97,12 +97,12 @@ func NewCommand() *cobra.Command {
)
err := claimCompareFiles.MarkFlagRequired("claim1")
if err != nil {
log.Errorf("Failed to mark flag claim1 as required: %v", err)
log.Error("Failed to mark flag claim1 as required: %v", err)
return nil
}
err = claimCompareFiles.MarkFlagRequired("claim2")
if err != nil {
log.Errorf("Failed to mark flag claim2 as required: %v", err)
log.Error("Failed to mark flag claim2 as required: %v", err)
return nil
}

Expand All @@ -112,7 +112,8 @@ func NewCommand() *cobra.Command {
func claimCompare(_ *cobra.Command, _ []string) error {
err := claimCompareFilesfunc(Claim1FilePathFlag, Claim2FilePathFlag)
if err != nil {
log.Fatalf("Error comparing claim files: %v", err)
log.Error("Error comparing claim files: %v", err)
os.Exit(1)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/tnf/generate/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"sort"
"strings"

"github.com/sirupsen/logrus"
"github.com/test-network-function/cnf-certification-test/cnf-certification-test/identifiers"
"github.com/test-network-function/cnf-certification-test/internal/log"
"github.com/test-network-function/cnf-certification-test/pkg/arrayhelper"
"github.com/test-network-function/test-network-function-claim/pkg/claim"

Expand Down Expand Up @@ -254,7 +254,7 @@ func summaryToMD(aSummary catalogSummary) (out string) {
func outputJS() {
out, err := json.MarshalIndent(identifiers.Classification, "", " ")
if err != nil {
logrus.Errorf("could not Marshall classification, err=%s", err)
log.Error("could not Marshall classification, err=%s", err)
return
}
fmt.Printf("classification= %s ", out)
Expand Down
7 changes: 5 additions & 2 deletions cmd/tnf/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
log "github.com/sirupsen/logrus"
"os"

"github.com/spf13/cobra"
"github.com/test-network-function/cnf-certification-test/internal/log"

"github.com/test-network-function/cnf-certification-test/cmd/tnf/check"
"github.com/test-network-function/cnf-certification-test/cmd/tnf/claim"
Expand All @@ -22,6 +24,7 @@ func main() {
rootCmd.AddCommand(check.NewCommand())

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
log.Error("%v", err)
os.Exit(1)
}
}
8 changes: 4 additions & 4 deletions cnf-certification-test/accesscontrol/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"context"
"fmt"

"github.com/sirupsen/logrus"
"github.com/test-network-function/cnf-certification-test/internal/clientsholder"
"github.com/test-network-function/cnf-certification-test/internal/log"
"github.com/test-network-function/cnf-certification-test/pkg/loghelper"
"github.com/test-network-function/cnf-certification-test/pkg/stringhelper"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand All @@ -41,7 +41,7 @@ func TestCrsNamespaces(crds []*apiextv1.CustomResourceDefinition, configNamespac
}
for namespace, crNames := range crNamespaces {
if !stringhelper.StringInSlice(configNamespaces, namespace, false) {
logrus.Debugf("CRD: %s (kind:%s/ plural:%s) has CRs %v deployed in namespace (%s) not in configured namespaces %v",
log.Debug("CRD: %s (kind:%s/ plural:%s) has CRs %v deployed in namespace (%s) not in configured namespaces %v",
crd.Name, crd.Spec.Names.Kind, crd.Spec.Names.Plural, crNames, namespace, configNamespaces)
// Initialize this map dimension before use
if invalidCrs[crd.Name] == nil {
Expand All @@ -64,10 +64,10 @@ func getCrsPerNamespaces(aCrd *apiextv1.CustomResourceDefinition) (crdNamespaces
Version: version.Name,
Resource: aCrd.Spec.Names.Plural,
}
logrus.Debugf("Looking for CRs from CRD: %s api version:%s group:%s plural:%s", aCrd.Name, version.Name, aCrd.Spec.Group, aCrd.Spec.Names.Plural)
log.Debug("Looking for CRs from CRD: %s api version:%s group:%s plural:%s", aCrd.Name, version.Name, aCrd.Spec.Group, aCrd.Spec.Names.Plural)
crs, err := oc.DynamicClient.Resource(gvr).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logrus.Errorf("error getting %s: %v\n", aCrd.Name, err)
log.Error("error getting %s: %v\n", aCrd.Name, err)
return crdNamespaces, err
}
crdNamespaces = make(map[string][]string)
Expand Down
4 changes: 2 additions & 2 deletions cnf-certification-test/accesscontrol/rbac/automount.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"context"
"fmt"

"github.com/sirupsen/logrus"
"github.com/test-network-function/cnf-certification-test/internal/clientsholder"
"github.com/test-network-function/cnf-certification-test/internal/log"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -30,7 +30,7 @@ func AutomountServiceAccountSetOnSA(serviceAccountName, podNamespace string) (*b
clientsHolder := clientsholder.GetClientsHolder()
sa, err := clientsHolder.K8sClient.CoreV1().ServiceAccounts(podNamespace).Get(context.TODO(), serviceAccountName, metav1.GetOptions{})
if err != nil {
logrus.Errorf("executing serviceaccount command failed with error: %v", err)
log.Error("executing serviceaccount command failed with error: %v", err)
return nil, err
}
return sa.AutomountServiceAccountToken, nil
Expand Down
20 changes: 10 additions & 10 deletions cnf-certification-test/accesscontrol/resources/resources.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package resources

import (
"github.com/test-network-function/cnf-certification-test/internal/log"
"github.com/test-network-function/cnf-certification-test/pkg/provider"
"github.com/test-network-function/cnf-certification-test/pkg/tnf"
)

func HasRequestsAndLimitsSet(cut *provider.Container) bool {
passed := true
// Parse the limits.
if len(cut.Resources.Limits) == 0 {
tnf.ClaimFilePrintf("Container has been found missing resource limits: %s", cut.String())
log.Debug("Container has been found missing resource limits: %s", cut.String())
passed = false
} else {
if cut.Resources.Limits.Cpu().IsZero() {
tnf.ClaimFilePrintf("Container has been found missing CPU limits: %s", cut.String())
log.Debug("Container has been found missing CPU limits: %s", cut.String())
passed = false
}

if cut.Resources.Limits.Memory().IsZero() {
tnf.ClaimFilePrintf("Container has been found missing memory limits: %s", cut.String())
log.Debug("Container has been found missing memory limits: %s", cut.String())
passed = false
}
}

// Parse the requests.
if len(cut.Resources.Requests) == 0 {
tnf.ClaimFilePrintf("Container has been found missing resource requests: %s", cut.String())
log.Debug("Container has been found missing resource requests: %s", cut.String())
passed = false
} else {
if cut.Resources.Requests.Cpu().IsZero() {
tnf.ClaimFilePrintf("Container has been found missing CPU requests: %s", cut.String())
log.Debug("Container has been found missing CPU requests: %s", cut.String())
passed = false
}

if cut.Resources.Requests.Memory().IsZero() {
tnf.ClaimFilePrintf("Container has been found missing memory requests: %s", cut.String())
log.Debug("Container has been found missing memory requests: %s", cut.String())
passed = false
}
}
Expand All @@ -48,14 +48,14 @@ func HasExclusiveCPUsAssigned(cut *provider.Container) bool {

// if no cpu or memory limits are specified the container will run in the shared cpu pool
if cpuLimits.IsZero() || memLimits.IsZero() {
tnf.ClaimFilePrintf("Container has been found missing cpu/memory resource limits: %s", cut.String())
log.Debug("Container has been found missing cpu/memory resource limits: %s", cut.String())
return false
}

// if the cpu limits quantity is not an integer the container will run in the shared cpu pool
cpuLimitsVal, isInteger := cpuLimits.AsInt64()
if !isInteger {
tnf.ClaimFilePrintf("Container's cpu resource limit is not an integer: %s", cut.String())
log.Debug("Container's cpu resource limit is not an integer: %s", cut.String())
return false
}

Expand All @@ -68,6 +68,6 @@ func HasExclusiveCPUsAssigned(cut *provider.Container) bool {
}

// if the cpu limits and request are different, the container will run in the shared cpu pool
tnf.ClaimFilePrintf("Container's cpu/memory resources and limits are not equal to each other: %s", cut.String())
log.Debug("Container's cpu/memory resources and limits are not equal to each other: %s", cut.String())
return false
}
Loading

0 comments on commit 3bb9620

Please sign in to comment.