forked from redhat-developer/odo
/
application.go
139 lines (123 loc) · 4.78 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package application
import (
"fmt"
"github.com/openshift/odo/pkg/storage"
"github.com/golang/glog"
"github.com/openshift/odo/pkg/application"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/occlient"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/url"
"github.com/pkg/errors"
"github.com/openshift/odo/pkg/component"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/odo/util/completion"
"github.com/openshift/odo/pkg/service"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// RecommendedCommandName is the recommended app command name
const RecommendedCommandName = "app"
// NewCmdApplication implements the odo application command
func NewCmdApplication(name, fullName string) *cobra.Command {
delete := NewCmdDelete(deleteRecommendedCommandName, odoutil.GetFullName(fullName, deleteRecommendedCommandName))
describe := NewCmdDescribe(describeRecommendedCommandName, odoutil.GetFullName(fullName, describeRecommendedCommandName))
list := NewCmdList(listRecommendedCommandName, odoutil.GetFullName(fullName, listRecommendedCommandName))
applicationCmd := &cobra.Command{
Use: name,
Short: "Perform application operations",
Long: `Performs application operations related to your OpenShift project.`,
Example: fmt.Sprintf("%s\n%s\n%s",
delete.Example,
describe.Example,
list.Example),
Aliases: []string{"application"},
Run: func(cmd *cobra.Command, args []string) {
},
}
applicationCmd.AddCommand(delete, describe, list)
// Add a defined annotation in order to appear in the help menu
applicationCmd.Annotations = map[string]string{"command": "other"}
applicationCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
return applicationCmd
}
// AddApplicationFlag adds a `app` flag to the given cobra command
// Also adds a completion handler to the flag
func AddApplicationFlag(cmd *cobra.Command) {
cmd.Flags().String(genericclioptions.ApplicationFlagName, "", "Application, defaults to active application")
completion.RegisterCommandFlagHandler(cmd, "app", completion.AppCompletionHandler)
}
// printDeleteAppInfo will print things which will be deleted
func printDeleteAppInfo(client *occlient.Client, appName string, projectName string) error {
componentList, err := component.List(client, appName)
if err != nil {
return errors.Wrap(err, "failed to get Component list")
}
if len(componentList.Items) != 0 {
log.Info("This application has following components that will be deleted")
for _, currentComponent := range componentList.Items {
componentDesc, err := component.GetComponent(client, currentComponent.Name, appName, projectName)
if err != nil {
return errors.Wrap(err, "unable to get component description")
}
log.Info("component named", currentComponent.Name)
if len(componentDesc.Spec.URL) != 0 {
ul, err := url.List(client, componentDesc.Name, appName)
if err != nil {
return errors.Wrap(err, "Could not get url list")
}
log.Info("This component has following urls that will be deleted with component")
for _, u := range ul.Items {
log.Info("URL named", u.GetName(), "with host", u.Spec.Host, "having protocol", u.Spec.Protocol, "at port", u.Spec.Port)
}
}
storages, err := storage.List(client, currentComponent.Name, appName)
odoutil.LogErrorAndExit(err, "")
if len(storages.Items) != 0 {
log.Info("The component has following storages which will be deleted with the component")
for _, storageName := range componentDesc.Spec.Storage {
store := storages.Get(storageName)
log.Info("Storage named", store.GetName(), "of size", store.Spec.Size)
}
}
}
// List services that will be removed
serviceList, err := service.List(client, appName)
if err != nil {
log.Info("No services / could not get services")
glog.V(4).Info(err.Error())
}
if len(serviceList) != 0 {
log.Info("This application has following service that will be deleted")
for _, ser := range serviceList {
log.Info("service named", ser.Name, "of type", ser.Type)
}
}
}
return nil
}
// getMachineReadableFormat returns resource information in machine readable format
func getMachineReadableFormat(client *occlient.Client, appName string, projectName string, active bool) application.App {
componentList, _ := component.List(client, appName)
var compList []string
for _, component := range componentList.Items {
compList = append(compList, component.Name)
}
appDef := application.App{
TypeMeta: metav1.TypeMeta{
Kind: "app",
APIVersion: "odo.openshift.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Namespace: projectName,
},
Spec: application.AppSpec{
Components: compList,
},
Status: application.AppStatus{
Active: active,
},
}
return appDef
}