forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strategy_add.go
57 lines (46 loc) · 1.8 KB
/
strategy_add.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
package identitymapper
import (
"context"
userapi "github.com/openshift/api/user/v1"
userclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
kerrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
var _ = UserForNewIdentityGetter(&StrategyAdd{})
// StrategyAdd associates a new identity with a user with the identity's preferred username,
// adding to any existing identities associated with the user
type StrategyAdd struct {
user userclient.UserInterface
initializer Initializer
}
func NewStrategyAdd(user userclient.UserInterface, initializer Initializer) UserForNewIdentityGetter {
return &StrategyAdd{user, initializer}
}
func (s *StrategyAdd) UserForNewIdentity(ctx context.Context, preferredUserName string, identity *userapi.Identity) (*userapi.User, error) {
persistedUser, err := s.user.Get(preferredUserName, metav1.GetOptions{})
switch {
case kerrs.IsNotFound(err):
// CreateUser a new user
desiredUser := &userapi.User{}
desiredUser.Name = preferredUserName
desiredUser.Identities = []string{identity.Name}
s.initializer.InitializeUser(identity, desiredUser)
return s.user.Create(desiredUser)
case err == nil:
// If the existing user already references our identity, we're done
if sets.NewString(persistedUser.Identities...).Has(identity.Name) {
return persistedUser, nil
}
// Otherwise add our identity and update
persistedUser.Identities = append(persistedUser.Identities, identity.Name)
// If our newly added identity is the only one, initialize the user
if len(persistedUser.Identities) == 1 {
s.initializer.InitializeUser(identity, persistedUser)
}
return s.user.Update(persistedUser)
default:
// Fail on errors other than "not found"
return nil, err
}
}