Skip to content

Commit

Permalink
Replace klog with zap for logging (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
sallyom committed Aug 10, 2022
1 parent c9ff8fa commit 496e79b
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 115 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ require (
github.com/go-co-op/gocron v1.13.0
github.com/go-git/go-git/v5 v5.4.2
github.com/gobwas/glob v0.2.3
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/opencontainers/runtime-spec v1.0.3-0.20211214071223-8958f93039ab
github.com/openshift/build-machinery-go v0.0.0-20220121085309-f94edc2d6874
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.0
go.uber.org/zap v1.17.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/api v0.22.4
k8s.io/apimachinery v0.22.4
k8s.io/klog/v2 v2.9.0
sigs.k8s.io/yaml v1.3.0
)

Expand Down Expand Up @@ -129,6 +130,8 @@ require (
go.etcd.io/bbolt v1.3.6 // indirect
go.mozilla.org/pkcs7 v0.0.0-20200128120323-432b2356ecb1 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce // indirect
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
Expand All @@ -145,5 +148,6 @@ require (
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.9.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,8 @@ github.com/mwitkow/go-proto-validators v0.0.0-20180403085117-0950a7990007/go.mod
github.com/mwitkow/go-proto-validators v0.2.0/go.mod h1:ZfA1hW+UH/2ZHOWvQ3HnQaU0DtnpXu850MZiy+YUgcc=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/nakabonne/nestif v0.3.1/go.mod h1:9EtoZochLn5iUprVDmDjqGKPofoUEBL8U4Ngq6aY7OE=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/nbutton23/zxcvbn-go v0.0.0-20210217022336-fa2cb2858354/go.mod h1:KSVJerMDfblTH7p5MZaTt+8zaT2iEk3AkVb9PQdZuE8=
github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
Expand Down Expand Up @@ -1362,14 +1364,17 @@ go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqe
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.4.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/crypto v0.0.0-20171113213409-9f005a07e0d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20180501155221-613d6eafa307/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down Expand Up @@ -1989,6 +1994,7 @@ gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/square/go-jose.v2 v2.2.2/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
Expand Down
16 changes: 7 additions & 9 deletions pkg/engine/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/opencontainers/runtime-spec/specs-go"

"k8s.io/klog/v2"
)

const ansibleMethod = "ansible"
Expand All @@ -36,20 +34,20 @@ func (ans *Ansible) Process(ctx, conn context.Context, PAT string, skew int) {
if ans.initialRun {
err := getRepo(target, PAT)
if err != nil {
klog.Errorf("Failed to clone repository %s: %v", target.url, err)
logger.Errorf("Failed to clone repository %s: %v", target.url, err)
return
}

err = zeroToCurrent(ctx, conn, ans, target, &tag)
if err != nil {
klog.Errorf("Error moving to current: %v", err)
logger.Errorf("Error moving to current: %v", err)
return
}
}

err := currentToLatest(ctx, conn, ans, target, &tag)
if err != nil {
klog.Errorf("Error moving current to latest: %v", err)
logger.Errorf("Error moving current to latest: %v", err)
return
}

Expand All @@ -76,12 +74,12 @@ func (ans *Ansible) ansiblePodman(ctx, conn context.Context, path string) error
if path == deleteFile {
return nil
}
klog.Infof("Deploying Ansible playbook %s\n", path)
logger.Infof("Deploying Ansible playbook %s", path)

copyFile := ("/opt/" + path)
sshImage := "quay.io/fetchit/fetchit-ansible:latest"

klog.Infof("Identifying if fetchit-ansible image exists locally")
logger.Infof("Identifying if fetchit-ansible image exists locally")
if err := detectOrFetchImage(conn, sshImage, true); err != nil {
return err
}
Expand All @@ -106,7 +104,7 @@ func (ans *Ansible) ansiblePodman(ctx, conn context.Context, path string) error
if err != nil {
return err
}
klog.Infof("Container created.")
logger.Infof("Container created.")
if err := containers.Start(conn, createResponse.ID, nil); err != nil {
return err
}
Expand All @@ -115,6 +113,6 @@ func (ans *Ansible) ansiblePodman(ctx, conn context.Context, path string) error
if err != nil {
return err
}
klog.Infof("Container started....Requeuing")
logger.Infof("Container started....Requeuing")
return nil
}
15 changes: 7 additions & 8 deletions pkg/engine/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/containers/podman/v4/pkg/bindings/system"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"k8s.io/klog/v2"
)

const pruneMethod = "prune"
Expand Down Expand Up @@ -41,7 +40,7 @@ func (p *Prune) Process(ctx, conn context.Context, PAT string, skew int) {

err := p.prunePodman(ctx, conn, opts)
if err != nil {
klog.Warningf("Repository: %s Method: %s encountered error: %v, resetting...", target.url, pruneMethod, err)
logger.Debugf("Repository: %s Method: %s encountered error: %v, resetting...", target.url, pruneMethod, err)
}

}
Expand All @@ -55,28 +54,28 @@ func (p *Prune) Apply(ctx, conn context.Context, currentState, desiredState plum
}

func (p *Prune) prunePodman(ctx, conn context.Context, opts system.PruneOptions) error {
klog.Info("Pruning system")
logger.Info("Pruning system")
report, err := system.Prune(conn, &opts)
if err != nil {
return utils.WrapErr(err, "Error pruning system")
}
for _, report := range report.ContainerPruneReports {
klog.Infof("Pruned container of size %v with id: %s\n", report.Size, report.Id)
logger.Infof("Pruned container of size %v with id: %s", report.Size, report.Id)
}

for _, report := range report.ImagePruneReports {
klog.Infof("Pruned image of size %v with id: %s\n", report.Size, report.Id)
logger.Infof("Pruned image of size %v with id: %s", report.Size, report.Id)
}

for _, report := range report.PodPruneReport {
klog.Infof("Pruned pod with id: %s\n", report.Id)
logger.Infof("Pruned pod with id: %s", report.Id)
}

for _, report := range report.VolumePruneReports {
klog.Infof("Pruned volume of size %v with id: %s\n", report.Size, report.Id)
logger.Infof("Pruned volume of size %v with id: %s", report.Size, report.Id)
}

klog.Infof("Reclaimed %vB\n", report.ReclaimedSpace)
logger.Infof("Reclaimed %vB", report.ReclaimedSpace)

return nil
}
7 changes: 3 additions & 4 deletions pkg/engine/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"k8s.io/klog/v2"
)

