Skip to content

Commit b387749

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

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ bin
2323
*.swp
2424
*.swo
2525
*~
26+
27+
main

controllers/eip_controller.go

Lines changed: 24 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
@@ -80,6 +81,7 @@ func (r *EIPReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
8081

8182
addr := resp.Addresses[0]
8283

84+
r.Log.Info("Running reconcileTags in Reconcile")
8385
if err := r.reconcileTags(ctx, &eip, addr.Tags); err != nil {
8486
return ctrl.Result{}, err
8587
}
@@ -190,6 +192,17 @@ func (r *EIPReconciler) allocateEIP(ctx context.Context, eip *awsv1alpha1.EIP, l
190192
}
191193
}
192194

195+
tags := ec2.TagSpecification{
196+
ResourceType: aws.String("elastic-ip"),
197+
}
198+
for k, v := range r.Tags {
199+
tags.Tags = append(tags.Tags, &ec2.Tag{
200+
Key: aws.String(k),
201+
Value: aws.String(v),
202+
})
203+
}
204+
input.TagSpecifications = []*ec2.TagSpecification{&tags}
205+
193206
if resp, err := r.EC2.AllocateAddressWithContext(ctx, input); err != nil {
194207
return err
195208
} else {
@@ -225,6 +238,16 @@ func (r *EIPReconciler) reconcileTags(ctx context.Context, eip *awsv1alpha1.EIP,
225238
tagsToCreate = append(tagsToCreate, &ec2.Tag{Key: aws.String(k), Value: aws.String(v)})
226239
}
227240
}
241+
242+
// Add default tags
243+
for k, v := range r.Tags {
244+
tagsToCreate = append(tagsToCreate, &ec2.Tag{
245+
Key: aws.String(k),
246+
Value: aws.String(v),
247+
})
248+
}
249+
250+
r.Log.Info(fmt.Sprintf("tags %+v", tagsToCreate))
228251
if len(tagsToCreate) > 0 {
229252
if _, err := r.EC2.CreateTagsWithContext(ctx, &ec2.CreateTagsInput{
230253
Resources: resources,
@@ -240,6 +263,7 @@ func (r *EIPReconciler) reconcileTags(ctx context.Context, eip *awsv1alpha1.EIP,
240263
tagsToRemove = append(tagsToRemove, tag)
241264
}
242265
}
266+
r.Log.Info(fmt.Sprintf("tags to remove %+v", tagsToRemove))
243267
if len(tagsToRemove) > 0 {
244268
_, err := r.EC2.DeleteTagsWithContext(ctx, &ec2.DeleteTagsInput{
245269
Resources: resources,

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.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)