forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templatelookup.go
100 lines (84 loc) · 2.75 KB
/
templatelookup.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
package app
import (
"fmt"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/client"
templateapi "github.com/openshift/origin/pkg/template/api"
)
// TemplateResolver resolves stored template arguments into template objects
type TemplateResolver struct {
Client client.TemplatesNamespacer
TemplateConfigsNamespacer client.TemplateConfigsNamespacer
Namespaces []string
}
// Resolve searches for a template and returns a match with the object representation
func (r TemplateResolver) Resolve(value string) (*ComponentMatch, error) {
checked := util.NewStringSet()
for _, namespace := range r.Namespaces {
if checked.Has(namespace) {
continue
}
checked.Insert(namespace)
glog.V(4).Infof("checking template %s/%s", namespace, value)
repo, err := r.Client.Templates(namespace).Get(value)
if err != nil {
if errors.IsNotFound(err) || errors.IsForbidden(err) {
continue
}
return nil, err
}
return &ComponentMatch{
Value: value,
Argument: fmt.Sprintf("--template=%q", value),
Name: value,
Description: fmt.Sprintf("Template %s in project %s", repo.Name, repo.Namespace),
Score: 0,
Template: repo,
}, nil
}
return nil, ErrNoMatch{value: value}
}
// IsPossibleTemplateFile returns true if the argument can be a template file
func IsPossibleTemplateFile(value string) bool {
return isFile(value)
}
// TemplateFileResolver resolves template files into remplate objects
type TemplateFileResolver struct {
Mapper meta.RESTMapper
Typer runtime.ObjectTyper
ClientMapper resource.ClientMapper
Namespace string
}
// Resolve attemps to read the template file and transform it into a template object
func (r *TemplateFileResolver) Resolve(value string) (*ComponentMatch, error) {
var isSingular bool
obj, err := resource.NewBuilder(r.Mapper, r.Typer, r.ClientMapper).
NamespaceParam(r.Namespace).RequireNamespace().
FilenameParam(value).
Do().
IntoSingular(&isSingular).
Object()
if err != nil {
return nil, err
}
if !isSingular {
return nil, fmt.Errorf("there is more than one object in %q", value)
}
template, ok := obj.(*templateapi.Template)
if !ok {
return nil, fmt.Errorf("object in %q is not a template", value)
}
return &ComponentMatch{
Value: value,
Argument: fmt.Sprintf("--file=%q", value),
Name: value,
Description: fmt.Sprintf("Template file %s", value),
Score: 0,
Template: template,
}, nil
}