Skip to content

Commit d3ac622

Browse files
authored
Experimental configurations to override controller names (#494)
Since the early days of ACK, we've always relied on the `serviceID` to name the controller repositories. The way names, go modules, import paths, helm charts are named relied on this `serviceID`. Which was the main reason why some controllers had a bit of odd names, like `prometheusservice-controller` instead of `amp-controller`, `acmpca-controller` instead of `pca-controller`. This patch introduces experimental configurations that instructs the code generator to override the serviceID/servicePackageName when it comes to import paths/helm charts names etc... Related issue: aws-controllers-k8s/community#1411 Signed-off-by: Amine Hilaly <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 947081f commit d3ac622

35 files changed

+103
-63
lines changed

cmd/ack-generate/command/common.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ func getLatestAPIVersion(apiVersions []ackmetadata.ServiceVersion) (string, erro
9999

100100
// getServiceAccountName gets the service account name from the optional flag passed into ack-generate
101101
func getServiceAccountName() (string, error) {
102-
103102
if optServiceAccountName != "" {
104103
return optServiceAccountName, nil
105104
}

cmd/ack-generate/command/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func FallBackFindServiceID(sdkDir, svcAlias string) (string, error) {
127127
defer f.Close()
128128
scanner := bufio.NewScanner(f)
129129
for scanner.Scan() {
130-
if strings.Contains(scanner.Text(), "serviceId") {
130+
if strings.Contains(scanner.Text(), "serviceId") && !strings.Contains(scanner.Text(), "serviceIdentifier") {
131131
getServiceID := strings.Split(scanner.Text(), ":")
132132
re := regexp.MustCompile(`[," \t]`)
133133
svcID := strings.ToLower(re.ReplaceAllString(getServiceID[1], ``))

pkg/config/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ type Config struct {
4444
// only the service model name, but also the iface name to `PipesAPI`. See
4545
// https://github.com/aws/aws-sdk-go/blob/main/service/pipes/pipesiface/interface.go#L62
4646
SDKNames SDKNames `json:"sdk_names"`
47+
// ControllerName lets you specify a service alias override. This can be
48+
// used to only override the controller name exposed to the user. Meaning that
49+
// it will only change the module name, the import paths, and the controller
50+
// name. This is useful when the service identifier is not very user friendly
51+
// and you want to expose a more user friendly name to the user. e.g docdb ->
52+
// documentdb.
53+
// This will also change the helm chart and image names.
54+
ControllerName string `json:"controller_name,omitempty"`
4755
}
4856

4957
// SDKNames contains information on the SDK Client package. More precisely
@@ -56,6 +64,14 @@ type SDKNames struct {
5664
// model name is `opensearch` and the service package is called
5765
// `opensearchservice`.
5866
Model string `json:"model_name,omitempty"`
67+
// Package let you define the package name of the service client. This field
68+
// is optional and only needed for services such as documentdb where the service
69+
// controller is called `documentdb` and the package is called `docdb`.
70+
//
71+
// You might be wondering why not just use the `model_name` field... well the
72+
// answer is prometheusservice... the model name is `amp` and the service package
73+
// is called `prometheusservice`. :shrug:
74+
Package string `json:"package_name,omitempty"`
5975
// ClientInterface is the name of the interface that defines the "shape" of
6076
// the a sdk service client. e.g PipesAPI, LambdaAPI, etc...
6177
ClientInterface string `json:"client_interface,omitempty"`

pkg/generate/ack/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func Release(
100100
templateBasePaths,
101101
releaseIncludePaths,
102102
releaseCopyPaths,
103-
releaseFuncMap(m.MetaVars().ServicePackageName),
103+
releaseFuncMap(m.MetaVars().ControllerName),
104104
)
105105
metaVars := m.MetaVars()
106106

pkg/generate/templateset/vars.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ package templateset
1616
// MetaVars contains template variables that most templates need access to
1717
// that describe the service alias, its package name, etc
1818
type MetaVars struct {
19+
// ControllerName contains the exact string used to identify the ACK
20+
// controller in the aws-controllers-k8s project. This name is used as the
21+
// name of the ACK controller's module, repository and helm chart.
22+
ControllerName string
1923
// ServiceModelName contains the exact string used to identify the AWS
2024
// service API in the aws-sdk-go's models/apis/ directory. Note that some
2125
// APIs this name does not match the ServiceID. e.g. The AWS Step Functions

pkg/model/model.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,24 @@ type Model struct {
5252
// MetaVars returns a MetaVars struct populated with metadata about the AWS
5353
// service API
5454
func (m *Model) MetaVars() templateset.MetaVars {
55+
controllerName := m.cfg.ControllerName
56+
if controllerName == "" {
57+
controllerName = m.servicePackageName
58+
}
59+
// NOTE(a-hilaly): I know this is a bit of a hack and it's confusing, but
60+
// long time ago, we assumed that model_name is always equal to the service
61+
// name. This is not the case anymore, prometheusservice and documentdb
62+
// are examples of services that have different model names.
63+
//
64+
// TODO(a-hilaly): We should probably rework all this naming stuff to be
65+
// more consistent. To whoever is reading this, I'm sorry.
66+
servicePackageName := m.servicePackageName
67+
if m.cfg.SDKNames.Package != "" {
68+
servicePackageName = m.cfg.SDKNames.Package
69+
}
5570
return templateset.MetaVars{
56-
ServicePackageName: m.servicePackageName,
71+
ControllerName: controllerName,
72+
ServicePackageName: servicePackageName,
5773
ServiceID: m.SDKAPI.ServiceID(),
5874
ServiceModelName: m.cfg.SDKNames.Model,
5975
APIGroup: m.APIGroup(),
@@ -943,7 +959,11 @@ func (m *Model) APIGroup() string {
943959
if m.SDKAPI.APIGroupSuffix != "" {
944960
suffix = m.SDKAPI.APIGroupSuffix
945961
}
946-
return fmt.Sprintf("%s.%s", m.servicePackageName, suffix)
962+
name := m.GetConfig().ControllerName
963+
if name == "" {
964+
name = m.servicePackageName
965+
}
966+
return fmt.Sprintf("%s.%s", name, suffix)
947967
}
948968

949969
// ClientInterfaceTypeName returns the name of the aws-sdk-go primary API

templates/cmd/controller/main.go.tpl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,27 @@ import (
2525
If these referenced types are not added to scheme, this service controller will not be able to read
2626
resources across service controller. */ -}}
2727
{{- $servicePackageName := .ServicePackageName }}
28+
{{- $controllerName := .ControllerName }}
2829
{{- $apiVersion := .APIVersion }}
2930
{{- range $referencedServiceName := .ReferencedServiceNames }}
3031
{{- if not (eq $referencedServiceName $servicePackageName) }}
3132
{{ $referencedServiceName }}apitypes "github.com/aws-controllers-k8s/{{ $referencedServiceName }}-controller/apis/{{ $apiVersion }}"
3233
{{- end }}
3334
{{- end }}
3435

35-
svcresource "github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/pkg/resource"
36+
svcresource "github.com/aws-controllers-k8s/{{ .ControllerName }}-controller/pkg/resource"
3637
svcsdk "github.com/aws/aws-sdk-go/service/{{ .ServicePackageName }}"
37-
svctypes "github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/apis/{{ .APIVersion }}"
38+
svctypes "github.com/aws-controllers-k8s/{{ .ControllerName }}-controller/apis/{{ .APIVersion }}"
3839

3940
{{/* TODO(a-hilaly): import apis/* packages to register webhooks */}}
40-
{{range $crdName := .SnakeCasedCRDNames }}_ "github.com/aws-controllers-k8s/{{ $servicePackageName }}-controller/pkg/resource/{{ $crdName }}"
41+
{{range $crdName := .SnakeCasedCRDNames }}_ "github.com/aws-controllers-k8s/{{ $controllerName }}-controller/pkg/resource/{{ $crdName }}"
4142
{{end}}
42-
"github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/pkg/version"
43+
"github.com/aws-controllers-k8s/{{ .ControllerName }}-controller/pkg/version"
4344
)
4445

4546
var (
4647
awsServiceAPIGroup = "{{ .APIGroup }}"
47-
awsServiceAlias = "{{ .ServicePackageName }}"
48+
awsServiceAlias = "{{ .ControllerName }}"
4849
awsServiceEndpointsID = svcsdk.EndpointsID
4950
scheme = runtime.NewScheme()
5051
setupLog = ctrlrt.Log.WithName("setup")

templates/config/controller/deployment.yaml.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ metadata:
66
apiVersion: apps/v1
77
kind: Deployment
88
metadata:
9-
name: ack-{{ .ServicePackageName }}-controller
9+
name: ack-{{ .ControllerName }}-controller
1010
namespace: ack-system
1111
labels:
12-
app.kubernetes.io/name: ack-{{ .ServicePackageName }}-controller
12+
app.kubernetes.io/name: ack-{{ .ControllerName }}-controller
1313
app.kubernetes.io/part-of: ack-system
1414
spec:
1515
selector:
1616
matchLabels:
17-
app.kubernetes.io/name: ack-{{ .ServicePackageName }}-controller
17+
app.kubernetes.io/name: ack-{{ .ControllerName }}-controller
1818
replicas: 1
1919
template:
2020
metadata:
2121
labels:
22-
app.kubernetes.io/name: ack-{{ .ServicePackageName }}-controller
22+
app.kubernetes.io/name: ack-{{ .ControllerName }}-controller
2323
spec:
2424
containers:
2525
- command:

templates/config/controller/olm-kustomization.yaml.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ patches:
1515
target:
1616
group: apps
1717
kind: Deployment
18-
name: ack-{{ .ServicePackageName }}-controller
18+
name: ack-{{ .ControllerName }}-controller
1919
version: v1
2020
- path: user-env.yaml

templates/config/controller/service.yaml.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
apiVersion: v1
22
kind: Service
33
metadata:
4-
name: ack-{{ .ServicePackageName }}-metrics-service
4+
name: ack-{{ .ControllerName }}-metrics-service
55
namespace: ack-system
66
spec:
77
selector:
8-
app.kubernetes.io/name: ack-{{ .ServicePackageName }}-controller
8+
app.kubernetes.io/name: ack-{{ .ControllerName }}-controller
99
ports:
1010
- name: metricsport
1111
port: 8080

0 commit comments

Comments
 (0)