diff --git a/controllers/eip_controller.go b/controllers/eip_controller.go index e972b38..1d9204e 100644 --- a/controllers/eip_controller.go +++ b/controllers/eip_controller.go @@ -40,6 +40,7 @@ type EIPReconciler struct { NonCachingClient client.Client Log logr.Logger EC2 *ec2.EC2 + Tags map[string]string } // +kubebuilder:rbac:groups=aws.k8s.logmein.com,resources=eips,verbs=get;list;watch;create;update;patch;delete @@ -190,6 +191,17 @@ func (r *EIPReconciler) allocateEIP(ctx context.Context, eip *awsv1alpha1.EIP, l } } + tags := ec2.TagSpecification{ + ResourceType: aws.String("elastic-ip"), + } + for k, v := range r.Tags { + tags.Tags = append(tags.Tags, &ec2.Tag{ + Key: aws.String(k), + Value: aws.String(v), + }) + } + input.TagSpecifications = []*ec2.TagSpecification{&tags} + if resp, err := r.EC2.AllocateAddressWithContext(ctx, input); err != nil { return err } else { diff --git a/controllers/eni_controller.go b/controllers/eni_controller.go index ef15860..16c61bc 100644 --- a/controllers/eni_controller.go +++ b/controllers/eni_controller.go @@ -41,6 +41,7 @@ type ENIReconciler struct { NonCachingClient client.Client Log logr.Logger EC2 *ec2.EC2 + Tags map[string]string } // +kubebuilder:rbac:groups=aws.k8s.logmein.com,resources=enis,verbs=get;list;watch;create;update;patch;delete @@ -74,6 +75,18 @@ func (r *ENIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R if eni.Spec.SecondaryPrivateIPAddressCount > 0 { input.SecondaryPrivateIpAddressCount = aws.Int64(eni.Spec.SecondaryPrivateIPAddressCount) } + + tags := ec2.TagSpecification{ + ResourceType: aws.String("elastic-ip"), + } + for k, v := range r.Tags { + tags.Tags = append(tags.Tags, &ec2.Tag{ + Key: aws.String(k), + Value: aws.String(v), + }) + } + input.TagSpecifications = []*ec2.TagSpecification{&tags} + resp, err := r.EC2.CreateNetworkInterface(input) if err != nil { return ctrl.Result{}, err diff --git a/main.go b/main.go index 6f897ca..856a20f 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ package main import ( "flag" "os" + "strings" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" @@ -45,11 +46,12 @@ func init() { } func main() { - var metricsAddr, region, leaderElectionID, leaderElectionNamespace string + var metricsAddr, region, leaderElectionID, leaderElectionNamespace, defaultTags string flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") flag.StringVar(®ion, "region", "", "AWS region") flag.StringVar(&leaderElectionID, "leader-election-id", "k8s-aws-operator", "the name of the configmap do use as leader election lock") flag.StringVar(&leaderElectionNamespace, "leader-election-namespace", "", "the namespace in which the leader election lock will be held") + flag.StringVar(&defaultTags, "default-tags", "", "default tags to add to created resources, in the format key1=value1,key2=value2") opts := zap.Options{ Development: true, } @@ -90,11 +92,18 @@ func main() { os.Exit(1) } + defaultTagsMap := make(map[string]string) + if defaultTags != "" { + parseTags(&defaultTagsMap, defaultTags) + setupLog.Info("Default tags set", "tags", defaultTagsMap) + } + err = (&controllers.EIPReconciler{ Client: cachingClient, NonCachingClient: nonCachingClient, Log: ctrl.Log.WithName("controllers").WithName("EIP"), EC2: ec2, + Tags: defaultTagsMap, }).SetupWithManager(mgr) if err != nil { setupLog.Error(err, "unable to create controller", "controller", "EIP") @@ -105,6 +114,7 @@ func main() { NonCachingClient: nonCachingClient, Log: ctrl.Log.WithName("controllers").WithName("ENI"), EC2: ec2, + Tags: defaultTagsMap, }).SetupWithManager(mgr) if err != nil { setupLog.Error(err, "unable to create controller", "controller", "ENI") @@ -126,3 +136,12 @@ func main() { os.Exit(1) } } + +func parseTags(tagMap *map[string]string, tags string) { + for _, tag := range strings.Split(tags, ",") { + kv := strings.SplitN(tag, "=", 2) + if len(kv) == 2 { + (*tagMap)[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1]) + } + } +}