Skip to content

Commit

Permalink
fix: multiple tags +error logs not send + tags documentation (estahn#425
Browse files Browse the repository at this point in the history
)

Errors without the `.Msg("")` were not correctly flushed.

Co-authored-by: Enrico Stahn <enrico.stahn@gmail.com>
Co-authored-by: Richard Hillmann <2286479+project0@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 23, 2023
1 parent f3e08df commit 8976124
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .k8s-image-swapper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ target:
tags:
- key: CreatedBy
value: k8s-image-swapper
- key: AnotherTag
value: another-tag
imageTagMutability: MUTABLE
imageScanningConfiguration:
imageScanOnPush: true
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ A mutating webhook for Kubernetes, pointing the images to a new location.`,

imageSwapPolicy, err := types.ParseImageSwapPolicy(cfg.ImageSwapPolicy)
if err != nil {
log.Err(err)
log.Err(err).Str("policy", cfg.ImageSwapPolicy).Msg("parsing image swap policy failed")
}

imageCopyPolicy, err := types.ParseImageCopyPolicy(cfg.ImageCopyPolicy)
if err != nil {
log.Err(err)
log.Err(err).Str("policy", cfg.ImageCopyPolicy).Msg("parsing image copy policy failed")
}

imagePullSecretProvider := setupImagePullSecretsProvider()
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ It's a slice of `Key` and `Value`.
aws:
ecrOptions:
tags:
- name: cluster
- key: cluster
value: myCluster
```
4 changes: 2 additions & 2 deletions pkg/registry/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (e *ECRClient) buildEcrTags() []*ecr.Tag {
ecrTags := []*ecr.Tag{}

for _, t := range e.tags {
tag := ecr.Tag{Key: &t.Key, Value: &t.Value}
tag := ecr.Tag{Key: aws.String(t.Key), Value: aws.String(t.Value)}
ecrTags = append(ecrTags, &tag)
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func NewMockECRClient(ecrClient ecriface.ECRAPI, region string, ecrDomain string
scheduler: nil,
targetAccount: targetAccount,
authToken: []byte("mock-ecr-client-fake-auth-token"),
tags: []config.Tag{{Key: "CreatedBy", Value: "k8s-image-swapper"}},
tags: []config.Tag{{Key: "CreatedBy", Value: "k8s-image-swapper"}, {Key: "AnotherTag", Value: "another-tag"}},
}

return client, nil
Expand Down
20 changes: 9 additions & 11 deletions pkg/webhook/image_swapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,28 +215,26 @@ func (p *ImageSwapper) Mutate(ctx context.Context, ar *kwhmodel.AdmissionReview,
createRepoName := reference.TrimNamed(srcRef.DockerReference()).String()
log.Ctx(lctx).Debug().Str("repository", createRepoName).Msg("create repository")
if err := p.registryClient.CreateRepository(createRepoName); err != nil {
log.Err(err)
log.Err(err).Str("repository", createRepoName).Msg("failed to create repository")
}

// Retrieve secrets and auth credentials
imagePullSecrets, err := p.imagePullSecretProvider.GetImagePullSecrets(pod)
if err != nil {
log.Err(err)
log.Err(err).Msg("failed to retrieve image pull secrets from provider")
}

authFile, err := imagePullSecrets.AuthFile()
if authFile != nil {
defer func() {
if err := os.RemoveAll(authFile.Name()); err != nil {
log.Err(err)
}
}()
}

if err != nil {
log.Err(err)
log.Err(err).Msg("failed generating authFile")
}

defer func() {
if err := os.RemoveAll(authFile.Name()); err != nil {
log.Err(err).Str("file", authFile.Name()).Msg("failed removing auth file")
}
}()

// Copy image
// TODO: refactor to use structure instead of passing file name / string
// or transform registryClient creds into auth compatible form, e.g.
Expand Down
16 changes: 16 additions & 0 deletions pkg/webhook/image_swapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func TestImageSwapper_Mutate(t *testing.T) {
Key: aws.String("CreatedBy"),
Value: aws.String("k8s-image-swapper"),
},
{
Key: aws.String("AnotherTag"),
Value: aws.String("another-tag"),
},
},
}).Return(mock.Anything)
ecrClient.On(
Expand All @@ -267,6 +271,10 @@ func TestImageSwapper_Mutate(t *testing.T) {
Key: aws.String("CreatedBy"),
Value: aws.String("k8s-image-swapper"),
},
{
Key: aws.String("AnotherTag"),
Value: aws.String("another-tag"),
},
},
}).Return(mock.Anything)
ecrClient.On(
Expand All @@ -283,6 +291,10 @@ func TestImageSwapper_Mutate(t *testing.T) {
Key: aws.String("CreatedBy"),
Value: aws.String("k8s-image-swapper"),
},
{
Key: aws.String("AnotherTag"),
Value: aws.String("another-tag"),
},
},
}).Return(mock.Anything)

Expand Down Expand Up @@ -339,6 +351,10 @@ func TestImageSwapper_MutateWithImagePullSecrets(t *testing.T) {
Key: aws.String("CreatedBy"),
Value: aws.String("k8s-image-swapper"),
},
{
Key: aws.String("AnotherTag"),
Value: aws.String("another-tag"),
},
},
}).Return(mock.Anything)

Expand Down

0 comments on commit 8976124

Please sign in to comment.