Skip to content

Commit cecebf3

Browse files
authored
feat: allow to custom service account name (#6393)
* impl custom envoy sa name Signed-off-by: zirain <zirain2009@gmail.com> * address comment Signed-off-by: zirain <zirain2009@gmail.com> * lint Signed-off-by: zirain <zirain2009@gmail.com> * create sa with custom name Signed-off-by: zirain <zirain2009@gmail.com> * fix lint and test Signed-off-by: zirain <zirain2009@gmail.com> * fix test Signed-off-by: zirain <zirain2009@gmail.com> * fix Signed-off-by: zirain <zirain2009@gmail.com> * fix gen Signed-off-by: zirain <zirain2009@gmail.com> --------- Signed-off-by: zirain <zirain2009@gmail.com>
1 parent 393e2b4 commit cecebf3

32 files changed

+550
-21
lines changed

charts/gateway-helm/templates/_rbac.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ verbs:
203203
verbs:
204204
- create
205205
- get
206+
- list
206207
- delete
207208
- deletecollection
208209
- patch

internal/infrastructure/kubernetes/infra.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import (
2323
"github.com/envoyproxy/gateway/internal/logging"
2424
)
2525

26-
var _ ResourceRender = &proxy.ResourceRender{}
27-
28-
var _ ResourceRender = &ratelimit.ResourceRender{}
26+
var (
27+
_ ResourceRender = &proxy.ResourceRender{}
28+
_ ResourceRender = &ratelimit.ResourceRender{}
29+
)
2930

3031
// ResourceRender renders Kubernetes infrastructure resources
3132
// based on Infra IR resources.

internal/infrastructure/kubernetes/infra_resource.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,34 @@ func (i *Infra) createOrUpdateServiceAccount(ctx context.Context, r ResourceRend
3838
}
3939
)
4040

41-
if sa, err = r.ServiceAccount(); err != nil {
42-
resourceApplyTotal.WithFailure(metrics.ReasonError, labels...).Increment()
43-
return err
44-
}
45-
4641
defer func() {
4742
if err == nil {
4843
resourceApplyDurationSeconds.With(labels...).Record(time.Since(startTime).Seconds())
4944
resourceApplyTotal.WithSuccess(labels...).Increment()
5045
} else {
5146
resourceApplyTotal.WithFailure(metrics.ReasonError, labels...).Increment()
5247
}
48+
49+
if sa != nil {
50+
deleteErr := i.Client.DeleteAllExcept(ctx, &corev1.ServiceAccountList{}, client.ObjectKey{
51+
Namespace: sa.Namespace,
52+
Name: sa.Name,
53+
}, &client.ListOptions{
54+
Namespace: sa.Namespace,
55+
LabelSelector: r.LabelSelector(),
56+
})
57+
58+
if deleteErr != nil {
59+
i.logger.Error(deleteErr, "failed to delete all except serviceaccount", "name", sa.Name)
60+
}
61+
}
5362
}()
5463

64+
if sa, err = r.ServiceAccount(); err != nil {
65+
resourceApplyTotal.WithFailure(metrics.ReasonError, labels...).Increment()
66+
return err
67+
}
68+
5569
return i.Client.ServerSideApply(ctx, sa)
5670
}
5771

internal/infrastructure/kubernetes/proxy/resource_provider.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,34 @@ type KubernetesInfraProvider interface {
7878
GetResourceNamespace(ir *ir.Infra) string
7979
}
8080

