/
resource.go
96 lines (81 loc) · 2.57 KB
/
resource.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
package kubeconfig
import (
"time"
"github.com/giantswarm/apiextensions/pkg/apis/core/v1alpha1"
"github.com/giantswarm/certs"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
const (
// Name is the identifier of the resource.
Name = "kubeconfigv14"
)
// Config represents the configuration used to create a new kubeconfig resource.
type Config struct {
// Dependencies.
CertSearcher certs.Interface
GetClusterConfigFunc func(interface{}) (v1alpha1.ClusterGuestConfig, error)
K8sClient kubernetes.Interface
Logger micrologger.Logger
// Settings.
CertsWatchTimeout time.Duration
ProjectName string
ResourceNamespace string
}
// StateGetter implements the kubeconfig resource.
type StateGetter struct {
// Dependencies.
certsSearcher certs.Interface
k8sClient kubernetes.Interface
logger micrologger.Logger
getClusterConfigFunc func(interface{}) (v1alpha1.ClusterGuestConfig, error)
// Settings.
projectName string
resourceNamespace string
}
// New creates a new configured index resource.
func New(config Config) (*StateGetter, error) {
// Dependencies.
if config.CertSearcher == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.CertSearcher must not be empty", config)
}
if config.GetClusterConfigFunc == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.TransformFunc must not be empty", config)
}
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
// Settings
if config.ProjectName == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.ProjectName not be empty", config)
}
if config.ResourceNamespace == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.ResourceNamespace not be empty", config)
}
r := &StateGetter{
// Dependencies.
certsSearcher: config.CertSearcher,
k8sClient: config.K8sClient,
logger: config.Logger,
getClusterConfigFunc: config.GetClusterConfigFunc,
// Settings
projectName: config.ProjectName,
resourceNamespace: config.ResourceNamespace,
}
return r, nil
}
func toSecret(v interface{}) (*corev1.Secret, error) {
if v == nil {
return nil, nil
}
secret, ok := v.(*corev1.Secret)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", secret, v)
}
return secret, nil
}