Skip to content

Commit 8f8f2b4

Browse files
authored
Annotate KSA by default if GSA in use (#750)
* Update generated CRD * Annotate KSA by default if GSA in use
2 parents 0769fef + 755ca43 commit 8f8f2b4

File tree

7 files changed

+111
-13
lines changed

7 files changed

+111
-13
lines changed

config/crd/skiperator.kartverket.no_applications.yaml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ spec:
330330
a Container.
331331
properties:
332332
name:
333-
description: Name of the environment variable. Must be a C_IDENTIFIER.
333+
description: |-
334+
Name of the environment variable.
335+
May consist of any printable ASCII characters except '='.
334336
type: string
335337
value:
336338
description: |-
@@ -388,6 +390,43 @@ spec:
388390
- fieldPath
389391
type: object
390392
x-kubernetes-map-type: atomic
393+
fileKeyRef:
394+
description: |-
395+
FileKeyRef selects a key of the env file.
396+
Requires the EnvFiles feature gate to be enabled.
397+
properties:
398+
key:
399+
description: |-
400+
The key within the env file. An invalid key will prevent the pod from starting.
401+
The keys defined within a source may consist of any printable ASCII characters except '='.
402+
During Alpha stage of the EnvFiles feature gate, the key size is limited to 128 characters.
403+
type: string
404+
optional:
405+
default: false
406+
description: |-
407+
Specify whether the file or its key must be defined. If the file or key
408+
does not exist, then the env var is not published.
409+
If optional is set to true and the specified key does not exist,
410+
the environment variable will not be set in the Pod's containers.
411+
412+
If optional is set to false and the specified key does not exist,
413+
an error will be returned during Pod creation.
414+
type: boolean
415+
path:
416+
description: |-
417+
The path within the volume from which to select the file.
418+
Must be relative and may not contain the '..' path or start with '..'.
419+
type: string
420+
volumeName:
421+
description: The name of the volume mount containing
422+
the env file.
423+
type: string
424+
required:
425+
- key
426+
- path
427+
- volumeName
428+
type: object
429+
x-kubernetes-map-type: atomic
391430
resourceFieldRef:
392431
description: |-
393432
Selects a resource of the container: only resources limits and requests

config/crd/skiperator.kartverket.no_skipjobs.yaml

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,9 @@ spec:
304304
in a Container.
305305
properties:
306306
name:
307-
description: Name of the environment variable. Must be a
308-
C_IDENTIFIER.
307+
description: |-
308+
Name of the environment variable.
309+
May consist of any printable ASCII characters except '='.
309310
type: string
310311
value:
311312
description: |-
@@ -363,6 +364,43 @@ spec:
363364
- fieldPath
364365
type: object
365366
x-kubernetes-map-type: atomic
367+
fileKeyRef:
368+
description: |-
369+
FileKeyRef selects a key of the env file.
370+
Requires the EnvFiles feature gate to be enabled.
371+
properties:
372+
key:
373+
description: |-
374+
The key within the env file. An invalid key will prevent the pod from starting.
375+
The keys defined within a source may consist of any printable ASCII characters except '='.
376+
During Alpha stage of the EnvFiles feature gate, the key size is limited to 128 characters.
377+
type: string
378+
optional:
379+
default: false
380+
description: |-
381+
Specify whether the file or its key must be defined. If the file or key
382+
does not exist, then the env var is not published.
383+
If optional is set to true and the specified key does not exist,
384+
the environment variable will not be set in the Pod's containers.
385+
386+
If optional is set to false and the specified key does not exist,
387+
an error will be returned during Pod creation.
388+
type: boolean
389+
path:
390+
description: |-
391+
The path within the volume from which to select the file.
392+
Must be relative and may not contain the '..' path or start with '..'.
393+
type: string
394+
volumeName:
395+
description: The name of the volume mount containing
396+
the env file.
397+
type: string
398+
required:
399+
- key
400+
- path
401+
- volumeName
402+
type: object
403+
x-kubernetes-map-type: atomic
366404
resourceFieldRef:
367405
description: |-
368406
Selects a resource of the container: only resources limits and requests

pkg/resourcegenerator/serviceaccount/application.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ func generateForApplication(r reconciliation.Reconciliation) error {
2626
serviceAccount := corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Namespace: application.Namespace, Name: application.Name}}
2727

2828
if util.IsCloudSqlProxyEnabled(application.Spec.GCP) {
29-
setCloudSqlAnnotations(&serviceAccount, application)
29+
setGCPSAAnnotation(&serviceAccount, application.Spec.GCP.CloudSQLProxy.ServiceAccount)
30+
} else if util.GCPServiceAccountInUse(application.Spec.GCP) {
31+
setGCPSAAnnotation(&serviceAccount, application.Spec.GCP.Auth.ServiceAccount)
3032
}
33+
3134
r.AddResource(&serviceAccount)
3235
ctxLog.Debug("Finished generating service account for application", "application", application.Name)
3336
return nil

