Skip to content

Commit edebfda

Browse files
authored
Merge pull request #134 from oshoval/logv
logs: Add custom verbosity, and change selected logs verbosity
2 parents 65bf911 + b5c297e commit edebfda

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

cmd/main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"crypto/tls"
2121
"flag"
22+
"fmt"
2223
"os"
2324

2425
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -33,11 +34,12 @@ import (
3334
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3435
"k8s.io/client-go/rest"
3536

37+
klog "k8s.io/klog/v2"
38+
3639
ctrl "sigs.k8s.io/controller-runtime"
3740
"sigs.k8s.io/controller-runtime/pkg/cache"
3841
"sigs.k8s.io/controller-runtime/pkg/client"
3942
"sigs.k8s.io/controller-runtime/pkg/healthz"
40-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
4143
"sigs.k8s.io/controller-runtime/pkg/webhook"
4244

4345
virtv1 "kubevirt.io/api/core/v1"
@@ -90,13 +92,20 @@ func main() {
9092
"Define the namespace where the NAD to override the default network is located",
9193
)
9294

93-
opts := zap.Options{
94-
Development: true,
95+
klog.InitFlags(nil)
96+
97+
logLevelStr := os.Getenv("LOG_LEVEL")
98+
if logLevelStr == "" {
99+
logLevelStr = "0"
95100
}
96-
opts.BindFlags(flag.CommandLine)
101+
if err := flag.Set("v", logLevelStr); err != nil {
102+
fmt.Fprintf(os.Stderr, "failed to set log level: %v\n", err)
103+
os.Exit(1)
104+
}
105+
97106
flag.Parse()
98107

99-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
108+
ctrl.SetLogger(klog.NewKlogr())
100109

101110
// if the enable-http2 flag is false (the default), http/2 should be disabled
102111
// due to its vulnerabilities. More specifically, disabling http/2 will

config/manager/manager.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ spec:
6767
- --leader-elect
6868
image: controller:latest
6969
name: manager
70+
env:
71+
- name: LOG_LEVEL
72+
value: "0"
7073
securityContext:
7174
allowPrivilegeEscalation: false
7275
capabilities:

dist/install.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ spec:
225225
- --leader-elect
226226
command:
227227
- /manager
228+
env:
229+
- name: LOG_LEVEL
230+
value: "0"
228231
image: ghcr.io/kubevirt/ipam-controller:main
229232
livenessProbe:
230233
httpGet:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
k8s.io/api v0.32.5
1515
k8s.io/apimachinery v0.32.5
1616
k8s.io/client-go v0.32.5
17+
k8s.io/klog/v2 v2.130.1
1718
k8s.io/utils v0.0.0-20241210054802-24370beab758
1819
kubevirt.io/api v1.4.0
1920
sigs.k8s.io/controller-runtime v0.19.4
@@ -72,7 +73,6 @@ require (
7273
gopkg.in/inf.v0 v0.9.1 // indirect
7374
gopkg.in/yaml.v3 v3.0.1 // indirect
7475
k8s.io/apiextensions-apiserver v0.32.0 // indirect
75-
k8s.io/klog/v2 v2.130.1 // indirect
7676
k8s.io/kube-openapi v0.0.0-20241212222426-2c72e554b1e7 // indirect
7777
kubevirt.io/containerized-data-importer-api v1.57.0-alpha1 // indirect
7878
kubevirt.io/controller-lifecycle-operator-sdk/api v0.0.0-20220329064328-f3cc58c6ed90 // indirect

pkg/ipamclaimswebhook/podmutator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ func (a *IPAMClaimsValet) Handle(ctx context.Context, request admission.Request)
8383
return admission.Errored(http.StatusBadRequest, err)
8484
}
8585

86-
log.Info("webhook handling event")
86+
log.V(1).Info("webhook handling event")
8787

8888
vmName, hasVMAnnotation := pod.Annotations["kubevirt.io/domain"]
8989
if !hasVMAnnotation {
90-
log.Info(
90+
log.V(1).Info(
9191
"does not have the kubevirt VM annotation",
9292
)
9393
return admission.Allowed("not a VM")
@@ -191,7 +191,7 @@ func (a *IPAMClaimsValet) Handle(ctx context.Context, request admission.Request)
191191
return admission.Errored(http.StatusInternalServerError, err)
192192
}
193193

194-
log.Info("new pod annotations", "pod", newPod.Annotations)
194+
log.V(1).Info("new pod annotations", "pod", newPod.Annotations)
195195
return admission.PatchResponseFromRaw(request.Object.Raw, marshaledPod)
196196
}
197197

@@ -275,7 +275,7 @@ func ensureIPAMClaimRefAtNetworkSelectionElements(ctx context.Context,
275275
hasChangedNetworkSelectionElements := false
276276
for i, networkSelectionElement := range networkSelectionElements {
277277
nadName := fmt.Sprintf("%s/%s", networkSelectionElement.Namespace, networkSelectionElement.Name)
278-
log.Info(
278+
log.V(1).Info(
279279
"iterating network selection elements",
280280
"NAD", nadName,
281281
)
@@ -369,7 +369,7 @@ func primaryNetworkConfig(
369369
) (*config.RelevantConfig, error) {
370370
log := logf.FromContext(ctx)
371371

372-
log.Info(
372+
log.V(1).Info(
373373
"Looking for primary network config",
374374
"vmi",
375375
client.ObjectKeyFromObject(vmi),
@@ -381,7 +381,7 @@ func primaryNetworkConfig(
381381
}
382382

383383
if primaryNetworkNAD == nil {
384-
log.Info(
384+
log.V(1).Info(
385385
"Did not find primary network config",
386386
"namespace", vmi.Namespace,
387387
)

pkg/vminetworkscontroller/vmi_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (r *VirtualMachineInstanceReconciler) Reconcile(
112112
}
113113

114114
if len(existingIPAMClaim.OwnerReferences) == 1 && existingIPAMClaim.OwnerReferences[0].UID == ownerInfo.UID {
115-
r.Log.Info("found existing IPAMClaim belonging to this VM/VMI, nothing to do", "UID", ownerInfo.UID)
115+
r.Log.V(1).Info("found existing IPAMClaim belonging to this VM/VMI, nothing to do", "UID", ownerInfo.UID)
116116
continue
117117
} else {
118118
err := fmt.Errorf("failed since it found an existing IPAMClaim for %q", claimKey.Name)

0 commit comments

Comments
 (0)