diff --git a/pkg/storage/s3/endpoints.go b/pkg/storage/s3/endpoints.go new file mode 100644 index 000000000..9b2948433 --- /dev/null +++ b/pkg/storage/s3/endpoints.go @@ -0,0 +1,60 @@ +package s3 + +import ( + "github.com/aws/aws-sdk-go/aws/endpoints" + configv1 "github.com/openshift/api/config/v1" +) + +type endpointsResolver struct { + region string + serviceEndpoints map[string]string +} + +func newEndpointsResolver(region, s3Endpoint string, endpoints []configv1.AWSServiceEndpoint) *endpointsResolver { + serviceEndpoints := make(map[string]string) + for _, ep := range endpoints { + serviceEndpoints[ep.Name] = ep.URL + } + + if s3Endpoint != "" { + serviceEndpoints["s3"] = s3Endpoint + } + + return &endpointsResolver{ + region: region, + serviceEndpoints: serviceEndpoints, + } +} + +func (er *endpointsResolver) EndpointFor(service, region string, opts ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) { + if ep, ok := er.serviceEndpoints[service]; ok { + // The signing region and the cluster region may be different, see + // https://github.com/openshift/installer/commit/41a15b787b5f6d0b0766e1737dcdfeb5b23020d5 + signingRegion := er.region + def, _ := endpoints.DefaultResolver().EndpointFor(service, region) + if len(def.SigningRegion) > 0 { + signingRegion = def.SigningRegion + } + return endpoints.ResolvedEndpoint{ + URL: ep, + SigningRegion: signingRegion, + }, nil + } + return endpoints.DefaultResolver().EndpointFor(service, region, opts...) +} + +func isUnknownEndpointError(err error) bool { + _, ok := err.(endpoints.UnknownEndpointError) + return ok +} + +func regionHasDualStackS3(region string) (bool, error) { + _, err := endpoints.DefaultResolver().EndpointFor("s3", region, endpoints.UseDualStackEndpointOption) + if isUnknownEndpointError(err) { + return false, nil + } + if err != nil { + return false, err + } + return true, nil +} diff --git a/pkg/storage/s3/s3.go b/pkg/storage/s3/s3.go index 2d7815ae9..027707b7a 100644 --- a/pkg/storage/s3/s3.go +++ b/pkg/storage/s3/s3.go @@ -45,60 +45,6 @@ const ( imageRegistrySecretDataKey = "credentials" ) -type endpointsResolver struct { - region string - serviceEndpoints map[string]string -} - -func newEndpointsResolver(region, s3Endpoint string, endpoints []configv1.AWSServiceEndpoint) *endpointsResolver { - serviceEndpoints := make(map[string]string) - for _, ep := range endpoints { - serviceEndpoints[ep.Name] = ep.URL - } - - if s3Endpoint != "" { - serviceEndpoints["s3"] = s3Endpoint - } - - return &endpointsResolver{ - region: region, - serviceEndpoints: serviceEndpoints, - } -} - -func (er *endpointsResolver) EndpointFor(service, region string, opts ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) { - if ep, ok := er.serviceEndpoints[service]; ok { - // The signing region and the cluster region may be different, see - // https://github.com/openshift/installer/commit/41a15b787b5f6d0b0766e1737dcdfeb5b23020d5 - signingRegion := er.region - def, _ := endpoints.DefaultResolver().EndpointFor(service, region) - if len(def.SigningRegion) > 0 { - signingRegion = def.SigningRegion - } - return endpoints.ResolvedEndpoint{ - URL: ep, - SigningRegion: signingRegion, - }, nil - } - return endpoints.DefaultResolver().EndpointFor(service, region, opts...) -} - -func isUnknownEndpointError(err error) bool { - _, ok := err.(endpoints.UnknownEndpointError) - return ok -} - -func regionHasDualStackS3(region string) (bool, error) { - _, err := endpoints.DefaultResolver().EndpointFor("s3", region, endpoints.UseDualStackEndpointOption) - if isUnknownEndpointError(err) { - return false, nil - } - if err != nil { - return false, err - } - return true, nil -} - type driver struct { Context context.Context Config *imageregistryv1.ImageRegistryConfigStorageS3 @@ -336,6 +282,7 @@ func (d *driver) getS3Service() (*s3.S3, error) { HTTPClient: &http.Client{ Transport: tr, }, + LogLevel: aws.LogLevel(aws.LogDebug), }, SharedConfigState: session.SharedConfigEnable, SharedConfigFiles: []string{credentialsFilename}, @@ -408,6 +355,14 @@ func (d *driver) ConfigEnv() (envs envvar.List, err error) { if len(d.Config.RegionEndpoint) != 0 { envs = append(envs, envvar.EnvVar{Name: "REGISTRY_STORAGE_S3_REGIONENDPOINT", Value: d.Config.RegionEndpoint}) + } else { + // attempt to bypass validation error in distribution as a means + // to support new aws regions without requiring aws-sdk-go upgrades. + // regionEndpoint, _ := d.endpointsResolver.EndpointFor("s3", d.Config.Region) + regionEndpoint, _ := endpoints.DefaultResolver().EndpointFor("s3", d.Config.Region) + if regionEndpoint.URL != "" { + envs = append(envs, envvar.EnvVar{Name: "REGISTRY_STORAGE_S3_REGIONENDPOINT", Value: regionEndpoint.URL}) + } } if len(d.Config.KeyID) != 0 {