forked from kubeflow/kubeflow
/
server.go
333 lines (277 loc) · 8.78 KB
/
server.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2018 The Kubeflow 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 app
import (
"errors"
"io/ioutil"
"net"
"os"
"os/user"
"path"
"regexp"
"strconv"
"github.com/ghodss/yaml"
"github.com/kubeflow/kubeflow/bootstrap/cmd/bootstrap/app/options"
"github.com/kubeflow/kubeflow/bootstrap/version"
log "github.com/sirupsen/logrus"
"k8s.io/api/storage/v1"
k8sVersion "k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"context"
)
// RecommendedConfigPathEnvVar is a environment variable for path configuration
const RecommendedConfigPathEnvVar = "KUBECONFIG"
// DefaultStorageAnnotation is the Name of the default annotation used to indicate
// whether a storage class is the default.
const DefaultStorageAnnotation = "storageclass.beta.kubernetes.io/is-default-class"
// Assume gcloud is on the path.
const GcloudPath = "gcloud"
const RegistriesRoot = "/opt/registries"
type KsComponent struct {
Name string
Prototype string
}
type KsPackage struct {
Name string
// Registry should be the name of the registry containing the package.
Registry string
}
type KsParameter struct {
Component string
Name string
Value string
}
// RegistryConfig is used for two purposes:
// 1. used during image build, to configure registries that should be baked into the bootstrapper docker image.
// (See: https://github.com/kubeflow/kubeflow/blob/master/bootstrap/image_registries.yaml)
// 2. used during app create rpc call, specifies a registry to be added to an app.
// required info for registry: Name, Repo, Version, Path
// Additionally if any of required fields is blank we will try to map with one of
// the registries baked into the Docker image using the name.
type RegistryConfig struct {
Name string
Repo string
Version string
Path string
RegUri string
}
type AppConfig struct {
Registries []RegistryConfig
Packages []KsPackage
Components []KsComponent
Parameters []KsParameter
}
// RegistriesConfigFile corresponds to a YAML file specifying information
// about known registries.
type RegistriesConfigFile struct {
// Registries provides information about known registries.
Registries []RegistryConfig
}
// AppConfigFile corresponds to a YAML file specifying information
// about the app to create.
type AppConfigFile struct {
// App describes a ksonnet application.
App AppConfig
}
type LibrarySpec struct {
Version string
Path string
}
// KsRegistry corresponds to ksonnet.io/registry
// which is the registry.yaml file found in every registry.
type KsRegistry struct {
ApiVersion string
Kind string
Libraries map[string]LibrarySpec
}
// Load yaml config
func LoadConfig(path string, o interface{}) error {
if path == "" {
return errors.New("empty path")
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if err = yaml.Unmarshal(data, o); err != nil {
return err
}
return nil
}
// ModifyGcloudCommand modifies the cmd-path in the kubeconfig file.
//
// We do this because we want to be able to mount the kubeconfig file into the container.
// The kubeconfig file typically uses the full path for the binary. This won't work inside the boostrap
// container because the path will be different. However, we can assume gcloud is on the path.
// TODO(jlewi): Do we still use this?
func modifyGcloudCommand(config *clientcmdapi.Config) error {
for k, a := range config.AuthInfos {
if a.AuthProvider == nil || a.AuthProvider.Name != "gcp" {
continue
}
if p, hasP := a.AuthProvider.Config["cmd-path"]; hasP {
log.Infof("AuthInfo %v changing cmd-path %v to %v", k, p, GcloudPath)
a.AuthProvider.Config["cmd-path"] = GcloudPath
}
config.AuthInfos[k] = a
}
return nil
}
// getKubeConfigFile tries to find a kubeconfig file.
func getKubeConfigFile() string {
configFile := ""
usr, err := user.Current()
if err != nil {
log.Warningf("Could not get current user; error %v", err)
} else {
configFile = path.Join(usr.HomeDir, ".kube", "config")
}
if len(os.Getenv(RecommendedConfigPathEnvVar)) > 0 {
configFile = os.Getenv(RecommendedConfigPathEnvVar)
}
return configFile
}
// gGetClusterConfig obtain the config from the Kube configuration used by kubeconfig.
func getClusterConfig(inCluster bool) (*rest.Config, error) {
if inCluster {
return rest.InClusterConfig()
}
configFile := getKubeConfigFile()
if len(configFile) > 0 {
log.Infof("Reading config from file: %v", configFile)
// use the current context in kubeconfig
// This is very useful for running locally.
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: configFile},
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: ""}})
rawConfig, err := clientConfig.RawConfig()
if err != nil {
return nil, err
}
if err := modifyGcloudCommand(&rawConfig); err != nil {
return nil, err
}
config, err := clientConfig.ClientConfig()
return config, err
}
// Work around https://github.com/kubernetes/kubernetes/issues/40973
// See https://github.com/coreos/etcd-operator/issues/731#issuecomment-283804819
if len(os.Getenv("KUBERNETES_SERVICE_HOST")) == 0 {
addrs, err := net.LookupHost("kubernetes.default.svc")
if err != nil {
panic(err)
}
if err := os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0]); err != nil {
return nil, err
}
}
if len(os.Getenv("KUBERNETES_SERVICE_PORT")) == 0 {
if err := os.Setenv("KUBERNETES_SERVICE_PORT", "443"); err != nil {
panic(err)
}
}
config, err := rest.InClusterConfig()
return config, err
}
func isGke(v *k8sVersion.Info) bool {
pattern := regexp.MustCompile(`.*gke.*`)
log.Infof("Cluster platform: %v", v.Platform)
return pattern.MatchString(v.String())
}
func hasDefaultStorage(sClasses *v1.StorageClassList) bool {
for _, i := range sClasses.Items {
log.Infof("Storage class: %v", i.GetName())
if _, has := i.GetAnnotations()[DefaultStorageAnnotation]; !has {
log.Infof("Storage class %v doesn't have annotation %v", i.GetName(), DefaultStorageAnnotation)
continue
}
v, _ := i.GetAnnotations()[DefaultStorageAnnotation]
isDefault, err := strconv.ParseBool(v)
if err != nil {
log.Infof("Error parsing %v as bool; error %v", v, err)
}
log.Infof("StorageClass %v is default %v", i.GetName(), isDefault)
if isDefault {
return true
}
}
return false
}
// processFile creates an app based on a configuration file.
func processFile(opt *options.ServerOption, ksServer *ksServer) error {
ctx := context.Background()
appName := "kubeflow"
var appConfigFile AppConfigFile
if err := LoadConfig(opt.Config, &appConfigFile); err != nil {
return err
}
request := CreateRequest{
Name: appName,
AppConfig: appConfigFile.App,
Namespace: opt.NameSpace,
AutoConfigure: true,
}
if err := ksServer.CreateApp(ctx, request, nil); err != nil {
return err
}
if opt.Apply {
req := ApplyRequest{
Name: appName,
Environment: "default",
Components: make([]string, 0),
}
for _, component := range appConfigFile.App.Components {
req.Components = append(req.Components, component.Name)
}
if err := ksServer.Apply(ctx, req); err != nil {
log.Errorf("Failed to apply app %v; Error: %v", appName, err)
return err
}
}
return nil
}
// Run the application.
func Run(opt *options.ServerOption) error {
// Check if the -version flag was passed and, if so, print the version and exit.
if opt.PrintVersion {
version.PrintVersionAndExit()
}
// Load information about the default registries.
var regConfig RegistriesConfigFile
if opt.RegistriesConfigFile != "" {
log.Infof("Loading registry info in file %v", opt.RegistriesConfigFile)
if err := LoadConfig(opt.RegistriesConfigFile, ®Config); err != nil {
return err
}
} else {
log.Info("--registries-config-file not provided; not loading any registries")
}
ksServer, err := NewServer(opt.AppDir, regConfig.Registries, opt.GkeVersionOverride)
if err != nil {
return err
}
if opt.Config != "" {
log.Infof("Processing file: %v", opt.Config)
if err := processFile(opt, ksServer); err != nil {
log.Errorf("Error occurred tyring to process file %v; %v", opt.Config, err)
}
}
if opt.KeepAlive {
log.Infof("Starting http server.")
ksServer.StartHttp(opt.Port)
}
return nil
}