Skip to content

Commit 3e0f017

Browse files
committed
use config
1 parent fa3e774 commit 3e0f017

File tree

5 files changed

+72
-52
lines changed

5 files changed

+72
-52
lines changed

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ A mutating webhook for Kubernetes, pointing the images to a new location.`,
6363
//metricsRec := metrics.NewPrometheus(promReg)
6464
log.Trace().Interface("config", cfg).Msg("config")
6565

66-
rClient, err := registry.NewECRClient(cfg.Target.AWS.Region, cfg.Target.AWS.EcrDomain(), cfg.Target.AWS.AccountID, cfg.Target.AWS.Role, cfg.Target.AWS.AccessPolicy, cfg.Target.AWS.LifecyclePolicy)
66+
rClient, err := registry.NewECRClient(cfg.Target.AWS.Region, cfg.Target.AWS.EcrDomain(), cfg.Target.AWS.AccountID, cfg.Target.AWS.Role, cfg.Target.AWS.ECROptions.AccessPolicy, cfg.Target.AWS.ECROptions.LifecyclePolicy)
6767
if err != nil {
6868
log.Err(err).Msg("error connecting to registry client")
6969
os.Exit(1)
7070
}
7171

72-
rClient.SetRepositoryCustomTags(cfg.RepositoryCustomTags)
72+
rClient.SetRepositoryTags(cfg.Target.AWS.ECROptions.Tags)
7373

7474
imageSwapPolicy, err := types.ParseImageSwapPolicy(cfg.ImageSwapPolicy)
7575
if err != nil {

pkg/config/config.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ type Config struct {
3131

3232
ListenAddress string
3333

34-
DryRun bool `yaml:"dryRun"`
35-
ImageSwapPolicy string `yaml:"imageSwapPolicy" validate:"oneof=always exists"`
36-
ImageCopyPolicy string `yaml:"imageCopyPolicy" validate:"oneof=delayed immediate force"`
37-
Source Source `yaml:"source"`
38-
Target Target `yaml:"target"`
39-
RepositoryCustomTags []CustomTag `yaml:"repositoryCustomTags"`
34+
DryRun bool `yaml:"dryRun"`
35+
ImageSwapPolicy string `yaml:"imageSwapPolicy" validate:"oneof=always exists"`
36+
ImageCopyPolicy string `yaml:"imageCopyPolicy" validate:"oneof=delayed immediate force"`
37+
Source Source `yaml:"source"`
38+
Target Target `yaml:"target"`
4039

4140
TLSCertFile string
4241
TLSKeyFile string
@@ -50,21 +49,38 @@ type JMESPathFilter struct {
5049
JMESPath string `yaml:"jmespath"`
5150
}
5251

53-
type CustomTag struct {
54-
Name string `yaml:"name"`
55-
Value string `yaml:"value"`
56-
}
57-
5852
type Target struct {
5953
AWS AWS `yaml:"aws"`
6054
}
6155

6256
type AWS struct {
63-
AccountID string `yaml:"accountId"`
64-
Region string `yaml:"region"`
65-
Role string `yaml:"role"`
66-
AccessPolicy string `yaml:"accessPolicy"`
67-
LifecyclePolicy string `yaml:"lifecyclePolicy"`
57+
AccountID string `yaml:"accountId"`
58+
Region string `yaml:"region"`
59+
Role string `yaml:"role"`
60+
ECROptions ECROptions `yaml:"ecrOptions"`
61+
}
62+
63+
type ECROptions struct {
64+
AccessPolicy string `yaml:"accessPolicy"`
65+
LifecyclePolicy string `yaml:"lifecyclePolicy"`
66+
Tags []Tag `yaml:"tags"`
67+
ImageTagMutability string `yaml:"imageTagMutability"`
68+
ImageScanningConfiguration ImageScanningConfiguration `yaml:"imageScanningConfiguration"`
69+
EncryptionConfiguration EncryptionConfiguration `yaml:"encryptionConfiguration"`
70+
}
71+
72+
type Tag struct {
73+
Key string `yaml:"key"`
74+
Value string `yaml:"value"`
75+
}
76+
77+
type ImageScanningConfiguration struct {
78+
ImageScanOnPush bool `yaml:"imageScanOnPush"`
79+
}
80+
81+
type EncryptionConfiguration struct {
82+
EncryptionType string `yaml:"encryptionType"`
83+
KmsKey string `yaml:"kmsKey"`
6884
}
6985

7086
func (a *AWS) EcrDomain() string {

pkg/config/config_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,40 @@ source:
3838
},
3939
},
4040
{
41-
name: "should render custom tags config",
41+
name: "should render tags config",
4242
cfg: `
43-
repositoryCustomTags:
44-
- name: A
45-
value: b
43+
target:
44+
type: aws
45+
aws:
46+
accountId: 123456789
47+
region: ap-southeast-2
48+
role: arn:aws:iam::123456789012:role/roleName
49+
ecrOptions:
50+
tags:
51+
- key: CreatedBy
52+
value: k8s-image-swapper
53+
- key: A
54+
value: B
4655
`,
4756
expCfg: Config{
48-
RepositoryCustomTags: []CustomTag{
49-
{Name: "A", Value: "b"},
57+
Target: Target{
58+
AWS: AWS{
59+
AccountID: "123456789",
60+
Region: "ap-southeast-2",
61+
Role: "arn:aws:iam::123456789012:role/roleName",
62+
ECROptions: ECROptions{
63+
Tags: []Tag{
64+
{
65+
Key: "CreatedBy",
66+
Value: "k8s-image-swapper",
67+
},
68+
{
69+
Key: "A",
70+
Value: "B",
71+
},
72+
},
73+
},
74+
},
5075
},
5176
},
5277
},

pkg/registry/ecr.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type ECRClient struct {
2929
targetAccount string
3030
accessPolicy string
3131
lifecyclePolicy string
32-
customTags []config.CustomTag
32+
tags []config.Tag
3333
}
3434

3535
func (e *ECRClient) Credentials() string {
@@ -99,20 +99,15 @@ func (e *ECRClient) CreateRepository(name string) error {
9999
return nil
100100
}
101101

102-
func (e *ECRClient) SetRepositoryCustomTags(tags []config.CustomTag) {
103-
e.customTags = tags
102+
func (e *ECRClient) SetRepositoryTags(tags []config.Tag) {
103+
e.tags = tags
104104
}
105105

106106
func (e *ECRClient) buildEcrTags() []*ecr.Tag {
107-
ecrTags := []*ecr.Tag{
108-
{
109-
Key: aws.String("CreatedBy"),
110-
Value: aws.String("k8s-image-swapper"),
111-
},
112-
}
107+
ecrTags := []*ecr.Tag{}
113108

114-
for _, t := range e.customTags {
115-
tag := ecr.Tag{Key: &t.Name, Value: &t.Value}
109+
for _, t := range e.tags {
110+
tag := ecr.Tag{Key: &t.Key, Value: &t.Value}
116111
ecrTags = append(ecrTags, &tag)
117112
}
118113

@@ -266,7 +261,7 @@ func NewMockECRClient(ecrClient ecriface.ECRAPI, region string, ecrDomain string
266261
scheduler: nil,
267262
targetAccount: targetAccount,
268263
authToken: []byte("mock-ecr-client-fake-auth-token"),
269-
customTags: []config.CustomTag{{Name: "Mock", Value: "mocked-tag"}},
264+
tags: []config.Tag{{Key: "CreatedBy", Value: "k8s-image-swapper"}},
270265
}
271266

272267
return client, nil

pkg/webhook/image_swapper_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,6 @@ func TestImageSwapper_Mutate(t *testing.T) {
251251
Key: aws.String("CreatedBy"),
252252
Value: aws.String("k8s-image-swapper"),
253253
},
254-
{
255-
Key: aws.String("Mock"),
256-
Value: aws.String("mocked-tag"),
257-
},
258254
},
259255
}).Return(mock.Anything)
260256
ecrClient.On(
@@ -271,10 +267,6 @@ func TestImageSwapper_Mutate(t *testing.T) {
271267
Key: aws.String("CreatedBy"),
272268
Value: aws.String("k8s-image-swapper"),
273269
},
274-
{
275-
Key: aws.String("Mock"),
276-
Value: aws.String("mocked-tag"),
277-
},
278270
},
279271
}).Return(mock.Anything)
280272
ecrClient.On(
@@ -291,10 +283,6 @@ func TestImageSwapper_Mutate(t *testing.T) {
291283
Key: aws.String("CreatedBy"),
292284
Value: aws.String("k8s-image-swapper"),
293285
},
294-
{
295-
Key: aws.String("Mock"),
296-
Value: aws.String("mocked-tag"),
297-
},
298286
},
299287
}).Return(mock.Anything)
300288

@@ -351,10 +339,6 @@ func TestImageSwapper_MutateWithImagePullSecrets(t *testing.T) {
351339
Key: aws.String("CreatedBy"),
352340
Value: aws.String("k8s-image-swapper"),
353341
},
354-
{
355-
Key: aws.String("Mock"),
356-
Value: aws.String("mocked-tag"),
357-
},
358342
},
359343
}).Return(mock.Anything)
360344

0 commit comments

Comments
 (0)