Skip to content

Commit 3015cbf

Browse files
committed
migrate verbose info logging to contextual logging across remaining folders
1 parent f0e1f0e commit 3015cbf

File tree

7 files changed

+79
-48
lines changed

7 files changed

+79
-48
lines changed

pkg/provider/securitygroup/azure_securitygroup_repo.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/securitygroupclient"
3535
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
3636
"sigs.k8s.io/cloud-provider-azure/pkg/consts"
37+
"sigs.k8s.io/cloud-provider-azure/pkg/log"
3738
"sigs.k8s.io/cloud-provider-azure/pkg/util/errutils"
3839
)
3940

@@ -55,6 +56,7 @@ type securityGroupRepo struct {
5556
}
5657

5758
func NewSecurityGroupRepo(securityGroupResourceGroup string, securityGroupName string, nsgCacheTTLInSeconds int, disableAPICallCache bool, securityGroupClient securitygroupclient.Interface) (Repository, error) {
59+
logger := log.Background().WithName("NewSecurityGroupRepo")
5860
getter := func(ctx context.Context, key string) (interface{}, error) {
5961
nsg, err := securityGroupClient.Get(ctx, securityGroupResourceGroup, key)
6062
exists, rerr := errutils.CheckResourceExistsFromAzcoreError(err)
@@ -63,7 +65,7 @@ func NewSecurityGroupRepo(securityGroupResourceGroup string, securityGroupName s
6365
}
6466

6567
if !exists {
66-
klog.V(2).Infof("Security group %q not found", key)
68+
logger.V(2).Info("Security group not found", "securityGroup", key)
6769
return nil, nil
6870
}
6971

@@ -90,8 +92,9 @@ func NewSecurityGroupRepo(securityGroupResourceGroup string, securityGroupName s
9092

9193
// CreateOrUpdateSecurityGroup invokes az.SecurityGroupsClient.CreateOrUpdate with exponential backoff retry
9294
func (az *securityGroupRepo) CreateOrUpdateSecurityGroup(ctx context.Context, sg *armnetwork.SecurityGroup) error {
95+
logger := log.Background().WithName("CreateOrUpdateSecurityGroup")
9396
_, rerr := az.securigyGroupClient.CreateOrUpdate(ctx, az.securityGroupResourceGroup, *sg.Name, *sg)
94-
klog.V(10).Infof("SecurityGroupsClient.CreateOrUpdate(%s): end", *sg.Name)
97+
logger.V(10).Info("SecurityGroupsClient.CreateOrUpdate: end", "securityGroupName", *sg.Name)
9598
if rerr == nil {
9699
// Invalidate the cache right after updating
97100
_ = az.nsgCache.Delete(*sg.Name)
@@ -104,13 +107,13 @@ func (az *securityGroupRepo) CreateOrUpdateSecurityGroup(ctx context.Context, sg
104107

105108
// Invalidate the cache because ETAG precondition mismatch.
106109
if respError.StatusCode == http.StatusPreconditionFailed {
107-
klog.V(3).Infof("SecurityGroup cache for %s is cleanup because of http.StatusPreconditionFailed", *sg.Name)
110+
logger.V(3).Info("SecurityGroup cache is cleanup because of http.StatusPreconditionFailed", "securityGroupName", *sg.Name)
108111
_ = az.nsgCache.Delete(*sg.Name)
109112
}
110113

111114
// Invalidate the cache because another new operation has canceled the current request.
112115
if strings.Contains(strings.ToLower(respError.Error()), consts.OperationCanceledErrorMessage) {
113-
klog.V(3).Infof("SecurityGroup cache for %s is cleanup because CreateOrUpdateSecurityGroup is canceled by another operation", *sg.Name)
116+
logger.V(3).Info("SecurityGroup cache is cleanup because CreateOrUpdateSecurityGroup is canceled by another operation", "securityGroupName", *sg.Name)
114117
_ = az.nsgCache.Delete(*sg.Name)
115118
}
116119
}

pkg/provider/storage/azure_storageaccount.go

Lines changed: 54 additions & 34 deletions
Large diffs are not rendered by default.

pkg/provider/storage/fileservice/fileservice_repo.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"time"
2424

2525
armstorage "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage/v2"
26-
"k8s.io/klog/v2"
2726

2827
"sigs.k8s.io/cloud-provider-azure/pkg/azclient"
2928
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/cache"
29+
"sigs.k8s.io/cloud-provider-azure/pkg/log"
3030
azureconfig "sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
3131
)
3232

@@ -52,6 +52,7 @@ func NewRepository(config azureconfig.Config, clientFactory azclient.ClientFacto
5252
}, nil
5353
}
5454
func (az *fileServicePropertiesRepo) Get(ctx context.Context, subsID, resourceGroup, account string) (*armstorage.FileServiceProperties, error) {
55+
logger := log.Background().WithName("Get")
5556
if az.clientFactory == nil {
5657
return nil, fmt.Errorf("clientFactory is nil")
5758
}
@@ -65,7 +66,7 @@ func (az *fileServicePropertiesRepo) Get(ctx context.Context, subsID, resourceGr
6566
return nil, err
6667
}
6768
if cache != nil {
68-
klog.V(2).Infof("Get service properties(%s) from cache", account)
69+
logger.V(2).Info("Get service properties from cache", "account", account)
6970
return cache, nil
7071
}
7172

pkg/provider/storage/storage_account.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import (
2222
"strings"
2323
"time"
2424

25-
"k8s.io/klog/v2"
26-
2725
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/accountclient"
26+
"sigs.k8s.io/cloud-provider-azure/pkg/log"
2827
)
2928

3029
// GetStorageAccesskey gets the storage account access key
3130
// getLatestAccountKey: get the latest account key per CreationTime if true, otherwise get the first account key
3231
func GetStorageAccesskey(ctx context.Context, saClient accountclient.Interface, account, resourceGroup string, getLatestAccountKey bool) (string, error) {
32+
logger := log.Background().WithName("GetStorageAccesskey")
3333
if saClient == nil {
3434
return "", fmt.Errorf("StorageAccountClient is nil")
3535
}
@@ -61,12 +61,12 @@ func GetStorageAccesskey(ctx context.Context, saClient accountclient.Interface,
6161
if k.CreationTime != nil {
6262
creationTime = *k.CreationTime
6363
}
64-
klog.V(2).Infof("got storage account key with creation time: %v", creationTime)
64+
logger.V(2).Info("got storage account key with creation time", "creationTime", creationTime)
6565
} else {
6666
if k.CreationTime != nil && creationTime.Before(*k.CreationTime) {
6767
key = v
6868
creationTime = *k.CreationTime
69-
klog.V(2).Infof("got storage account key with latest creation time: %v", creationTime)
69+
logger.V(2).Info("got storage account key with latest creation time", "creationTime", creationTime)
7070
}
7171
}
7272
}

pkg/provider/subnet/subnet.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"k8s.io/klog/v2"
2424

2525
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/subnetclient"
26+
"sigs.k8s.io/cloud-provider-azure/pkg/log"
2627
)
2728

2829
type Repository interface {
@@ -42,8 +43,9 @@ func NewRepo(subnetsClient subnetclient.Interface) (Repository, error) {
4243

4344
// CreateOrUpdateSubnet invokes az.SubnetClient.CreateOrUpdate with exponential backoff retry
4445
func (az *repo) CreateOrUpdate(ctx context.Context, rg string, vnetName string, subnetName string, subnet armnetwork.Subnet) error {
46+
logger := log.Background().WithName("SubnetsClient.CreateOrUpdate")
4547
_, rerr := az.SubnetsClient.CreateOrUpdate(ctx, rg, vnetName, subnetName, subnet)
46-
klog.V(10).Infof("SubnetsClient.CreateOrUpdate(%s): end", subnetName)
48+
logger.V(10).Info("end", "subnetName", subnetName)
4749
if rerr != nil {
4850
klog.Errorf("SubnetClient.CreateOrUpdate(%s) failed: %s", subnetName, rerr.Error())
4951
return rerr

pkg/util/controller/node/controller_utils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"k8s.io/client-go/tools/cache"
2525
"k8s.io/client-go/tools/record"
2626
"k8s.io/klog/v2"
27+
28+
"sigs.k8s.io/cloud-provider-azure/pkg/log"
2729
)
2830

2931
// CreateAddNodeHandler creates an add node handler.
@@ -89,14 +91,15 @@ func GetNodeCondition(status *v1.NodeStatus, conditionType v1.NodeConditionType)
8991

9092
// RecordNodeStatusChange records a event related to a node status change. (Common to lifecycle and ipam)
9193
func RecordNodeStatusChange(recorder record.EventRecorder, node *v1.Node, newStatus string) {
94+
logger := log.Background().WithName("RecordNodeStatusChange")
9295
ref := &v1.ObjectReference{
9396
APIVersion: "v1",
9497
Kind: "Node",
9598
Name: node.Name,
9699
UID: node.UID,
97100
Namespace: "",
98101
}
99-
klog.V(2).Infof("Recording status change %s event message for node %s", newStatus, node.Name)
102+
logger.V(2).Info("Recording status change event message for node", "status", newStatus, "node", node.Name)
100103
// TODO: This requires a transaction, either both node status is updated
101104
// and event is recorded or neither should happen, see issue #6055.
102105
recorder.Eventf(ref, v1.EventTypeNormal, newStatus, "Node %s status is now: %s", node.Name, newStatus)

pkg/util/node/node.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/types"
2828
clientset "k8s.io/client-go/kubernetes"
29-
"k8s.io/klog/v2"
29+
30+
"sigs.k8s.io/cloud-provider-azure/pkg/log"
3031
)
3132

3233
type nodeForCIDRMergePatch struct {
@@ -66,6 +67,7 @@ func PatchNodeCIDR(c clientset.Interface, node types.NodeName, cidr string) erro
6667

6768
// PatchNodeCIDRs patches the specified node.CIDR=cidrs[0] and node.CIDRs to the given value.
6869
func PatchNodeCIDRs(c clientset.Interface, node types.NodeName, cidrs []string) error {
70+
logger := log.Background().WithName("PatchNodeCIDRs")
6971
// set the pod cidrs list and set the old pod cidr field
7072
patch := nodeForCIDRMergePatch{
7173
Spec: nodeSpecForMergePatch{
@@ -78,7 +80,7 @@ func PatchNodeCIDRs(c clientset.Interface, node types.NodeName, cidrs []string)
7880
if err != nil {
7981
return fmt.Errorf("failed to json.Marshal CIDR: %w", err)
8082
}
81-
klog.V(4).Infof("cidrs patch bytes for node %s are:%s", string(node), string(patchBytes))
83+
logger.V(4).Info("cidrs patch bytes for node", "node", string(node), "cidrsPatchBytes", string(patchBytes))
8284
if _, err := c.CoreV1().Nodes().Patch(context.TODO(), string(node), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}); err != nil {
8385
return fmt.Errorf("failed to patch node CIDR: %w", err)
8486
}

0 commit comments

Comments
 (0)