Skip to content

Commit 25aea82

Browse files
committed
feat: Add default tags CLI option [K8SPCORE-1707]
1 parent a8120f7 commit 25aea82

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

controllers/eip_controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type EIPReconciler struct {
4040
NonCachingClient client.Client
4141
Log logr.Logger
4242
EC2 *ec2.EC2
43+
Tags map[string]string
4344
}
4445

4546
// +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
190191
}
191192
}
192193

194+
tags := ec2.TagSpecification{
195+
ResourceType: aws.String("elastic-ip"),
196+
}
197+
for k, v := range r.Tags {
198+
tags.Tags = append(tags.Tags, &ec2.Tag{
199+
Key: aws.String(k),
200+
Value: aws.String(v),
201+
})
202+
}
203+
input.TagSpecifications = []*ec2.TagSpecification{&tags}
204+
193205
if resp, err := r.EC2.AllocateAddressWithContext(ctx, input); err != nil {
194206
return err
195207
} else {

controllers/eni_controller.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type ENIReconciler struct {
4141
NonCachingClient client.Client
4242
Log logr.Logger
4343
EC2 *ec2.EC2
44+
Tags map[string]string
4445
}
4546

4647
// +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
7475
if eni.Spec.SecondaryPrivateIPAddressCount > 0 {
7576
input.SecondaryPrivateIpAddressCount = aws.Int64(eni.Spec.SecondaryPrivateIPAddressCount)
7677
}
78+
79+
tags := ec2.TagSpecification{
80+
ResourceType: aws.String("elastic-ip"),
81+
}
82+
for k, v := range r.Tags {
83+
tags.Tags = append(tags.Tags, &ec2.Tag{
84+
Key: aws.String(k),
85+
Value: aws.String(v),
86+
})
87+
}
88+
input.TagSpecifications = []*ec2.TagSpecification{&tags}
89+
7790
resp, err := r.EC2.CreateNetworkInterface(input)
7891
if err != nil {
7992
return ctrl.Result{}, err

main

64.4 MB
Binary file not shown.

main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818
import (
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

4748
func 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

Comments
 (0)