/
spec.go
100 lines (81 loc) · 2.06 KB
/
spec.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
package cluster
import (
"context"
application "github.com/giantswarm/apiextensions-application/api/v1alpha1"
infrastructure "github.com/giantswarm/apiextensions/v6/pkg/apis/infrastructure/v1alpha3"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
capz "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
capi "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type GetOptions struct {
Name string
Provider string
Namespace string
FallbackToCapi bool
}
type PatchOptions struct {
PatchSpecs []PatchSpec
}
type PatchSpec struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
// Interface represents the contract for the clusters service.
// Using this instead of a regular 'struct' makes mocking the
// service in tests much simpler.
type Interface interface {
Get(context.Context, GetOptions) (Resource, error)
Patch(context.Context, client.Object, PatchOptions) error
}
type Resource interface {
Object() runtime.Object
}
// Cluster contains the resources needed to represent a cluster on any supported provider.
type Cluster struct {
Cluster *capi.Cluster
// helm-based clusters
ClusterApp *application.App
DefaultAppsApp *application.App
// infrastructure provider cluster
AWSCluster *infrastructure.AWSCluster
AzureCluster *capz.AzureCluster
}
func (n *Cluster) Object() runtime.Object {
if n.Cluster != nil {
return n.Cluster
}
return nil
}
func (n *Cluster) ClientObject() client.Object {
if n.Cluster != nil {
return n.Cluster
}
return nil
}
// Collection wraps a list of clusters.
type Collection struct {
Items []Cluster
}
func (cc *Collection) Object() runtime.Object {
list := &meta.List{
TypeMeta: meta.TypeMeta{
Kind: "List",
APIVersion: "v1",
},
ListMeta: meta.ListMeta{},
}
for _, item := range cc.Items {
obj := item.Object()
if obj == nil {
continue
}
raw := runtime.RawExtension{
Object: obj,
}
list.Items = append(list.Items, raw)
}
return list
}