forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
51 lines (45 loc) · 1.7 KB
/
util.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
package openshift
import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrorutils "k8s.io/apimachinery/pkg/util/errors"
kapi "k8s.io/kubernetes/pkg/api"
"github.com/openshift/origin/pkg/client"
configcmd "github.com/openshift/origin/pkg/config/cmd"
genappcmd "github.com/openshift/origin/pkg/generate/app/cmd"
"github.com/openshift/origin/pkg/oc/bootstrap/docker/errors"
)
func instantiateTemplate(client client.Interface, mapper configcmd.Mapper, templateNamespace, templateName, targetNamespace string, params map[string]string, ignoreExistsErrors bool) error {
template, err := client.Templates(templateNamespace).Get(templateName, metav1.GetOptions{})
if err != nil {
return errors.NewError("cannot retrieve template %q from namespace %q", templateName, templateNamespace).WithCause(err)
}
// process the template
result, err := genappcmd.TransformTemplate(template, client, targetNamespace, params, false)
if err != nil {
return errors.NewError("cannot process template %s/%s", templateNamespace, templateName).WithCause(err)
}
// Create objects
bulk := &configcmd.Bulk{
Mapper: mapper,
Op: configcmd.Create,
}
itemsToCreate := &kapi.List{
Items: result.Objects,
}
if errs := bulk.Run(itemsToCreate, targetNamespace); len(errs) > 0 {
filteredErrs := []error{}
for _, err := range errs {
if kerrors.IsAlreadyExists(err) && ignoreExistsErrors {
continue
}
filteredErrs = append(filteredErrs, err)
}
if len(filteredErrs) == 0 {
return nil
}
err = kerrorutils.NewAggregate(filteredErrs)
return errors.NewError("cannot create objects from template %s/%s", templateNamespace, templateName).WithCause(err)
}
return nil
}