-
Notifications
You must be signed in to change notification settings - Fork 244
/
catalog.go
88 lines (76 loc) · 2.42 KB
/
catalog.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
package catalog
import (
"strings"
"github.com/pkg/errors"
"github.com/redhat-developer/odo/pkg/occlient"
log "github.com/sirupsen/logrus"
)
// List lists all the available component types
func List(client *occlient.Client) ([]string, error) {
var catalogList []string
imageStreams, err := getDefaultBuilderImages(client)
if err != nil {
return nil, errors.Wrap(err, "unable to get image streams")
}
catalogList = append(catalogList, imageStreams...)
// TODO: uncomment when component create supports template creation
//clusterServiceClasses, err := client.GetClusterServiceClassExternalNames()
//if err != nil {
// return nil, errors.Wrap(err, "unable to get cluster service classes")
//}
//catalogList = append(catalogList, clusterServiceClasses...)
return catalogList, nil
}
// Search searches for the component
func Search(client *occlient.Client, name string) ([]string, error) {
var result []string
componentList, err := List(client)
if err != nil {
return nil, errors.Wrap(err, "unable to list components")
}
// do a partial search in all the components
for _, component := range componentList {
if strings.Contains(component, name) {
result = append(result, component)
}
}
return result, nil
}
// Exists returns true if the given component type is valid, false if not
func Exists(client *occlient.Client, componentType string) (bool, error) {
catalogList, err := List(client)
if err != nil {
return false, errors.Wrapf(err, "unable to list catalog")
}
for _, supported := range catalogList {
if componentType == supported {
return true, nil
}
}
return false, nil
}
// getDefaultBuilderImages returns the default builder images available in the
// openshift namespace
func getDefaultBuilderImages(client *occlient.Client) ([]string, error) {
imageStreams, err := client.GetImageStreams(occlient.OpenShiftNameSpace)
if err != nil {
return nil, errors.Wrap(err, "unable to get Image Streams")
}
var builderImages []string
// Get builder images from the available imagestreams
outer:
for _, imageStream := range imageStreams {
for _, tag := range imageStream.Spec.Tags {
if _, ok := tag.Annotations["tags"]; ok {
for _, t := range strings.Split(tag.Annotations["tags"], ",") {
if t == "builder" {
builderImages = append(builderImages, imageStream.Name)
continue outer
}
}
}
}
}
log.Debugf("Found builder images: %v", builderImages)
return builderImages, nil
}