@@ -3,35 +3,30 @@ package appliance
3
3
import (
4
4
"context"
5
5
6
- appsv1 "k8s.io/api/apps/v1"
7
- corev1 "k8s.io/api/core/v1"
8
- apierrors "k8s.io/apimachinery/pkg/api/errors"
9
- "k8s.io/apimachinery/pkg/api/resource"
10
- "k8s.io/apimachinery/pkg/util/intstr"
11
- "sigs.k8s.io/controller-runtime/pkg/client"
12
-
13
- "github.com/sourcegraph/sourcegraph/lib/errors"
14
- "github.com/sourcegraph/sourcegraph/lib/pointers"
15
-
16
- "github.com/sourcegraph/sourcegraph/internal/appliance/hash"
17
6
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/container"
18
7
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/deployment"
19
8
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/pod"
20
9
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/pvc"
21
10
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/service"
22
- "github.com/sourcegraph/sourcegraph/internal/maps"
11
+ "github.com/sourcegraph/sourcegraph/lib/errors"
12
+ "github.com/sourcegraph/sourcegraph/lib/pointers"
13
+ appsv1 "k8s.io/api/apps/v1"
14
+ corev1 "k8s.io/api/core/v1"
15
+ "k8s.io/apimachinery/pkg/api/resource"
16
+ "k8s.io/apimachinery/pkg/util/intstr"
17
+ "sigs.k8s.io/controller-runtime/pkg/client"
23
18
)
24
19
25
- func (r * Reconciler ) reconcileBlobstore (ctx context.Context , sg * Sourcegraph ) error {
26
- if err := r .reconcileBlobstorePersistentVolumeClaims (ctx , sg ); err != nil {
20
+ func (r * Reconciler ) reconcileBlobstore (ctx context.Context , sg * Sourcegraph , owner client. Object ) error {
21
+ if err := r .reconcileBlobstorePersistentVolumeClaims (ctx , sg , owner ); err != nil {
27
22
return err
28
23
}
29
24
30
- if err := r .reconcileBlobstoreServices (ctx , sg ); err != nil {
25
+ if err := r .reconcileBlobstoreServices (ctx , sg , owner ); err != nil {
31
26
return err
32
27
}
33
28
34
- if err := r .reconcileBlobstoreDeployments (ctx , sg ); err != nil {
29
+ if err := r .reconcileBlobstoreDeployments (ctx , sg , owner ); err != nil {
35
30
return err
36
31
}
37
32
@@ -72,42 +67,13 @@ func buildBlobstorePersistentVolumeClaim(sg *Sourcegraph) (corev1.PersistentVolu
72
67
return p , nil
73
68
}
74
69
75
- func (r * Reconciler ) reconcileBlobstorePersistentVolumeClaims (ctx context.Context , sg * Sourcegraph ) error {
70
+ func (r * Reconciler ) reconcileBlobstorePersistentVolumeClaims (ctx context.Context , sg * Sourcegraph , owner client. Object ) error {
76
71
p , err := buildBlobstorePersistentVolumeClaim (sg )
77
72
if err != nil {
78
73
return err
79
74
}
80
75
81
- p .Labels = hash .SetTemplateHashLabel (p .Labels , p .Spec )
82
-
83
- var existing corev1.PersistentVolumeClaim
84
- if r .IsObjectFound (ctx , p .Name , p .Namespace , & existing ) {
85
- if sg .Spec .Blobstore .Disabled {
86
- return nil
87
- }
88
-
89
- // Object exists update if needed
90
- if hash .GetTemplateHashLabel (existing .Labels ) == hash .GetTemplateHashLabel (p .Labels ) {
91
- // no updates needed
92
- return nil
93
- }
94
-
95
- // need to update
96
- existing .Labels = maps .Merge (existing .Labels , p .Labels )
97
- existing .Annotations = maps .Merge (existing .Annotations , p .Annotations )
98
- existing .Spec = p .Spec
99
-
100
- return r .Update (ctx , & existing )
101
- }
102
-
103
- if sg .Spec .Blobstore .Disabled {
104
- return nil
105
- }
106
-
107
- // Note: we don't set a controller reference here as we want PVCs to persist if blobstore is deleted.
108
- // This helps to protect against accidental data deletions.
109
-
110
- return r .Create (ctx , & p )
76
+ return reconcileBlobStoreObject (ctx , r , & p , & corev1.PersistentVolumeClaim {}, sg , owner )
111
77
}
112
78
113
79
func buildBlobstoreService (sg * Sourcegraph ) (corev1.Service , error ) {
@@ -131,53 +97,12 @@ func buildBlobstoreService(sg *Sourcegraph) (corev1.Service, error) {
131
97
return s , nil
132
98
}
133
99
134
- func (r * Reconciler ) reconcileBlobstoreServices (ctx context.Context , sg * Sourcegraph ) error {
100
+ func (r * Reconciler ) reconcileBlobstoreServices (ctx context.Context , sg * Sourcegraph , owner client. Object ) error {
135
101
s , err := buildBlobstoreService (sg )
136
102
if err != nil {
137
103
return err
138
104
}
139
-
140
- s .Labels = hash .SetTemplateHashLabel (s .Labels , s .Spec )
141
-
142
- var existing corev1.Service
143
- if r .IsObjectFound (ctx , s .Name , sg .Namespace , & existing ) {
144
- if sg .Spec .Blobstore .Disabled {
145
- // blobstore service exists, but has been disabled. Delete the service.
146
- //
147
- // Using a precondition to make sure the version of the resource that is deleted
148
- // is the version we intend, and not a resource that was already resgeated.
149
- err = r .Delete (ctx , & existing , client.Preconditions {
150
- UID : & existing .UID ,
151
- ResourceVersion : & existing .ResourceVersion ,
152
- })
153
-
154
- if err != nil && ! apierrors .IsNotFound (err ) {
155
- return err
156
- }
157
-
158
- return nil
159
- }
160
- // Object exists update if needed
161
- if hash .GetTemplateHashLabel (existing .Labels ) == hash .GetTemplateHashLabel (s .Labels ) {
162
- // no updates needed
163
- return nil
164
- }
165
-
166
- // need to update
167
- existing .Labels = maps .Merge (existing .Labels , s .Labels )
168
- existing .Annotations = maps .Merge (existing .Annotations , s .Annotations )
169
- existing .Spec = s .Spec
170
-
171
- return r .Update (ctx , & existing )
172
- }
173
-
174
- if sg .Spec .Blobstore .Disabled {
175
- return nil
176
- }
177
-
178
- // TODO set owner ref
179
-
180
- return r .Create (ctx , & s )
105
+ return reconcileBlobStoreObject (ctx , r , & s , & corev1.Service {}, sg , owner )
181
106
}
182
107
183
108
func buildBlobstoreDeployment (sg * Sourcegraph ) (appsv1.Deployment , error ) {
@@ -272,6 +197,7 @@ func buildBlobstoreDeployment(sg *Sourcegraph) (appsv1.Deployment, error) {
272
197
defaultDeployment , err := deployment .NewDeployment (
273
198
name ,
274
199
sg .Namespace ,
200
+ sg .Spec .RequestedVersion ,
275
201
deployment .WithPodTemplateSpec (podTemplate .Template ),
276
202
)
277
203
@@ -282,51 +208,29 @@ func buildBlobstoreDeployment(sg *Sourcegraph) (appsv1.Deployment, error) {
282
208
return defaultDeployment , nil
283
209
}
284
210
285
- func (r * Reconciler ) reconcileBlobstoreDeployments (ctx context.Context , sg * Sourcegraph ) error {
211
+ func (r * Reconciler ) reconcileBlobstoreDeployments (ctx context.Context , sg * Sourcegraph , owner client. Object ) error {
286
212
d , err := buildBlobstoreDeployment (sg )
287
213
if err != nil {
288
214
return err
289
215
}
216
+ return reconcileBlobStoreObject (ctx , r , & d , & appsv1.Deployment {}, sg , owner )
217
+ }
290
218
291
- d .Labels = hash .SetTemplateHashLabel (d .Labels , d .Spec )
292
-
293
- var existing appsv1.Deployment
294
- if r .IsObjectFound (ctx , d .Name , sg .Namespace , & existing ) {
295
- if sg .Spec .Blobstore .Disabled {
296
- // blobstore deployment exists, but has been disabled. Delete the deployment.
297
- //
298
- // Using a precondition to make sure the version of the resource that is deleted
299
- // is the version we intend, and not a resource that was already recreated.
300
- err = r .Delete (ctx , & existing , client.Preconditions {
301
- UID : & existing .UID ,
302
- ResourceVersion : & existing .ResourceVersion ,
303
- })
304
-
305
- if err != nil && ! apierrors .IsNotFound (err ) {
306
- return err
307
- }
308
-
309
- return nil
310
- }
311
- // Object exists update if needed
312
- if hash .GetTemplateHashLabel (existing .Labels ) == hash .GetTemplateHashLabel (d .Labels ) {
313
- // no updates needed
314
- return nil
315
- }
316
-
317
- // need to update
318
- existing .Labels = maps .Merge (existing .Labels , d .Labels )
319
- existing .Annotations = maps .Merge (existing .Annotations , d .Annotations )
320
- existing .Spec = d .Spec
321
-
322
- return r .Update (ctx , & existing )
323
- }
324
-
219
+ func reconcileBlobStoreObject [T client.Object ](ctx context.Context , r * Reconciler , obj , objKind T , sg * Sourcegraph , owner client.Object ) error {
325
220
if sg .Spec .Blobstore .Disabled {
326
- return nil
221
+ return r . ensureObjectDeleted ( ctx , obj )
327
222
}
328
223
329
- // TODO set owner ref
224
+ // Any secrets (or other configmaps) referenced in BlobStoreSpec can be
225
+ // added to this struct so that they are hashed, and cause an update to the
226
+ // Deployment if changed.
227
+ updateIfChanged := struct {
228
+ BlobstoreSpec
229
+ Version string
230
+ }{
231
+ BlobstoreSpec : sg .Spec .Blobstore ,
232
+ Version : sg .Spec .RequestedVersion ,
233
+ }
330
234
331
- return r . Create (ctx , & d )
235
+ return createOrUpdateObject (ctx , r , updateIfChanged , owner , obj , objKind )
332
236
}
0 commit comments