Skip to content

Commit

Permalink
use opts approach to make metrics injectable
Browse files Browse the repository at this point in the history
  • Loading branch information
balpert89 committed Oct 18, 2024
1 parent 6de1d96 commit af10519
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
44 changes: 26 additions & 18 deletions cmd/libvirt-provider/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/ironcore-dev/ironcore-image/oci/remote"
apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
"github.com/ironcore-dev/ironcore/broker/common"
commongrpc "github.com/ironcore-dev/ironcore/broker/common/grpc"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"

"github.com/ironcore-dev/libvirt-provider/api"
"github.com/ironcore-dev/libvirt-provider/internal/console"
"github.com/ironcore-dev/libvirt-provider/internal/controllers"
Expand All @@ -32,7 +33,6 @@ import (
"github.com/ironcore-dev/libvirt-provider/internal/libvirt/guest"
libvirtutils "github.com/ironcore-dev/libvirt-provider/internal/libvirt/utils"
"github.com/ironcore-dev/libvirt-provider/internal/mcr"
"github.com/ironcore-dev/libvirt-provider/internal/metrics"
"github.com/ironcore-dev/libvirt-provider/internal/networkinterfaceplugin"
"github.com/ironcore-dev/libvirt-provider/internal/oci"
volumeplugin "github.com/ironcore-dev/libvirt-provider/internal/plugins/volume"
Expand All @@ -42,11 +42,12 @@ import (
"github.com/ironcore-dev/libvirt-provider/internal/raw"
"github.com/ironcore-dev/libvirt-provider/internal/server"
"github.com/ironcore-dev/libvirt-provider/internal/strategy"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"

"github.com/ironcore-dev/ironcore-image/oci/remote"
apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
"github.com/ironcore-dev/ironcore/broker/common"
commongrpc "github.com/ironcore-dev/ironcore/broker/common/grpc"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -85,7 +86,8 @@ type Options struct {

EnableHugepages bool

GuestAgent GuestAgentOption
GuestAgent GuestAgentOption
MachineReconcilerMetrics *ReconcilerMetricsOptions

Libvirt LibvirtOptions
NicPlugin *networkinterfaceplugin.Options
Expand Down Expand Up @@ -174,6 +176,11 @@ func (o *Options) MarkFlagsRequired(cmd *cobra.Command) {
_ = cmd.MarkFlagRequired("supported-machine-classes")
}

func (o *Options) SetMetrics() {
o.MachineReconcilerMetrics = &ReconcilerMetricsOptions{}
o.MachineReconcilerMetrics.SetMachineReconcilerMetricsOptions()
}

func Command() *cobra.Command {
var (
zapOpts = zap.Options{Development: true}
Expand All @@ -188,9 +195,9 @@ func Command() *cobra.Command {
cmd.SetContext(ctrl.LoggerInto(cmd.Context(), ctrl.Log))
},
RunE: func(cmd *cobra.Command, args []string) error {
//flag parsing is done therefore we can silence the usage message
// flag parsing is done therefore we can silence the usage message
cmd.SilenceUsage = true
//error logging is done in the main
// error logging is done in the main
cmd.SilenceErrors = true
return Run(cmd.Context(), opts)
},
Expand All @@ -202,6 +209,7 @@ func Command() *cobra.Command {

opts.AddFlags(cmd.Flags())
opts.MarkFlagsRequired(cmd)
opts.SetMetrics()

return cmd
}
Expand Down Expand Up @@ -360,9 +368,9 @@ func Run(ctx context.Context, opts Options) error {
EnableHugepages: opts.EnableHugepages,
GCVMGracefulShutdownTimeout: opts.GCVMGracefulShutdownTimeout,
VolumeCachePolicy: opts.VolumeCachePolicy,
MetricsReconcileDuration: metrics.ControllerRuntimeReconcileDuration.WithLabelValues(controllers.MachineReconcilerName),
MetricsControllerRuntimeActiveWorker: metrics.ControllerRuntimeActiveWorker.WithLabelValues(controllers.MachineReconcilerName),
MetricsControllerRuntimeReconcileErrors: metrics.ControllerRuntimeReconcileErrors.WithLabelValues(controllers.MachineReconcilerName),
MetricsReconcileDuration: opts.MachineReconcilerMetrics.ReconcileDuration,
MetricsControllerRuntimeActiveWorker: opts.MachineReconcilerMetrics.ControllerRuntimeActiveWorker,
MetricsControllerRuntimeReconcileErrors: opts.MachineReconcilerMetrics.ControllerRuntimeReconcileErrors,
},
)
if err != nil {
Expand Down Expand Up @@ -473,7 +481,7 @@ func Run(ctx context.Context, opts Options) error {
return g.Wait()
}

func runGRPCServer(ctx context.Context, setupLog logr.Logger, log logr.Logger, srv *server.Server, opts Options) error {
func runGRPCServer(ctx context.Context, setupLog, log logr.Logger, srv *server.Server, opts Options) error {
setupLog.V(1).Info("Cleaning up any previous socket")
if err := common.CleanupSocketIfExists(opts.Address); err != nil {
return fmt.Errorf("error cleaning up socket: %w", err)
Expand Down
16 changes: 16 additions & 0 deletions cmd/libvirt-provider/app/option_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@ import (
"reflect"
"slices"

"github.com/prometheus/client_golang/prometheus"

"github.com/ironcore-dev/libvirt-provider/api"
"github.com/ironcore-dev/libvirt-provider/internal/controllers"
"github.com/ironcore-dev/libvirt-provider/internal/metrics"
)

type ReconcilerMetricsOptions struct {
ReconcileDuration prometheus.Observer
ControllerRuntimeActiveWorker prometheus.Gauge
ControllerRuntimeReconcileErrors prometheus.Counter
}

func (r *ReconcilerMetricsOptions) SetMachineReconcilerMetricsOptions() {
r.ReconcileDuration = metrics.ControllerRuntimeReconcileDuration.WithLabelValues(controllers.MachineReconcilerName)
r.ControllerRuntimeActiveWorker = metrics.ControllerRuntimeActiveWorker.WithLabelValues(controllers.MachineReconcilerName)
r.ControllerRuntimeReconcileErrors = metrics.ControllerRuntimeReconcileErrors.WithLabelValues(controllers.MachineReconcilerName)
}

type GuestAgentOption api.GuestAgent

func (g *GuestAgentOption) String() string {
Expand Down
37 changes: 18 additions & 19 deletions internal/controllers/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import (
"sync"
"time"

"github.com/digitalocean/go-libvirt"
libvirt "github.com/digitalocean/go-libvirt"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
"libvirt.org/go/libvirtxml"

"github.com/ironcore-dev/libvirt-provider/api"
"github.com/ironcore-dev/libvirt-provider/internal/event"
machineEvent "github.com/ironcore-dev/libvirt-provider/internal/event/machineevent"
Expand All @@ -32,12 +35,11 @@ import (
"github.com/ironcore-dev/libvirt-provider/internal/raw"
"github.com/ironcore-dev/libvirt-provider/internal/store"
"github.com/ironcore-dev/libvirt-provider/internal/utils"
"github.com/prometheus/client_golang/prometheus"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/ptr"
"libvirt.org/go/libvirtxml"
)

const (
Expand All @@ -52,20 +54,18 @@ const (
MachineReconcilerOpsLibvirtEvent = "libvirt-event"
)

var (
// TODO: improve domainStateToMachineState since some states are mapped to computev1alpha1.MachineStatePending
// where it doesn't make that much sense.
domainStateToMachineState = map[libvirt.DomainState]api.MachineState{
libvirt.DomainNostate: api.MachineStatePending,
libvirt.DomainRunning: api.MachineStateRunning,
libvirt.DomainBlocked: api.MachineStatePending,
libvirt.DomainPaused: api.MachineStatePending,
libvirt.DomainShutdown: api.MachineStateTerminating,
// it isn't probably supported by transient domain
libvirt.DomainShutoff: api.MachineStateTerminated,
libvirt.DomainPmsuspended: api.MachineStatePending,
}
)
// TODO: improve domainStateToMachineState since some states are mapped to computev1alpha1.MachineStatePending
// where it doesn't make that much sense.
var domainStateToMachineState = map[libvirt.DomainState]api.MachineState{
libvirt.DomainNostate: api.MachineStatePending,
libvirt.DomainRunning: api.MachineStateRunning,
libvirt.DomainBlocked: api.MachineStatePending,
libvirt.DomainPaused: api.MachineStatePending,
libvirt.DomainShutdown: api.MachineStateTerminating,
// it isn't probably supported by transient domain
libvirt.DomainShutoff: api.MachineStateTerminated,
libvirt.DomainPmsuspended: api.MachineStatePending,
}

type MachineReconcilerOptions struct {
GuestCapabilities guest.Capabilities
Expand Down Expand Up @@ -169,7 +169,7 @@ type MachineReconciler struct {
func (r *MachineReconciler) Start(ctx context.Context) error {
log := r.log

//todo make configurable
// todo make configurable
workerSize := 15
metrics.ControllerRuntimeMaxConccurrentReconciles.WithLabelValues(MachineReconcilerName).Set(float64(workerSize))

Expand Down Expand Up @@ -371,7 +371,6 @@ func (r *MachineReconciler) startGarbageCollector(ctx context.Context) {
logger.Error(err, "failed to garbage collect machine")
}
}

}, r.resyncIntervalGarbageCollector)
}

Expand Down

0 comments on commit af10519

Please sign in to comment.