Skip to content

Commit 639f431

Browse files
committed
use klog to align other capi projects
1 parent ef8f925 commit 639f431

File tree

8 files changed

+67
-32
lines changed

8 files changed

+67
-32
lines changed

cloud/scheduler/scheduler.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ func (m *Manager) GetOrCreateScheduler(client *proxmox.Service) *Scheduler {
5858
sched, ok := m.table[*schedID]
5959
if !ok {
6060
// create and register new scheduler
61-
m.params.Logger.V(5).Info("registering new scheduler")
61+
m.params.Logger.V(4).Info("registering new scheduler")
6262
sched := m.NewScheduler(client)
63-
sched.logger = sched.logger.WithValues("scheduler ID", *schedID)
63+
sched.logger = sched.logger.WithValues("schedulerID", &schedID)
6464
m.table[*schedID] = sched
6565
return sched
6666
}
67-
sched.logger.V(5).Info("using existing scheduler")
67+
sched.logger.V(4).Info("using existing scheduler")
6868
return sched
6969
}
7070

@@ -193,13 +193,13 @@ func (s *Scheduler) Stop() {
193193
// retrieve one qemuSpec from queue and try to create
194194
// new qemu according to the qemuSpec
195195
func (s *Scheduler) ScheduleOne(ctx context.Context) {
196+
s.logger.Info("getting next qemu from scheduling queue")
196197
qemu, shutdown := s.schedulingQueue.Get()
197198
if shutdown {
198199
return
199200
}
200201
config := qemu.Config()
201202
qemuCtx := qemu.Context()
202-
s.logger = s.logger.WithValues("qemu", config.Name)
203203
s.logger.Info("scheduling qemu")
204204

205205
state := framework.NewCycleState()
@@ -257,6 +257,8 @@ func (s *Scheduler) WaitStatus(ctx context.Context, config *api.VirtualMachineCr
257257

258258
// create new qemu with given spec and context
259259
func (s *Scheduler) CreateQEMU(ctx context.Context, config *api.VirtualMachineCreateOptions) (framework.SchedulerResult, error) {
260+
s.logger = s.logger.WithValues("qemu", config.Name)
261+
s.logger.Info("adding qemu to scheduler queue")
260262
// add qemu spec into the queue
261263
s.schedulingQueue.Add(ctx, config)
262264

cloud/services/compute/instance/cloudinit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func (s *Service) reconcileCloudInit(ctx context.Context) error {
3030

3131
// delete CloudConfig
3232
func (s *Service) deleteCloudConfig(ctx context.Context) error {
33+
log := log.FromContext(ctx)
34+
log.Info("deleting cloud config file")
35+
3336
storageName := s.scope.GetClusterStorage().Name
3437
path := userSnippetPath(s.scope.Name())
3538
volumeID := fmt.Sprintf("%s:%s", storageName, path)

cloud/services/compute/instance/image.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ const (
2020
// reconcileBootDevice
2121
func (s *Service) reconcileBootDevice(ctx context.Context, vm *proxmox.VirtualMachine) error {
2222
log := log.FromContext(ctx)
23-
log.Info("reconcile boot device")
23+
log.Info("reconciling boot device")
2424

2525
// os image
2626
if err := s.setCloudImage(ctx); err != nil {
2727
return err
2828
}
2929

30-
// volume
30+
// boot disk
31+
log.Info("resizing boot disk")
3132
if err := vm.ResizeVolume(ctx, bootDvice, s.scope.GetHardware().Disk); err != nil {
3233
return err
3334
}

cloud/services/compute/instance/qemu.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func (s *Service) reconcileQEMU(ctx context.Context) (*proxmox.VirtualMachine, e
3535

3636
// get QEMU gets proxmox vm from vmid
3737
func (s *Service) getQEMU(ctx context.Context) (*proxmox.VirtualMachine, error) {
38+
log := log.FromContext(ctx)
39+
log.Info("getting qemu from vmid")
3840
vmid := s.scope.GetVMID()
3941
if vmid != nil {
4042
return s.client.VirtualMachine(ctx, *vmid)
@@ -44,12 +46,14 @@ func (s *Service) getQEMU(ctx context.Context) (*proxmox.VirtualMachine, error)
4446

4547
func (s *Service) createQEMU(ctx context.Context) (*proxmox.VirtualMachine, error) {
4648
log := log.FromContext(ctx)
49+
log.Info("creating qemu")
4750

4851
if err := s.ensureStorageAvailable(ctx); err != nil {
4952
return nil, err
5053
}
5154

5255
// create qemu
56+
log.Info("making qemu spec")
5357
vmoption := s.generateVMOptions()
5458
// bind annotation key-values to context
5559
schedCtx := framework.ContextWithMap(ctx, s.scope.Annotations())

cloud/services/compute/instance/reconcile.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ func (s *Service) Reconcile(ctx context.Context) error {
3333
return err
3434
}
3535

36-
log.Info(fmt.Sprintf("Reconciled instance: bios-uuid=%s", *uuid))
36+
log.Info("updating instance status")
3737
if err := s.scope.SetProviderID(*uuid); err != nil {
3838
return err
3939
}
4040
s.scope.SetInstanceStatus(infrav1.InstanceStatus(instance.VM.Status))
4141
s.scope.SetNodeName(instance.Node)
4242
s.scope.SetVMID(instance.VM.VMID)
4343

44+
log.Info("updating instance config status")
4445
config, err := instance.GetConfig(ctx)
4546
if err != nil {
4647
return err
@@ -54,6 +55,7 @@ func (s *Service) Delete(ctx context.Context) error {
5455
log := log.FromContext(ctx)
5556
log.Info("Deleting instance resources")
5657

58+
log.Info("trying to get qemu")
5759
instance, err := s.getQEMU(ctx)
5860
if err != nil {
5961
if !rest.IsNotFound(err) {
@@ -164,7 +166,7 @@ func (s *Service) createInstance(ctx context.Context) (*proxmox.VirtualMachine,
164166

165167
func ensureRunning(ctx context.Context, instance proxmox.VirtualMachine) error {
166168
log := log.FromContext(ctx)
167-
// ensure instance is running
169+
log.Info("ensuring qemu is running")
168170
switch instance.VM.Status {
169171
case api.ProcessStatusRunning:
170172
return nil
@@ -186,6 +188,7 @@ func ensureRunning(ctx context.Context, instance proxmox.VirtualMachine) error {
186188

187189
func ensureStoppedOrPaused(ctx context.Context, instance proxmox.VirtualMachine) error {
188190
log := log.FromContext(ctx)
191+
log.Info("ensuring qemus is stopped or paused")
189192
switch instance.VM.Status {
190193
case api.ProcessStatusRunning:
191194
if err := instance.Stop(ctx, api.VirtualMachineStopOption{}); err != nil {

cloud/services/compute/instance/storage.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
"strings"
77

88
"github.com/sp-yduck/proxmox-go/api"
9+
"sigs.k8s.io/controller-runtime/pkg/log"
910
)
1011

1112
// make sure storage exists and supports "images" type of content
1213
func (s *Service) ensureStorageAvailable(ctx context.Context) error {
14+
log := log.FromContext(ctx)
15+
log.Info("ensuring storage is available")
1316
storageName := s.scope.GetStorage()
1417
if storageName == "" { // no storage specified, find available storage
1518
storage, err := s.findVMStorage(ctx)
@@ -19,6 +22,7 @@ func (s *Service) ensureStorageAvailable(ctx context.Context) error {
1922
storageName = storage.Storage
2023
s.scope.SetStorage(storageName)
2124
} else { // storage specified, check if it supports "images" type of content
25+
log.Info("checking if specified storage supports image type of content")
2226
storage, err := s.client.RESTClient().GetStorage(ctx, storageName)
2327
if err != nil {
2428
return err
@@ -32,6 +36,8 @@ func (s *Service) ensureStorageAvailable(ctx context.Context) error {
3236

3337
// get one storage supporting "images" type of content
3438
func (s *Service) findVMStorage(ctx context.Context) (*api.Storage, error) {
39+
log := log.FromContext(ctx)
40+
log.Info("finding available storage")
3541
storages, err := s.client.RESTClient().GetStorages(ctx)
3642
if err != nil {
3743
return nil, err

cmd/main.go

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,58 @@ import (
2323
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2424
// to ensure that exec-entrypoint and run can make use of them.
2525
_ "k8s.io/client-go/plugin/pkg/client/auth"
26+
"k8s.io/klog/v2"
2627

2728
"k8s.io/apimachinery/pkg/runtime"
2829
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2930
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
31+
cliflag "k8s.io/component-base/cli/flag"
32+
"k8s.io/component-base/logs"
33+
logsv1 "k8s.io/component-base/logs/api/v1"
3034
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3135
ctrl "sigs.k8s.io/controller-runtime"
3236
"sigs.k8s.io/controller-runtime/pkg/healthz"
33-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3437

3538
infrastructurev1beta1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
3639
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scheduler"
3740
controller "github.com/sp-yduck/cluster-api-provider-proxmox/controllers"
41+
"github.com/spf13/pflag"
3842
//+kubebuilder:scaffold:imports
3943
)
4044

4145
var (
4246
scheme = runtime.NewScheme()
4347
setupLog = ctrl.Log.WithName("setup")
48+
49+
// flags
50+
metricsAddr string
51+
enableLeaderElection bool
52+
probeAddr string
53+
pluginConfig string
54+
logOptions = logs.NewOptions()
4455
)
4556

4657
func init() {
4758
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
4859

4960
utilruntime.Must(infrastructurev1beta1.AddToScheme(scheme))
50-
utilruntime.Must(infrastructurev1beta1.AddToScheme(scheme))
51-
_ = clusterv1.AddToScheme(scheme)
61+
utilruntime.Must(clusterv1.AddToScheme(scheme))
62+
5263
//+kubebuilder:scaffold:scheme
5364
}
5465

5566
func main() {
56-
var metricsAddr string
57-
var enableLeaderElection bool
58-
var probeAddr string
59-
var pluginConfig string
60-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
61-
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
62-
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
63-
"Enable leader election for controller manager. "+
64-
"Enabling this will ensure there is only one active controller manager.")
65-
flag.StringVar(&pluginConfig, "scheduler-plugin-config", "", "The config file path for qemu-scheduler plugins")
66-
opts := zap.Options{
67-
Development: true,
68-
}
69-
opts.BindFlags(flag.CommandLine)
70-
flag.Parse()
71-
72-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
67+
InitFlags(pflag.CommandLine)
68+
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
69+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
70+
// Set log level 2 as default.
71+
// if err := pflag.CommandLine.Set("v", "2"); err != nil {
72+
// setupLog.Error(err, "failed to set log level: %v")
73+
// os.Exit(1)
74+
// }
75+
pflag.Parse()
76+
77+
ctrl.SetLogger(klog.Background())
7378

7479
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
7580
Scheme: scheme,
@@ -96,7 +101,7 @@ func main() {
96101
}
97102
schedManager, err := scheduler.NewManager(
98103
scheduler.SchedulerParams{
99-
Logger: zap.New(zap.UseFlagOptions(&opts)),
104+
Logger: klog.Background(),
100105
PluginConfigFile: pluginConfig,
101106
},
102107
)
@@ -137,3 +142,14 @@ func main() {
137142
os.Exit(1)
138143
}
139144
}
145+
146+
func InitFlags(fs *pflag.FlagSet) {
147+
logsv1.AddFlags(logOptions, fs)
148+
149+
fs.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
150+
fs.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
151+
fs.BoolVar(&enableLeaderElection, "leader-elect", false,
152+
"Enable leader election for controller manager. "+
153+
"Enabling this will ensure there is only one active controller manager.")
154+
fs.StringVar(&pluginConfig, "scheduler-plugin-config", "", "The config file path for qemu-scheduler plugins")
155+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ require (
99
github.com/onsi/gomega v1.29.0
1010
github.com/pkg/errors v0.9.1
1111
github.com/sp-yduck/proxmox-go v0.0.0-20231028112758-ef366c611274
12+
github.com/spf13/pflag v1.0.5
1213
gopkg.in/yaml.v3 v3.0.1
1314
k8s.io/api v0.27.2
1415
k8s.io/apimachinery v0.27.2
1516
k8s.io/client-go v0.27.2
17+
k8s.io/component-base v0.27.2
18+
k8s.io/klog/v2 v2.90.1
1619
k8s.io/utils v0.0.0-20230209194617-a36077c30491
1720
sigs.k8s.io/cluster-api v1.5.3
1821
sigs.k8s.io/cluster-api/test v1.5.3
@@ -93,7 +96,6 @@ require (
9396
github.com/spf13/cast v1.5.1 // indirect
9497
github.com/spf13/cobra v1.7.0 // indirect
9598
github.com/spf13/jwalterweatherman v1.1.0 // indirect
96-
github.com/spf13/pflag v1.0.5 // indirect
9799
github.com/spf13/viper v1.16.0 // indirect
98100
github.com/stoewer/go-strcase v1.2.0 // indirect
99101
github.com/subosito/gotenv v1.4.2 // indirect
@@ -120,8 +122,6 @@ require (
120122
k8s.io/apiextensions-apiserver v0.27.2 // indirect
121123
k8s.io/apiserver v0.27.2 // indirect
122124
k8s.io/cluster-bootstrap v0.27.2 // indirect
123-
k8s.io/component-base v0.27.2 // indirect
124-
k8s.io/klog/v2 v2.90.1 // indirect
125125
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
126126
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
127127
sigs.k8s.io/kind v0.20.0 // indirect

0 commit comments

Comments
 (0)