Skip to content

Commit

Permalink
Merge pull request #646 from fluxcd/oci-cr-logger
Browse files Browse the repository at this point in the history
oci: Use controller-runtime pkg/log specifically
  • Loading branch information
stefanprodan authored Sep 7, 2023
2 parents 488252e + e6669d8 commit eedb1a0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
4 changes: 2 additions & 2 deletions oci/auth/aws/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/google/go-containerregistry/pkg/authn"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/fluxcd/pkg/oci"
)
Expand Down Expand Up @@ -135,7 +135,7 @@ func (c *Client) getLoginAuth(ctx context.Context, awsEcrRegion string) (authn.A
// Login attempts to get the authentication material for ECR.
func (c *Client) Login(ctx context.Context, autoLogin bool, image string) (authn.Authenticator, error) {
if autoLogin {
ctrl.LoggerFrom(ctx).Info("logging in to AWS ECR for " + image)
log.FromContext(ctx).Info("logging in to AWS ECR for " + image)
_, awsEcrRegion, ok := ParseRegistry(image)
if !ok {
return nil, errors.New("failed to parse AWS ECR image, invalid ECR image")
Expand Down
8 changes: 4 additions & 4 deletions oci/auth/azure/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/fluxcd/pkg/oci"
)
Expand Down Expand Up @@ -126,13 +126,13 @@ func ValidHost(host string) bool {
// ensure that the passed image is a valid ACR image using ValidHost().
func (c *Client) Login(ctx context.Context, autoLogin bool, image string, ref name.Reference) (authn.Authenticator, error) {
if autoLogin {
ctrl.LoggerFrom(ctx).Info("logging in to Azure ACR for " + image)
log.FromContext(ctx).Info("logging in to Azure ACR for " + image)
// get registry host from image
strArr := strings.SplitN(image, "/", 2)
endpoint := fmt.Sprintf("%s://%s", c.scheme, strArr[0])
authConfig, err := c.getLoginAuth(ctx, endpoint)
if err != nil {
ctrl.LoggerFrom(ctx).Info("error logging into ACR " + err.Error())
log.FromContext(ctx).Info("error logging into ACR " + err.Error())
return nil, err
}

Expand All @@ -149,7 +149,7 @@ func (c *Client) Login(ctx context.Context, autoLogin bool, image string, ref na
func (c *Client) OIDCLogin(ctx context.Context, registryUrl string) (authn.Authenticator, error) {
authConfig, err := c.getLoginAuth(ctx, registryUrl)
if err != nil {
ctrl.LoggerFrom(ctx).Info("error logging into ACR " + err.Error())
log.FromContext(ctx).Info("error logging into ACR " + err.Error())
return nil, err
}

Expand Down
36 changes: 36 additions & 0 deletions oci/auth/flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2023 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Make sure we never inject non-test flags when the auth packages are imported.
// Refer https://github.com/fluxcd/pkg/issues/645.
package auth_test

import (
"flag"
"strings"
"testing"

_ "github.com/fluxcd/pkg/oci/auth/login"
)

func TestNonTestFlagCheck(t *testing.T) {
flagCheck := func(f *flag.Flag) {
if !strings.HasPrefix(f.Name, "test.") {
t.Errorf("found non-test command line flag: %q", f.Name)
}
}
flag.VisitAll(flagCheck)
}
8 changes: 4 additions & 4 deletions oci/auth/gcp/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/fluxcd/pkg/oci"
)
Expand Down Expand Up @@ -105,10 +105,10 @@ func (c *Client) getLoginAuth(ctx context.Context) (authn.AuthConfig, error) {
// ensure that the passed image is a valid GCR image using ValidHost().
func (c *Client) Login(ctx context.Context, autoLogin bool, image string, ref name.Reference) (authn.Authenticator, error) {
if autoLogin {
ctrl.LoggerFrom(ctx).Info("logging in to GCP GCR for " + image)
log.FromContext(ctx).Info("logging in to GCP GCR for " + image)
authConfig, err := c.getLoginAuth(ctx)
if err != nil {
ctrl.LoggerFrom(ctx).Info("error logging into GCP " + err.Error())
log.FromContext(ctx).Info("error logging into GCP " + err.Error())
return nil, err
}

Expand All @@ -122,7 +122,7 @@ func (c *Client) Login(ctx context.Context, autoLogin bool, image string, ref na
func (c *Client) OIDCLogin(ctx context.Context) (authn.Authenticator, error) {
authConfig, err := c.getLoginAuth(ctx)
if err != nil {
ctrl.LoggerFrom(ctx).Info("error logging into GCP " + err.Error())
log.FromContext(ctx).Info("error logging into GCP " + err.Error())
return nil, err
}

Expand Down
8 changes: 4 additions & 4 deletions oci/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/fluxcd/pkg/oci"
"github.com/fluxcd/pkg/oci/auth/aws"
Expand Down Expand Up @@ -140,19 +140,19 @@ func (m *Manager) OIDCLogin(ctx context.Context, registryURL string, opts Provid
if !opts.AwsAutoLogin {
return nil, fmt.Errorf("ECR authentication failed: %w", oci.ErrUnconfiguredProvider)
}
ctrl.LoggerFrom(ctx).Info("logging in to AWS ECR for " + u.Host)
log.FromContext(ctx).Info("logging in to AWS ECR for " + u.Host)
return m.ecr.OIDCLogin(ctx, u.Host)
case oci.ProviderGCP:
if !opts.GcpAutoLogin {
return nil, fmt.Errorf("GCR authentication failed: %w", oci.ErrUnconfiguredProvider)
}
ctrl.LoggerFrom(ctx).Info("logging in to GCP GCR for " + u.Host)
log.FromContext(ctx).Info("logging in to GCP GCR for " + u.Host)
return m.gcr.OIDCLogin(ctx)
case oci.ProviderAzure:
if !opts.AzureAutoLogin {
return nil, fmt.Errorf("ACR authentication failed: %w", oci.ErrUnconfiguredProvider)
}
ctrl.LoggerFrom(ctx).Info("logging in to Azure ACR for " + u.Host)
log.FromContext(ctx).Info("logging in to Azure ACR for " + u.Host)
return m.acr.OIDCLogin(ctx, fmt.Sprintf("%s://%s", u.Scheme, u.Host))
}
return nil, nil
Expand Down

0 comments on commit eedb1a0

Please sign in to comment.