Skip to content
Closed
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
12 changes: 12 additions & 0 deletions controllers/eip_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions controllers/eni_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(&region, "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,
}
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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])
}
}
}
Loading