-
Notifications
You must be signed in to change notification settings - Fork 153
/
adapters.go
208 lines (164 loc) · 5.13 KB
/
adapters.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
package internal
import (
"errors"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
pb "github.com/weaveworks/weave-gitops/pkg/api/core"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Reconcilable represents a Kubernetes object that Flux can reconcile
type Reconcilable interface {
client.Object
GetAnnotations() map[string]string
SetAnnotations(map[string]string)
GetStatusConditions() *[]metav1.Condition
GetLastHandledReconcileRequest() string
AsClientObject() client.Object
GroupVersionKind() schema.GroupVersionKind
}
type SourceRef interface {
APIVersion() string
Kind() string
Name() string
Namespace() string
}
type Automation interface {
Reconcilable
SourceRef() SourceRef
}
func NewReconcileableSource(obj client.Object) Reconcilable {
switch o := obj.(type) {
case *sourcev1.GitRepository:
return GitRepositoryAdapter{GitRepository: o}
case *sourcev1.HelmRepository:
return HelmRepositoryAdapter{HelmRepository: o}
case *sourcev1.Bucket:
return BucketAdapter{Bucket: o}
case *sourcev1.HelmChart:
return HelmChartAdapter{HelmChart: o}
}
return nil
}
type GitRepositoryAdapter struct {
*sourcev1.GitRepository
}
func (o GitRepositoryAdapter) GetLastHandledReconcileRequest() string {
return o.Status.GetLastHandledReconcileRequest()
}
func (o GitRepositoryAdapter) AsClientObject() client.Object {
return o.GitRepository
}
func (o GitRepositoryAdapter) GroupVersionKind() schema.GroupVersionKind {
return sourcev1.GroupVersion.WithKind(sourcev1.GitRepositoryKind)
}
type BucketAdapter struct {
*sourcev1.Bucket
}
func (obj BucketAdapter) GetLastHandledReconcileRequest() string {
return obj.Status.GetLastHandledReconcileRequest()
}
func (obj BucketAdapter) AsClientObject() client.Object {
return obj.Bucket
}
func (o BucketAdapter) GroupVersionKind() schema.GroupVersionKind {
return sourcev1.GroupVersion.WithKind(sourcev1.BucketKind)
}
type HelmChartAdapter struct {
*sourcev1.HelmChart
}
func (obj HelmChartAdapter) GetLastHandledReconcileRequest() string {
return obj.Status.GetLastHandledReconcileRequest()
}
func (obj HelmChartAdapter) AsClientObject() client.Object {
return obj.HelmChart
}
func (o HelmChartAdapter) GroupVersionKind() schema.GroupVersionKind {
return sourcev1.GroupVersion.WithKind(sourcev1.HelmChartKind)
}
type HelmRepositoryAdapter struct {
*sourcev1.HelmRepository
}
func (obj HelmRepositoryAdapter) GetLastHandledReconcileRequest() string {
return obj.Status.GetLastHandledReconcileRequest()
}
func (obj HelmRepositoryAdapter) AsClientObject() client.Object {
return obj.HelmRepository
}
func (o HelmRepositoryAdapter) GroupVersionKind() schema.GroupVersionKind {
return sourcev1.GroupVersion.WithKind(sourcev1.HelmChartKind)
}
type HelmReleaseAdapter struct {
*helmv2.HelmRelease
}
func (obj HelmReleaseAdapter) GetLastHandledReconcileRequest() string {
return obj.Status.GetLastHandledReconcileRequest()
}
func (obj HelmReleaseAdapter) AsClientObject() client.Object {
return obj.HelmRelease
}
func (o HelmReleaseAdapter) SourceRef() SourceRef {
sr := o.Spec.Chart.Spec.SourceRef
return sRef{
apiVersion: sr.APIVersion,
name: sr.Name,
namespace: sr.Namespace,
kind: sr.Kind,
}
}
func (o HelmReleaseAdapter) GroupVersionKind() schema.GroupVersionKind {
return helmv2.GroupVersion.WithKind(helmv2.HelmReleaseKind)
}
type KustomizationAdapter struct {
*kustomizev1.Kustomization
}
func (o KustomizationAdapter) GetLastHandledReconcileRequest() string {
return o.Status.GetLastHandledReconcileRequest()
}
func (o KustomizationAdapter) AsClientObject() client.Object {
return o.Kustomization
}
func (o KustomizationAdapter) SourceRef() SourceRef {
return sRef{
apiVersion: o.Spec.SourceRef.APIVersion,
name: o.Spec.SourceRef.Name,
namespace: o.Spec.SourceRef.Namespace,
kind: o.Spec.SourceRef.Kind,
}
}
func (o KustomizationAdapter) GroupVersionKind() schema.GroupVersionKind {
return kustomizev1.GroupVersion.WithKind(kustomizev1.KustomizationKind)
}
type sRef struct {
apiVersion string
name string
kind string
namespace string
}
func (s sRef) APIVersion() string {
return s.apiVersion
}
func (s sRef) Name() string {
return s.name
}
func (s sRef) Namespace() string {
return s.namespace
}
func (s sRef) Kind() string {
return s.kind
}
func ToReconcileableSource(sourceType pb.SourceRef_SourceKind) (client.ObjectList, Reconcilable, error) {
switch sourceType {
case pb.SourceRef_GitRepository:
return &sourcev1.GitRepositoryList{}, NewReconcileableSource(&sourcev1.GitRepository{}), nil
case pb.SourceRef_Bucket:
return &sourcev1.GitRepositoryList{}, NewReconcileableSource(&sourcev1.Bucket{}), nil
case pb.SourceRef_HelmRepository:
return &sourcev1.GitRepositoryList{}, NewReconcileableSource(&sourcev1.HelmRepository{}), nil
case pb.SourceRef_HelmChart:
return &sourcev1.GitRepositoryList{}, NewReconcileableSource(&sourcev1.HelmChart{}), nil
}
return nil, nil, errors.New("could not find source type")
}