Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 0 additions & 103 deletions api/api.go

This file was deleted.

71 changes: 11 additions & 60 deletions api/apiutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
package api

import (
"encoding/json"
"fmt"

"github.com/ironcore-dev/controller-utils/metautils"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
apiutils "github.com/ironcore-dev/provider-utils/apiutils/api"
)

func GetObjectMetadata(o Metadata) (*irimeta.ObjectMetadata, error) {
annotations, err := GetAnnotationsAnnotation(o)
func GetObjectMetadata(o apiutils.Metadata) (*irimeta.ObjectMetadata, error) {
annotations, err := apiutils.GetAnnotationsAnnotation(o, AnnotationsAnnotation)
if err != nil {
return nil, err
}

labels, err := GetLabelsAnnotation(o)
labels, err := apiutils.GetLabelsAnnotation(o, LabelsAnnotation)
if err != nil {
return nil, err
}
Expand All @@ -37,77 +35,30 @@ func GetObjectMetadata(o Metadata) (*irimeta.ObjectMetadata, error) {
}, nil
}

func SetObjectMetadata(o Object, metadata *irimeta.ObjectMetadata) error {
if err := SetAnnotationsAnnotation(o, metadata.Annotations); err != nil {
func SetObjectMetadata(o apiutils.Object, metadata *irimeta.ObjectMetadata) error {
if err := apiutils.SetAnnotationsAnnotation(o, AnnotationsAnnotation, metadata.Annotations); err != nil {
return err
}
if err := SetLabelsAnnotation(o, metadata.Labels); err != nil {
if err := apiutils.SetLabelsAnnotation(o, LabelsAnnotation, metadata.Labels); err != nil {
return err
}
return nil
}

func SetLabelsAnnotation(o Object, labels map[string]string) error {
data, err := json.Marshal(labels)
if err != nil {
return fmt.Errorf("error marshalling labels: %w", err)
}
metautils.SetAnnotation(o, LabelsAnnotation, string(data))
return nil
}

func GetLabelsAnnotation(o Metadata) (map[string]string, error) {
data, ok := o.GetAnnotations()[LabelsAnnotation]
if !ok {
return nil, fmt.Errorf("object has no labels at %s", LabelsAnnotation)
}

var labels map[string]string
if err := json.Unmarshal([]byte(data), &labels); err != nil {
return nil, err
}

return labels, nil
}

func SetAnnotationsAnnotation(o Object, annotations map[string]string) error {
data, err := json.Marshal(annotations)
if err != nil {
return fmt.Errorf("error marshalling annotations: %w", err)
}
metautils.SetAnnotation(o, AnnotationsAnnotation, string(data))

return nil
}

func GetAnnotationsAnnotation(o Metadata) (map[string]string, error) {
data, ok := o.GetAnnotations()[AnnotationsAnnotation]
if !ok {
return nil, fmt.Errorf("object has no annotations at %s", AnnotationsAnnotation)
}

var annotations map[string]string
if err := json.Unmarshal([]byte(data), &annotations); err != nil {
return nil, err
}

return annotations, nil
}

func SetManagerLabel(o Object, manager string) {
func SetManagerLabel(o apiutils.Object, manager string) {
metautils.SetLabel(o, ManagerLabel, manager)
}

func SetClassLabel(o Object, class string) {
func SetClassLabel(o apiutils.Object, class string) {
metautils.SetLabel(o, ClassLabel, class)
}

func GetClassLabel(o Object) (string, bool) {
func GetClassLabel(o apiutils.Object) (string, bool) {
class, found := o.GetLabels()[ClassLabel]
return class, found
}

func IsManagedBy(o Object, manager string) bool {
func IsManagedBy(o apiutils.Object, manager string) bool {
actual, ok := o.GetLabels()[ManagerLabel]
return ok && actual == manager
}
4 changes: 3 additions & 1 deletion api/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package api

import (
"time"

apiutils "github.com/ironcore-dev/provider-utils/apiutils/api"
)

type Machine struct {
Metadata `json:"metadata,omitempty"`
apiutils.Metadata `json:"metadata,omitempty"`

Spec MachineSpec `json:"spec"`
Status MachineStatus `json:"status"`
Expand Down
34 changes: 16 additions & 18 deletions cmd/libvirt-provider/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (

"github.com/go-logr/logr"
"github.com/ironcore-dev/ironcore-image/oci/remote"
ocistore "github.com/ironcore-dev/ironcore-image/oci/store"
"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/ironcore-dev/libvirt-provider/api"
"github.com/ironcore-dev/libvirt-provider/internal/console"
"github.com/ironcore-dev/libvirt-provider/internal/controllers"
"github.com/ironcore-dev/libvirt-provider/internal/event"
"github.com/ironcore-dev/libvirt-provider/internal/event/machineevent"
"github.com/ironcore-dev/libvirt-provider/internal/healthcheck"
"github.com/ironcore-dev/libvirt-provider/internal/host"
"github.com/ironcore-dev/libvirt-provider/internal/libvirt/guest"
Expand All @@ -36,10 +35,12 @@ import (
volumeplugin "github.com/ironcore-dev/libvirt-provider/internal/plugins/volume"
"github.com/ironcore-dev/libvirt-provider/internal/plugins/volume/ceph"
"github.com/ironcore-dev/libvirt-provider/internal/plugins/volume/emptydisk"
"github.com/ironcore-dev/libvirt-provider/internal/qcow2"
"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/ironcore-dev/provider-utils/eventutils/event"
"github.com/ironcore-dev/provider-utils/eventutils/recorder"
hostutils "github.com/ironcore-dev/provider-utils/storeutils/host"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -79,7 +80,7 @@ type Options struct {
GCVMGracefulShutdownTimeout time.Duration
ResyncIntervalGarbageCollector time.Duration

MachineEventStore machineevent.EventStoreOptions
MachineEventStore recorder.EventStoreOptions

VolumeCachePolicy string
}
Expand Down Expand Up @@ -134,15 +135,13 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(&o.Libvirt.PreferredDomainTypes, "preferred-domain-types", []string{"kvm", "qemu"}, "Ordered list of preferred domain types to use.")
fs.StringSliceVar(&o.Libvirt.PreferredMachineTypes, "preferred-machine-types", []string{"pc-q35"}, "Ordered list of preferred machine types to use.")

fs.StringVar(&o.Libvirt.Qcow2Type, "qcow2-type", qcow2.Default(), fmt.Sprintf("qcow2 implementation to use. Available: %v", qcow2.Available()))

fs.DurationVar(&o.GCVMGracefulShutdownTimeout, "gc-vm-graceful-shutdown-timeout", 5*time.Minute, "Duration to wait for the VM to gracefully shut down. If the VM does not shut down within this period, it will be forcibly destroyed by garbage collector.")
fs.DurationVar(&o.ResyncIntervalGarbageCollector, "gc-resync-interval", 1*time.Minute, "Interval for resynchronizing the garbage collector.")

// Machine event store options
fs.IntVar(&o.MachineEventStore.MachineEventMaxEvents, "machine-event-max-events", 100, "Maximum number of machine events that can be stored.")
fs.DurationVar(&o.MachineEventStore.MachineEventTTL, "machine-event-ttl", 5*time.Minute, "Time to live for machine events.")
fs.DurationVar(&o.MachineEventStore.MachineEventResyncInterval, "machine-event-resync-interval", 1*time.Minute, "Interval for resynchronizing the machine events.")
fs.IntVar(&o.MachineEventStore.MaxEvents, "machine-event-max-events", 100, "Maximum number of machine events that can be stored.")
fs.DurationVar(&o.MachineEventStore.TTL, "machine-event-ttl", 5*time.Minute, "Time to live for machine events.")
fs.DurationVar(&o.MachineEventStore.ResyncInterval, "machine-event-resync-interval", 1*time.Minute, "Interval for resynchronizing the machine events.")

// Volume cache policy option
fs.StringVar(&o.VolumeCachePolicy, "volume-cache-policy", "none",
Expand Down Expand Up @@ -227,15 +226,15 @@ func Run(ctx context.Context, opts Options) error {
return err
}

imgCache, err := oci.NewLocalCache(log, reg, providerHost.OCIStore())
ociStore, err := ocistore.New(providerHost.ImagesDir())
if err != nil {
setupLog.Error(err, "failed to initialize oci manager")
setupLog.Error(err, "error creating oci store")
return err
}

qcow2Inst, err := qcow2.Instance(opts.Libvirt.Qcow2Type)
imgCache, err := oci.NewLocalCache(log, reg, ociStore)
if err != nil {
setupLog.Error(err, "failed to initialize qcow2 instance")
setupLog.Error(err, "failed to initialize oci manager")
return err
}

Expand All @@ -258,7 +257,7 @@ func Run(ctx context.Context, opts Options) error {
volumePlugins := volumeplugin.NewPluginManager()
if err := volumePlugins.InitPlugins(providerHost, []volumeplugin.Plugin{
ceph.NewPlugin(),
emptydisk.NewPlugin(qcow2Inst, rawInst),
emptydisk.NewPlugin(rawInst),
}); err != nil {
setupLog.Error(err, "failed to initialize volume plugin manager")
return err
Expand All @@ -281,7 +280,7 @@ func Run(ctx context.Context, opts Options) error {
}

setupLog.Info("Configuring machine store", "Directory", providerHost.MachineStoreDir())
machineStore, err := host.NewStore(host.Options[*api.Machine]{
machineStore, err := hostutils.NewStore[*api.Machine](hostutils.Options[*api.Machine]{
NewFunc: func() *api.Machine { return &api.Machine{} },
CreateStrategy: strategy.MachineStrategy,
Dir: providerHost.MachineStoreDir(),
Expand All @@ -301,19 +300,18 @@ func Run(ctx context.Context, opts Options) error {
return err
}

eventStore := machineevent.NewEventStore(log, opts.MachineEventStore)
eventStore := recorder.NewEventStore(log, opts.MachineEventStore)

machineReconciler, err := controllers.NewMachineReconciler(
log.WithName("machine-reconciler"),
libvirt,
providerHost,
machineStore,
machineEvents,
eventStore,
controllers.MachineReconcilerOptions{
GuestCapabilities: caps,
ImageCache: imgCache,
Raw: rawInst,
Host: providerHost,
VolumePluginManager: volumePlugins,
NetworkInterfacePlugin: nicPlugin,
ResyncIntervalVolumeSize: opts.ResyncIntervalVolumeSize,
Expand Down
Loading
Loading