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

Support private ECR-only bundle generation and promotion #1005

Merged
merged 1 commit into from
Oct 25, 2023
Merged
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
2 changes: 1 addition & 1 deletion generatebundlefile/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ clean: ## Clean output directory, and the built binary
build: ## Build release binary.
mkdir -p $(REPO_ROOT)/generatebundlefile/bin
$(GO) mod tidy
$(GO) build -o $(REPO_ROOT)/generatebundlefile/bin/generatebundlefile *.go
CGO_ENABLED=0 $(GO) build -o $(REPO_ROOT)/generatebundlefile/bin/generatebundlefile *.go

build-linux:
[ -d bin ] || mkdir bin
Expand Down
1 change: 1 addition & 0 deletions generatebundlefile/hack/docker-ecr-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"382577505035.dkr.ecr.us-west-2.amazonaws.com": "ecr-login",
"646717423341.dkr.ecr.us-west-2.amazonaws.com": "ecr-login",
"783794618700.dkr.ecr.us-west-2.amazonaws.com": "ecr-login",
"067575901363.dkr.ecr.us-west-2.amazonaws.com": "ecr-login",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need all the other registries added here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're building only to this account, this should suffice. The replication happens outside of this Go code.

"public.ecr.aws": "ecr-login"
}
}
28 changes: 20 additions & 8 deletions generatebundlefile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func main() {
opts := NewOptions()
opts.SetupLogger()

newBuildModeEnvvar := os.Getenv("NEW_BUILD_MODE")
if newBuildModeEnvvar == "true" {
opts.newBuildMode = true
} else {
opts.newBuildMode = false
}

if opts.generateSample {
outputFilename := filepath.Join(opts.outputFolder, "bundle.yaml")
f, err := os.OpenFile(outputFilename, os.O_WRONLY|os.O_CREATE, 0644)
Expand Down Expand Up @@ -120,20 +127,25 @@ func cmdPromote(opts *Options) error {
}
}

clients, err := GetSDKClients()
clients, err := GetSDKClients(opts.newBuildMode)
if err != nil {
return fmt.Errorf("getting SDK clients: %w", err)
}
clients.ecrPublicClient.SourceRegistry, err = clients.ecrPublicClient.GetRegistryURI()
if err != nil {
return fmt.Errorf("getting registry URI: %w", err)
}

dockerStruct := &DockerAuth{
Auths: map[string]DockerAuthRegistry{
fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com", clients.stsClient.AccountID, ecrRegion): {clients.ecrClient.AuthConfig},
"public.ecr.aws": {clients.ecrPublicClient.AuthConfig},
},
}

if !opts.newBuildMode {
clients.ecrPublicClient.SourceRegistry, err = clients.ecrPublicClient.GetRegistryURI()
if err != nil {
return fmt.Errorf("getting registry URI: %w", err)
}
dockerStruct.Auths["public.ecr.aws"] = DockerAuthRegistry{clients.ecrPublicClient.AuthConfig}
}

dockerAuth, err := NewAuthFile(dockerStruct)
if err != nil {
return fmt.Errorf("creating auth file: %w", err)
Expand Down Expand Up @@ -417,7 +429,7 @@ func cmdGenerate(opts *Options) error {
// push packages to private ECR.
if opts.publicProfile != "" {
BundleLog.Info("Starting release public ECR process....")
clients, err := GetSDKClients()
clients, err := GetSDKClients(opts.newBuildMode)
if err != nil {
BundleLog.Error(err, "getting sdk clients")
os.Exit(1)
Expand Down Expand Up @@ -483,7 +495,7 @@ func cmdGenerate(opts *Options) error {
// if o.publicProfile != "" && if o.privateProfile != "" {}
if opts.privateProfile != "" {
BundleLog.Info("Starting release to private ECR process....")
clients, err := GetSDKClients()
clients, err := GetSDKClients(opts.newBuildMode)
if err != nil {
BundleLog.Error(err, "getting SDK clients")
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions generatebundlefile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Options struct {
privateProfile string
bundleFile string
regionCheck bool
newBuildMode bool
}

func (o *Options) SetupLogger() {
Expand Down
28 changes: 16 additions & 12 deletions generatebundlefile/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,12 @@ type SDKClients struct {
}

// GetSDKClients is used to handle the creation of different SDK clients.
func GetSDKClients() (*SDKClients, error) {
func GetSDKClients(newBuildMode bool) (*SDKClients, error) {
clients := &SDKClients{}
var err error
// ECR Public Connection with us-east-1 region
conf, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(ecrPublicRegion))
if err != nil {
return nil, fmt.Errorf("loading default AWS config: %w", err)
}
client := ecrpublic.NewFromConfig(conf)
clients.ecrPublicClient, err = NewECRPublicClient(client, true)
if err != nil {
return nil, fmt.Errorf("creating default public ECR client: %w", err)
}

// STS Connection with us-west-2 region
conf, err = config.LoadDefaultConfig(context.TODO(), config.WithRegion(ecrRegion))
conf, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(ecrRegion))
if err != nil {
return nil, fmt.Errorf("loading default AWS config: %w", err)
}
Expand All @@ -58,6 +48,20 @@ func GetSDKClients() (*SDKClients, error) {
if err != nil {
return nil, fmt.Errorf("Unable to create SDK connection to ECR %s", err)
}

if !newBuildMode {
// ECR Public Connection with us-east-1 region
conf, err = config.LoadDefaultConfig(context.TODO(), config.WithRegion(ecrPublicRegion))
if err != nil {
return nil, fmt.Errorf("loading default AWS config: %w", err)
}
client := ecrpublic.NewFromConfig(conf)
clients.ecrPublicClient, err = NewECRPublicClient(client, true)
if err != nil {
return nil, fmt.Errorf("creating default public ECR client: %w", err)
}
}

return clients, nil
}

Expand Down
Loading