Skip to content

Commit

Permalink
Refactor: replace with contextual logging
Browse files Browse the repository at this point in the history
  • Loading branch information
zarvd committed Jun 25, 2024
1 parent b0f421d commit 2512f70
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 169 deletions.
67 changes: 67 additions & 0 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package log

import (
"context"
"encoding/json"
"fmt"

"github.com/go-logr/logr"
"k8s.io/klog/v2"
)

// FromContextOrBackground returns a logger from the context if it exists, otherwise it returns a background logger.
// Current implementation uses klog as the background logger.
func FromContextOrBackground(ctx context.Context) logr.Logger {
return klog.FromContext(ctx)
}

// NewContext returns a new context with the provided logger.
func NewContext(ctx context.Context, logger logr.Logger) context.Context {
return klog.NewContext(ctx, logger)
}

// Background returns the background logger.
// Current implementation uses klog as the background logger.
func Background() logr.Logger {
return klog.Background()
}

// Noop returns a logger that discards all log messages.
func Noop() logr.Logger {
return logr.Discard()
}

// ValueAsMap converts a value to a map[string]any.
// It returns a map with an "error" key if the conversion fails.
// NOTE:
//
// It should ONLY be used when the default klog formatter failed to serialize the value in JSON format.
// Protobuf messages had implemented `String()` method, which the value is hard to read. Use this method to bypass instead.
func ValueAsMap(value any) map[string]any {
v, err := json.Marshal(value)
if err != nil {
return map[string]any{"error": fmt.Sprintf("<log.ValueAsMap error> %s", err)}
}
var rv map[string]any
if err := json.Unmarshal(v, &rv); err != nil {
return map[string]any{"error": fmt.Sprintf("<log.ValueAsMap error> %s", err)}
}

return rv
}
164 changes: 94 additions & 70 deletions pkg/provider/azure_loadbalancer.go

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions pkg/provider/azure_loadbalancer_accesscontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
"fmt"
"net/netip"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"

"sigs.k8s.io/cloud-provider-azure/pkg/consts"
"sigs.k8s.io/cloud-provider-azure/pkg/log"
"sigs.k8s.io/cloud-provider-azure/pkg/provider/loadbalancer"
"sigs.k8s.io/cloud-provider-azure/pkg/provider/loadbalancer/fnutil"
)
Expand Down Expand Up @@ -60,34 +59,34 @@ func (az *Cloud) listSharedIPPortMapping(
ingressIPs []netip.Addr,
) (map[network.SecurityRuleProtocol][]int32, error) {
var (
logger = klog.FromContext(ctx).WithName("listSharedIPPortMapping")
logger = log.FromContextOrBackground(ctx).WithName("listSharedIPPortMapping")
rv = make(map[network.SecurityRuleProtocol][]int32)
)

var services []*v1.Service
{
var err error
logger.Info("Listing all services")
logger.V(5).Info("Listing all services")
services, err = az.serviceLister.List(labels.Everything())
if err != nil {
logger.Error(err, "Failed to list all services")
return nil, fmt.Errorf("list all services: %w", err)
}
logger.Info("Listed all services", "num-all-services", len(services))
logger.V(5).Info("Listed all services", "num-all-services", len(services))

// Filter services by ingress IPs or backend node pool IPs (when disable floating IP)
if consts.IsK8sServiceDisableLoadBalancerFloatingIP(svc) {
logger.Info("Filter service by disableFloatingIP")
logger.V(5).Info("Filter service by disableFloatingIP")
services = filterServicesByDisableFloatingIP(services)
} else {
logger.Info("Filter service by external IPs")
logger.V(5).Info("Filter service by external IPs")
services = filterServicesByIngressIPs(services, ingressIPs)
}
}
logger.Info("Filtered services", "num-filtered-services", len(services))
logger.V(5).Info("Filtered services", "num-filtered-services", len(services))

for _, s := range services {
logger.V(4).Info("iterating service", "service", s.Name, "namespace", s.Namespace)
logger.V(5).Info("Iterating service", "service", s.Name, "namespace", s.Namespace)
if svc.Namespace == s.Namespace && svc.Name == s.Name {
// skip the service itself
continue
Expand All @@ -103,7 +102,7 @@ func (az *Cloud) listSharedIPPortMapping(
}
}

logger.V(4).Info("retain port mapping", "port-mapping", rv)
logger.V(5).Info("Retain port mapping", "port-mapping", rv)

return rv, nil
}
Loading

0 comments on commit 2512f70

Please sign in to comment.