Skip to content

Commit

Permalink
use config
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweaver87 committed Dec 15, 2022
1 parent 5b00b61 commit 35d2908
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 52 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ A mutating webhook for Kubernetes, pointing the images to a new location.`,
//metricsRec := metrics.NewPrometheus(promReg)
log.Trace().Interface("config", cfg).Msg("config")

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)
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)
if err != nil {
log.Err(err).Msg("error connecting to registry client")
os.Exit(1)
}

rClient.SetRepositoryCustomTags(cfg.RepositoryCustomTags)
rClient.SetRepositoryTags(cfg.Target.AWS.ECROptions.Tags)

imageSwapPolicy, err := types.ParseImageSwapPolicy(cfg.ImageSwapPolicy)
if err != nil {
Expand Down
48 changes: 32 additions & 16 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ type Config struct {

ListenAddress string

DryRun bool `yaml:"dryRun"`
ImageSwapPolicy string `yaml:"imageSwapPolicy" validate:"oneof=always exists"`
ImageCopyPolicy string `yaml:"imageCopyPolicy" validate:"oneof=delayed immediate force"`
Source Source `yaml:"source"`
Target Target `yaml:"target"`
RepositoryCustomTags []CustomTag `yaml:"repositoryCustomTags"`
DryRun bool `yaml:"dryRun"`
ImageSwapPolicy string `yaml:"imageSwapPolicy" validate:"oneof=always exists"`
ImageCopyPolicy string `yaml:"imageCopyPolicy" validate:"oneof=delayed immediate force"`
Source Source `yaml:"source"`
Target Target `yaml:"target"`

TLSCertFile string
TLSKeyFile string
Expand All @@ -50,21 +49,38 @@ type JMESPathFilter struct {
JMESPath string `yaml:"jmespath"`
}

type CustomTag struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}

type Target struct {
AWS AWS `yaml:"aws"`
}

type AWS struct {
AccountID string `yaml:"accountId"`
Region string `yaml:"region"`
Role string `yaml:"role"`
AccessPolicy string `yaml:"accessPolicy"`
LifecyclePolicy string `yaml:"lifecyclePolicy"`
AccountID string `yaml:"accountId"`
Region string `yaml:"region"`
Role string `yaml:"role"`
ECROptions ECROptions `yaml:"ecrOptions"`
}

type ECROptions struct {
AccessPolicy string `yaml:"accessPolicy"`
LifecyclePolicy string `yaml:"lifecyclePolicy"`
Tags []Tag `yaml:"tags"`
ImageTagMutability string `yaml:"imageTagMutability"`
ImageScanningConfiguration ImageScanningConfiguration `yaml:"imageScanningConfiguration"`
EncryptionConfiguration EncryptionConfiguration `yaml:"encryptionConfiguration"`
}

type Tag struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}

type ImageScanningConfiguration struct {
ImageScanOnPush bool `yaml:"imageScanOnPush"`
}

type EncryptionConfiguration struct {
EncryptionType string `yaml:"encryptionType"`
KmsKey string `yaml:"kmsKey"`
}

func (a *AWS) EcrDomain() string {
Expand Down
37 changes: 31 additions & 6 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,40 @@ source:
},
},
{
name: "should render custom tags config",
name: "should render tags config",
cfg: `
repositoryCustomTags:
- name: A
value: b
target:
type: aws
aws:
accountId: 123456789
region: ap-southeast-2
role: arn:aws:iam::123456789012:role/roleName
ecrOptions:
tags:
- key: CreatedBy
value: k8s-image-swapper
- key: A
value: B
`,
expCfg: Config{
RepositoryCustomTags: []CustomTag{
{Name: "A", Value: "b"},
Target: Target{
AWS: AWS{
AccountID: "123456789",
Region: "ap-southeast-2",
Role: "arn:aws:iam::123456789012:role/roleName",
ECROptions: ECROptions{
Tags: []Tag{
{
Key: "CreatedBy",
Value: "k8s-image-swapper",
},
{
Key: "A",
Value: "B",
},
},
},
},
},
},
},
Expand Down
19 changes: 7 additions & 12 deletions pkg/registry/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type ECRClient struct {
targetAccount string
accessPolicy string
lifecyclePolicy string
customTags []config.CustomTag
tags []config.Tag
}

func (e *ECRClient) Credentials() string {
Expand Down Expand Up @@ -99,20 +99,15 @@ func (e *ECRClient) CreateRepository(name string) error {
return nil
}

func (e *ECRClient) SetRepositoryCustomTags(tags []config.CustomTag) {
e.customTags = tags
func (e *ECRClient) SetRepositoryTags(tags []config.Tag) {
e.tags = tags
}

func (e *ECRClient) buildEcrTags() []*ecr.Tag {
ecrTags := []*ecr.Tag{
{
Key: aws.String("CreatedBy"),
Value: aws.String("k8s-image-swapper"),
},
}
ecrTags := []*ecr.Tag{}

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

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

return client, nil
Expand Down
16 changes: 0 additions & 16 deletions pkg/webhook/image_swapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,6 @@ func TestImageSwapper_Mutate(t *testing.T) {
Key: aws.String("CreatedBy"),
Value: aws.String("k8s-image-swapper"),
},
{
Key: aws.String("Mock"),
Value: aws.String("mocked-tag"),
},
},
}).Return(mock.Anything)
ecrClient.On(
Expand All @@ -271,10 +267,6 @@ func TestImageSwapper_Mutate(t *testing.T) {
Key: aws.String("CreatedBy"),
Value: aws.String("k8s-image-swapper"),
},
{
Key: aws.String("Mock"),
Value: aws.String("mocked-tag"),
},
},
}).Return(mock.Anything)
ecrClient.On(
Expand All @@ -291,10 +283,6 @@ func TestImageSwapper_Mutate(t *testing.T) {
Key: aws.String("CreatedBy"),
Value: aws.String("k8s-image-swapper"),
},
{
Key: aws.String("Mock"),
Value: aws.String("mocked-tag"),
},
},
}).Return(mock.Anything)

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

Expand Down

0 comments on commit 35d2908

Please sign in to comment.