forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
templatelookup.go
173 lines (152 loc) · 5.15 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
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
package app
import (
"encoding/json"
"fmt"
"strings"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubectl/resource"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/template"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
)
// TemplateSearcher resolves stored template arguments into template objects
type TemplateSearcher struct {
Client client.TemplatesNamespacer
TemplateConfigsNamespacer client.TemplateConfigsNamespacer
Namespaces []string
StopOnExactMatch bool
}
// Search searches for a template and returns matches with the object representation
func (r TemplateSearcher) Search(precise bool, terms ...string) (ComponentMatches, []error) {
matches := ComponentMatches{}
var errs []error
for _, term := range terms {
ref, err := template.ParseTemplateReference(term)
if err != nil {
glog.V(2).Infof("template references must be of the form [<namespace>/]<name>, term %q did not qualify", term)
continue
}
if term == "__template_fail" {
errs = append(errs, fmt.Errorf("unable to find the specified template: %s", term))
continue
}
namespaces := r.Namespaces
if ref.HasNamespace() {
namespaces = []string{ref.Namespace}
}
checkedNamespaces := sets.NewString()
for _, namespace := range namespaces {
if checkedNamespaces.Has(namespace) {
continue
}
checkedNamespaces.Insert(namespace)
templates, err := r.Client.Templates(namespace).List(metav1.ListOptions{})
if err != nil {
if errors.IsNotFound(err) || errors.IsForbidden(err) {
continue
}
errs = append(errs, err)
continue
}
exact := false
for i := range templates.Items {
template := &templates.Items[i]
glog.V(4).Infof("checking namespace %s for template %s", namespace, ref.Name)
if score, scored := templateScorer(*template, ref.Name); scored {
if score == 0.0 {
exact = true
}
glog.V(4).Infof("Adding template %q in project %q with score %f", template.Name, template.Namespace, score)
fullName := fmt.Sprintf("%s/%s", template.Namespace, template.Name)
matches = append(matches, &ComponentMatch{
Value: term,
Argument: fmt.Sprintf("--template=%q", fullName),
Name: fullName,
Description: fmt.Sprintf("Template %q in project %q", template.Name, template.Namespace),
Score: score,
Template: template,
})
}
}
// If we found one or more exact matches in this namespace, do not continue looking at
// other namespaces
if exact && precise {
break
}
}
}
return matches, errs
}
// IsPossibleTemplateFile returns true if the argument can be a template file
func IsPossibleTemplateFile(value string) (bool, error) {
return isFile(value)
}
// TemplateFileSearcher resolves template files into template objects
type TemplateFileSearcher struct {
Mapper meta.RESTMapper
Typer runtime.ObjectTyper
ClientMapper resource.ClientMapper
Namespace string
}
// Search attempts to read template files and transform it into template objects
func (r *TemplateFileSearcher) Search(precise bool, terms ...string) (ComponentMatches, []error) {
matches := ComponentMatches{}
var errs []error
for _, term := range terms {
if term == "__templatefile_fail" {
errs = append(errs, fmt.Errorf("unable to find the specified template file: %s", term))
continue
}
var isSingleItemImplied bool
obj, err := resource.NewBuilder(r.Mapper, r.Typer, r.ClientMapper, kapi.Codecs.UniversalDecoder()).
NamespaceParam(r.Namespace).RequireNamespace().
FilenameParam(false, &resource.FilenameOptions{Recursive: false, Filenames: terms}).
Do().
IntoSingleItemImplied(&isSingleItemImplied).
Object()
if err != nil {
switch {
case strings.Contains(err.Error(), "does not exist") && strings.Contains(err.Error(), "the path"):
continue
case strings.Contains(err.Error(), "not a directory") && strings.Contains(err.Error(), "the path"):
continue
default:
if syntaxErr, ok := err.(*json.SyntaxError); ok {
err = fmt.Errorf("at offset %d: %v", syntaxErr.Offset, err)
}
errs = append(errs, fmt.Errorf("unable to load template file %q: %v", term, err))
continue
}
}
if list, isList := obj.(*kapi.List); isList && !isSingleItemImplied {
if len(list.Items) == 1 {
obj = list.Items[0]
isSingleItemImplied = true
}
}
if !isSingleItemImplied {
errs = append(errs, fmt.Errorf("there is more than one object in %q", term))
continue
}
template, ok := obj.(*templateapi.Template)
if !ok {
errs = append(errs, fmt.Errorf("object in %q is not a template", term))
continue
}
matches = append(matches, &ComponentMatch{
Value: term,
Argument: fmt.Sprintf("--file=%q", template.Name),
Name: template.Name,
Description: fmt.Sprintf("Template file %s", term),
Score: 0,
Template: template,
})
}
return matches, errs
}