forked from redhat-developer/odo
/
application.go
116 lines (97 loc) · 3.23 KB
/
application.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
package application
import (
"github.com/golang/glog"
"github.com/pkg/errors"
applabels "github.com/openshift/odo/pkg/application/labels"
"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/occlient"
"github.com/openshift/odo/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
appAPIVersion = "odo.openshift.io/v1alpha1"
appKind = "Application"
appList = "List"
)
// List all applications in current project
func List(client *occlient.Client) ([]string, error) {
return ListInProject(client)
}
// ListInProject lists all applications in given project by Querying the cluster
func ListInProject(client *occlient.Client) ([]string, error) {
var appNames []string
// Get all DeploymentConfigs with the "app" label
deploymentConfigAppNames, err := client.GetDeploymentConfigLabelValues(applabels.ApplicationLabel, applabels.ApplicationLabel)
if err != nil {
return nil, errors.Wrap(err, "unable to list applications from deployment config")
}
appNames = append(appNames, deploymentConfigAppNames...)
// Get all ServiceInstances with the "app" label
// Okay, so there is an edge-case here.. if Service Catalog is *not* enabled in the cluster, we shouldn't error out..
// however, we should at least warn the user.
serviceInstanceAppNames, err := client.GetServiceInstanceLabelValues(applabels.ApplicationLabel, applabels.ApplicationLabel)
if err != nil {
glog.V(4).Infof("Unable to list Service Catalog instances: %s", err)
} else {
appNames = append(deploymentConfigAppNames, serviceInstanceAppNames...)
}
// Filter out any names, as there could be multiple components but within the same application
return util.RemoveDuplicates(appNames), nil
}
// Exists checks whether the given app exist or not
func Exists(app string, client *occlient.Client) (bool, error) {
appList, err := List(client)
if err != nil {
return false, err
}
for _, appName := range appList {
if appName == app {
return true, nil
}
}
return false, nil
}
// Delete deletes the given application
func Delete(client *occlient.Client, name string) error {
glog.V(4).Infof("Deleting application %s", name)
labels := applabels.GetLabels(name, false)
// delete application from cluster
err := client.Delete(labels)
if err != nil {
return errors.Wrapf(err, "unable to delete application %s", name)
}
return nil
}
// GetMachineReadableFormat returns resource information in machine readable format
func GetMachineReadableFormat(client *occlient.Client, appName string, projectName string) App {
componentList, _ := component.List(client, appName, nil)
var compList []string
for _, comp := range componentList.Items {
compList = append(compList, comp.Name)
}
appDef := App{
TypeMeta: metav1.TypeMeta{
Kind: appKind,
APIVersion: appAPIVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Namespace: projectName,
},
Spec: AppSpec{
Components: compList,
},
}
return appDef
}
// GetMachineReadableFormatForList returns application list in machine readable format
func GetMachineReadableFormatForList(apps []App) AppList {
return AppList{
TypeMeta: metav1.TypeMeta{
Kind: appList,
APIVersion: appAPIVersion,
},
ListMeta: metav1.ListMeta{},
Items: apps,
}
}