Skip to content

Commit b6806cb

Browse files
authored
Replaced store with provider-utils (#526)
Signed-off-by: Lukas Frank <[email protected]>
1 parent 4801e32 commit b6806cb

34 files changed

+289
-1589
lines changed

api/api.go

Lines changed: 0 additions & 103 deletions
This file was deleted.

api/apiutils.go

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44
package api
55

66
import (
7-
"encoding/json"
8-
"fmt"
9-
107
"github.com/ironcore-dev/controller-utils/metautils"
118
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
9+
apiutils "github.com/ironcore-dev/provider-utils/apiutils/api"
1210
)
1311

14-
func GetObjectMetadata(o Metadata) (*irimeta.ObjectMetadata, error) {
15-
annotations, err := GetAnnotationsAnnotation(o)
12+
func GetObjectMetadata(o apiutils.Metadata) (*irimeta.ObjectMetadata, error) {
13+
annotations, err := apiutils.GetAnnotationsAnnotation(o, AnnotationsAnnotation)
1614
if err != nil {
1715
return nil, err
1816
}
1917

20-
labels, err := GetLabelsAnnotation(o)
18+
labels, err := apiutils.GetLabelsAnnotation(o, LabelsAnnotation)
2119
if err != nil {
2220
return nil, err
2321
}
@@ -37,77 +35,30 @@ func GetObjectMetadata(o Metadata) (*irimeta.ObjectMetadata, error) {
3735
}, nil
3836
}
3937

40-
func SetObjectMetadata(o Object, metadata *irimeta.ObjectMetadata) error {
41-
if err := SetAnnotationsAnnotation(o, metadata.Annotations); err != nil {
38+
func SetObjectMetadata(o apiutils.Object, metadata *irimeta.ObjectMetadata) error {
39+
if err := apiutils.SetAnnotationsAnnotation(o, AnnotationsAnnotation, metadata.Annotations); err != nil {
4240
return err
4341
}
44-
if err := SetLabelsAnnotation(o, metadata.Labels); err != nil {
42+
if err := apiutils.SetLabelsAnnotation(o, LabelsAnnotation, metadata.Labels); err != nil {
4543
return err
4644
}
4745
return nil
4846
}
4947

50-
func SetLabelsAnnotation(o Object, labels map[string]string) error {
51-
data, err := json.Marshal(labels)
52-
if err != nil {
53-
return fmt.Errorf("error marshalling labels: %w", err)
54-
}
55-
metautils.SetAnnotation(o, LabelsAnnotation, string(data))
56-
return nil
57-
}
58-
59-
func GetLabelsAnnotation(o Metadata) (map[string]string, error) {
60-
data, ok := o.GetAnnotations()[LabelsAnnotation]
61-
if !ok {
62-
return nil, fmt.Errorf("object has no labels at %s", LabelsAnnotation)
63-
}
64-
65-
var labels map[string]string
66-
if err := json.Unmarshal([]byte(data), &labels); err != nil {
67-
return nil, err
68-
}
69-
70-
return labels, nil
71-
}
72-
73-
func SetAnnotationsAnnotation(o Object, annotations map[string]string) error {
74-
data, err := json.Marshal(annotations)
75-
if err != nil {
76-
return fmt.Errorf("error marshalling annotations: %w", err)
77-
}
78-
metautils.SetAnnotation(o, AnnotationsAnnotation, string(data))
79-
80-
return nil
81-
}
82-
83-
func GetAnnotationsAnnotation(o Metadata) (map[string]string, error) {
84-
data, ok := o.GetAnnotations()[AnnotationsAnnotation]
85-
if !ok {
86-
return nil, fmt.Errorf("object has no annotations at %s", AnnotationsAnnotation)
87-
}
88-
89-
var annotations map[string]string
90-
if err := json.Unmarshal([]byte(data), &annotations); err != nil {
91-
return nil, err
92-
}
93-
94-
return annotations, nil
95-
}
96-
97-
func SetManagerLabel(o Object, manager string) {
48+
func SetManagerLabel(o apiutils.Object, manager string) {
9849
metautils.SetLabel(o, ManagerLabel, manager)
9950
}
10051

101-
func SetClassLabel(o Object, class string) {
52+
func SetClassLabel(o apiutils.Object, class string) {
10253
metautils.SetLabel(o, ClassLabel, class)
10354
}
10455

105-
func GetClassLabel(o Object) (string, bool) {
56+
func GetClassLabel(o apiutils.Object) (string, bool) {
10657
class, found := o.GetLabels()[ClassLabel]
10758
return class, found
10859
}
10960

110-
func IsManagedBy(o Object, manager string) bool {
61+
func IsManagedBy(o apiutils.Object, manager string) bool {
11162
actual, ok := o.GetLabels()[ManagerLabel]
11263
return ok && actual == manager
11364
}

api/machine.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package api
55

66
import (
77
"time"
8+
9+
apiutils "github.com/ironcore-dev/provider-utils/apiutils/api"
810
)
911

1012
type Machine struct {
11-
Metadata `json:"metadata,omitempty"`
13+
apiutils.Metadata `json:"metadata,omitempty"`
1214

1315
Spec MachineSpec `json:"spec"`
1416
Status MachineStatus `json:"status"`

cmd/libvirt-provider/app/app.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ import (
1818

1919
"github.com/go-logr/logr"
2020
"github.com/ironcore-dev/ironcore-image/oci/remote"
21+
ocistore "github.com/ironcore-dev/ironcore-image/oci/store"
2122
"github.com/ironcore-dev/ironcore/broker/common"
2223
commongrpc "github.com/ironcore-dev/ironcore/broker/common/grpc"
2324
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
2425
"github.com/ironcore-dev/libvirt-provider/api"
2526
"github.com/ironcore-dev/libvirt-provider/internal/console"
2627
"github.com/ironcore-dev/libvirt-provider/internal/controllers"
27-
"github.com/ironcore-dev/libvirt-provider/internal/event"
28-
"github.com/ironcore-dev/libvirt-provider/internal/event/machineevent"
2928
"github.com/ironcore-dev/libvirt-provider/internal/healthcheck"
3029
"github.com/ironcore-dev/libvirt-provider/internal/host"
3130
"github.com/ironcore-dev/libvirt-provider/internal/libvirt/guest"
@@ -36,10 +35,12 @@ import (
3635
volumeplugin "github.com/ironcore-dev/libvirt-provider/internal/plugins/volume"
3736
"github.com/ironcore-dev/libvirt-provider/internal/plugins/volume/ceph"
3837
"github.com/ironcore-dev/libvirt-provider/internal/plugins/volume/emptydisk"
39-
"github.com/ironcore-dev/libvirt-provider/internal/qcow2"
4038
"github.com/ironcore-dev/libvirt-provider/internal/raw"
4139
"github.com/ironcore-dev/libvirt-provider/internal/server"
4240
"github.com/ironcore-dev/libvirt-provider/internal/strategy"
41+
"github.com/ironcore-dev/provider-utils/eventutils/event"
42+
"github.com/ironcore-dev/provider-utils/eventutils/recorder"
43+
hostutils "github.com/ironcore-dev/provider-utils/storeutils/host"
4344
"github.com/prometheus/client_golang/prometheus/promhttp"
4445
"github.com/spf13/cobra"
4546
"github.com/spf13/pflag"
@@ -79,7 +80,7 @@ type Options struct {
7980
GCVMGracefulShutdownTimeout time.Duration
8081
ResyncIntervalGarbageCollector time.Duration
8182

82-
MachineEventStore machineevent.EventStoreOptions
83+
MachineEventStore recorder.EventStoreOptions
8384

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

137-
fs.StringVar(&o.Libvirt.Qcow2Type, "qcow2-type", qcow2.Default(), fmt.Sprintf("qcow2 implementation to use. Available: %v", qcow2.Available()))
138-
139138
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.")
140139
fs.DurationVar(&o.ResyncIntervalGarbageCollector, "gc-resync-interval", 1*time.Minute, "Interval for resynchronizing the garbage collector.")
141140

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

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

230-
imgCache, err := oci.NewLocalCache(log, reg, providerHost.OCIStore())
229+
ociStore, err := ocistore.New(providerHost.ImagesDir())
231230
if err != nil {
232-
setupLog.Error(err, "failed to initialize oci manager")
231+
setupLog.Error(err, "error creating oci store")
233232
return err
234233
}
235234

236-
qcow2Inst, err := qcow2.Instance(opts.Libvirt.Qcow2Type)
235+
imgCache, err := oci.NewLocalCache(log, reg, ociStore)
237236
if err != nil {
238-
setupLog.Error(err, "failed to initialize qcow2 instance")
237+
setupLog.Error(err, "failed to initialize oci manager")
239238
return err
240239
}
241240

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

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

304-
eventStore := machineevent.NewEventStore(log, opts.MachineEventStore)
303+
eventStore := recorder.NewEventStore(log, opts.MachineEventStore)
305304

306305
machineReconciler, err := controllers.NewMachineReconciler(
307306
log.WithName("machine-reconciler"),
308-
libvirt,
307+
providerHost,
309308
machineStore,
310309
machineEvents,
311310
eventStore,
312311
controllers.MachineReconcilerOptions{
313312
GuestCapabilities: caps,
314313
ImageCache: imgCache,
315314
Raw: rawInst,
316-
Host: providerHost,
317315
VolumePluginManager: volumePlugins,
318316
NetworkInterfacePlugin: nicPlugin,
319317
ResyncIntervalVolumeSize: opts.ResyncIntervalVolumeSize,

0 commit comments

Comments
 (0)