Skip to content

add support for default-tags #1696

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

Merged
merged 1 commit into from
Nov 30, 2020
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 controllers/ingress/group_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewGroupReconciler(cloud aws.Cloud, k8sClient client.Client, eventRecorder
cloud.EC2(), cloud.ACM(),
annotationParser, subnetsResolver,
authConfigBuilder, enhancedBackendBuilder,
cloud.VpcID(), config.ClusterName, logger)
cloud.VpcID(), config.ClusterName, config.DefaultTags, logger)
stackMarshaller := deploy.NewDefaultStackMarshaller()
stackDeployer := deploy.NewDefaultStackDeployer(cloud, k8sClient, networkingSGManager, networkingSGReconciler,
config, ingressTagPrefix, logger)
Expand Down
2 changes: 1 addition & 1 deletion controllers/service/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewServiceReconciler(cloud aws.Cloud, k8sClient client.Client, eventRecorde
config config.ControllerConfig, logger logr.Logger) *serviceReconciler {

annotationParser := annotations.NewSuffixAnnotationParser(serviceAnnotationPrefix)
modelBuilder := service.NewDefaultModelBuilder(annotationParser, subnetsResolver, config.ClusterName)
modelBuilder := service.NewDefaultModelBuilder(annotationParser, subnetsResolver, config.ClusterName, config.DefaultTags)
stackMarshaller := deploy.NewDefaultStackMarshaller()
stackDeployer := deploy.NewDefaultStackDeployer(cloud, k8sClient, networkingSGManager, networkingSGReconciler, config, serviceTagPrefix, logger)
return &serviceReconciler{
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/controller_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
const (
flagLogLevel = "log-level"
flagK8sClusterName = "cluster-name"
flagDefaultTags = "default-tags"
flagServiceMaxConcurrentReconciles = "service-max-concurrent-reconciles"
flagTargetGroupBindingMaxConcurrentReconciles = "targetgroupbinding-max-concurrent-reconciles"
defaultLogLevel = "info"
Expand All @@ -33,6 +34,9 @@ type ControllerConfig struct {
// Configurations for Addons feature
AddonsConfig AddonsConfig

// Default AWS Tags that will be applied to all AWS resources managed by this controller.
DefaultTags map[string]string

// Max concurrent reconcile loops for Service objects
ServiceMaxConcurrentReconciles int
// Max concurrent reconcile loops for TargetGroupBinding objects
Expand All @@ -44,6 +48,8 @@ func (cfg *ControllerConfig) BindFlags(fs *pflag.FlagSet) {
fs.StringVar(&cfg.LogLevel, flagLogLevel, defaultLogLevel,
"Set the controller log level - info(default), debug")
fs.StringVar(&cfg.ClusterName, flagK8sClusterName, "", "Kubernetes cluster name")
fs.StringToStringVar(&cfg.DefaultTags, flagDefaultTags, nil,
"Default AWS Tags that will be applied to all AWS resources managed by this controller")
fs.IntVar(&cfg.ServiceMaxConcurrentReconciles, flagServiceMaxConcurrentReconciles, defaultMaxConcurrentReconciles,
"Maximum number of concurrently running reconcile loops for service")
fs.IntVar(&cfg.TargetGroupBindingMaxConcurrentReconciles, flagTargetGroupBindingMaxConcurrentReconciles, defaultMaxConcurrentReconciles,
Expand Down
13 changes: 10 additions & 3 deletions pkg/ingress/model_build_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,26 @@ func (t *defaultModelBuildTask) buildLoadBalancerAttributes(_ context.Context) (
}

func (t *defaultModelBuildTask) buildLoadBalancerTags(_ context.Context) (map[string]string, error) {
mergedTags := make(map[string]string)
annotationTags := make(map[string]string)
for _, ing := range t.ingGroup.Members {
var rawTags map[string]string
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixTags, &rawTags, ing.Annotations); err != nil {
return nil, err
}
for tagKey, tagValue := range rawTags {
if existingTagValue, exists := mergedTags[tagKey]; exists && existingTagValue != tagValue {
if existingTagValue, exists := annotationTags[tagKey]; exists && existingTagValue != tagValue {
return nil, errors.Errorf("conflicting tag %v: %v | %v", tagKey, existingTagValue, tagValue)
}
mergedTags[tagKey] = tagValue
annotationTags[tagKey] = tagValue
}
}
mergedTags := make(map[string]string)
for k, v := range t.defaultTags {
mergedTags[k] = v
}
for k, v := range annotationTags {
mergedTags[k] = v
}
return mergedTags, nil
}

Expand Down
164 changes: 164 additions & 0 deletions pkg/ingress/model_build_load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,167 @@ func Test_defaultModelBuildTask_buildLoadBalancerCOIPv4Pool(t *testing.T) {
})
}
}

func Test_defaultModelBuildTask_buildLoadBalancerTags(t *testing.T) {
type fields struct {
ingGroup Group
defaultTags map[string]string
}
tests := []struct {
name string
fields fields
want map[string]string
wantErr error
}{
{
name: "empty default tags, no tags annotation",
fields: fields{
ingGroup: Group{
Members: []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
},
},
},
defaultTags: nil,
},
want: map[string]string{},
},
{
name: "empty default tags, non-empty tags annotation",
fields: fields{
ingGroup: Group{
Members: []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"alb.ingress.kubernetes.io/tags": "k1=v1",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"alb.ingress.kubernetes.io/tags": "k2=v2",
},
},
},
},
},
defaultTags: nil,
},
want: map[string]string{
"k1": "v1",
"k2": "v2",
},
},
{
name: "non-empty default tags, empty tags annotation",
fields: fields{
ingGroup: Group{
Members: []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
},
},
},
defaultTags: map[string]string{
"k3": "v3",
"k4": "v4",
},
},
want: map[string]string{
"k3": "v3",
"k4": "v4",
},
},
{
name: "non-empty default tags, non-empty tags annotation",
fields: fields{
ingGroup: Group{
Members: []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"alb.ingress.kubernetes.io/tags": "k1=v1,k3=v3a",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"alb.ingress.kubernetes.io/tags": "k2=v2",
},
},
},
},
},
defaultTags: map[string]string{
"k3": "v3",
"k4": "v4",
},
},
want: map[string]string{
"k1": "v1",
"k2": "v2",
"k3": "v3a",
"k4": "v4",
},
},
{
name: "empty default tags, conflicting tags annotation",
fields: fields{
ingGroup: Group{
Members: []*networking.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"alb.ingress.kubernetes.io/tags": "k1=v1,k3=v3a",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"alb.ingress.kubernetes.io/tags": "k2=v2,k3=v3b",
},
},
},
},
},
defaultTags: nil,
},
wantErr: errors.New("conflicting tag k3: v3a | v3b"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
task := &defaultModelBuildTask{
ingGroup: tt.fields.ingGroup,
defaultTags: tt.fields.defaultTags,
annotationParser: annotations.NewSuffixAnnotationParser("alb.ingress.kubernetes.io"),
}
got, err := task.buildLoadBalancerTags(context.Background())
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}
13 changes: 10 additions & 3 deletions pkg/ingress/model_build_managed_sg.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,26 @@ func (t *defaultModelBuildTask) buildManagedSecurityGroupName(_ context.Context)
}

func (t *defaultModelBuildTask) buildManagedSecurityGroupTags(_ context.Context) (map[string]string, error) {
mergedTags := make(map[string]string)
annotationTags := make(map[string]string)
for _, ing := range t.ingGroup.Members {
var rawTags map[string]string
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixTags, &rawTags, ing.Annotations); err != nil {
return nil, err
}
for tagKey, tagValue := range rawTags {
if existingTagValue, exists := mergedTags[tagKey]; exists && existingTagValue != tagValue {
if existingTagValue, exists := annotationTags[tagKey]; exists && existingTagValue != tagValue {
return nil, errors.Errorf("conflicting tag %v: %v | %v", tagKey, existingTagValue, tagValue)
}
mergedTags[tagKey] = tagValue
annotationTags[tagKey] = tagValue
}
}
mergedTags := make(map[string]string)
for k, v := range t.defaultTags {
mergedTags[k] = v
}
for k, v := range annotationTags {
mergedTags[k] = v
}
return mergedTags, nil
}

Expand Down
Loading