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

add a deadline to cloud client registration #120

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ jobs:
vendor-buildkit-rootless:
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -104,6 +105,7 @@ jobs:
vendor-buildkit:
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -136,6 +138,7 @@ jobs:
vendor-vector:
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -149,10 +152,7 @@ jobs:

helm:
runs-on: ubuntu-latest
needs:
- docker
- vendor-buildkit
- vendor-vector
needs: docker
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ require (
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/aws/aws-sdk-go-v2 v1.21.2
github.com/aws/aws-sdk-go-v2/config v1.19.0
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.13
github.com/aws/aws-sdk-go-v2/service/ecr v1.20.2
github.com/aws/smithy-go v1.15.0
github.com/containerd/console v1.0.3
github.com/distribution/reference v0.5.0
github.com/docker/cli v24.0.6+incompatible
Expand Down Expand Up @@ -64,15 +66,13 @@ require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/avast/retry-go/v4 v4.3.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.43 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.13 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.43 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.37 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.45 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.37 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.15.2 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.17.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.23.2 // indirect
github.com/aws/smithy-go v1.15.0 // indirect
github.com/banzaicloud/k8s-objectmatcher v1.8.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down
15 changes: 13 additions & 2 deletions pkg/controller/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -30,6 +31,16 @@ import (
// +kubebuilder:scaffold:imports
)

var cloudAuthRegistrationTimeout = 30 * time.Second

func init() {
if cloudAuthRegTimeoutEnv := os.Getenv("CLOUD_AUTH_REGISTRATION_TIMEOUT"); cloudAuthRegTimeoutEnv != "" {
if duration, err := time.ParseDuration(cloudAuthRegTimeoutEnv); err == nil {
steved marked this conversation as resolved.
Show resolved Hide resolved
cloudAuthRegistrationTimeout = duration
}
}
}

// Start creates a new controller manager, registers controllers, and starts
// their control loops for resource reconciliation.
func Start(cfg config.Controller) error {
Expand Down Expand Up @@ -67,10 +78,10 @@ func Start(cfg config.Controller) error {
return err
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), cloudAuthRegistrationTimeout)
defer cancel()

log.Info("Registering cloud auth providers")
log.Info(fmt.Sprintf("Registering cloud auth providers with %s timeout", cloudAuthRegistrationTimeout))
steved marked this conversation as resolved.
Show resolved Hide resolved
if err = credentials.LoadCloudProviders(ctx, log); err != nil {
return err
}
Expand Down
34 changes: 30 additions & 4 deletions pkg/controller/support/credentials/cloudauth/ecr/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/smithy-go/logging"
"github.com/docker/docker/api/types/registry"
"github.com/go-logr/logr"

"github.com/dominodatalab/hephaestus/pkg/controller/support/credentials/cloudauth"
"github.com/go-logr/logr"
)

type ecrClient interface {
Expand All @@ -25,21 +26,46 @@ type ecrClient interface {
) (*ecr.GetAuthorizationTokenOutput, error)
}

type awsLogger struct {
logr.Logger
}

func (l awsLogger) Logf(classification logging.Classification, format string, v ...interface{}) {
var level int
if classification == logging.Debug {
level = 1
}

l.V(level).Info(fmt.Sprintf(format, v...))
}

var (
client ecrClient
urlRegex = regexp.MustCompile(
`^(?P<aws_account_id>[a-zA-Z\d][a-zA-Z\d-_]*)\.dkr\.ecr(-fips)?\.([a-zA-Z\d][a-zA-Z\d-_]*)\.amazonaws\.com(\.cn)?`,
)
clientMode = aws.LogRequest | aws.LogResponse | aws.LogRetries
steved marked this conversation as resolved.
Show resolved Hide resolved
)

func Register(ctx context.Context, logger logr.Logger, registry *cloudauth.Registry) error {
config, err := config.LoadDefaultConfig(ctx, config.WithEC2IMDSRegion())
clientLogger := &awsLogger{logger}
awsConfig, err := config.LoadDefaultConfig(
ctx,
config.WithEC2IMDSRegion(func(o *config.UseEC2IMDSRegion) {
o.Client = imds.New(imds.Options{
ClientLogMode: clientMode,
Logger: clientLogger,
})
}),
config.WithLogger(clientLogger),
config.WithClientLogMode(clientMode),
)
if err != nil {
logger.Info("ECR not registered", "error", err)
return nil
}

client = ecr.NewFromConfig(config)
client = ecr.NewFromConfig(awsConfig)

registry.Register(urlRegex, authenticate)
logger.Info("ECR registered")
Expand Down