Skip to content

Commit b9c8c82

Browse files
feat: flag to enable/disable middleware logging (#1031)
Co-authored-by: Alex Leites <[email protected]>
1 parent 12c548e commit b9c8c82

File tree

10 files changed

+35
-18
lines changed

10 files changed

+35
-18
lines changed

Makefile-az.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
AZURE_LOCATION ?= westus2
22
AZURE_VM_SIZE ?= ""
33
COMMON_NAME ?= karpenter
4+
ENABLE_AZURE_SDK_LOGGING ?= true
45
ifeq ($(CODESPACES),true)
56
AZURE_RESOURCE_GROUP ?= $(CODESPACE_NAME)
67
AZURE_ACR_NAME ?= $(subst -,,$(CODESPACE_NAME))
@@ -138,8 +139,8 @@ az-mkaks-savm: az-mkrg ## Create experimental cluster with standalone VMs (+ ACR
138139
az-rmrg: ## Destroy test ACR and AKS cluster by deleting the resource group (use with care!)
139140
az group delete --name $(AZURE_RESOURCE_GROUP)
140141

141-
az-configure-values: ## Generate cluster-related values for Karpenter Helm chart
142-
hack/deploy/configure-values.sh $(AZURE_CLUSTER_NAME) $(AZURE_RESOURCE_GROUP) $(KARPENTER_SERVICE_ACCOUNT_NAME) $(AZURE_KARPENTER_USER_ASSIGNED_IDENTITY_NAME)
142+
az-configure-values: ## Generate cluster-related values for Karpenter Helm chart and set middleware logging flag
143+
hack/deploy/configure-values.sh $(AZURE_CLUSTER_NAME) $(AZURE_RESOURCE_GROUP) $(KARPENTER_SERVICE_ACCOUNT_NAME) $(AZURE_KARPENTER_USER_ASSIGNED_IDENTITY_NAME) $(ENABLE_AZURE_SDK_LOGGING)
143144

144145
az-mkvmssflex: ## Create VMSS Flex (optional, only if creating VMs referencing this VMSS)
145146
az vmss create --name $(AZURE_CLUSTER_NAME)-vmss --resource-group $(AZURE_RESOURCE_GROUP_MC) --location $(AZURE_LOCATION) \

hack/deploy/configure-values.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ set -euo pipefail
33
# This script interrogates the AKS cluster and Azure resources to generate
44
# the karpenter-values.yaml file using the karpenter-values-template.yaml file as a template.
55

6-
if [ "$#" -ne 4 ]; then
7-
echo "Usage: $0 <cluster-name> <resource-group> <karpenter-service-account-name> <karpenter-user-assigned-identity-name>"
6+
if [ "$#" -ne 5 ]; then
7+
echo "Usage: $0 <cluster-name> <resource-group> <karpenter-service-account-name> <karpenter-user-assigned-identity-name> <enable-azure-sdk-logging>"
88
exit 1
99
fi
1010

@@ -14,6 +14,7 @@ CLUSTER_NAME=$1
1414
AZURE_RESOURCE_GROUP=$2
1515
KARPENTER_SERVICE_ACCOUNT_NAME=$3
1616
AZURE_KARPENTER_USER_ASSIGNED_IDENTITY_NAME=$4
17+
ENABLE_AZURE_SDK_LOGGING=$5
1718

1819
# Optional values through env vars:
1920
LOG_LEVEL=${LOG_LEVEL:-"info"}
@@ -70,7 +71,7 @@ KUBELET_IDENTITY_CLIENT_ID=$(jq -r ".identityProfile.kubeletidentity.clientId //
7071

7172
export CLUSTER_NAME AZURE_LOCATION AZURE_RESOURCE_GROUP_MC KARPENTER_SERVICE_ACCOUNT_NAME \
7273
CLUSTER_ENDPOINT BOOTSTRAP_TOKEN SSH_PUBLIC_KEY VNET_SUBNET_ID KARPENTER_USER_ASSIGNED_CLIENT_ID NODE_IDENTITIES AZURE_SUBSCRIPTION_ID NETWORK_PLUGIN NETWORK_PLUGIN_MODE NETWORK_POLICY \
73-
LOG_LEVEL VNET_GUID KUBELET_IDENTITY_CLIENT_ID
74+
LOG_LEVEL VNET_GUID KUBELET_IDENTITY_CLIENT_ID ENABLE_AZURE_SDK_LOGGING
7475

7576
# get karpenter-values-template.yaml, if not already present (e.g. outside of repo context)
7677
if [ ! -f karpenter-values-template.yaml ]; then

karpenter-values-template.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
replicas: 1 # for better debugging experience
32
controller:
43
env:
@@ -39,6 +38,8 @@ controller:
3938
value: ${KUBELET_IDENTITY_CLIENT_ID}
4039
- name: AZURE_NODE_RESOURCE_GROUP
4140
value: ${AZURE_RESOURCE_GROUP_MC}
41+
- name: ENABLE_AZURE_SDK_LOGGING
42+
value: ${ENABLE_AZURE_SDK_LOGGING:-false}
4243

4344
# managed karpenter settings
4445
- name: USE_SIG

pkg/operator/operator.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
108108
azClient, err := instance.NewAZClient(ctx, azConfig, env, cred)
109109
lo.Must0(err, "creating Azure client")
110110
if options.FromContext(ctx).VnetGUID == "" && options.FromContext(ctx).NetworkPluginMode == consts.NetworkPluginModeOverlay {
111-
vnetGUID, err := getVnetGUID(cred, azConfig, options.FromContext(ctx).SubnetID)
111+
vnetGUID, err := getVnetGUID(ctx, cred, azConfig, options.FromContext(ctx).SubnetID)
112112
lo.Must0(err, "getting VNET GUID")
113113
options.FromContext(ctx).VnetGUID = vnetGUID
114114
}
@@ -232,7 +232,7 @@ func getCABundle(restConfig *rest.Config) (*string, error) {
232232
return lo.ToPtr(base64.StdEncoding.EncodeToString(transportConfig.TLS.CAData)), nil
233233
}
234234

235-
func getVnetGUID(creds azcore.TokenCredential, cfg *auth.Config, subnetID string) (string, error) {
235+
func getVnetGUID(ctx context.Context, creds azcore.TokenCredential, cfg *auth.Config, subnetID string) (string, error) {
236236
// TODO: Current the VNET client isn't used anywhere but this method. As such, it is not
237237
// held on azclient like the other clients.
238238
// We should possibly just put the vnet client on azclient, and then pass azclient in here, rather than
@@ -242,7 +242,8 @@ func getVnetGUID(creds azcore.TokenCredential, cfg *auth.Config, subnetID string
242242
return "", err
243243
}
244244

245-
opts := armopts.DefaultARMOpts(env.Cloud)
245+
o := options.FromContext(ctx)
246+
opts := armopts.DefaultARMOpts(env.Cloud, o.EnableAzureSDKLogging)
246247
vnetClient, err := armnetwork.NewVirtualNetworksClient(cfg.SubscriptionID, creds, opts)
247248
if err != nil {
248249
return "", err

pkg/operator/options/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type Options struct {
8989
SIGSubscriptionID string `json:"sigSubscriptionId,omitempty"`
9090
NodeResourceGroup string `json:"nodeResourceGroup,omitempty"`
9191
AdditionalTags map[string]string `json:"additionalTags,omitempty"`
92+
EnableAzureSDKLogging bool `json:"enableAzureSDKLogging,omitempty"` // Controls whether Azure SDK middleware logging is enabled
9293
DiskEncryptionSetID string `json:"diskEncryptionSetId,omitempty"`
9394
}
9495

@@ -121,6 +122,7 @@ func (o *Options) AddFlags(fs *coreoptions.FlagSet) {
121122
}
122123
// See https://github.com/Azure/karpenter-provider-azure/issues/1042 for issue discussing improvements around this
123124
fs.Var(additionalTagsFlag, "additional-tags", "Additional tags to apply to the resources in Azure. Format is key1=value1,key2=value2. These tags will be merged with the tags specified on the NodePool. In the case of a tag collision, the NodePool tag wins. These tags only apply to new nodes and do not trigger drift, which means that adding tags to this collection will not update existing nodes until drift triggers for some other reason.")
125+
fs.BoolVar(&o.EnableAzureSDKLogging, "enable-azure-sdk-logging", env.WithDefaultBool("ENABLE_AZURE_SDK_LOGGING", true), "If set to false then Azure SDK middleware logging is disabled for debugging, and won't be logging all HTTP requests/responses to Azure APIs.")
124126
}
125127

126128
func (o *Options) GetAPIServerName() string {

pkg/operator/options/suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var _ = Describe("Options", func() {
6666
"KUBELET_IDENTITY_CLIENT_ID",
6767
"LINUX_ADMIN_USERNAME",
6868
"ADDITIONAL_TAGS",
69+
"ENABLE_AZURE_SDK_LOGGING",
6970
}
7071

7172
var fs *coreoptions.FlagSet

pkg/providers/imagefamily/nodebootstrappingclient.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type NodeBootstrappingClient struct {
6868
resourceName string
6969
credential azcore.TokenCredential
7070
tokenProvider *tokenProvider
71+
enableLogging bool
7172
}
7273

7374
// NewNodeBootstrappingClient creates a new NodeBootstrappingClient with token caching enabled.
@@ -79,6 +80,7 @@ func NewNodeBootstrappingClient(
7980
resourceName string,
8081
credential azcore.TokenCredential,
8182
serverURL string,
83+
enableLogging bool,
8284
) (*NodeBootstrappingClient, error) {
8385
return &NodeBootstrappingClient{
8486
serverURL: serverURL,
@@ -89,6 +91,7 @@ func NewNodeBootstrappingClient(
8991
tokenProvider: &tokenProvider{
9092
cloud: cloud,
9193
},
94+
enableLogging: enableLogging,
9295
}, nil
9396
}
9497

@@ -107,10 +110,12 @@ func (c *NodeBootstrappingClient) Get(
107110
}
108111
transport.DefaultAuthentication = httptransport.BearerToken(token.Token)
109112

110-
// Middleware logging
111-
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
112-
loggingClient := restlogger.NewLoggingClient(logger)
113-
transport.Transport = loggingClient.Transport
113+
// Middleware logging only if ENABLE_AZURE_SDK_LOGGING flag is enabled
114+
if c.enableLogging {
115+
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
116+
loggingClient := restlogger.NewLoggingClient(logger)
117+
transport.Transport = loggingClient.Transport
118+
}
114119

115120
// Create the client
116121
client := client.New(transport, strfmt.Default)

pkg/providers/instance/azure_client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func NewAZClientFromAPI(
123123
// nolint: gocyclo
124124
func NewAZClient(ctx context.Context, cfg *auth.Config, env *auth.Environment, cred azcore.TokenCredential) (*AZClient, error) {
125125
o := options.FromContext(ctx)
126-
opts := armopts.DefaultARMOpts(env.Cloud)
126+
opts := armopts.DefaultARMOpts(env.Cloud, o.EnableAzureSDKLogging)
127127
extensionsClient, err := armcompute.NewVirtualMachineExtensionsClient(cfg.SubscriptionID, cred, opts)
128128
if err != nil {
129129
return nil, err
@@ -193,7 +193,8 @@ func NewAZClient(ctx context.Context, cfg *auth.Config, env *auth.Environment, c
193193
cfg.ResourceGroup,
194194
o.ClusterName,
195195
cred,
196-
o.NodeBootstrappingServerURL)
196+
o.NodeBootstrappingServerURL,
197+
o.EnableAzureSDKLogging)
197198
if err != nil {
198199
return nil, err
199200
}

pkg/test/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type OptionsFields struct {
4545
VnetGUID *string
4646
KubeletIdentityClientID *string
4747
AdditionalTags map[string]string
48+
EnableAzureSDKLogging *bool
4849
DiskEncryptionSetID *string
4950

5051
// SIG Flags not required by the self hosted offering
@@ -79,6 +80,7 @@ func Options(overrides ...OptionsFields) *azoptions.Options {
7980
NodeResourceGroup: lo.FromPtrOr(options.NodeResourceGroup, "test-resourceGroup"),
8081
ProvisionMode: lo.FromPtrOr(options.ProvisionMode, "aksscriptless"),
8182
NodeBootstrappingServerURL: lo.FromPtrOr(options.NodeBootstrappingServerURL, ""),
83+
EnableAzureSDKLogging: lo.FromPtrOr(options.EnableAzureSDKLogging, true),
8284
UseSIG: lo.FromPtrOr(options.UseSIG, false),
8385
SIGSubscriptionID: lo.FromPtrOr(options.SIGSubscriptionID, "12345678-1234-1234-1234-123456789012"),
8486
SIGAccessTokenServerURL: lo.FromPtrOr(options.SIGAccessTokenServerURL, "https://test-sig-access-token-server.com"),

pkg/utils/opts/armopts.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ import (
2929
"github.com/Azure/karpenter-provider-azure/pkg/auth"
3030
)
3131

32-
func DefaultARMOpts(cloudConfig cloud.Configuration) *arm.ClientOptions {
32+
func DefaultARMOpts(cloudConfig cloud.Configuration, enableLogging bool) *arm.ClientOptions {
3333
opts := &arm.ClientOptions{}
3434
opts.Telemetry = DefaultTelemetryOpts()
3535
opts.Retry = DefaultRetryOpts()
3636
opts.Transport = defaultHTTPClient
3737
opts.Cloud = cloudConfig
3838

39-
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
40-
opts.PerCallPolicies = append(opts.PerCallPolicies, shPolicy.NewLoggingPolicy(*logger))
39+
if enableLogging {
40+
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
41+
opts.PerCallPolicies = append(opts.PerCallPolicies, shPolicy.NewLoggingPolicy(*logger))
42+
}
4143
return opts
4244
}
4345

0 commit comments

Comments
 (0)