-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_account.go
109 lines (100 loc) · 4.1 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
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
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package serviceaccount
import (
"context"
"fmt"
operatorv1alpha1 "github.com/projectsesame/sesame-operator/api/v1alpha1"
utilequality "github.com/projectsesame/sesame-operator/internal/equality"
objSesame "github.com/projectsesame/sesame-operator/internal/objects/sesame"
"github.com/projectsesame/sesame-operator/pkg/labels"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// EnsureServiceAccount ensures a ServiceAccount resource exists with the provided name
// and sesame namespace/name for the owning sesame labels.
func EnsureServiceAccount(ctx context.Context, cli client.Client, name string, Sesame *operatorv1alpha1.Sesame) (*corev1.ServiceAccount, error) {
desired := DesiredServiceAccount(name, Sesame)
current, err := CurrentServiceAccount(ctx, cli, Sesame.Spec.Namespace.Name, name)
if err != nil {
if errors.IsNotFound(err) {
updated, err := createServiceAccount(ctx, cli, desired)
if err != nil {
return nil, fmt.Errorf("failed to create service account %s/%s: %w", desired.Namespace, desired.Name, err)
}
return updated, nil
}
return nil, fmt.Errorf("failed to get service account %s/%s: %w", desired.Namespace, desired.Name, err)
}
updated, err := updateSvcAcctIfNeeded(ctx, cli, Sesame, current, desired)
if err != nil {
return nil, fmt.Errorf("failed to update service account %s/%s: %w", desired.Namespace, desired.Name, err)
}
return updated, nil
}
// DesiredServiceAccount generates the desired ServiceAccount resource for the
// given sesame.
func DesiredServiceAccount(name string, Sesame *operatorv1alpha1.Sesame) *corev1.ServiceAccount {
sa := &corev1.ServiceAccount{
TypeMeta: metav1.TypeMeta{
Kind: rbacv1.ServiceAccountKind,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: Sesame.Spec.Namespace.Name,
Name: name,
},
}
sa.Labels = map[string]string{
operatorv1alpha1.OwningSesameNameLabel: Sesame.Name,
operatorv1alpha1.OwningSesameNsLabel: Sesame.Namespace,
}
return sa
}
// CurrentServiceAccount returns the current ServiceAccount for the provided ns/name.
func CurrentServiceAccount(ctx context.Context, cli client.Client, ns, name string) (*corev1.ServiceAccount, error) {
current := &corev1.ServiceAccount{}
key := types.NamespacedName{
Namespace: ns,
Name: name,
}
err := cli.Get(ctx, key, current)
if err != nil {
return nil, err
}
return current, nil
}
// createServiceAccount creates a ServiceAccount resource for the provided sa.
func createServiceAccount(ctx context.Context, cli client.Client, sa *corev1.ServiceAccount) (*corev1.ServiceAccount, error) {
if err := cli.Create(ctx, sa); err != nil {
return nil, fmt.Errorf("failed to create service account %s/%s: %w", sa.Namespace, sa.Name, err)
}
return sa, nil
}
// updateSvcAcctIfNeeded updates a ServiceAccount resource if current does not match desired,
// using sesame to verify the existence of owner labels.
func updateSvcAcctIfNeeded(ctx context.Context, cli client.Client, Sesame *operatorv1alpha1.Sesame, current, desired *corev1.ServiceAccount) (*corev1.ServiceAccount, error) {
if labels.Exist(current, objSesame.OwnerLabels(Sesame)) {
sa, updated := utilequality.ServiceAccountConfigChanged(current, desired)
if updated {
if err := cli.Update(ctx, sa); err != nil {
return nil, fmt.Errorf("failed to update service account %s/%s: %w", sa.Namespace, sa.Name, err)
}
return sa, nil
}
}
return current, nil
}