Skip to content

Commit 755ca43

Browse files
committed
Annotate KSA by default if GSA in use
1 parent d872542 commit 755ca43

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

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)