Skip to content

Commit ae6e790

Browse files
authored
refactor tag-format constants to a separate package (#89)
Issue #, if available: aws-controllers-k8s/community#1261 Description of changes: * Using tag-format constants inside `pkg/config/config.go` rather than hardcoded strings * Move the tag format definition into a separate package to avoid cyclic import between `pkg/config` & `pkg/runtime` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 50de420 commit ae6e790

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

pkg/config/config.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"go.uber.org/zap/zapcore"
2626
ctrlrt "sigs.k8s.io/controller-runtime"
2727
"sigs.k8s.io/controller-runtime/pkg/log/zap"
28+
29+
acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
2830
)
2931

3032
const (
@@ -43,8 +45,13 @@ const (
4345

4446
var (
4547
defaultResourceTags = []string{
46-
"services.k8s.aws/controller-version=%CONTROLLER_VERSION%",
47-
"services.k8s.aws/namespace=%K8S_NAMESPACE%",
48+
fmt.Sprintf("services.k8s.aws/controller-version=%s-%s",
49+
acktags.ServiceAliasTagFormat,
50+
acktags.ControllerVersionTagFormat,
51+
),
52+
fmt.Sprintf("services.k8s.aws/namespace=%s",
53+
acktags.NamespaceTagFormat,
54+
),
4855
}
4956
)
5057

pkg/runtime/tags.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
rtclient "sigs.k8s.io/controller-runtime/pkg/client"
2020

2121
ackconfig "github.com/aws-controllers-k8s/runtime/pkg/config"
22+
acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
2223
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
2324
)
2425

@@ -29,11 +30,7 @@ type resolveTagFormat func(rtclient.Object, acktypes.ServiceControllerMetadata)
2930
const (
3031
// MissingImageTagValue is the placeholder value when ACK controller
3132
// image tag(release semver) cannot be determined.
32-
MissingImageTagValue = "unknown"
33-
ServiceAliasTagFormat = "%CONTROLLER_SERVICE%"
34-
ControllerVersionTagFormat = "%CONTROLLER_VERSION%"
35-
NamespaceTagFormat = "%K8S_NAMESPACE%"
36-
ResourceNameTagFormat = "%K8S_RESOURCE_NAME%"
33+
MissingImageTagValue = "unknown"
3734
)
3835

3936
// ACKResourceTagFormats is map of ACK resource tag formats to it's
@@ -43,14 +40,14 @@ const (
4340
// resolveTagFormat function and expandTagValue() method will start
4441
// expanding the new resource tag format.
4542
var ACKResourceTagFormats = map[string]resolveTagFormat{
46-
ServiceAliasTagFormat: func(
43+
acktags.ServiceAliasTagFormat: func(
4744
obj rtclient.Object,
4845
md acktypes.ServiceControllerMetadata,
4946
) string {
5047
return md.ServiceAlias
5148
},
5249

53-
ControllerVersionTagFormat: func(
50+
acktags.ControllerVersionTagFormat: func(
5451
obj rtclient.Object,
5552
md acktypes.ServiceControllerMetadata,
5653
) string {
@@ -65,14 +62,14 @@ var ACKResourceTagFormats = map[string]resolveTagFormat{
6562
return controllerImageTag
6663
},
6764

68-
NamespaceTagFormat: func(
65+
acktags.NamespaceTagFormat: func(
6966
obj rtclient.Object,
7067
md acktypes.ServiceControllerMetadata,
7168
) string {
7269
return obj.GetNamespace()
7370
},
7471

75-
ResourceNameTagFormat: func(
72+
acktags.ResourceNameTagFormat: func(
7673
obj rtclient.Object,
7774
md acktypes.ServiceControllerMetadata,
7875
) string {

pkg/runtime/tags_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
114
package runtime_test
215

316
import (
417
"fmt"
518
"testing"
619

7-
"github.com/aws-controllers-k8s/runtime/pkg/config"
20+
"github.com/stretchr/testify/assert"
821

922
mocks "github.com/aws-controllers-k8s/runtime/mocks/controller-runtime/pkg/client"
23+
"github.com/aws-controllers-k8s/runtime/pkg/config"
1024
"github.com/aws-controllers-k8s/runtime/pkg/runtime"
11-
"github.com/aws-controllers-k8s/runtime/pkg/types"
12-
"github.com/stretchr/testify/assert"
25+
acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
26+
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
1327
)
1428

1529
func TestGetDefaultTags(t *testing.T) {
@@ -20,9 +34,9 @@ func TestGetDefaultTags(t *testing.T) {
2034

2135
cfg := config.Config{}
2236

23-
md := types.ServiceControllerMetadata{
37+
md := acktypes.ServiceControllerMetadata{
2438
ServiceAlias: "s3",
25-
VersionInfo: types.VersionInfo{
39+
VersionInfo: acktypes.VersionInfo{
2640
GitVersion: "v0.0.10",
2741
},
2842
}
@@ -56,14 +70,14 @@ func TestGetDefaultTags(t *testing.T) {
5670
cfg.ResourceTags = []string{
5771
"foo=bar",
5872
fmt.Sprintf("services.k8s.aws/controller-version=%s-%s",
59-
runtime.ServiceAliasTagFormat,
60-
runtime.ControllerVersionTagFormat,
73+
acktags.ServiceAliasTagFormat,
74+
acktags.ControllerVersionTagFormat,
6175
),
6276
fmt.Sprintf("services.k8s.aws/namespace=%s",
63-
runtime.NamespaceTagFormat,
77+
acktags.NamespaceTagFormat,
6478
),
6579
fmt.Sprintf("services.k8s.aws/name=%s",
66-
runtime.ResourceNameTagFormat,
80+
acktags.ResourceNameTagFormat,
6781
),
6882
}
6983
expandedTags = runtime.GetDefaultTags(&cfg, &obj, md)

pkg/tags/tag_format.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package tags
15+
16+
const (
17+
ServiceAliasTagFormat = "%CONTROLLER_SERVICE%"
18+
ControllerVersionTagFormat = "%CONTROLLER_VERSION%"
19+
NamespaceTagFormat = "%K8S_NAMESPACE%"
20+
ResourceNameTagFormat = "%K8S_RESOURCE_NAME%"
21+
)

0 commit comments

Comments
 (0)