Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Migrate to zap logger #164

Merged
merged 8 commits into from
Sep 27, 2018
Merged

Migrate to zap logger #164

merged 8 commits into from
Sep 27, 2018

Conversation

harveylowndes
Copy link
Contributor

@harveylowndes harveylowndes commented Aug 17, 2018

Migrates logging from to go.uber.org/zap from github.com/golang/glog and adds the following flags:

--log-level=debug  # control log level
--log-json  # output log as json
--logfile-path  # if provided reload

@harveylowndes harveylowndes force-pushed the migrate-logs-to-zap branch 5 times, most recently from 8a007d2 to 8d81a23 Compare August 17, 2018 14:48
@harveylowndes harveylowndes force-pushed the migrate-logs-to-zap branch 2 times, most recently from 8ce6d07 to 7da5bfb Compare August 29, 2018 10:43
@harveylowndes harveylowndes requested a review from prydie August 31, 2018 08:15
Copy link
Contributor

@prydie prydie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't gone through FSS in detail but the same kind of changes apply 🙂

func FromConfig(cfg *Config) (ProvisionerClient, error) {
config, err := newConfigurationProvider(cfg)
func FromConfig(logger *zap.SugaredLogger, cfg *Config) (ProvisionerClient, error) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\n

cmd/main.go Outdated
@@ -69,25 +80,25 @@ func main() {
// to use to communicate with Kubernetes
config, err := clientcmd.BuildConfigFromFlags(*master, *kubeconfig)
if err != nil {
glog.Fatalf("Failed to load config: %v", err)
logger.Fatalf("Failed to load config: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.With(zap.Error(err)).Fatal("Failed to load config")

cmd/main.go Outdated
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
glog.Fatalf("Failed to create client: %v", err)
logger.Fatalf("Failed to create client: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.With(zap.Error(err)).Fatal("Failed to create Kubernetes client")

cmd/main.go Outdated
}

// The controller needs to know what the server version is because out-of-tree
// provisioners aren't officially supported until 1.5
serverVersion, err := clientset.Discovery().ServerVersion()
if err != nil {
glog.Fatalf("Error getting server version: %v", err)
logger.Fatalf("Error getting server version: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.With(zap.Error(err)).Fatal("Failed to get kube-apiserver version")

cmd/main.go Outdated
@@ -96,21 +107,22 @@ func main() {
provisionerType = core.ProvisionerNameBlock
}

glog.Infof("Starting volume provisioner in %s mode", provisionerType)
logger.With("provisionerType", provisionerType).Info("Starting volume provisioner in %s mode", provisionerType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger = logger.With("provisionerType", provisionerType)
logger.Info("Starting volume provisioner in %q mode", provisionerType)


if volumeRoundingEnabled(options.Parameters) {
if block.volumeRoundingEnabled && block.minVolumeSize.Cmp(capacity) == 1 {
glog.Warningf("PVC requested storage less than %s. Rounding up to ensure volume creation", block.minVolumeSize.String())
block.logger.Warnf("PVC requested storage less than %s. Rounding up to ensure volume creation.", block.minVolumeSize.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.Warn("Attempted to provision volume with a capacity less than the minimum. Rounding up to ensure volume creation.")


volSizeMB = int(roundUpSize(block.minVolumeSize.Value(), 1024*1024))
capacity = block.minVolumeSize
}
}

glog.Infof("Creating volume size=%v AD=%s compartmentOCID=%q", volSizeMB, *ad.Name, block.client.CompartmentOCID())
block.logger.With("volumeSize", volSizeMB).With("name", *ad.Name).With("compartmentOCID", block.client.CompartmentOCID()).Info("Creating volume.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed (see log in comment above with all this)

@@ -171,7 +174,7 @@ func (block *blockProvisioner) Provision(options controller.VolumeOptions, ad *i
}

if value, ok := options.PVC.Annotations[ociVolumeBackupID]; ok {
glog.Infof("Creating volume from backup ID %s", value)
block.logger.With("volumeBackupOCID", value).Info("Creating volume from backup.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/block.logger/logger/

configPath, ok := os.LookupEnv("CONFIG_YAML_FILENAME")
if !ok {
configPath = configFilePath
}

f, err := os.Open(configPath)
if err != nil {
glog.Fatalf("Unable to load volume provisioner configuration file: %v", configPath)
logger.With("configPath", configPath).Fatal("Unable to load volume provisioner configuration file.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.With(zap.Error(err), "configPath", configPath).Fatal("Unable to load volume provisioner configuration file.")

@@ -80,7 +79,7 @@ func (p *OCIProvisioner) chooseAvailabilityDomain(ctx context.Context, pvc *v1.P
return "", nil, fmt.Errorf("failed to choose availability domain; no zone labels (%q) on nodes", metav1.LabelZoneFailureDomain)
}
availabilityDomainName = util.ChooseZoneForVolume(validADs, pvc.Name)
glog.Infof("Zone not specified so %s selected", availabilityDomainName)
p.logger.With("zone", availabilityDomainName).Info("Zone not specified, selected alternative zone.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.logger.With("availabilityDomain", availabilityDomainName).Info("No availability domain provided. Selecting one automatically.")

@harveylowndes harveylowndes force-pushed the migrate-logs-to-zap branch 2 times, most recently from daedd0c to 96f725d Compare September 3, 2018 15:19
Copy link
Contributor

@prydie prydie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we're getting into the real nitty-gritty 🙂


if volumeRoundingEnabled(options.Parameters) {
if block.volumeRoundingEnabled && block.minVolumeSize.Cmp(capacity) == 1 {
glog.Warningf("PVC requested storage less than %s. Rounding up to ensure volume creation", block.minVolumeSize.String())

logger.Warn("Attempted to provision volume with a capacity less than the minimum. Rounding up to ensure volume creation.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably move this log down a line and then add a .With("roundedVolumeSize", volSizeMB)

glog.Infof("Volume size: %dMB", volSizeMB)

logger := block.logger.With(
"compartmentID", block.client.CompartmentOCID(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On reflection adding the "compartmentID" and "tenancyID" fields should be moved to the constructor (NewBlockProvisioner) so that they're applied in both the Provision() and Delete() methods.

return &filesystemProvisioner{
client: client,
logger: logger,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the "compartmentID" and "tenancyID" fields here (same as suggested above in NewBlockProvisioner())

@@ -19,6 +19,7 @@ import (
"reflect"
"testing"

"go.uber.org/zap"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import grouping

ctx, cancel := context.WithTimeout(ctx, fsp.client.Timeout())
defer cancel()
if _, err := fsp.client.FSS().DeleteExport(ctx, filestorage.DeleteExportRequest{
ExportId: &exportID,
}); err != nil {
if !provisioner.IsNotFound(err) {
glog.Errorf("Failed to delete export exportID=%q: %v", exportID, err)
logger.With(zap.Error(err), "exportOCID", exportID).Error("Failed to delete export.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove exportID field (will be assigned above).

@@ -171,7 +178,7 @@ func (block *blockProvisioner) Provision(options controller.VolumeOptions, ad *i
}

if value, ok := options.PVC.Annotations[ociVolumeBackupID]; ok {
glog.Infof("Creating volume from backup ID %s", value)
logger.With("volumeBackupOCID", value).Info("Creating volume from backup.")
volumeDetails.SourceDetails = &core.VolumeSourceFromVolumeBackupDetails{Id: &value}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below log that we're waitForVolumeAvailable() .With("volumeID", *newVolume.Id)

@@ -146,6 +154,9 @@ func TestCreateVolumeFailure(t *testing.T) {
}

func TestVolumeRoundingLogic(t *testing.T) {

logger := zap.S()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't bother assigning; just pass zap.S() directly.

@@ -100,6 +105,9 @@ func TestCreateVolumeFromBackup(t *testing.T) {
}

func TestCreateVolumeFailure(t *testing.T) {

logger := zap.S()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't bother assigning; just pass zap.S() directly.

@@ -61,6 +63,9 @@ func TestResolveFSTypeWhenConfigured(t *testing.T) {

func TestCreateVolumeFromBackup(t *testing.T) {
// test creating a volume from an existing backup

logger := zap.S()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't bother assigning; just pass zap.S() directly.

@@ -20,7 +20,8 @@ import (
"strings"
"time"

"github.com/golang/glog"
"go.uber.org/zap"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import grouping

@harveylowndes harveylowndes force-pushed the migrate-logs-to-zap branch 2 times, most recently from fde08e3 to 543a114 Compare September 4, 2018 10:34
@harveylowndes harveylowndes changed the title WIP: Migrate to zap logger Migrate to zap logger Sep 4, 2018
Copy link
Contributor

@prydie prydie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more

return &filesystemProvisioner{
client: client,
logger: logger.With(
"compartmentID", client.CompartmentOCID,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you be calling these as they're methods?

// Create the FileSystem.
var fsID string
{
ctx, cancel := context.WithTimeout(ctx, fsp.client.Timeout())
defer cancel()
logger.Info("Creating FileSystem")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log above outside of this block.

@@ -195,13 +206,13 @@ func (fsp *filesystemProvisioner) Provision(options controller.VolumeOptions, ad
PrivateIpId: &id,
})
if err != nil {
glog.Errorf("Failed to retrieve IP address for mount target privateIpID=%q: %v", id, err)
logger.Errorf("Failed to retrieve IP address for mount target privateIpID=%q: %v", id, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.With(zap.Error(err), "privateIPID", id)

glog.Infof("Deleting export for filesystemID %v", filesystemID)
logger := fsp.logger.With(
"fileSystemOCID", filesystemID,
"volumeOCID", volume,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not done.

return nil, err
}
serverIP = *getPrivateIPResponse.PrivateIp.IpAddress
}

glog.Infof("Creating persistent volume on mount target with private IP address %s", serverIP)
logger.With("privateIP", serverIP).Info("Creating persistent volume on mount target with private IP address.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe reword message to something along the lines of "Chose Mount Target Private IP"?

},
})
if err != nil {
glog.Errorf("Failed to create a file system options=%#v: %v", options, err)
return nil, err
}
fsID = *resp.FileSystem.Id
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reassign logger .With("fileSystemID", fsID)

@@ -174,15 +185,15 @@ func (fsp *filesystemProvisioner) Provision(options controller.VolumeOptions, ad
},
})
if err != nil {
glog.Errorf("Failed to create export: %v", err)
logger.With(zap.Error(err)).Error("Failed to create export.")
return nil, err
}
exportSetID = *resp.Export.Id
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reassign logger .With("exportSetID", exportSetID)

return nil, err
}
exportSetID = *resp.Export.Id
}

if len(target.PrivateIpIds) == 0 {
glog.Errorf("Failed to find server IDs associated with the Mount Target (OCID %s) to provision a persistent volume", target.Id)
return nil, errors.Errorf("failed to find server IDs associated with the Mount Target with OCID %q", target.Id)
logger.With("targetID", *target.Id).Error("Failed to find server IDs associated with the Mount Target to provision a persistent volume")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change the error and log message to something along the lines of "Could not find any Private IPs for Mount Target"?

@harveylowndes harveylowndes force-pushed the migrate-logs-to-zap branch 3 times, most recently from aae1dc4 to affa9cf Compare September 5, 2018 16:30
Copy link
Contributor

@prydie prydie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple final changes and needs rebasing but looking good 👍

cmd/main.go Outdated

//logger.Infof("logtostderr is set to %b", flag.Lookup("logtostderr").Value.(flag.Getter).Get().(bool))

//logger.Sugar().With("version", version, "build", build).Info("oci-volume-provisioner")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should log the version and build.

cmd/main.go Outdated

logger := log.Sugar()

//logger.Infof("logtostderr is set to %b", flag.Lookup("logtostderr").Value.(flag.Getter).Get().(bool))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed.

cmd/main.go Outdated
"github.com/oracle/oci-volume-provisioner/pkg/provisioner/core"
"github.com/oracle/oci-volume-provisioner/pkg/signals"
"go.uber.org/zap"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import grouping

cmd/main.go Outdated
kubeconfig := flag.String("kubeconfig", "", "Path to Kubeconfig file with authorization and master location information.")
volumeRoundingEnabled := flag.Bool("rounding-enabled", true, "When enabled volumes will be rounded up if less than 'minVolumeSizeMB'")
minVolumeSize := flag.String("min-volume-size", "50Gi", "The minimum size for a block volume. By default OCI only supports block volumes > 50GB")
master := flag.String("master", "", "The address of the Kubernetes API server (overrides any value in kubeconfig).")
flag.Parse()
flag.Set("logtostderr", "true")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this now (it's glog specific).

"github.com/golang/glog"
"github.com/pkg/errors"
"go.uber.org/zap"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusingly github.com/oracle/oci-go-sdk is actually an 3rd partly lib rather than internal and as such should be grouped with zap and errors etc.

@@ -111,24 +114,30 @@ func (p *provisionerClient) Timeout() time.Duration {
func (p *provisionerClient) CompartmentOCID() (compartmentOCID string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func (p *provisionerClient) CompartmentOCID() string {
	if p.cfg.CompartmentOCID == "" {
		if p.metadata == nil {
			p.logger.Fatal("Unable to get compartment OCID. Please provide this via config.")
			return
		}


		p.logger.With("compartmentOCID", p.metadata.CompartmentOCID).Infof("'CompartmentID' not given. Using compartment OCID from instance metadata.")
		p.cfg.CompartmentOCID = p.metadata.CompartmentOCID
	}

	return p.cfg.CompartmentOCID
}

} else {
compartmentOCID = p.cfg.CompartmentOCID
}
return
}

func (p *provisionerClient) TenancyOCID() string {
return p.cfg.Auth.TenancyOCID
func (p *provisionerClient) TenancyOCID() (tenancyOCID string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to name the return type here

blockStorage *core.BlockstorageClient
identity *identity.IdentityClient
fileStorage *filestorage.FileStorageClient
virtualNetwork *core.VirtualNetworkClient
context context.Context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contexts should never be added as a property on a struct.

@harveylowndes harveylowndes force-pushed the migrate-logs-to-zap branch 3 times, most recently from c5696fa to 76e4ba4 Compare September 24, 2018 15:38
@owainlewis
Copy link
Member

Awaiting fix for system-tests

@prydie prydie merged commit 4e4fed8 into master Sep 27, 2018
@prydie prydie deleted the migrate-logs-to-zap branch September 27, 2018 11:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants