Skip to content

Commit 08a6708

Browse files
authored
update for default resource tags (#87)
Issue #, if available: aws-controllers-k8s/community#1261 Description of changes: * This PR changes the default ACK resource tags from empty list to []string{"services.k8s.aws/controller-version=%CONTROLLER_VERSION%" ,"services.k8s.aws/namespace=%K8S_NAMESPACE%"}. * Instead of statically passing the controller release version from release artifacts, i made it a dynamic `%CONTROLLER_VERSION%` variable. The dynamic variable will allow customers to use this variable in their custom tags as well. * I refactored the various service controller metadata fields into a new `ServiceControllerMetadata` type as well. Tested locally that the resource tags get resolved as expected. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 70b4f7c commit 08a6708

File tree

6 files changed

+96
-37
lines changed

6 files changed

+96
-37
lines changed

mocks/pkg/types/service_controller.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const (
4141
envVarAWSRegion = "AWS_REGION"
4242
)
4343

44+
var (
45+
defaultResourceTags = []string{
46+
"services.k8s.aws/controller-version=%CONTROLLER_VERSION%",
47+
"services.k8s.aws/namespace=%K8S_NAMESPACE%",
48+
}
49+
)
50+
4451
// Config contains configuration otpions for ACK service controllers
4552
type Config struct {
4653
MetricsAddr string
@@ -104,7 +111,7 @@ func (cfg *Config) BindFlags() {
104111
)
105112
flag.StringSliceVar(
106113
&cfg.ResourceTags, flagResourceTags,
107-
[]string{},
114+
defaultResourceTags,
108115
"Configures the ACK service controller to always set key/value pairs tags on resources that it manages.",
109116
)
110117
flag.StringVar(

pkg/runtime/service_controller.go

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,11 @@ import (
3333
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
3434
)
3535

36-
// VersionInfo contains information about the version of the runtime and
37-
// service controller in use
38-
type VersionInfo struct {
39-
// GitCommit is the SHA1 commit for the service controller's code
40-
GitCommit string
41-
// GitVersion is the latest Git tag from the service controller's code
42-
GitVersion string
43-
// BuildDate is a timestamp of when the code was built
44-
BuildDate string
45-
}
46-
4736
// serviceController wraps a number of `controller-runtime.Reconciler` that are
4837
// related to a specific AWS service API.
4938
type serviceController struct {
39+
acktypes.ServiceControllerMetadata
5040
metaLock sync.RWMutex
51-
// ServiceAlias is a string with the alias of the service API, e.g. "s3"
52-
ServiceAlias string
53-
// ServiceAPIGroup is a string with the full DNS-correct API group that
54-
// this service controller manages, e.g. "s3.services.k8s.aws"
55-
ServiceAPIGroup string
56-
// ServiceEndpointsID is a string with the service API's EndpointsID, e.g. "api.sagemaker"
57-
ServiceEndpointsID string
58-
// VersionInfo describes the service controller's built code
59-
VersionInfo VersionInfo
6041
// rmFactories is a map of resource manager factories, keyed by the
6142
// GroupKind of the resource managed by the resource manager produced by
6243
// that factory
@@ -258,18 +239,25 @@ func (c *serviceController) BindControllerManager(mgr ctrlrt.Manager, cfg ackcfg
258239
return nil
259240
}
260241

242+
// GetMetadata returns the metadata associated with the service controller.
243+
func (c *serviceController) GetMetadata() acktypes.ServiceControllerMetadata {
244+
return c.ServiceControllerMetadata
245+
}
246+
261247
// NewServiceController returns a new serviceController instance
262248
func NewServiceController(
263249
svcAlias string,
264250
svcAPIGroup string,
265251
svcEndpointsID string,
266-
versionInfo VersionInfo,
252+
versionInfo acktypes.VersionInfo,
267253
) acktypes.ServiceController {
268254
return &serviceController{
269-
ServiceAlias: svcAlias,
270-
ServiceAPIGroup: svcAPIGroup,
271-
ServiceEndpointsID: svcEndpointsID,
272-
VersionInfo: versionInfo,
273-
metrics: ackmetrics.NewMetrics(svcAlias),
255+
ServiceControllerMetadata: acktypes.ServiceControllerMetadata{
256+
VersionInfo: versionInfo,
257+
ServiceAlias: svcAlias,
258+
ServiceAPIGroup: svcAPIGroup,
259+
ServiceEndpointsID: svcEndpointsID,
260+
},
261+
metrics: ackmetrics.NewMetrics(svcAlias),
274262
}
275263
}

pkg/runtime/service_controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ import (
4040
"sigs.k8s.io/controller-runtime/pkg/webhook"
4141

4242
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
43+
mocks "github.com/aws-controllers-k8s/runtime/mocks/pkg/types"
4344
ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config"
4445
ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"
45-
46-
mocks "github.com/aws-controllers-k8s/runtime/mocks/pkg/types"
46+
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
4747
)
4848

4949
var (
@@ -147,7 +147,7 @@ func TestServiceController(t *testing.T) {
147147
reg := ackrt.NewRegistry()
148148
reg.RegisterResourceManagerFactory(rmf)
149149

150-
vi := ackrt.VersionInfo{
150+
vi := acktypes.VersionInfo{
151151
GitCommit: "test-commit",
152152
GitVersion: "test-version",
153153
BuildDate: "now",

pkg/runtime/tags.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,42 @@
1414
package runtime
1515

1616
import (
17+
"fmt"
1718
"strings"
18-
"time"
1919

20-
ackconfig "github.com/aws-controllers-k8s/runtime/pkg/config"
2120
rtclient "sigs.k8s.io/controller-runtime/pkg/client"
21+
22+
ackconfig "github.com/aws-controllers-k8s/runtime/pkg/config"
23+
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
24+
)
25+
26+
const (
27+
// MissingImageTagValue is the placeholder value when ACK controller
28+
// image tag(release semver) cannot be determined.
29+
MissingImageTagValue = "unknown"
2230
)
2331

2432
// GetDefaultTags provides Default tags (key value pairs) for given resource
2533
func GetDefaultTags(
2634
config *ackconfig.Config,
2735
object rtclient.Object,
36+
md acktypes.ServiceControllerMetadata,
2837
) map[string]string {
2938
if object == nil || config == nil || len(config.ResourceTags) == 0 {
3039
return nil
3140
}
3241
var populatedTags = make(map[string]string)
3342
for _, tagKeyVal := range config.ResourceTags {
3443
keyVal := strings.Split(tagKeyVal, "=")
35-
if keyVal == nil && len(keyVal) != 2 {
44+
if keyVal == nil || len(keyVal) != 2 {
3645
continue
3746
}
3847
key := strings.TrimSpace(keyVal[0])
3948
val := strings.TrimSpace(keyVal[1])
4049
if key == "" || val == "" {
4150
continue
4251
}
43-
populatedValue := expandTagValue(&val, object)
52+
populatedValue := expandTagValue(&val, object, md)
4453
populatedTags[key] = *populatedValue
4554
}
4655
if len(populatedTags) == 0 {
@@ -52,18 +61,34 @@ func GetDefaultTags(
5261
func expandTagValue(
5362
value *string,
5463
obj rtclient.Object,
64+
md acktypes.ServiceControllerMetadata,
5565
) *string {
5666
if value == nil || obj == nil {
5767
return nil
5868
}
5969
var expandedValue string = ""
6070
switch *value {
61-
case "%UTCNOW%":
62-
expandedValue = time.Now().UTC().String()
63-
case "%KUBERNETES_NAMESPACE%":
71+
case "%CONTROLLER_VERSION%":
72+
expandedValue = generateControllerVersion(md)
73+
case "%K8S_NAMESPACE%":
6474
expandedValue = obj.GetNamespace()
6575
default:
6676
expandedValue = *value
6777
}
6878
return &expandedValue
6979
}
80+
81+
// generateControllerVersion creates the tag value for key
82+
// "services.k8s.aws/controller-version". The value for this tag is in the
83+
// format "<service-name>-<controller-image-tag>". Ex: s3-v0.0.10
84+
func generateControllerVersion(md acktypes.ServiceControllerMetadata) string {
85+
controllerImageTag := md.GitVersion
86+
// ACK controller released from the ACK CD pipeline will have the correct
87+
// GitVersion. But this value can be empty when manually building ACK
88+
// controller image locally and not passing the go ldflags.
89+
// Add a placeholder value when git tag is found missing.
90+
if controllerImageTag == "" {
91+
controllerImageTag = MissingImageTagValue
92+
}
93+
return fmt.Sprintf("%s-%s", md.ServiceAlias, controllerImageTag)
94+
}

pkg/types/service_controller.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ import (
2424
ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config"
2525
)
2626

27+
// VersionInfo contains information about the version of the runtime and
28+
// service controller in use
29+
type VersionInfo struct {
30+
// GitCommit is the SHA1 commit for the service controller's code
31+
GitCommit string
32+
// GitVersion is the latest Git tag from the service controller's code
33+
GitVersion string
34+
// BuildDate is a timestamp of when the code was built
35+
BuildDate string
36+
}
37+
38+
type ServiceControllerMetadata struct {
39+
VersionInfo
40+
// ServiceAlias is a string with the alias of the service API, e.g. "s3"
41+
ServiceAlias string
42+
// ServiceAPIGroup is a string with the full DNS-correct API group that
43+
// this service controller manages, e.g. "s3.services.k8s.aws"
44+
ServiceAPIGroup string
45+
// ServiceEndpointsID is a string with the service API's EndpointsID, e.g. "api.sagemaker"
46+
ServiceEndpointsID string
47+
}
48+
2749
// ServiceController wraps one or more reconcilers (for individual resources in
2850
// an AWS API) with the upstream common controller-runtime machinery.
2951
type ServiceController interface {
@@ -64,4 +86,7 @@ type ServiceController interface {
6486
ackv1alpha1.AWSResourceName,
6587
schema.GroupVersionKind,
6688
) (*session.Session, error)
89+
90+
// GetMetadata returns the metadata associated with the service controller.
91+
GetMetadata() ServiceControllerMetadata
6792
}

0 commit comments

Comments
 (0)