type CommonMethod struct {
Expand Down Expand Up @@ -60,7 +59,7 @@ func zeroToCurrent(ctx, conn context.Context, m Method, target *Target, tag *[]s
return fmt.Errorf("Failed to apply changes: %v", err)
}

klog.Infof("Moved %s to commit %s for git target %s", m.GetName(), current, target.url)
logger.Infof("Moved %s to commit %s for git target %s", m.GetName(), current, target.url)
}

return nil
Expand Down Expand Up @@ -95,9 +94,9 @@ func currentToLatest(ctx, conn context.Context, m Method, target *Target, tag *[
return fmt.Errorf("Failed to apply changes: %v", err)
}
updateCurrent(ctx, target, latest, m.GetKind(), m.GetName())
klog.Infof("Moved %s from %s to %s for git target %s", m.GetName(), current, latest, target.url)
logger.Infof("Moved %s from %s to %s for git target %s", m.GetName(), current, latest, target.url)
} else {
klog.Infof("No changes applied to git target %s this run, %s currently at %s", directory, m.GetKind(), current)
logger.Infof("No changes applied to git target %s this run, %s currently at %s", directory, m.GetKind(), current)
}

return nil
Expand Down
27 changes: 13 additions & 14 deletions pkg/engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/containers/podman/v4/pkg/bindings"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"k8s.io/klog/v2"
)

const configFileMethod = "config"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (c *ConfigReload) Process(ctx, conn context.Context, PAT string, skew int)
os.Setenv("FETCHIT_CONFIG_URL", envURL)
// If ConfigURL is not populated, warn and leave
if envURL == "" && c.Device == "" {
klog.Warningf("Fetchit ConfigReload found, but neither $FETCHIT_CONFIG_URL on system nor ConfigReload.ConfigURL are set, exiting without updating the config.")
logger.Debugf("Fetchit ConfigReload found, but neither $FETCHIT_CONFIG_URL on system nor ConfigReload.ConfigURL are set, exiting without updating the config.")
}
// CheckForConfigUpdates downloads & places config file in defaultConfigPath
// if the downloaded config file differs from what's currently on the system.
Expand All @@ -60,14 +59,14 @@ func (c *ConfigReload) Process(ctx, conn context.Context, PAT string, skew int)
if !restart {
return
}
klog.Info("Updated config processed, restarting with new targets")
logger.Info("Updated config processed, restarting with new targets")
fetchitConfig.Restart()
} else if c.Device != "" {
restart := checkForDisconUpdates(c.Device, c.ConfigPath, true, false)
if !restart {
return
}
klog.Info("Updated config processed, restarting with new targets")
logger.Info("Updated config processed, restarting with new targets")
fetchitConfig.Restart()
}

