-
Notifications
You must be signed in to change notification settings - Fork 22
/
service_account.go
56 lines (45 loc) · 1.87 KB
/
service_account.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
package oathkeeper
import (
"context"
_ "embed"
"fmt"
"github.com/kyma-project/api-gateway/apis/operator/v1alpha1"
"github.com/kyma-project/api-gateway/internal/reconciliations"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
//go:embed service_account.yaml
var serviceAccount []byte
const serviceAccountName = "ory-oathkeeper"
func reconcileOryOathkeeperServiceAccount(ctx context.Context, k8sClient client.Client, apiGatewayCR v1alpha1.APIGateway) error {
ctrl.Log.Info("Reconciling Ory Oathkeeper ServiceAccount", "name", serviceAccountName, "Namespace", reconciliations.Namespace)
if apiGatewayCR.IsInDeletion() {
return deleteServiceAccount(ctx, k8sClient, serviceAccountName, reconciliations.Namespace)
}
templateValues := make(map[string]string)
templateValues["Name"] = serviceAccountName
templateValues["Namespace"] = reconciliations.Namespace
return reconciliations.ApplyResource(ctx, k8sClient, serviceAccount, templateValues)
}
func deleteServiceAccount(ctx context.Context, k8sClient client.Client, name, namespace string) error {
ctrl.Log.Info("Deleting Oathkeeper ServiceAccount if it exists", "name", name, "Namespace", namespace)
s := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
err := k8sClient.Delete(ctx, &s)
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("failed to delete Oathkeeper ServiceAccount %s/%s: %v", namespace, name, err)
}
if k8serrors.IsNotFound(err) {
ctrl.Log.Info("Skipped deletion of Oathkeeper ServiceAccount as it wasn't present", "name", name, "Namespace", namespace)
} else {
ctrl.Log.Info("Successfully deleted Oathkeeper ServiceAccount", "name", name, "Namespace", namespace)
}
return nil
}