pkg/resourcegenerator/serviceaccount/service_account.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package serviceaccount
33
import (
44
"maps"
55

6-
skiperatorv1alpha1 "github.com/kartverket/skiperator/api/v1alpha1"
76
"github.com/kartverket/skiperator/pkg/reconciliation"
87
"github.com/kartverket/skiperator/pkg/resourcegenerator/resourceutils/generator"
98
corev1 "k8s.io/api/core/v1"
@@ -15,13 +14,13 @@ func Generate(r reconciliation.Reconciliation) error {
1514
return multiGenerator.Generate(r, "ServiceAccount")
1615
}
1716

18-
func setCloudSqlAnnotations(serviceAccount *corev1.ServiceAccount, gcp skiperatorv1alpha1.SKIPObject) {
17+
func setGCPSAAnnotation(serviceAccount *corev1.ServiceAccount, saEmail string) {
1918
annotations := serviceAccount.GetAnnotations()
2019
if len(annotations) == 0 {
2120
annotations = make(map[string]string)
2221
}
2322
maps.Copy(annotations, map[string]string{
24-
"iam.gke.io/gcp-service-account": gcp.GetCommonSpec().GCP.CloudSQLProxy.ServiceAccount,
23+
"iam.gke.io/gcp-service-account": saEmail,
2524
})
2625
serviceAccount.SetAnnotations(annotations)
2726
}

pkg/resourcegenerator/serviceaccount/skipjob.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ func generateForSKIPJob(r reconciliation.Reconciliation) error {
2626
serviceAccount := corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Namespace: skipJob.Namespace, Name: skipJob.KindPostFixedName()}}
2727

2828
if util.IsCloudSqlProxyEnabled(skipJob.Spec.Container.GCP) {
29-
setCloudSqlAnnotations(&serviceAccount, skipJob)
29+
setGCPSAAnnotation(&serviceAccount, skipJob.Spec.Container.GCP.CloudSQLProxy.ServiceAccount)
30+
} else if util.GCPServiceAccountInUse(skipJob.Spec.Container.GCP) {
31+
setGCPSAAnnotation(&serviceAccount, skipJob.Spec.Container.GCP.Auth.ServiceAccount)
3032
}
33+
3134
r.AddResource(&serviceAccount)
3235
ctxLog.Debug("Finished generating service account for skipjob", "skipjob", skipJob.Name)
3336
return nil

pkg/util/helperfunctions.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@ package util
33
import (
44
"context"
55
"fmt"
6+
"hash/fnv"
7+
"net/url"
8+
"regexp"
9+
"strings"
10+
"unicode"
11+
612
"github.com/kartverket/skiperator/api/v1alpha1/digdirator"
713
"github.com/kartverket/skiperator/api/v1alpha1/podtypes"
814
"github.com/mitchellh/hashstructure/v2"
915
nais_io_v1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
1016
"github.com/nais/liberator/pkg/namegen"
11-
"hash/fnv"
1217
corev1 "k8s.io/api/core/v1"
1318
"k8s.io/apimachinery/pkg/api/errors"
1419
"k8s.io/apimachinery/pkg/runtime"
1520
"k8s.io/apimachinery/pkg/types"
1621
"k8s.io/apimachinery/pkg/util/validation"
1722
"k8s.io/client-go/tools/record"
18-
"net/url"
19-
"regexp"
2023
"sigs.k8s.io/controller-runtime/pkg/client"
21-
"strings"
22-
"unicode"
2324
)
2425

2526
//TODO Clean up this file, move functions to more appropriate files
@@ -177,6 +178,14 @@ func EnsurePrefix(s string, prefix string) string {
177178
return s
178179
}
179180

181+
func GCPServiceAccountInUse(gcp *podtypes.GCP) bool {
182+
if gcp == nil || gcp.Auth == nil || gcp.Auth.ServiceAccount == "" {
183+
return false
184+
}
185+
186+
return true
187+
}
188+
180189
func IsCloudSqlProxyEnabled(gcp *podtypes.GCP) bool {
181190
return gcp != nil && gcp.CloudSQLProxy != nil
182191
}

tests/application/gcp/application-assert.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,10 @@ data:
5959
kind: ConfigMap
6060
metadata:
6161
name: gcp-gcp-auth
62+
---
63+
apiVersion: v1
64+
kind: ServiceAccount
65+
metadata:
66+
name: gcp
67+
annotations:
68+
iam.gke.io/gcp-service-account: something@verdier.com

0 commit comments

Comments
 (0)