-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
110 lines (85 loc) · 2.92 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package server
import (
"context"
"fmt"
"net/url"
"path"
"sync"
"github.com/digitalocean/go-libvirt"
"github.com/go-logr/logr"
"github.com/google/uuid"
"github.com/ironcore-dev/ironcore/broker/common/idgen"
"github.com/ironcore-dev/ironcore/broker/common/request"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
"github.com/ironcore-dev/libvirt-provider/api"
providernetworkinterface "github.com/ironcore-dev/libvirt-provider/internal/plugins/networkinterface"
"github.com/ironcore-dev/libvirt-provider/internal/plugins/volume"
"github.com/ironcore-dev/libvirt-provider/internal/store"
"github.com/ironcore-dev/libvirt-provider/internal/utils"
ctrl "sigs.k8s.io/controller-runtime"
)
var _ iri.MachineRuntimeServer = (*Server)(nil)
type Server struct {
baseURL *url.URL
idGen idgen.IDGen
machineStore store.Store[*api.Machine]
networkInterfacePlugin providernetworkinterface.Plugin
volumePlugins *volume.PluginManager
machineClasses MachineClassRegistry
execRequestCache request.Cache[*iri.ExecRequest]
activeConsoles sync.Map
libvirt *libvirt.Libvirt
enableHugepages bool
guestAgent api.GuestAgent
}
type Options struct {
// BaseURL is the base URL in form http(s)://host:port/path?query to produce request URLs relative to.
BaseURL string
Libvirt *libvirt.Libvirt
IDGen idgen.IDGen
MachineStore store.Store[*api.Machine]
MachineClasses MachineClassRegistry
VolumePlugins *volume.PluginManager
NetworkPlugins providernetworkinterface.Plugin
EnableHugepages bool
GuestAgent api.GuestAgent
}
func setOptionsDefaults(o *Options) {
if o.IDGen == nil {
o.IDGen = utils.IdGenerateFunc(uuid.NewString)
}
}
func New(opts Options) (*Server, error) {
setOptionsDefaults(&opts)
baseURL, err := url.ParseRequestURI(opts.BaseURL)
if err != nil {
return nil, fmt.Errorf("invalid base url %q: %w", opts.BaseURL, err)
}
return &Server{
baseURL: baseURL,
idGen: opts.IDGen,
libvirt: opts.Libvirt,
machineStore: opts.MachineStore,
volumePlugins: opts.VolumePlugins,
networkInterfacePlugin: opts.NetworkPlugins,
machineClasses: opts.MachineClasses,
enableHugepages: opts.EnableHugepages,
guestAgent: opts.GuestAgent,
execRequestCache: request.NewCache[*iri.ExecRequest](),
activeConsoles: sync.Map{},
}, nil
}
func (s *Server) loggerFrom(ctx context.Context, keysWithValues ...interface{}) logr.Logger {
return ctrl.LoggerFrom(ctx, keysWithValues...)
}
type MachineClassRegistry interface {
Get(volumeClassName string) (*iri.MachineClass, bool)
List() []*iri.MachineClass
}
func (s *Server) buildURL(method string, token string) string {
return s.baseURL.ResolveReference(&url.URL{
Path: path.Join(method, token),
}).String()
}