forked from kubernetes/kops
/
factory.go
120 lines (97 loc) · 3.29 KB
/
factory.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"fmt"
"net/url"
"strings"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/rest"
gceacls "k8s.io/kops/pkg/acls/gce"
s3acls "k8s.io/kops/pkg/acls/s3"
kopsclient "k8s.io/kops/pkg/client/clientset_generated/clientset"
"k8s.io/kops/pkg/client/simple"
"k8s.io/kops/pkg/client/simple/api"
"k8s.io/kops/pkg/client/simple/vfsclientset"
"k8s.io/kops/util/pkg/vfs"
)
type FactoryOptions struct {
RegistryPath string
}
type Factory struct {
options *FactoryOptions
clientset simple.Clientset
}
func NewFactory(options *FactoryOptions) *Factory {
gceacls.Register()
s3acls.Register()
return &Factory{
options: options,
}
}
const (
STATE_ERROR = `Please set the --state flag or export KOPS_STATE_STORE.
For example, a valid value follows the format s3://<bucket>.
You can find the supported stores in https://github.com/kubernetes/kops/blob/master/docs/state.md.`
INVALID_STATE_ERROR = `Unable to read state store.
Please use a valid state store when setting --state or KOPS_STATE_STORE env var.
For example, a valid value follows the format s3://<bucket>.
Trailing slash will be trimmed.`
)
func (f *Factory) Clientset() (simple.Clientset, error) {
if f.clientset == nil {
registryPath := f.options.RegistryPath
glog.V(2).Infof("state store %s", registryPath)
if registryPath == "" {
return nil, field.Required(field.NewPath("State Store"), STATE_ERROR)
}
// We recognize a `k8s` scheme; this might change in future so we won't document it yet
// In practice nobody is going to hit this accidentally, so I don't think we need a feature flag.
if strings.HasPrefix(registryPath, "k8s://") {
u, err := url.Parse(registryPath)
if err != nil {
return nil, fmt.Errorf("Invalid kops server url: %q", registryPath)
}
u.Scheme = "https"
config := &rest.Config{
Host: u.Scheme + "://" + u.Host,
}
glog.Warning("Using insecure TLS")
config.Insecure = true
kopsClient, err := kopsclient.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("error building kops API client: %v", err)
}
f.clientset = &api.RESTClientset{
BaseURL: &url.URL{
Scheme: "k8s",
Host: u.Host,
},
KopsClient: kopsClient.Kops(),
}
} else {
basePath, err := vfs.Context.BuildVfsPath(registryPath)
if err != nil {
return nil, fmt.Errorf("error building path for %q: %v", registryPath, err)
}
if !vfs.IsClusterReadable(basePath) {
return nil, field.Invalid(field.NewPath("State Store"), registryPath, INVALID_STATE_ERROR)
}
// For kops CLI / controller, we do allow vfs list (unlike nodeup!)
allowVFSList := true
f.clientset = vfsclientset.NewVFSClientset(basePath, allowVFSList)
}
}
return f.clientset, nil
}