forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
82 lines (70 loc) · 2.24 KB
/
info.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
// Copyright 2021 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package proxy
import (
"context"
"encoding/json"
"fmt"
"github.com/juju/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
core "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
serviceAccountSecretCADataKey = "ca.crt"
serviceAccountSecretTokenKey = "token"
)
func GetControllerProxy(
name,
apiHost string,
configI core.ConfigMapInterface,
saI core.ServiceAccountInterface,
secretI core.SecretInterface,
) (*Proxier, error) {
cm, err := configI.Get(context.TODO(), name, meta.GetOptions{})
if k8serrors.IsNotFound(err) {
return nil, errors.NotFoundf("controller proxy config %s", name)
} else if err != nil {
return nil, errors.Trace(err)
}
config := ControllerProxyConfig{}
if err := json.Unmarshal([]byte(cm.Data[ProxyConfigMapKey]), &config); err != nil {
return nil, errors.Trace(err)
}
sa, err := saI.Get(context.TODO(), config.Name, meta.GetOptions{})
if k8serrors.IsNotFound(err) {
return nil, errors.NotFoundf("controller proxy service account for %s", name)
} else if err != nil {
return nil, errors.Trace(err)
}
if secLen := len(sa.Secrets); secLen < 1 || secLen > 1 {
return nil, fmt.Errorf("unsupported number of service account secrets: %d", secLen)
}
sec, err := secretI.Get(context.TODO(), sa.Secrets[0].Name, meta.GetOptions{})
if k8serrors.IsNotFound(err) {
return nil, fmt.Errorf("could not get proxy service account secret: %s", sa.Secrets[0].Name)
} else if err != nil {
return nil, errors.Trace(err)
}
proxierConfig := ProxierConfig{
APIHost: apiHost,
CAData: string(sec.Data[serviceAccountSecretCADataKey]),
Namespace: config.Namespace,
RemotePort: config.RemotePort,
Service: config.TargetService,
ServiceAccountToken: string(sec.Data[serviceAccountSecretTokenKey]),
}
return NewProxier(proxierConfig), nil
}
func HasControllerProxy(
name string,
configI core.ConfigMapInterface,
) (bool, error) {
_, err := configI.Get(context.TODO(), name, meta.GetOptions{})
if k8serrors.IsNotFound(err) {
return false, nil
} else if err != nil {
return false, errors.Trace(err)
}
return true, nil
}