/
apiserver.go
607 lines (530 loc) · 21.9 KB
/
apiserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package apiserver
import (
"context"
"errors"
"fmt"
"net"
"slices"
"time"
"github.com/Masterminds/semver/v3"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/component"
"github.com/gardener/gardener/pkg/component/apiserver"
"github.com/gardener/gardener/pkg/utils"
kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/gardener/gardener/pkg/utils/kubernetes/health"
"github.com/gardener/gardener/pkg/utils/managedresources"
"github.com/gardener/gardener/pkg/utils/retry"
secretsmanager "github.com/gardener/gardener/pkg/utils/secrets/manager"
)
const (
// SecretNameUserKubeconfig is the name for the user kubeconfig.
SecretNameUserKubeconfig = "user-kubeconfig"
// ServicePortName is the name of the port in the service.
ServicePortName = "kube-apiserver"
// UserNameVPNSeedClient is the user name for the HA vpn-seed-client components (used as common name in its client certificate)
UserNameVPNSeedClient = "vpn-seed-client"
userName = "system:kube-apiserver:kubelet"
)
// Interface contains functions for a kube-apiserver deployer.
type Interface interface {
apiserver.Interface
component.MonitoringComponent
// GetValues returns the current configuration values of the deployer.
GetValues() Values
// SetExternalHostname sets the ExternalHostname field in the Values of the deployer.
SetExternalHostname(string)
// SetExternalServer sets the ExternalServer field in the Values of the deployer.
SetExternalServer(string)
// SetNodeNetworkCIDR sets the nodesCIDR of the shoot network.
SetNodeNetworkCIDR(*string)
// SetServerCertificateConfig sets the ServerCertificateConfig field in the Values of the deployer.
SetServerCertificateConfig(ServerCertificateConfig)
// SetServiceAccountConfig sets the ServiceAccount field in the Values of the deployer.
SetServiceAccountConfig(ServiceAccountConfig)
// SetSNIConfig sets the SNI field in the Values of the deployer.
SetSNIConfig(SNIConfig)
}
// Values contains configuration values for the kube-apiserver resources.
type Values struct {
apiserver.Values
// AnonymousAuthenticationEnabled states whether anonymous authentication is enabled.
AnonymousAuthenticationEnabled bool
// APIAudiences are identifiers of the API. The service account token authenticator will validate that tokens used
// against the API are bound to at least one of these audiences.
APIAudiences []string
// AuthenticationWebhook contains configuration for the authentication webhook.
AuthenticationWebhook *AuthenticationWebhook
// AuthorizationWebhook contains configuration for the authorization webhook.
AuthorizationWebhook *AuthorizationWebhook
// DefaultNotReadyTolerationSeconds indicates the tolerationSeconds of the toleration for notReady:NoExecute
// that is added by default to every pod that does not already have such a toleration (flag `--default-not-ready-toleration-seconds`).
DefaultNotReadyTolerationSeconds *int64
// DefaultUnreachableTolerationSeconds indicates the tolerationSeconds of the toleration for unreachable:NoExecute
// that is added by default to every pod that does not already have such a toleration (flag `--default-unreachable-toleration-seconds`).
DefaultUnreachableTolerationSeconds *int64
// EventTTL is the amount of time to retain events.
EventTTL *metav1.Duration
// ExternalHostname is the external hostname which should be exposed by the kube-apiserver.
ExternalHostname string
// ExternalServer is the external server which should be used when generating the user kubeconfig.
ExternalServer string
// Images is a set of container images used for the containers of the kube-apiserver pods.
Images Images
// IsWorkerless specifies whether the cluster managed by this API server has worker nodes.
IsWorkerless bool
// NamePrefix is the prefix for the resource names.
NamePrefix string
// OIDC contains information for configuring OIDC settings for the kube-apiserver.
OIDC *gardencorev1beta1.OIDCConfig
// PriorityClassName is the name of the priority class.
PriorityClassName string
// ResourcesToStoreInETCDEvents is a list of resources which should be stored in the etcd-events instead of the
// etcd-main. The `events` resource in the `core` group is always stored in etcd-events.
ResourcesToStoreInETCDEvents []schema.GroupResource
// RuntimeConfig is the set of runtime configurations.
RuntimeConfig map[string]bool
// ServerCertificate contains configuration for the server certificate.
ServerCertificate ServerCertificateConfig
// ServiceAccount contains information for configuring ServiceAccount settings for the kube-apiserver.
ServiceAccount ServiceAccountConfig
// ServiceNetworkCIDR is the CIDR of the service network.
ServiceNetworkCIDR string
// SNI contains information for configuring SNI settings for the kube-apiserver.
SNI SNIConfig
// StaticTokenKubeconfigEnabled indicates whether static token kubeconfig secret will be created for shoot.
StaticTokenKubeconfigEnabled *bool
// Version is the Kubernetes version for the kube-apiserver.
Version *semver.Version
// VPN contains information for configuring the VPN settings for the kube-apiserver.
VPN VPNConfig
}
// AuthenticationWebhook contains configuration for the authentication webhook.
type AuthenticationWebhook struct {
// Kubeconfig contains the webhook configuration for token authentication in kubeconfig format. The API server will
// query the remote service to determine authentication for bearer tokens.
Kubeconfig []byte
// CacheTTL is the duration to cache responses from the webhook token authenticator.
CacheTTL *time.Duration
// Version is the API version of the authentication.k8s.io TokenReview to send to and expect from the webhook.
Version *string
}
// AuthorizationWebhook contains configuration for the authorization webhook.
type AuthorizationWebhook struct {
// Kubeconfig contains the webhook configuration in kubeconfig format. The API server will query the remote service
// to determine access on the API server's secure port.
Kubeconfig []byte
// CacheAuthorizedTTL is the duration to cache 'authorized' responses from the webhook authorizer.
CacheAuthorizedTTL *time.Duration
// CacheUnauthorizedTTL is the duration to cache 'unauthorized' responses from the webhook authorizer.
CacheUnauthorizedTTL *time.Duration
// Version is the API version of the authorization.k8s.io SubjectAccessReview to send to and expect from the
// webhook.
Version *string
}
// Images is a set of container images used for the containers of the kube-apiserver pods.
type Images struct {
// KubeAPIServer is the container image for the kube-apiserver.
KubeAPIServer string
// VPNClient is the container image for the vpn-seed-client.
VPNClient string
}
// VPNConfig contains information for configuring the VPN settings for the kube-apiserver.
type VPNConfig struct {
// Enabled states whether VPN is enabled.
Enabled bool
// PodNetworkCIDR is the CIDR of the pod network.
PodNetworkCIDR string
// NodeNetworkCIDR is the CIDR of the node network.
NodeNetworkCIDR *string
// HighAvailabilityEnabled states if VPN uses HA configuration.
HighAvailabilityEnabled bool
// HighAvailabilityNumberOfSeedServers is the number of VPN seed servers used for HA
HighAvailabilityNumberOfSeedServers int
// HighAvailabilityNumberOfShootClients is the number of VPN shoot clients used for HA
HighAvailabilityNumberOfShootClients int
}
// ServerCertificateConfig contains configuration for the server certificate.
type ServerCertificateConfig struct {
// ExtraIPAddresses is a list of additional IP addresses to use for the SANS of the server certificate.
ExtraIPAddresses []net.IP
// ExtraDNSNames is a list of additional DNS names to use for the SANS of the server certificate.
ExtraDNSNames []string
}
// ServiceAccountConfig contains information for configuring ServiceAccountConfig settings for the kube-apiserver.
type ServiceAccountConfig struct {
// Issuer is the issuer of service accounts.
Issuer string
// AcceptedIssuers is an additional set of issuers that are used to determine which service account tokens are accepted.
AcceptedIssuers []string
// JWKSURI is used to overwrite the URI for the JSON Web Key Set in the discovery document served at /.well-known/openid-configuration.
JWKSURI *string
// ExtendTokenExpiration states whether the service account token expirations should be extended.
ExtendTokenExpiration *bool
// MaxTokenExpiration states what the maximal token expiration should be.
MaxTokenExpiration *metav1.Duration
// RotationPhase specifies the credentials rotation phase of the service account signing key.
RotationPhase gardencorev1beta1.CredentialsRotationPhase
}
// SNIConfig contains information for configuring SNI settings for the kube-apiserver.
type SNIConfig struct {
// Enabled states whether the SNI feature is enabled.
Enabled bool
// AdvertiseAddress is the address which should be advertised by the kube-apiserver.
AdvertiseAddress string
// TLS contains information for configuring the TLS SNI settings for the kube-apiserver.
TLS []TLSSNIConfig
}
// TLSSNIConfig contains information for configuring the TLS SNI settings for the kube-apiserver.
type TLSSNIConfig struct {
// SecretName is the name for an existing secret containing the TLS certificate and private key. Either this or both
// Certificate and PrivateKey must be specified. If both is provided, SecretName is taking precedence.
SecretName *string
// Certificate is the TLS certificate. Either both this and PrivateKey, or SecretName must be specified. If both is
// provided, SecretName is taking precedence.
Certificate []byte
// PrivateKey is the TLS certificate. Either both this and Certificate, or SecretName must be specified. If both is
// provided, SecretName is taking precedence.
PrivateKey []byte
// DomainPatterns is an optional list of domain patterns which are fully qualified domain names, possibly with
// prefixed wildcard segments. The domain patterns also allow IP addresses, but IPs should only be used if the
// apiserver has visibility to the IP address requested by a client. If no domain patterns are provided, the names
// of the certificate are extracted. Non-wildcard matches trump over wildcard matches, explicit domain patterns
// trump over extracted names.
DomainPatterns []string
}
// New creates a new instance of DeployWaiter for the kube-apiserver.
func New(client kubernetes.Interface, namespace string, secretsManager secretsmanager.Interface, values Values) Interface {
return &kubeAPIServer{
client: client,
namespace: namespace,
secretsManager: secretsManager,
values: values,
}
}
type kubeAPIServer struct {
client kubernetes.Interface
namespace string
secretsManager secretsmanager.Interface
values Values
}
func (k *kubeAPIServer) Deploy(ctx context.Context) error {
var (
deployment = k.emptyDeployment()
podDisruptionBudget = k.emptyPodDisruptionBudget()
horizontalPodAutoscaler = k.emptyHorizontalPodAutoscaler()
verticalPodAutoscaler = k.emptyVerticalPodAutoscaler()
hvpa = k.emptyHVPA()
secretETCDEncryptionConfiguration = k.emptySecret(v1beta1constants.SecretNamePrefixETCDEncryptionConfiguration)
secretOIDCCABundle = k.emptySecret(secretOIDCCABundleNamePrefix)
secretAuditWebhookKubeconfig = k.emptySecret(secretAuditWebhookKubeconfigNamePrefix)
secretAuthenticationWebhookKubeconfig = k.emptySecret(secretAuthenticationWebhookKubeconfigNamePrefix)
secretAuthorizationWebhookKubeconfig = k.emptySecret(secretAuthorizationWebhookKubeconfigNamePrefix)
configMapAdmissionConfigs = k.emptyConfigMap(configMapAdmissionNamePrefix)
secretAdmissionKubeconfigs = k.emptySecret(secretAdmissionKubeconfigsNamePrefix)
configMapAuditPolicy = k.emptyConfigMap(configMapAuditPolicyNamePrefix)
configMapEgressSelector = k.emptyConfigMap(configMapEgressSelectorNamePrefix)
)
if err := k.reconcilePodDisruptionBudget(ctx, podDisruptionBudget); err != nil {
return err
}
if err := k.reconcileHorizontalPodAutoscaler(ctx, horizontalPodAutoscaler, deployment); err != nil {
return err
}
if err := k.reconcileVerticalPodAutoscaler(ctx, verticalPodAutoscaler, deployment); err != nil {
return err
}
if err := k.reconcileHVPA(ctx, hvpa, deployment); err != nil {
return err
}
if err := k.reconcileSecretETCDEncryptionConfiguration(ctx, secretETCDEncryptionConfiguration); err != nil {
return err
}
if err := k.reconcileSecretOIDCCABundle(ctx, secretOIDCCABundle); err != nil {
return err
}
if err := k.reconcileSecretAuthenticationWebhookKubeconfig(ctx, secretAuthenticationWebhookKubeconfig); err != nil {
return err
}
if err := k.reconcileSecretAuthorizationWebhookKubeconfig(ctx, secretAuthorizationWebhookKubeconfig); err != nil {
return err
}
secretServiceAccountKey, err := k.reconcileSecretServiceAccountKey(ctx)
if err != nil {
return err
}
secretHTTPProxy, err := k.reconcileSecretHTTPProxy(ctx)
if err != nil {
return err
}
secretKubeAggregator, err := k.reconcileSecretKubeAggregator(ctx)
if err != nil {
return err
}
secretKubeletClient, err := k.reconcileSecretKubeletClient(ctx)
if err != nil {
return err
}
secretServer, err := k.reconcileSecretServer(ctx)
if err != nil {
return err
}
secretStaticToken, err := k.reconcileSecretStaticToken(ctx)
if err != nil {
return err
}
if err := apiserver.ReconcileConfigMapAdmission(ctx, k.client.Client(), configMapAdmissionConfigs, k.values.Values); err != nil {
return err
}
if err := apiserver.ReconcileSecretAdmissionKubeconfigs(ctx, k.client.Client(), secretAdmissionKubeconfigs, k.values.Values); err != nil {
return err
}
if err := apiserver.ReconcileConfigMapAuditPolicy(ctx, k.client.Client(), configMapAuditPolicy, k.values.Audit); err != nil {
return err
}
if err := apiserver.ReconcileSecretAuditWebhookKubeconfig(ctx, k.client.Client(), secretAuditWebhookKubeconfig, k.values.Audit); err != nil {
return err
}
if err := k.reconcileConfigMapEgressSelector(ctx, configMapEgressSelector); err != nil {
return err
}
secretHAVPNSeedClient, err := k.reconcileSecretHAVPNSeedClient(ctx)
if err != nil {
return err
}
secretHAVPNClientSeedTLSAuth, err := k.reconcileSecretHAVPNSeedClientTLSAuth(ctx)
if err != nil {
return err
}
tlsSNISecrets, err := k.reconcileTLSSNISecrets(ctx)
if err != nil {
return err
}
var serviceAccount *corev1.ServiceAccount
if k.values.VPN.Enabled && k.values.VPN.HighAvailabilityEnabled {
serviceAccount = k.emptyServiceAccount()
if err := k.reconcileServiceAccount(ctx, serviceAccount); err != nil {
return err
}
if err := k.reconcileRoleHAVPN(ctx); err != nil {
return err
}
if err := k.reconcileRoleBindingHAVPN(ctx, serviceAccount); err != nil {
return err
}
} else {
if err := kubernetesutils.DeleteObjects(ctx, k.client.Client(),
k.emptyServiceAccount(),
k.emptyRoleHAVPN(),
k.emptyRoleBindingHAVPN(),
); err != nil {
return err
}
}
if err := k.reconcileDeployment(
ctx,
deployment,
serviceAccount,
configMapAuditPolicy,
configMapAdmissionConfigs,
secretAdmissionKubeconfigs,
configMapEgressSelector,
secretETCDEncryptionConfiguration,
secretOIDCCABundle,
secretServiceAccountKey,
secretStaticToken,
secretServer,
secretKubeletClient,
secretKubeAggregator,
secretHTTPProxy,
secretHAVPNSeedClient,
secretHAVPNClientSeedTLSAuth,
secretAuditWebhookKubeconfig,
secretAuthenticationWebhookKubeconfig,
secretAuthorizationWebhookKubeconfig,
tlsSNISecrets,
); err != nil {
return err
}
if ptr.Deref(k.values.StaticTokenKubeconfigEnabled, true) {
if err := k.reconcileSecretUserKubeconfig(ctx, secretStaticToken); err != nil {
return err
}
}
// apiserver deployed for garden cluster
if k.values.NamePrefix != "" {
if err := k.reconcileServiceMonitor(ctx, k.emptyServiceMonitor()); err != nil {
return err
}
}
if !k.values.IsWorkerless {
data, err := k.computeShootResourcesData()
if err != nil {
return err
}
return managedresources.CreateForShoot(ctx, k.client.Client(), k.namespace, ManagedResourceName, managedresources.LabelValueGardener, false, data)
}
return nil
}
func (k *kubeAPIServer) Destroy(ctx context.Context) error {
return kubernetesutils.DeleteObjects(ctx, k.client.Client(),
k.emptyManagedResource(),
k.emptyManagedResourceSecret(),
k.emptyHorizontalPodAutoscaler(),
k.emptyVerticalPodAutoscaler(),
k.emptyHVPA(),
k.emptyPodDisruptionBudget(),
k.emptyDeployment(),
k.emptyServiceAccount(),
k.emptyRoleHAVPN(),
k.emptyRoleBindingHAVPN(),
k.emptyServiceMonitor(),
)
}
var (
// IntervalWaitForDeployment is the interval used while waiting for the Deployments to become healthy
// or deleted.
IntervalWaitForDeployment = 5 * time.Second
// TimeoutWaitForDeployment is the timeout used while waiting for the Deployments to become healthy
// or deleted.
TimeoutWaitForDeployment = 5 * time.Minute
// Until is an alias for retry.Until. Exposed for tests.
Until = retry.Until
)
func (k *kubeAPIServer) Wait(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForDeployment)
defer cancel()
deployment := k.emptyDeployment()
if err := Until(timeoutCtx, IntervalWaitForDeployment, health.IsDeploymentUpdated(k.client.APIReader(), deployment)); err != nil {
var (
retryError *retry.Error
headBytes *int64
tailLines = ptr.To[int64](10)
)
if !errors.As(err, &retryError) {
return err
}
newestPod, err2 := kubernetesutils.NewestPodForDeployment(ctx, k.client.APIReader(), deployment)
if err2 != nil {
return fmt.Errorf("failure to find the newest pod for deployment to read the logs: %s: %w", err2.Error(), err)
}
if newestPod == nil {
return err
}
logs, err2 := kubernetesutils.MostRecentCompleteLogs(ctx, k.client.Kubernetes().CoreV1().Pods(newestPod.Namespace), newestPod, ContainerNameKubeAPIServer, tailLines, headBytes)
if err2 != nil {
return fmt.Errorf("failure to read the logs: %s: %w", err2.Error(), err)
}
return fmt.Errorf("%s, logs of newest pod:\n%s", err.Error(), logs)
}
return nil
}
func (k *kubeAPIServer) WaitCleanup(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForDeployment)
defer cancel()
return retry.Until(timeoutCtx, IntervalWaitForDeployment, func(ctx context.Context) (done bool, err error) {
deploy := k.emptyDeployment()
err = k.client.Client().Get(ctx, client.ObjectKeyFromObject(deploy), deploy)
switch {
case apierrors.IsNotFound(err):
return retry.Ok()
case err == nil:
return retry.MinorError(err)
default:
return retry.SevereError(err)
}
})
}
func (k *kubeAPIServer) GetValues() Values {
return k.values
}
func (k *kubeAPIServer) SetAutoscalingAPIServerResources(resources corev1.ResourceRequirements) {
k.values.Autoscaling.APIServerResources = resources
}
func (k *kubeAPIServer) GetAutoscalingReplicas() *int32 {
return k.values.Autoscaling.Replicas
}
func (k *kubeAPIServer) SetAutoscalingReplicas(replicas *int32) {
k.values.Autoscaling.Replicas = replicas
}
func (k *kubeAPIServer) SetETCDEncryptionConfig(config apiserver.ETCDEncryptionConfig) {
k.values.ETCDEncryption = config
}
func (k *kubeAPIServer) SetExternalHostname(hostname string) {
k.values.ExternalHostname = hostname
}
func (k *kubeAPIServer) SetExternalServer(server string) {
k.values.ExternalServer = server
}
func (k *kubeAPIServer) SetNodeNetworkCIDR(nodes *string) {
k.values.VPN.NodeNetworkCIDR = nodes
}
func (k *kubeAPIServer) SetServerCertificateConfig(config ServerCertificateConfig) {
k.values.ServerCertificate = config
}
func (k *kubeAPIServer) SetServiceAccountConfig(config ServiceAccountConfig) {
k.values.ServiceAccount = config
}
func (k *kubeAPIServer) SetSNIConfig(config SNIConfig) {
k.values.SNI = config
}
// GetLabels returns the labels for the kube-apiserver.
func GetLabels() map[string]string {
return utils.MergeStringMaps(getLabels(), map[string]string{
v1beta1constants.GardenRole: v1beta1constants.GardenRoleControlPlane,
})
}
func getLabels() map[string]string {
return map[string]string{
v1beta1constants.LabelApp: v1beta1constants.LabelKubernetes,
v1beta1constants.LabelRole: v1beta1constants.LabelAPIServer,
}
}
// ComputeKubeAPIServerServiceAccountConfig computes the [ServiceAccountConfig]
// needed to configure a kube-apiserver.
func ComputeKubeAPIServerServiceAccountConfig(
config *gardencorev1beta1.ServiceAccountConfig,
externalHostname string,
serviceAccountKeyRotationPhase gardencorev1beta1.CredentialsRotationPhase,
) ServiceAccountConfig {
var (
defaultIssuer = "https://" + externalHostname
out = ServiceAccountConfig{
Issuer: defaultIssuer,
RotationPhase: serviceAccountKeyRotationPhase,
}
)
if config == nil {
return out
}
out.ExtendTokenExpiration = config.ExtendTokenExpiration
out.MaxTokenExpiration = config.MaxTokenExpiration
if config.Issuer != nil {
out.Issuer = *config.Issuer
}
out.AcceptedIssuers = config.AcceptedIssuers
if out.Issuer != defaultIssuer && !slices.Contains(out.AcceptedIssuers, defaultIssuer) {
out.AcceptedIssuers = append(out.AcceptedIssuers, defaultIssuer)
}
if config.Issuer == nil {
// ensure defaultIssuer is not duplicated in the accepted issuers
for i, val := range out.AcceptedIssuers {
if val == defaultIssuer {
out.AcceptedIssuers = append(out.AcceptedIssuers[:i], out.AcceptedIssuers[i+1:]...)
break
}
}
}
return out
}