/
machine.go
239 lines (194 loc) · 6.66 KB
/
machine.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Copyright 2023 Teppei Sudo.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scope
import (
"context"
"github.com/k8s-proxmox/proxmox-go/api"
"github.com/k8s-proxmox/proxmox-go/proxmox"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/noderefutil"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1 "github.com/k8s-proxmox/cluster-api-provider-proxmox/api/v1beta1"
"github.com/k8s-proxmox/cluster-api-provider-proxmox/cloud/providerid"
"github.com/k8s-proxmox/cluster-api-provider-proxmox/cloud/scheduler"
)
type MachineScopeParams struct {
ProxmoxServices
Client client.Client
Machine *clusterv1.Machine
ProxmoxMachine *infrav1.ProxmoxMachine
ClusterGetter *ClusterScope
SchedulerManager *scheduler.Manager
}
func NewMachineScope(params MachineScopeParams) (*MachineScope, error) {
if params.Client == nil {
return nil, errors.New("client is required when creating a MachineScope")
}
if params.Machine == nil {
return nil, errors.New("failed to generate new scope from nil Machine")
}
if params.ProxmoxMachine == nil {
return nil, errors.New("failed to generate new scope from nil ProxmoxMachine")
}
if params.ClusterGetter == nil {
return nil, errors.New("failed to generate new scope form nil ClusterScope")
}
if params.SchedulerManager == nil {
return nil, errors.New("failed to generate new scope form nil SchedulerManager")
}
helper, err := patch.NewHelper(params.ProxmoxMachine, params.Client)
if err != nil {
return nil, errors.Wrap(err, "failed to init patch helper")
}
return &MachineScope{
client: params.Client,
Machine: params.Machine,
ProxmoxMachine: params.ProxmoxMachine,
patchHelper: helper,
ClusterGetter: params.ClusterGetter,
SchedulerManager: params.SchedulerManager,
}, err
}
type MachineScope struct {
client client.Client
patchHelper *patch.Helper
Machine *clusterv1.Machine
ProxmoxMachine *infrav1.ProxmoxMachine
ClusterGetter *ClusterScope
SchedulerManager *scheduler.Manager
}
func (m *MachineScope) CloudClient() *proxmox.Service {
return m.ClusterGetter.CloudClient()
}
func (m *MachineScope) GetScheduler(client *proxmox.Service) *scheduler.Scheduler {
sched := m.SchedulerManager.GetOrCreateScheduler(client)
sched.RunAsync()
return sched
}
func (m *MachineScope) GetClusterStorage() infrav1.Storage {
return m.ClusterGetter.Storage()
}
func (m *MachineScope) GetStorage() string {
return m.ProxmoxMachine.Spec.Storage
}
func (m *MachineScope) Name() string {
return m.ProxmoxMachine.Name
}
func (m *MachineScope) Namespace() string {
return m.ProxmoxMachine.Namespace
}
func (m *MachineScope) Annotations() map[string]string {
return m.ProxmoxMachine.Annotations
}
func (m *MachineScope) NodeName() string {
return m.ProxmoxMachine.Spec.Node
}
func (m *MachineScope) SetNodeName(name string) {
m.ProxmoxMachine.Spec.Node = name
}
func (m *MachineScope) SetStorage(name string) {
m.ProxmoxMachine.Spec.Storage = name
}
// func (m *MachineScope) Client() Compute {
// return m.ClusterGetter.Client()
// }
func (m *MachineScope) GetBootstrapData() (string, error) {
if m.Machine.Spec.Bootstrap.DataSecretName == nil {
return "", errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
}
secret := &corev1.Secret{}
key := types.NamespacedName{Namespace: m.Namespace(), Name: *m.Machine.Spec.Bootstrap.DataSecretName}
if err := m.client.Get(context.TODO(), key, secret); err != nil {
return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for ProxmoxMachine %s/%s", m.Namespace(), m.Name())
}
value, ok := secret.Data["value"]
if !ok {
return "", errors.New("error retrieving bootstrap data: secret value key is missing")
}
return string(value), nil
}
func (m *MachineScope) Close() error {
return m.PatchObject()
}
func (m *MachineScope) GetInstanceStatus() *infrav1.InstanceStatus {
return m.ProxmoxMachine.Status.InstanceStatus
}
// SetInstanceStatus sets the ProxmoxMachine instance status.
func (m *MachineScope) SetInstanceStatus(v infrav1.InstanceStatus) {
m.ProxmoxMachine.Status.InstanceStatus = &v
}
func (m *MachineScope) GetBiosUUID() *string {
parsed, err := noderefutil.NewProviderID(m.GetProviderID()) //nolint: staticcheck
if err != nil {
return nil
}
return pointer.String(parsed.ID()) //nolint: staticcheck
}
func (m *MachineScope) GetProviderID() string {
if m.ProxmoxMachine.Spec.ProviderID != nil {
return *m.ProxmoxMachine.Spec.ProviderID
}
return ""
}
func (m *MachineScope) GetVMID() *int {
return m.ProxmoxMachine.Spec.VMID
}
func (m *MachineScope) GetImage() infrav1.Image {
return m.ProxmoxMachine.Spec.Image
}
func (m *MachineScope) GetCloudInit() infrav1.CloudInit {
return m.ProxmoxMachine.Spec.CloudInit
}
func (m *MachineScope) GetNetwork() infrav1.Network {
return m.ProxmoxMachine.Spec.Network
}
func (m *MachineScope) GetHardware() infrav1.Hardware {
return m.ProxmoxMachine.Spec.Hardware
}
func (m *MachineScope) GetOptions() infrav1.Options {
return m.ProxmoxMachine.Spec.Options
}
// SetProviderID sets the ProxmoxMachine providerID in spec.
func (m *MachineScope) SetProviderID(uuid string) error {
providerid, err := providerid.New(uuid)
if err != nil {
return err
}
m.ProxmoxMachine.Spec.ProviderID = pointer.String(providerid.String())
return nil
}
func (m *MachineScope) SetVMID(vmid int) {
m.ProxmoxMachine.Spec.VMID = &vmid
}
func (m *MachineScope) SetConfigStatus(config api.VirtualMachineConfig) {
m.ProxmoxMachine.Status.Config = config
}
func (m *MachineScope) SetReady() {
m.ProxmoxMachine.Status.Ready = true
}
func (m *MachineScope) SetFailureMessage(v error) {
m.ProxmoxMachine.Status.FailureMessage = pointer.String(v.Error())
}
func (m *MachineScope) SetFailureReason(v capierrors.MachineStatusError) {
m.ProxmoxMachine.Status.FailureReason = &v
}
// PatchObject persists the cluster configuration and status.
func (s *MachineScope) PatchObject() error {
return s.patchHelper.Patch(context.TODO(), s.ProxmoxMachine)
}