Skip to content

Commit c748ebd

Browse files
committed
This commit adds a ServiceAccount (config/rbac/service_account.yaml)
and changes the default service account name from default to controller-manager such that a user can specify which service account their manager should be created in. They may change this value by updating files referencing the metadata.name value in service_account.yaml. pkg/plugins/golang/v3: update all scaffold referencing the default service account with references to the "controller-manager" service account, specified in service_account.yaml. Signed-off-by: Eric Stroczynski <[email protected]>
1 parent b6e2ead commit c748ebd

34 files changed

+139
-23
lines changed

pkg/plugins/golang/v3/scaffolds/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (s *initScaffolder) scaffold() error {
105105
&rbac.RoleBinding{},
106106
&rbac.LeaderElectionRole{},
107107
&rbac.LeaderElectionRoleBinding{},
108+
&rbac.ServiceAccount{},
108109
&manager.Kustomization{},
109110
&manager.Config{Image: imageName},
110111
&manager.ControllerManagerConfig{},

pkg/plugins/golang/v3/scaffolds/internal/templates/config/manager/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,6 @@ spec:
100100
requests:
101101
cpu: 100m
102102
memory: 20Mi
103+
serviceAccountName: controller-manager
103104
terminationGracePeriodSeconds: 10
104105
`

pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_role_binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ roleRef:
5050
name: proxy-role
5151
subjects:
5252
- kind: ServiceAccount
53-
name: default
53+
name: controller-manager
5454
namespace: system
5555
`

pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/kustomization.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func (f *Kustomization) SetTemplateDefaults() error {
4343
}
4444

4545
const kustomizeRBACTemplate = `resources:
46+
# All RBAC will be applied under this service account in
47+
# the deployment namespace. You may comment out this resource
48+
# if your manager will use a service account that exists at
49+
# runtime. Be sure to update RoleBinding and ClusterRoleBinding
50+
# subjects if changing service account names.
51+
- service_account.yaml
4652
- role.yaml
4753
- role_binding.yaml
4854
- leader_election_role.yaml

pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/leader_election_role_binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ roleRef:
5050
name: leader-election-role
5151
subjects:
5252
- kind: ServiceAccount
53-
name: default
53+
name: controller-manager
5454
namespace: system
5555
`

pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/role_binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ roleRef:
5050
name: manager-role
5151
subjects:
5252
- kind: ServiceAccount
53-
name: default
53+
name: controller-manager
5454
namespace: system
5555
`
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package rbac
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/v3/pkg/model/file"
23+
)
24+
25+
var _ file.Template = &ServiceAccount{}
26+
27+
// ServiceAccount scaffolds a file that defines the service account the manager is deployed in.
28+
type ServiceAccount struct {
29+
file.TemplateMixin
30+
}
31+
32+
// SetTemplateDefaults implements file.Template
33+
func (f *ServiceAccount) SetTemplateDefaults() error {
34+
if f.Path == "" {
35+
f.Path = filepath.Join("config", "rbac", "service_account.yaml")
36+
}
37+
38+
f.TemplateBody = serviceAccountTemplate
39+
40+
return nil
41+
}
42+
43+
const serviceAccountTemplate = `apiVersion: v1
44+
kind: ServiceAccount
45+
metadata:
46+
name: controller-manager
47+
namespace: system
48+
`

test/e2e/utils/kubectl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import (
2727
// Kubectl contains context to run kubectl commands
2828
type Kubectl struct {
2929
*CmdContext
30-
Namespace string
30+
Namespace string
31+
ServiceAccount string
3132
}
3233

3334
// Command is a general func to run kubectl commands

test/e2e/utils/test_context.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ func NewTestContext(binaryName string, env ...string) (*TestContext, error) {
5656

5757
// Use kubectl to get Kubernetes client and cluster version.
5858
kubectl := &Kubectl{
59-
Namespace: fmt.Sprintf("e2e-%s-system", testSuffix),
60-
CmdContext: cc,
59+
Namespace: fmt.Sprintf("e2e-%s-system", testSuffix),
60+
ServiceAccount: fmt.Sprintf("e2e-%s-controller-manager", testSuffix),
61+
CmdContext: cc,
6162
}
6263
k8sVersion, err := kubectl.Version()
6364
if err != nil {

test/e2e/v3/plugin_cluster_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ var _ = Describe("kubebuilder", func() {
6969
})
7070

7171
It("should generate a runnable project", func() {
72+
// go/v3 uses a unqiue-per-project service account name,
73+
// while go/v2 still uses "default".
74+
tmp := kbc.Kubectl.ServiceAccount
75+
kbc.Kubectl.ServiceAccount = "default"
76+
defer func() { kbc.Kubectl.ServiceAccount = tmp }()
7277
GenerateV2(kbc)
7378
Run(kbc)
7479
})
@@ -166,7 +171,7 @@ func Run(kbc *utils.TestContext) {
166171
_, err = kbc.Kubectl.Command(
167172
"create", "clusterrolebinding", fmt.Sprintf("metrics-%s", kbc.TestSuffix),
168173
fmt.Sprintf("--clusterrole=e2e-%s-metrics-reader", kbc.TestSuffix),
169-
fmt.Sprintf("--serviceaccount=%s:default", kbc.Kubectl.Namespace))
174+
fmt.Sprintf("--serviceaccount=%s:%s", kbc.Kubectl.Namespace, kbc.Kubectl.ServiceAccount))
170175
ExpectWithOffset(1, err).NotTo(HaveOccurred())
171176

172177
_ = curlMetrics(kbc)
@@ -263,18 +268,23 @@ func Run(kbc *utils.TestContext) {
263268
// curlMetrics curl's the /metrics endpoint, returning all logs once a 200 status is returned.
264269
func curlMetrics(kbc *utils.TestContext) string {
265270
By("reading the metrics token")
266-
b64Token, err := kbc.Kubectl.Get(true, "secrets", "-o=jsonpath={.items[0].data.token}")
271+
// Filter token query by service account in case more than one exists in a namespace.
272+
query := fmt.Sprintf(`{.items[?(@.metadata.annotations.kubernetes\.io/service-account\.name=="%s")].data.token}`,
273+
kbc.Kubectl.ServiceAccount,
274+
)
275+
b64Token, err := kbc.Kubectl.Get(true, "secrets", "-o=jsonpath="+query)
267276
ExpectWithOffset(2, err).NotTo(HaveOccurred())
268277
token, err := base64.StdEncoding.DecodeString(strings.TrimSpace(b64Token))
269278
ExpectWithOffset(2, err).NotTo(HaveOccurred())
270279
ExpectWithOffset(2, len(token)).To(BeNumerically(">", 0))
271280

272281
By("creating a curl pod")
273282
cmdOpts := []string{
274-
"run", "--generator=run-pod/v1", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", "--",
283+
"run", "--generator=run-pod/v1", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure",
284+
"--serviceaccount=" + kbc.Kubectl.ServiceAccount, "--",
275285
"curl", "-v", "-k", "-H", fmt.Sprintf(`Authorization: Bearer %s`, token),
276-
fmt.Sprintf("https://e2e-%v-controller-manager-metrics-service.e2e-%v-system.svc:8443/metrics",
277-
kbc.TestSuffix, kbc.TestSuffix),
286+
fmt.Sprintf("https://e2e-%s-controller-manager-metrics-service.%s.svc:8443/metrics",
287+
kbc.TestSuffix, kbc.Kubectl.Namespace),
278288
}
279289
_, err = kbc.Kubectl.CommandInNamespace(cmdOpts...)
280290
ExpectWithOffset(2, err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)