Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: support aws new regions without sdk upgrades #1154

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions pkg/storage/s3/endpoints.go
Original file line number Diff line number Diff line change
@@ -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
}
63 changes: 9 additions & 54 deletions pkg/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down