Expand All @@ -92,7 +91,7 @@ func checkForConfigUpdates(envURL string, existsAlready bool, initial bool) bool
}
reset, err := downloadUpdateConfigFile(envURL, existsAlready, initial)
if err != nil {
klog.Info(err)
logger.Info(err)
}
return reset
}
Expand All @@ -105,19 +104,19 @@ func checkForDisconUpdates(device, configPath string, existsAlready bool, initia
dest := cache + "/" + "config.yaml"
conn, err := bindings.NewConnection(ctx, "unix://run/podman/podman.sock")
if err != nil {
klog.Error("Failed to create connection to podman")
logger.Error("Failed to create connection to podman")
return false
}
// Ensure that the device is present
_, exitCode, err := localDeviceCheck(name, device, "")
if err != nil {
klog.Error("Failed to check device")
logger.Error("Failed to check device")
return false
}
if exitCode != 0 {
// remove the diff file
err = os.Remove(dest)
klog.Info("Device not present...requeuing")
logger.Info("Device not present...requeuing")
return false
} else if exitCode == 0 {
if _, err := os.Stat(dest); os.IsNotExist(err) {
Expand All @@ -131,19 +130,19 @@ func checkForDisconUpdates(device, configPath string, existsAlready bool, initia
}
// Wait for the container to finish
waitAndRemoveContainer(conn, createResponse.ID)
klog.Info("container created", createResponse.ID)
logger.Info("container created", createResponse.ID)
currentConfigBytes, err := ioutil.ReadFile(defaultConfigPath)
newBytes, err := ioutil.ReadFile(dest)
if err != nil {
klog.Error("Failed to read config file")
logger.Error("Failed to read config file")
} else {
if bytes.Equal(newBytes, currentConfigBytes) {
return false
} else {
// Replace the old config file at defaultConfigPath with the new one from dest and restart
os.WriteFile(defaultConfigBackup, currentConfigBytes, 0600)
os.WriteFile(defaultConfigPath, newBytes, 0600)
klog.Infof("Current config backup placed at %s", defaultConfigBackup)
logger.Infof("Current config backup placed at %s", defaultConfigBackup)
return true
}
}
Expand Down Expand Up @@ -182,7 +181,7 @@ func downloadUpdateConfigFile(urlStr string, existsAlready, initial bool) (bool,
if !initial {
currentConfigBytes, err := ioutil.ReadFile(defaultConfigPath)
if err != nil {
klog.Infof("unable to read current config, will try with new downloaded config file: %v", err)
logger.Infof("unable to read current config, will try with new downloaded config file: %v", err)
existsAlready = false
} else {
if bytes.Equal(newBytes, currentConfigBytes) {
Expand All @@ -194,13 +193,13 @@ func downloadUpdateConfigFile(urlStr string, existsAlready, initial bool) (bool,
if err := os.WriteFile(defaultConfigBackup, currentConfigBytes, 0600); err != nil {
return false, fmt.Errorf("could not copy %s to path %s: %v", defaultConfigPath, defaultConfigBackup, err)
}
klog.Infof("Current config backup placed at %s", defaultConfigBackup)
logger.Infof("Current config backup placed at %s", defaultConfigBackup)
}
}
if err := os.WriteFile(defaultConfigPath, newBytes, 0600); err != nil {
return false, fmt.Errorf("unable to write new config contents, reverting to old config: %v", err)
}

klog.Infof("Config updates found from url: %s, will load new targets", urlStr)
logger.Infof("Config updates found from url: %s, will load new targets", urlStr)
return true, nil
}
Loading

0 comments on commit 496e79b

Please sign in to comment.