-
Notifications
You must be signed in to change notification settings - Fork 64
/
iterator_v1alpha1.go
211 lines (163 loc) · 6.23 KB
/
iterator_v1alpha1.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package platform
import (
"context"
"fmt"
zip "api.zip"
"github.com/acorn-io/baaah/pkg/merr"
machinev1alpha1 "kraftkit.sh/api/machine/v1alpha1"
)
type machineV1alpha1ServiceIterator struct {
strategies map[Platform]machinev1alpha1.MachineService
}
// NewMachineV1alpha1ServiceIterator returns a
// machinev1alpha1.MachineService-compatible implementation which iterates over
// each supported host platform and calls the representing method. This is
// useful in circumstances where the platform is not supplied. The first
// platform strategy to succeed is returned in all circumstances.
func NewMachineV1alpha1ServiceIterator(ctx context.Context) (machinev1alpha1.MachineService, error) {
var err error
iterator := machineV1alpha1ServiceIterator{
strategies: map[Platform]machinev1alpha1.MachineService{},
}
for platform, strategy := range hostSupportedStrategies() {
iterator.strategies[platform], err = strategy.NewMachineV1alpha1(ctx)
if err != nil {
return nil, err
}
}
return &iterator, nil
}
// Create implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Create(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) {
var errs []error
for _, strategy := range iterator.strategies {
ret, err := strategy.Create(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return ret, nil
}
return machine, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}
// Start implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Start(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) {
var errs []error
for _, strategy := range iterator.strategies {
ret, err := strategy.Start(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return ret, nil
}
return machine, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}
// Pause implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Pause(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) {
var errs []error
for _, strategy := range iterator.strategies {
ret, err := strategy.Pause(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return ret, nil
}
return machine, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}
// Stop implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Stop(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) {
var errs []error
for _, strategy := range iterator.strategies {
ret, err := strategy.Stop(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return ret, nil
}
return machine, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}
// Update implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Update(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) {
var errs []error
for _, strategy := range iterator.strategies {
ret, err := strategy.Update(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return ret, nil
}
return machine, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}
// Delete implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Delete(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) {
var errs []error
for _, strategy := range iterator.strategies {
ret, err := strategy.Delete(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return ret, nil
}
return machine, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}
// Get implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Get(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) {
var errs []error
for _, strategy := range iterator.strategies {
ret, err := strategy.Get(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return ret, nil
}
return machine, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}
// List implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) List(ctx context.Context, cached *machinev1alpha1.MachineList) (*machinev1alpha1.MachineList, error) {
found := []zip.Object[machinev1alpha1.MachineSpec, machinev1alpha1.MachineStatus]{}
for _, strategy := range iterator.strategies {
ret, err := strategy.List(ctx, &machinev1alpha1.MachineList{})
if err != nil {
continue
}
found = append(found, ret.Items...)
}
cached.Items = found
return cached, nil
}
// Watch implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Watch(ctx context.Context, machine *machinev1alpha1.Machine) (chan *machinev1alpha1.Machine, chan error, error) {
var errs []error
for _, strategy := range iterator.strategies {
eventChan, errChan, err := strategy.Watch(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return eventChan, errChan, nil
}
return nil, nil, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}
// Logs implements kraftkit.sh/api/machine/v1alpha1.MachineService
func (iterator *machineV1alpha1ServiceIterator) Logs(ctx context.Context, machine *machinev1alpha1.Machine) (chan string, chan error, error) {
var errs []error
for _, strategy := range iterator.strategies {
logChan, errChan, err := strategy.Logs(ctx, machine)
if err != nil {
errs = append(errs, err)
continue
}
return logChan, errChan, nil
}
return nil, nil, fmt.Errorf("all iterated platforms failed: %w", merr.NewErrors(errs...))
}