Skip to content

Clean up all logging for the driver, log successes in all cases #409

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

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const (
)

func init() {
// klog verbosity guide for this package
// Use V(2) for one time config information
// Use V(4) for general debug information logging
// Use V(5) for GCE Cloud Provider Call informational logging
// Use V(6) for extra repeated/polling information
klog.InitFlags(flag.CommandLine)
}

Expand All @@ -57,7 +62,7 @@ func handle() {
if vendorVersion == "" {
klog.Fatalf("vendorVersion must be set at compile time")
}
klog.V(4).Infof("Driver vendor version %v", vendorVersion)
klog.V(2).Infof("Driver vendor version %v", vendorVersion)

gceDriver := driver.GetGCEDriver()

Expand Down
39 changes: 22 additions & 17 deletions pkg/gce-cloud-provider/compute/gce-compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type GCECompute interface {
// RepairUnderspecifiedVolumeKey will query the cloud provider and check each zone for the disk specified
// by the volume key and return a volume key with a correct zone
func (cloud *CloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, volumeKey *meta.Key) (*meta.Key, error) {
klog.V(5).Infof("Repairing potentially underspecified volume key %v", volumeKey)
region, err := common.GetRegionFromZones([]string{cloud.zone})
if err != nil {
return nil, fmt.Errorf("failed to get region from zones: %v", err)
Expand Down Expand Up @@ -114,6 +115,7 @@ func (cloud *CloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, v
}

func (cloud *CloudProvider) ListZones(ctx context.Context, region string) ([]string, error) {
klog.V(5).Infof("Listing zones in region: %v", region)
if len(cloud.zonesCache[region]) > 0 {
return cloud.zonesCache[region], nil
}
Expand All @@ -131,6 +133,7 @@ func (cloud *CloudProvider) ListZones(ctx context.Context, region string) ([]str
}

func (cloud *CloudProvider) ListSnapshots(ctx context.Context, filter string, maxEntries int64, pageToken string) ([]*computev1.Snapshot, string, error) {
klog.V(5).Infof("Listing snapshots with filter: %s, max entries: %v, page token: %s", filter, maxEntries, pageToken)
snapshots := []*computev1.Snapshot{}
snapshotList, err := cloud.service.Snapshots.List(cloud.project).Filter(filter).MaxResults(maxEntries).PageToken(pageToken).Do()
if err != nil {
Expand All @@ -144,6 +147,7 @@ func (cloud *CloudProvider) ListSnapshots(ctx context.Context, filter string, ma
}

func (cloud *CloudProvider) GetDisk(ctx context.Context, key *meta.Key) (*CloudDisk, error) {
klog.V(5).Infof("Getting disk %v", key)
switch key.Type() {
case meta.Zonal:
disk, err := cloud.getZonalDiskOrError(ctx, key.Zone, key.Name)
Expand All @@ -159,23 +163,20 @@ func (cloud *CloudProvider) GetDisk(ctx context.Context, key *meta.Key) (*CloudD
func (cloud *CloudProvider) getZonalDiskOrError(ctx context.Context, volumeZone, volumeName string) (*computev1.Disk, error) {
svc := cloud.service
project := cloud.project
klog.V(4).Infof("Getting disk %v from zone %v", volumeName, volumeZone)

disk, err := svc.Disks.Get(project, volumeZone, volumeName).Context(ctx).Do()
if err != nil {
return nil, err
}
klog.V(4).Infof("Got disk %v from zone %v", volumeName, volumeZone)
return disk, nil
}

func (cloud *CloudProvider) getRegionalDiskOrError(ctx context.Context, volumeRegion, volumeName string) (*computev1.Disk, error) {
project := cloud.project
klog.V(4).Infof("Getting disk %v from region %v", volumeName, volumeRegion)
disk, err := cloud.service.RegionDisks.Get(project, volumeRegion, volumeName).Context(ctx).Do()
if err != nil {
return nil, err
}
klog.V(4).Infof("Got disk %v from region %v", volumeName, volumeRegion)
return disk, nil
}

Expand All @@ -194,6 +195,7 @@ func (cloud *CloudProvider) getRegionURI(region string) string {
}

func (cloud *CloudProvider) ValidateExistingDisk(ctx context.Context, resp *CloudDisk, diskType string, reqBytes, limBytes int64) error {
klog.V(5).Infof("Validating existing disk %v with diskType: %s, reqested bytes: %v, limit bytes: %v", resp, diskType, reqBytes, limBytes)
if resp == nil {
return fmt.Errorf("disk does not exist")
}
Expand All @@ -212,13 +214,11 @@ func (cloud *CloudProvider) ValidateExistingDisk(ctx context.Context, resp *Clou
return fmt.Errorf("disk already exists with incompatible type. Need %v. Got %v",
diskType, respType[len(respType)-1])
}

// Volume exists with matching name, capacity, type.
klog.V(4).Infof("Compatible disk already exists. Reusing existing.")
return nil
}

func (cloud *CloudProvider) InsertDisk(ctx context.Context, volKey *meta.Key, diskType string, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID, diskEncryptionKmsKey string) error {
klog.V(5).Infof("Inserting disk %v", volKey)
switch volKey.Type() {
case meta.Zonal:
return cloud.insertZonalDisk(ctx, volKey, diskType, capBytes, capacityRange, snapshotID, diskEncryptionKmsKey)
Expand Down Expand Up @@ -349,6 +349,7 @@ func (cloud *CloudProvider) insertZonalDisk(ctx context.Context, volKey *meta.Ke
}

func (cloud *CloudProvider) DeleteDisk(ctx context.Context, volKey *meta.Key) error {
klog.V(5).Infof("Deleting disk: %v", volKey)
switch volKey.Type() {
case meta.Zonal:
return cloud.deleteZonalDisk(ctx, volKey.Zone, volKey.Name)
Expand Down Expand Up @@ -392,6 +393,7 @@ func (cloud *CloudProvider) deleteRegionalDisk(ctx context.Context, region, name
}

func (cloud *CloudProvider) AttachDisk(ctx context.Context, volKey *meta.Key, readWrite, diskType, instanceZone, instanceName string) error {
klog.V(5).Infof("Attaching disk %v to %s", volKey, instanceName)
source := cloud.GetDiskSourceURI(volKey)

deviceName, err := common.GetDeviceName(volKey)
Expand All @@ -418,6 +420,7 @@ func (cloud *CloudProvider) AttachDisk(ctx context.Context, volKey *meta.Key, re
}

func (cloud *CloudProvider) DetachDisk(ctx context.Context, deviceName, instanceZone, instanceName string) error {
klog.V(5).Infof("Detaching disk %v from %v", deviceName, instanceName)
op, err := cloud.service.Instances.DetachDisk(cloud.project, instanceZone, instanceName, deviceName).Context(ctx).Do()
if err != nil {
return err
Expand Down Expand Up @@ -516,11 +519,13 @@ func (cloud *CloudProvider) waitForGlobalOp(ctx context.Context, op *computev1.O
}

func (cloud *CloudProvider) WaitForAttach(ctx context.Context, volKey *meta.Key, instanceZone, instanceName string) error {
klog.V(5).Infof("Waiting for attach of disk %v to instance %v to complete...", volKey.Name, instanceName)
start := time.Now()
return wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
klog.V(6).Infof("Polling for attach of disk %v to instance %v to complete for %v", volKey.Name, instanceName, time.Since(start))
disk, err := cloud.GetDisk(ctx, volKey)
if err != nil {
klog.Errorf("GetDisk failed to get disk: %v", err)
return false, err
return false, fmt.Errorf("GetDisk failed to get disk: %v", err)
}

if disk == nil {
Expand All @@ -547,31 +552,29 @@ func opIsDone(op *computev1.Operation) (bool, error) {
}

func (cloud *CloudProvider) GetInstanceOrError(ctx context.Context, instanceZone, instanceName string) (*computev1.Instance, error) {
klog.V(5).Infof("Getting instance %v from zone %v", instanceName, instanceZone)
svc := cloud.service
project := cloud.project
klog.V(4).Infof("Getting instance %v from zone %v", instanceName, instanceZone)

instance, err := svc.Instances.Get(project, instanceZone, instanceName).Do()
if err != nil {
return nil, err
}
klog.V(4).Infof("Got instance %v from zone %v", instanceName, instanceZone)
return instance, nil
}

func (cloud *CloudProvider) GetSnapshot(ctx context.Context, snapshotName string) (*computev1.Snapshot, error) {
klog.V(5).Infof("Getting snapshot %v", snapshotName)
svc := cloud.service
project := cloud.project
klog.V(4).Infof("Getting snapshot %v", snapshotName)
snapshot, err := svc.Snapshots.Get(project, snapshotName).Context(ctx).Do()
if err != nil {
return nil, err
}
klog.V(4).Infof("Got snapshot %v", snapshotName)
return snapshot, nil
}

func (cloud *CloudProvider) DeleteSnapshot(ctx context.Context, snapshotName string) error {
klog.V(5).Infof("Deleting snapshot %v", snapshotName)
op, err := cloud.service.Snapshots.Delete(cloud.project, snapshotName).Context(ctx).Do()
if err != nil {
if IsGCEError(err, "notFound") {
Expand All @@ -588,6 +591,7 @@ func (cloud *CloudProvider) DeleteSnapshot(ctx context.Context, snapshotName str
}

func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key, snapshotName string) (*computev1.Snapshot, error) {
klog.V(5).Infof("Creating snapshot %s for volume %v", snapshotName, volKey)
switch volKey.Type() {
case meta.Zonal:
return cloud.createZonalDiskSnapshot(ctx, volKey, snapshotName)
Expand All @@ -599,6 +603,7 @@ func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key
}

func (cloud *CloudProvider) ResizeDisk(ctx context.Context, volKey *meta.Key, requestBytes int64) (int64, error) {
klog.V(5).Infof("Resizing disk %v to size %v", volKey, requestBytes)
cloudDisk, err := cloud.GetDisk(ctx, volKey)
if err != nil {
return -1, fmt.Errorf("failed to get disk: %v", err)
Expand Down Expand Up @@ -694,16 +699,16 @@ func (cloud *CloudProvider) waitForSnapshotCreation(ctx context.Context, snapsho
for {
select {
case <-ticker.C:
klog.V(5).Infof("Checking GCE Snapshot %s.", snapshotName)
klog.V(6).Infof("Checking GCE Snapshot %s.", snapshotName)
snapshot, err := cloud.GetSnapshot(ctx, snapshotName)
if err != nil {
klog.Warningf("Error in getting snapshot %s, %v", snapshotName, err)
} else if snapshot != nil {
if snapshot.Status != "CREATING" {
klog.V(5).Infof("Snapshot %s status is %s", snapshotName, snapshot.Status)
klog.V(6).Infof("Snapshot %s status is %s", snapshotName, snapshot.Status)
return snapshot, nil
} else {
klog.V(5).Infof("Snapshot %s is still creating ...", snapshotName)
klog.V(6).Infof("Snapshot %s is still creating ...", snapshotName)
}
}
case <-timer.C:
Expand Down
12 changes: 6 additions & 6 deletions pkg/gce-cloud-provider/compute/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func CreateCloudProvider(vendorVersion string, configPath string) (*CloudProvide
// At this point configFile could still be nil.
// Any following code that uses configFile should handle nil pointer gracefully.

klog.V(1).Infof("Using GCE provider config %+v", configFile)
klog.V(2).Infof("Using GCE provider config %+v", configFile)

tokenSource, err := generateTokenSource(configFile)
if err != nil {
Expand Down Expand Up @@ -109,7 +109,7 @@ func generateTokenSource(configFile *ConfigFile) (oauth2.TokenSource, error) {
// Use AltTokenSource

tokenSource := NewAltTokenSource(configFile.Global.TokenURL, configFile.Global.TokenBody)
klog.V(4).Infof("Using AltTokenSource %#v", tokenSource)
klog.V(2).Infof("Using AltTokenSource %#v", tokenSource)
return tokenSource, nil
}

Expand All @@ -122,11 +122,11 @@ func generateTokenSource(configFile *ConfigFile) (oauth2.TokenSource, error) {

// DefaultTokenSource relies on GOOGLE_APPLICATION_CREDENTIALS env var being set.
if gac, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS"); ok {
klog.V(4).Infof("GOOGLE_APPLICATION_CREDENTIALS env var set %v", gac)
klog.V(2).Infof("GOOGLE_APPLICATION_CREDENTIALS env var set %v", gac)
} else {
klog.Warningf("GOOGLE_APPLICATION_CREDENTIALS env var not set")
}
klog.V(4).Infof("Using DefaultTokenSource %#v", tokenSource)
klog.V(2).Infof("Using DefaultTokenSource %#v", tokenSource)

return tokenSource, err
}
Expand Down Expand Up @@ -198,10 +198,10 @@ func getProjectAndZone(config *ConfigFile) (string, string, error) {
if err != nil {
return "", "", err
}
klog.V(4).Infof("Using GCP project ID from the Metadata server: %q", projectID)
klog.V(2).Infof("Using GCP project ID from the Metadata server: %q", projectID)
} else {
projectID = config.Global.ProjectId
klog.V(4).Infof("Using GCP project ID from the local GCE cloud provider config file: %q", projectID)
klog.V(2).Infof("Using GCP project ID from the local GCE cloud provider config file: %q", projectID)
}

return projectID, zone, nil
Expand Down
Loading