resolver.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. "fmt"
  16. "net/url"
  17. "os"
  18. "github.com/aws/aws-sdk-go-v2/service/ecr"
  19. "github.com/aws/aws-sdk-go-v2/service/ecrpublic"
  20. smithyendpoints "github.com/aws/smithy-go/endpoints"
  21. )
  22. import (
  23. "context"
  24. )
  25. const (
  26. // ECREndpointEnv is the environment variable name for specifying a custom ECR endpoint.
  27. ECREndpointEnv = "AWS_ECR_ENDPOINT"
  28. // ECRPublicEndpointEnv is the environment variable name for specifying a custom ECR Public endpoint.
  29. ECRPublicEndpointEnv = "AWS_ECR_PUBLIC_ENDPOINT"
  30. )
  31. type ecrCustomEndpointResolver struct{}
  32. // ResolveEndpoint returns a ResolverFunc with customizable endpoints.
  33. func (c ecrCustomEndpointResolver) ResolveEndpoint(ctx context.Context, params ecr.EndpointParameters) (smithyendpoints.Endpoint, error) {
  34. endpoint := smithyendpoints.Endpoint{}
  35. if v := os.Getenv(ECREndpointEnv); v != "" {
  36. url, err := url.Parse(v)
  37. if err != nil {
  38. return endpoint, fmt.Errorf("failed to parse ecr endpoint %s: %w", v, err)
  39. }
  40. endpoint.URI = *url
  41. return endpoint, nil
  42. }
  43. defaultResolver := ecr.NewDefaultEndpointResolverV2()
  44. return defaultResolver.ResolveEndpoint(ctx, params)
  45. }
  46. type ecrPublicCustomEndpointResolver struct{}
  47. // ResolveEndpoint returns a ResolverFunc with customizable endpoints.
  48. func (c ecrPublicCustomEndpointResolver) ResolveEndpoint(ctx context.Context, params ecrpublic.EndpointParameters) (smithyendpoints.Endpoint, error) {
  49. endpoint := smithyendpoints.Endpoint{}
  50. if v := os.Getenv(ECRPublicEndpointEnv); v != "" {
  51. url, err := url.Parse(v)
  52. if err != nil {
  53. return endpoint, fmt.Errorf("failed to parse ecr public endpoint %s: %w", v, err)
  54. }
  55. endpoint.URI = *url
  56. return endpoint, nil
  57. }
  58. defaultResolver := ecrpublic.NewDefaultEndpointResolverV2()
  59. return defaultResolver.ResolveEndpoint(ctx, params)
  60. }