-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexternalstorage.go
More file actions
149 lines (123 loc) · 2.92 KB
/
externalstorage.go
File metadata and controls
149 lines (123 loc) · 2.92 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package externalstorage
import (
"fmt"
"os"
"sync"
snapclient "github.com/kubernetes-incubator/external-storage/snapshot/pkg/client"
"github.com/portworx/sched-ops/k8s/common"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var (
instance Ops
once sync.Once
)
// Ops is an interface to perform operations on external storage resources.
type Ops interface {
SnapshotOps
// SetConfig sets the config and resets the client
SetConfig(config *rest.Config)
}
// Instance returns a singleton instance of the client.
func Instance() Ops {
once.Do(func() {
if instance == nil {
instance = &Client{}
}
})
return instance
}
// SetInstance replaces the instance with the provided one. Should be used only for testing purposes.
func SetInstance(i Ops) {
instance = i
}
// New builds a new client for the given config.
func New(client rest.Interface) *Client {
return &Client{
snap: client,
}
}
// NewForConfig builds a new client for the given config.
func NewForConfig(c *rest.Config) (*Client, error) {
snap, _, err := snapclient.NewClient(c)
if err != nil {
return nil, err
}
return &Client{
snap: snap,
}, nil
}
// NewInstanceFromConfigFile returns new instance of client by using given
// config file
func NewInstanceFromConfigFile(config string) (Ops, error) {
newInstance := &Client{}
err := newInstance.loadClientFromKubeconfig(config)
if err != nil {
return nil, err
}
return newInstance, nil
}
// Client is a wrapper for the external-storage client.
type Client struct {
config *rest.Config
snap rest.Interface
}
// SetConfig sets the config and resets the client
func (c *Client) SetConfig(cfg *rest.Config) {
c.config = cfg
c.snap = nil
}
// initClient the k8s client if uninitialized
func (c *Client) initClient() error {
if c.snap != nil {
return nil
}
return c.setClient()
}
// setClient instantiates a client.
func (c *Client) setClient() error {
var err error
if c.config != nil {
err = c.loadClient()
} else {
kubeconfig := os.Getenv("KUBECONFIG")
if len(kubeconfig) > 0 {
err = c.loadClientFromKubeconfig(kubeconfig)
} else {
err = c.loadClientFromServiceAccount()
}
}
return err
}
// loadClientFromServiceAccount loads a k8s client from a ServiceAccount specified in the pod running px
func (c *Client) loadClientFromServiceAccount() error {
config, err := rest.InClusterConfig()
if err != nil {
return err
}
c.config = config
return c.loadClient()
}
func (c *Client) loadClientFromKubeconfig(kubeconfig string) error {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return err
}
c.config = config
return c.loadClient()
}
func (c *Client) loadClient() error {
if c.config == nil {
return fmt.Errorf("rest config is not provided")
}
var err error
err = common.SetRateLimiter(c.config)
if err != nil {
return err
}
c.snap, _, err = snapclient.NewClient(c.config)
if err != nil {
return err
}
return nil
}