/
systemaccount.go
61 lines (49 loc) · 1.71 KB
/
systemaccount.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
package nsc
import (
"context"
"fmt"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientsetv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"github.com/versori-oss/nats-account-operator/api/accounts/v1alpha1"
clientsetv1alpha1 "github.com/versori-oss/nats-account-operator/pkg/generated/clientset/versioned/typed/accounts/v1alpha1"
)
type SystemAccountLoader struct {
accounts clientsetv1alpha1.AccountsV1alpha1Interface
core clientsetv1.CoreV1Interface
}
func NewSystemAccountLoader(
accounts clientsetv1alpha1.AccountsV1alpha1Interface,
core clientsetv1.CoreV1Interface,
) *SystemAccountLoader {
return &SystemAccountLoader{
accounts: accounts,
core: core,
}
}
func (s *SystemAccountLoader) Load(ctx context.Context, operator *v1alpha1.Operator) (seed []byte, err error) {
// TODO: figure out how to handle errors here
if operator.Status.ResolvedSystemAccount == nil {
return nil, fmt.Errorf("operator %s/%s does not have a resolved system account", operator.Namespace, operator.Name)
}
account, err := s.accounts.Accounts(operator.Namespace).
Get(ctx, operator.Status.ResolvedSystemAccount.Name, v1.GetOptions{})
if err != nil {
return nil, err
}
if account.Status.KeyPair == nil {
return nil, fmt.Errorf("system account %s/%s does not have a keypair", account.Namespace, account.Name)
}
seedSecret, err := s.core.Secrets(account.Namespace).Get(ctx, account.Status.KeyPair.SeedSecretName, v1.GetOptions{})
if err != nil {
return nil, err
}
seedBytes, ok := seedSecret.Data[v1alpha1.NatsSecretSeedKey]
if !ok {
return nil, fmt.Errorf(
"secret %s/%s is invalid, missing field: %s",
seedSecret.Namespace,
seedSecret.Name,
v1alpha1.NatsSecretSeedKey)
}
return seedBytes, nil
}