81-
func NewResourceRender(ctx context.Context, kubernetesInfra KubernetesInfraProvider, infra *ir.Infra) (*ResourceRender, error) {
82-
ownerReference, err := kubernetesInfra.GetOwnerReferenceUID(ctx, infra)
81+
func NewResourceRender(ctx context.Context, kubeInfra KubernetesInfraProvider, infra *ir.Infra) (*ResourceRender, error) {
82+
ownerReference, err := kubeInfra.GetOwnerReferenceUID(ctx, infra)
8383
if err != nil {
8484
return nil, err
8585
}
8686

8787
return &ResourceRender{
88-
envoyNamespace: kubernetesInfra.GetResourceNamespace(infra),
89-
controllerNamespace: kubernetesInfra.GetControllerNamespace(),
90-
DNSDomain: kubernetesInfra.GetDNSDomain(),
88+
envoyNamespace: kubeInfra.GetResourceNamespace(infra),
89+
controllerNamespace: kubeInfra.GetControllerNamespace(),
90+
DNSDomain: kubeInfra.GetDNSDomain(),
9191
infra: infra.GetProxyInfra(),
92-
ShutdownManager: kubernetesInfra.GetEnvoyGateway().GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().ShutdownManager,
93-
GatewayNamespaceMode: kubernetesInfra.GetEnvoyGateway().GatewayNamespaceMode(),
92+
ShutdownManager: kubeInfra.GetEnvoyGateway().GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().ShutdownManager,
93+
GatewayNamespaceMode: kubeInfra.GetEnvoyGateway().GatewayNamespaceMode(),
9494
ownerReferenceUID: ownerReference,
9595
}, nil
9696
}
9797

98+
func (r *ResourceRender) serviceAccountName() string {
99+
prov := r.infra.GetProxyConfig().GetEnvoyProxyProvider().GetEnvoyProxyKubeProvider()
100+
if prov != nil &&
101+
prov.EnvoyServiceAccount != nil &&
102+
prov.EnvoyServiceAccount.Name != nil {
103+
return *prov.EnvoyServiceAccount.Name
104+
}
105+
106+
return r.Name()
107+
}
108+
98109
func (r *ResourceRender) Name() string {
99110
if r.GatewayNamespaceMode {
100111
return r.infra.Name
@@ -150,7 +161,7 @@ func (r *ResourceRender) ServiceAccount() (*corev1.ServiceAccount, error) {
150161
AutomountServiceAccountToken: ptr.To(false),
151162
ObjectMeta: metav1.ObjectMeta{
152163
Namespace: r.Namespace(),
153-
Name: r.Name(),
164+
Name: r.serviceAccountName(),
154165
Labels: saLabels,
155166
Annotations: r.infra.GetProxyMetadata().Annotations,
156167
OwnerReferences: r.OwnerReferences(),
@@ -393,7 +404,7 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) {
393404
AutomountServiceAccountToken: ptr.To(false),
394405
Containers: containers,
395406
InitContainers: deploymentConfig.InitContainers,
396-
ServiceAccountName: r.Name(),
407+
ServiceAccountName: r.serviceAccountName(),
397408
TerminationGracePeriodSeconds: expectedTerminationGracePeriodSeconds(proxyConfig.Spec.Shutdown),
398409
DNSPolicy: corev1.DNSClusterFirst,
399410
RestartPolicy: corev1.RestartPolicyAlways,

internal/infrastructure/kubernetes/proxy/resource_provider_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ func newTestInfraWithNamespacedName(gwNN types.NamespacedName) *ir.Infra {
106106
return i
107107
}
108108

109+
func newTestInfraWithCustomServiceAccount(gwNN types.NamespacedName) *ir.Infra {
110+
i := newTestInfraWithNamespacedName(gwNN)
111+
i.Proxy.Config = new(egv1a1.EnvoyProxy)
112+
i.Proxy.Config.Spec.Provider = egv1a1.DefaultEnvoyProxyProvider()
113+
i.Proxy.Config.Spec.Provider.Kubernetes = &egv1a1.EnvoyProxyKubernetesProvider{
114+
EnvoyServiceAccount: &egv1a1.KubernetesServiceAccountSpec{
115+
Name: ptr.To("custom-sa"),
116+
},
117+
}
118+
119+
return i
120+
}
121+
109122
func newTestInfraWithIPFamily(family *egv1a1.IPFamily) *ir.Infra {
110123
i := newTestInfra()
111124
i.Proxy.Config = &egv1a1.EnvoyProxy{
@@ -632,6 +645,12 @@ func TestDeployment(t *testing.T) {
632645
deploy: nil,
633646
gatewayNamespaceMode: true,
634647
},
648+
{
649+
caseName: "custom-sa",
650+
infra: newTestInfraWithCustomServiceAccount(types.NamespacedName{Namespace: "ns1", Name: "gateway-1"}),
651+
deploy: nil,
652+
gatewayNamespaceMode: true,
653+
},
635654
}
636655
for _, tc := range cases {
637656
t.Run(tc.caseName, func(t *testing.T) {
@@ -1442,6 +1461,11 @@ func TestServiceAccount(t *testing.T) {
14421461
infra: newTestInfraWithNamespacedName(types.NamespacedName{Namespace: "ns1", Name: "gateway-1"}),
14431462
gatewayNamespaceMode: true,
14441463
},
1464+
{
1465+
name: "custom-sa",
1466+
infra: newTestInfraWithCustomServiceAccount(types.NamespacedName{Namespace: "ns1", Name: "gateway-1"}),
1467+
gatewayNamespaceMode: false,
1468+
},
14451469
}
14461470

14471471
for _, tc := range cases {

0 commit comments

Comments
 (0)