forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.go
59 lines (53 loc) · 1.46 KB
/
import.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
package openshift
import (
"bytes"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/errors"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/oc/bootstrap"
)
// ImportObjects imports objects into OpenShift from a particular location
// into a given namespace
func ImportObjects(f *clientcmd.Factory, ns, location string) error {
schema, err := f.Validator(false, "")
if err != nil {
return err
}
data, err := bootstrap.Asset(location)
if err != nil {
return err
}
glog.V(8).Infof("Importing data:\n%s\n", string(data))
r := f.NewBuilder(true).
Schema(schema).
ContinueOnError().
NamespaceParam(ns).
DefaultNamespace().
Stream(bytes.NewBuffer(data), location).
Flatten().
Do()
return r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
glog.V(5).Infof("Creating %s/%s", info.Namespace, info.Name)
if err = createAndRefresh(info); err != nil {
return cmdutil.AddSourceToErr("creating", info.Source, err)
}
return nil
})
}
func createAndRefresh(info *resource.Info) error {
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object)
if err != nil {
if errors.IsAlreadyExists(err) {
glog.V(5).Infof("Object %s/%s already exists", info.Namespace, info.Name)
return nil
}
return err
}
info.Refresh(obj, true)
return nil
}