-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a ServiceAccount (config/rbac/service_account.yaml)
and kustomize ValueAddTransformer (config/default/service_account_transformer.yaml) that allow a user to specify which service account their manager should be created in. They may change this value by updating both files, minding the name prefix. pkg/plugins/golang/v3: update all scaffolds referencing the default service account with references to the "controller-manager" service account, specified in service_account.yaml, and scaffold the SerivceAccount and ValueAddTransformer Signed-off-by: Eric Stroczynski <ericstroczynski@gmail.com>
- Loading branch information
Showing
41 changed files
with
342 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ins/golang/v3/scaffolds/internal/templates/config/kdefault/service_account_transformer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kdefault | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/pkg/model/file" | ||
) | ||
|
||
var _ file.Template = &ServiceAccountTransformer{} | ||
|
||
// ServiceAccountTransformer scaffolds a file that defines the kustomization scheme for the default overlay folder | ||
type ServiceAccountTransformer struct { | ||
file.TemplateMixin | ||
file.ProjectNameMixin | ||
} | ||
|
||
// SetTemplateDefaults implements file.Template | ||
func (f *ServiceAccountTransformer) SetTemplateDefaults() error { | ||
if f.Path == "" { | ||
f.Path = filepath.Join("config", "default", "service_account_transformer.yaml") | ||
} | ||
|
||
f.TemplateBody = saReplaceTemplate | ||
|
||
f.IfExistsAction = file.Error | ||
|
||
return nil | ||
} | ||
|
||
//nolint:lll | ||
const saReplaceTemplate = `# This transformer replaces the service account name in config/rbac/service_account.yaml with | ||
# the result of applying the config/default name prefix to that name. | ||
apiVersion: builtin | ||
kind: ValueAddTransformer | ||
metadata: | ||
name: serviceAccountSubstitute | ||
# If you change this value, you must also update metadata.name in config/rbac/service_account.yaml. | ||
# Make sure the new value matches the result of applying any name prefix to the ServiceAccount's name. | ||
value: {{ .ProjectName }}-controller-manager | ||
# Update all RoleBindings and ClusterRoleBindings that have a ServiceAccount subject, | ||
# and the manager's Deployment. | ||
targets: | ||
- selector: | ||
kind: RoleBinding | ||
group: rbac.authorization.k8s.io | ||
version: v1 | ||
fieldPath: subjects/[kind=ServiceAccount]/name | ||
- selector: | ||
kind: ClusterRoleBinding | ||
group: rbac.authorization.k8s.io | ||
version: v1 | ||
fieldPath: subjects/[kind=ServiceAccount]/name | ||
- selector: | ||
kind: Deployment | ||
group: apps | ||
version: v1 | ||
name: controller-manager | ||
fieldPath: spec/template/spec/serviceAccountName | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/service_account.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rbac | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/pkg/model/file" | ||
) | ||
|
||
var _ file.Template = &ServiceAccount{} | ||
|
||
// ServiceAccount scaffolds a file that defines the service account the manager is deployed in. | ||
type ServiceAccount struct { | ||
file.TemplateMixin | ||
} | ||
|
||
// SetTemplateDefaults implements file.Template | ||
func (f *ServiceAccount) SetTemplateDefaults() error { | ||
if f.Path == "" { | ||
f.Path = filepath.Join("config", "rbac", "service_account.yaml") | ||
} | ||
|
||
f.TemplateBody = serviceAccountTemplate | ||
|
||
return nil | ||
} | ||
|
||
const serviceAccountTemplate = `apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: controller-manager | ||
namespace: system | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
testdata/project-v3-addon/config/default/service_account_transformer.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This transformer replaces the service account name in config/rbac/service_account.yaml with | ||
# the result of applying the config/default name prefix to that name. | ||
apiVersion: builtin | ||
kind: ValueAddTransformer | ||
metadata: | ||
name: serviceAccountSubstitute | ||
|
||
# If you change this value, you must also update metadata.name in config/rbac/service_account.yaml. | ||
# Make sure the new value matches the result of applying any name prefix to the ServiceAccount's name. | ||
value: project-v3-addon-controller-manager | ||
|
||
# Update all RoleBindings and ClusterRoleBindings that have a ServiceAccount subject, | ||
# and the manager's Deployment. | ||
targets: | ||
- selector: | ||
kind: RoleBinding | ||
group: rbac.authorization.k8s.io | ||
version: v1 | ||
fieldPath: subjects/[kind=ServiceAccount]/name | ||
- selector: | ||
kind: ClusterRoleBinding | ||
group: rbac.authorization.k8s.io | ||
version: v1 | ||
fieldPath: subjects/[kind=ServiceAccount]/name | ||
- selector: | ||
kind: Deployment | ||
group: apps | ||
version: v1 | ||
name: controller-manager | ||
fieldPath: spec/template/spec/serviceAccountName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,5 @@ spec: | |
requests: | ||
cpu: 100m | ||
memory: 20Mi | ||
serviceAccountName: controller-manager | ||
terminationGracePeriodSeconds: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: controller-manager | ||
namespace: system |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
testdata/project-v3-config/config/default/service_account_transformer.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This transformer replaces the service account name in config/rbac/service_account.yaml with | ||
# the result of applying the config/default name prefix to that name. | ||
apiVersion: builtin | ||
kind: ValueAddTransformer | ||
metadata: | ||
name: serviceAccountSubstitute | ||
|
||
# If you change this value, you must also update metadata.name in config/rbac/service_account.yaml. | ||
# Make sure the new value matches the result of applying any name prefix to the ServiceAccount's name. | ||
value: project-v3-config-controller-manager | ||
|
||
# Update all RoleBindings and ClusterRoleBindings that have a ServiceAccount subject, | ||
# and the manager's Deployment. | ||
targets: | ||
- selector: | ||
kind: RoleBinding | ||
group: rbac.authorization.k8s.io | ||
version: v1 | ||
fieldPath: subjects/[kind=ServiceAccount]/name | ||
- selector: | ||
kind: ClusterRoleBinding | ||
group: rbac.authorization.k8s.io | ||
version: v1 | ||
fieldPath: subjects/[kind=ServiceAccount]/name | ||
- selector: | ||
kind: Deployment | ||
group: apps | ||
version: v1 | ||
name: controller-manager | ||
fieldPath: spec/template/spec/serviceAccountName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,4 +50,5 @@ spec: | |
requests: | ||
cpu: 100m | ||
memory: 20Mi | ||
serviceAccountName: controller-manager | ||
terminationGracePeriodSeconds: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: controller-manager | ||
namespace: system |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.