@@ -18,6 +18,7 @@ package main
1818import (
1919 "flag"
2020 "os"
21+ "strings"
2122
2223 "github.com/aws/aws-sdk-go/aws"
2324 "github.com/aws/aws-sdk-go/aws/session"
@@ -45,11 +46,12 @@ func init() {
4546}
4647
4748func main () {
48- var metricsAddr , region , leaderElectionID , leaderElectionNamespace string
49+ var metricsAddr , region , leaderElectionID , leaderElectionNamespace , defaultTags string
4950 flag .StringVar (& metricsAddr , "metrics-addr" , ":8080" , "The address the metric endpoint binds to." )
5051 flag .StringVar (& region , "region" , "" , "AWS region" )
5152 flag .StringVar (& leaderElectionID , "leader-election-id" , "k8s-aws-operator" , "the name of the configmap do use as leader election lock" )
5253 flag .StringVar (& leaderElectionNamespace , "leader-election-namespace" , "" , "the namespace in which the leader election lock will be held" )
54+ flag .StringVar (& defaultTags , "default-tags" , "" , "default tags to add to created resources, in the format key1=value1,key2=value2" )
5355 opts := zap.Options {
5456 Development : true ,
5557 }
@@ -90,11 +92,18 @@ func main() {
9092 os .Exit (1 )
9193 }
9294
95+ defaultTagsMap := make (map [string ]string )
96+ if defaultTags != "" {
97+ parseTags (& defaultTagsMap , defaultTags )
98+ setupLog .Info ("Default tags set" , "tags" , defaultTagsMap )
99+ }
100+
93101 err = (& controllers.EIPReconciler {
94102 Client : cachingClient ,
95103 NonCachingClient : nonCachingClient ,
96104 Log : ctrl .Log .WithName ("controllers" ).WithName ("EIP" ),
97105 EC2 : ec2 ,
106+ Tags : defaultTagsMap ,
98107 }).SetupWithManager (mgr )
99108 if err != nil {
100109 setupLog .Error (err , "unable to create controller" , "controller" , "EIP" )
@@ -105,6 +114,7 @@ func main() {
105114 NonCachingClient : nonCachingClient ,
106115 Log : ctrl .Log .WithName ("controllers" ).WithName ("ENI" ),
107116 EC2 : ec2 ,
117+ Tags : defaultTagsMap ,
108118 }).SetupWithManager (mgr )
109119 if err != nil {
110120 setupLog .Error (err , "unable to create controller" , "controller" , "ENI" )
@@ -126,3 +136,12 @@ func main() {
126136 os .Exit (1 )
127137 }
128138}
139+
140+ func parseTags (tagMap * map [string ]string , tags string ) {
141+ for _ , tag := range strings .Split (tags , "," ) {
142+ kv := strings .SplitN (tag , "=" , 2 )
143+ if len (kv ) == 2 {
144+ (* tagMap )[strings .TrimSpace (kv [0 ])] = strings .TrimSpace (kv [1 ])
145+ }
146+ }
147+ }
0 commit comments