resolver.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ecr
  14. import (
  15. "context"
  16. "fmt"
  17. "net/url"
  18. "os"
  19. "github.com/aws/aws-sdk-go-v2/service/ecr"
  20. "github.com/aws/aws-sdk-go-v2/service/ecrpublic"
  21. smithyendpoints "github.com/aws/smithy-go/endpoints"
  22. )
  23. const (
  24. // ECREndpointEnv is the environment variable name for specifying a custom ECR endpoint.
  25. ECREndpointEnv = "AWS_ECR_ENDPOINT"
  26. // ECRPublicEndpointEnv is the environment variable name for specifying a custom ECR Public endpoint.
  27. ECRPublicEndpointEnv = "AWS_ECR_PUBLIC_ENDPOINT"
  28. )
  29. type ecrCustomEndpointResolver struct{}
  30. // ResolveEndpoint returns a ResolverFunc with customizable endpoints.
  31. func (c ecrCustomEndpointResolver) ResolveEndpoint(ctx context.Context, params ecr.EndpointParameters) (smithyendpoints.Endpoint, error) {
  32. endpoint := smithyendpoints.Endpoint{}
  33. if v := os.Getenv(ECREndpointEnv); v != "" {
  34. url, err := url.Parse(v)
  35. if err != nil {
  36. return endpoint, fmt.Errorf("failed to parse ecr endpoint %s: %w", v, err)
  37. }
  38. endpoint.URI = *url
  39. return endpoint, nil
  40. }
  41. defaultResolver := ecr.NewDefaultEndpointResolverV2()
  42. return defaultResolver.ResolveEndpoint(ctx, params)
  43. }
  44. type ecrPublicCustomEndpointResolver struct{}
  45. // ResolveEndpoint returns a ResolverFunc with customizable endpoints.
  46. func (c ecrPublicCustomEndpointResolver) ResolveEndpoint(ctx context.Context, params ecrpublic.EndpointParameters) (smithyendpoints.Endpoint, error) {
  47. endpoint := smithyendpoints.Endpoint{}
  48. if v := os.Getenv(ECRPublicEndpointEnv); v != "" {
  49. url, err := url.Parse(v)
  50. if err != nil {
  51. return endpoint, fmt.Errorf("failed to parse ecr public endpoint %s: %w", v, err)
  52. }
  53. endpoint.URI = *url
  54. return endpoint, nil
  55. }
  56. defaultResolver := ecrpublic.NewDefaultEndpointResolverV2()
  57. return defaultResolver.ResolveEndpoint(ctx, params)
  58. }