@@ -86,6 +86,10 @@ const (
8686 scanReasonInterval = "triggered by interval"
8787)
8888
89+ // insecureHTTPError occurs when insecure HTTP communication is tried
90+ // and such behaviour is blocked.
91+ var insecureHTTPError = errors .New ("use of insecure plain HTTP connections is blocked" )
92+
8993// getPatchOptions composes patch options based on the given parameters.
9094// It is used as the options used when patching an object.
9195func getPatchOptions (ownedConditions []string , controllerName string ) []patch.Option {
@@ -113,6 +117,7 @@ type ImageRepositoryReconciler struct {
113117 DatabaseReader
114118 }
115119 DeprecatedLoginOpts login.ProviderOptions
120+ AllowInsecureHTTP bool
116121
117122 patchOptions []patch.Option
118123}
@@ -249,9 +254,15 @@ func (r *ImageRepositoryReconciler) reconcile(ctx context.Context, sp *patch.Ser
249254 }
250255
251256 // Parse image reference.
252- ref , err := parseImageReference (obj .Spec .Image )
257+ ref , err := r . parseImageReference (obj .Spec .Image , obj . Spec . Insecure )
253258 if err != nil {
254- conditions .MarkStalled (obj , imagev1 .ImageURLInvalidReason , err .Error ())
259+ var reason string
260+ if errors .Is (err , insecureHTTPError ) {
261+ reason = meta .InsecureConnectionsDisallowedReason
262+ } else {
263+ reason = imagev1 .ImageURLInvalidReason
264+ }
265+ conditions .MarkStalled (obj , reason , err .Error ())
255266 result , retErr = ctrl.Result {}, nil
256267 return
257268 }
@@ -268,11 +279,18 @@ func (r *ImageRepositoryReconciler) reconcile(ctx context.Context, sp *patch.Ser
268279 // Check if it can be scanned now.
269280 ok , when , reasonMsg , err := r .shouldScan (* obj , startTime )
270281 if err != nil {
271- e := fmt .Errorf ("failed to determine if it's scan time: %w" , err )
272- conditions .MarkFalse (obj , meta .ReadyCondition , metav1 .StatusFailure , e .Error ())
282+ var e error
283+ if errors .Is (err , insecureHTTPError ) {
284+ e = err
285+ conditions .MarkStalled (obj , meta .InsecureConnectionsDisallowedReason , e .Error ())
286+ } else {
287+ e = fmt .Errorf ("failed to determine if it's scan time: %w" , err )
288+ conditions .MarkFalse (obj , meta .ReadyCondition , metav1 .StatusFailure , e .Error ())
289+ }
273290 result , retErr = ctrl.Result {}, e
274291 return
275292 }
293+ conditions .Delete (obj , meta .StalledCondition )
276294
277295 // Scan the repository if it's scan time. No scan is a no-op reconciliation.
278296 // The next scan time is not reset in case of no-op reconciliation.
@@ -458,7 +476,7 @@ func (r *ImageRepositoryReconciler) shouldScan(obj imagev1.ImageRepository, now
458476
459477 // If the canonical image name of the image is different from the last
460478 // observed name, scan now.
461- ref , err := parseImageReference (obj .Spec .Image )
479+ ref , err := r . parseImageReference (obj .Spec .Image , obj . Spec . Insecure )
462480 if err != nil {
463481 return false , scanInterval , "" , err
464482 }
@@ -560,13 +578,23 @@ func eventLogf(ctx context.Context, r kuberecorder.EventRecorder, obj runtime.Ob
560578}
561579
562580// parseImageReference parses the given URL into a container registry repository
563- // reference.
564- func parseImageReference (url string ) (name.Reference , error ) {
581+ // reference. If insecure is set to true, then the registry is deemed to be
582+ // located at an HTTP endpoint.
583+ func (r * ImageRepositoryReconciler ) parseImageReference (url string , insecure bool ) (name.Reference , error ) {
565584 if s := strings .Split (url , "://" ); len (s ) > 1 {
566585 return nil , fmt .Errorf (".spec.image value should not start with URL scheme; remove '%s://'" , s [0 ])
567586 }
568587
569- ref , err := name .ParseReference (url )
588+ var opts []name.Option
589+ if insecure {
590+ if r .AllowInsecureHTTP {
591+ opts = append (opts , name .Insecure )
592+ } else {
593+ return nil , insecureHTTPError
594+ }
595+ }
596+
597+ ref , err := name .ParseReference (url , opts ... )
570598 if err != nil {
571599 return nil , err
572600 }
0 commit comments