@@ -49,14 +49,12 @@ var helmRepositoryOCIReadyCondition = summarize.Conditions{
49
49
Target : meta .ReadyCondition ,
50
50
Owned : []string {
51
51
sourcev1 .FetchFailedCondition ,
52
- sourcev1 .SourceValidCondition ,
53
52
meta .ReadyCondition ,
54
53
meta .ReconcilingCondition ,
55
54
meta .StalledCondition ,
56
55
},
57
56
Summarize : []string {
58
57
sourcev1 .FetchFailedCondition ,
59
- sourcev1 .SourceValidCondition ,
60
58
meta .StalledCondition ,
61
59
meta .ReconcilingCondition ,
62
60
},
@@ -83,11 +81,17 @@ type HelmRepositoryOCIReconciler struct {
83
81
client.Client
84
82
kuberecorder.EventRecorder
85
83
helper.Metrics
86
- Getters helmgetter.Providers
87
- ControllerName string
88
- RegistryClient * registry. Client
84
+ Getters helmgetter.Providers
85
+ ControllerName string
86
+ RegistryClientGenerator RegistryClientGeneratorFunc
89
87
}
90
88
89
+ // RegistryClientGeneratorFunc is a function that returns a registry client
90
+ // and an optional file name.
91
+ // The file is used to store the registry client credentials.
92
+ // The caller is responsible for deleting the file.
93
+ type RegistryClientGeneratorFunc func (isLogin bool ) (* registry.Client , string , error )
94
+
91
95
// helmRepositoryOCIReconcileFunc is the function type for all the
92
96
// v1beta2.HelmRepository (sub)reconcile functions for OCI type. The type implementations
93
97
// are grouped and executed serially to perform the complete reconcile of the
@@ -283,55 +287,56 @@ func (r *HelmRepositoryOCIReconciler) reconcileSource(ctx context.Context, obj *
283
287
284
288
// validateSource the HelmRepository object by checking the url and connecting to the underlying registry
285
289
// with he provided credentials.
286
- func (r * HelmRepositoryOCIReconciler ) validateSource (ctx context.Context , obj * sourcev1.HelmRepository , loginOpts ... registry.LoginOption ) (sreconcile.Result , error ) {
287
- chartRepo , err := repository .NewOCIChartRepository (obj .Spec .URL , repository .WithOCIRegistryClient (r .RegistryClient ))
290
+ func (r * HelmRepositoryOCIReconciler ) validateSource (ctx context.Context , obj * sourcev1.HelmRepository , logOpts ... registry.LoginOption ) (sreconcile.Result , error ) {
291
+ registryClient , file , err := r .RegistryClientGenerator (logOpts != nil )
292
+ if err != nil {
293
+ e := & serror.Stalling {
294
+ Err : fmt .Errorf ("failed to create registry client:: %w" , err ),
295
+ Reason : meta .FailedReason ,
296
+ }
297
+ conditions .MarkFalse (obj , meta .ReadyCondition , e .Reason , e .Err .Error ())
298
+ return sreconcile .ResultEmpty , e
299
+ }
300
+
301
+ defer func () {
302
+ if file != "" {
303
+ os .Remove (file )
304
+ }
305
+ }()
306
+
307
+ chartRepo , err := repository .NewOCIChartRepository (obj .Spec .URL , repository .WithOCIRegistryClient (registryClient ))
288
308
if err != nil {
289
309
if strings .Contains (err .Error (), "parse" ) {
290
- e := & serror.Event {
310
+ e := & serror.Stalling {
291
311
Err : fmt .Errorf ("failed to parse URL '%s': %w" , obj .Spec .URL , err ),
292
- Reason : "ValidationError" ,
312
+ Reason : sourcev1 . URLInvalidReason ,
293
313
}
294
- conditions .MarkFalse (obj , sourcev1 . SourceValidCondition , e .Reason , e .Err .Error ())
314
+ conditions .MarkFalse (obj , meta . ReadyCondition , e .Reason , e .Err .Error ())
295
315
return sreconcile .ResultEmpty , e
296
316
} else if strings .Contains (err .Error (), "the url scheme is not supported" ) {
297
317
e := & serror.Event {
298
318
Err : err ,
299
- Reason : "ValidationError" ,
319
+ Reason : sourcev1 . URLInvalidReason ,
300
320
}
301
- conditions .MarkFalse (obj , sourcev1 . SourceValidCondition , e .Reason , e .Err .Error ())
321
+ conditions .MarkFalse (obj , meta . ReadyCondition , e .Reason , e .Err .Error ())
302
322
return sreconcile .ResultEmpty , e
303
323
}
304
324
}
305
325
306
326
// Attempt to login to the registry if credentials are provided.
307
- if loginOpts != nil {
308
- // create a temporary file to store the credentials
309
- // this is needed because otherwise the credentials are stored in ~/.docker/config.json.
310
- credentialFile , err := os .CreateTemp ("" , "credentials" )
327
+ if logOpts != nil {
328
+ err = chartRepo .Login (logOpts ... )
311
329
if err != nil {
312
330
e := & serror.Event {
313
331
Err : fmt .Errorf ("failed to create temporary file: %w" , err ),
314
- Reason : "ValidationError" ,
315
- }
316
- conditions .MarkFalse (obj , sourcev1 .SourceValidCondition , e .Reason , e .Err .Error ())
317
- return sreconcile .ResultEmpty , e
318
- }
319
- defer os .Remove (credentialFile .Name ())
320
-
321
- // set the credentials file to the registry client
322
- registry .ClientOptCredentialsFile (credentialFile .Name ())(r .RegistryClient )
323
- err = chartRepo .Login (loginOpts ... )
324
- if err != nil {
325
- e := & serror.Event {
326
- Err : fmt .Errorf ("failed to login to registry: %w" , err ),
327
- Reason : "ValidationError" ,
332
+ Reason : meta .FailedReason ,
328
333
}
329
- conditions .MarkFalse (obj , sourcev1 . SourceValidCondition , e .Reason , e .Err .Error ())
334
+ conditions .MarkFalse (obj , meta . ReadyCondition , e .Reason , e .Err .Error ())
330
335
return sreconcile .ResultEmpty , e
331
336
}
332
337
}
333
338
334
- conditions .MarkTrue (obj , sourcev1 . SourceValidCondition , meta .SucceededReason , "Helm repository %q is valid" , obj .Name )
339
+ conditions .MarkTrue (obj , meta . ReadyCondition , meta .SucceededReason , "Helm repository %q is valid" , obj .Name )
335
340
336
341
return sreconcile .ResultSuccess , nil
337
342
}
0 commit comments