-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_env.go
190 lines (168 loc) · 4.94 KB
/
get_env.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cmd
import (
"fmt"
"io"
"strings"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetEnvOptions containers the CLI options
type GetEnvOptions struct {
GetOptions
PromotionStrategy string
PreviewOnly bool
}
var (
getEnvLong = templates.LongDesc(`
Display one or many environments.
`)
getEnvExample = templates.Examples(`
# List all environments
jx get environments
# List all environments using the shorter alias
jx get env
`)
)
// NewCmdGetEnv creates the new command for: jx get env
func NewCmdGetEnv(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GetEnvOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "environments",
Short: "Display one or many Environments",
Aliases: []string{"envs", "environment", "env"},
Long: getEnvLong,
Example: getEnvExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addGetFlags(cmd)
cmd.Flags().StringVarP(&options.PromotionStrategy, "promote", "p", "", "Filters the environments by promotion strategy. Possible values: "+strings.Join(v1.PromotionStrategyTypeValues, ", "))
return cmd
}
// Run implements this command
func (o *GetEnvOptions) Run() error {
client, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
kubeClient, _, err := o.KubeClient()
if err != nil {
return err
}
apisClient, err := o.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterEnvironmentCRD(apisClient)
if err != nil {
return err
}
args := o.Args
if len(args) > 0 {
e := args[0]
env, err := client.JenkinsV1().Environments(ns).Get(e, metav1.GetOptions{})
if err != nil {
envNames, err := kube.GetEnvironmentNames(client, ns)
if err != nil {
return err
}
return util.InvalidArg(e, envNames)
}
// lets output one environment
spec := &env.Spec
table := o.CreateTable()
table.AddRow("NAME", "LABEL", "KIND", "NAMESPACE", "SOURCE", "REF", "PR")
table.AddRow(e, spec.Label, spec.Namespace, kindString(spec), spec.Source.URL, spec.Source.Ref, spec.PullRequestURL)
table.Render()
log.Blank()
ens := env.Spec.Namespace
if ens != "" {
deps, err := kubeClient.AppsV1beta1().Deployments(ens).List(metav1.ListOptions{})
if err != nil {
return fmt.Errorf("Could not find deployments in namespace %s: %s", ens, err)
}
table = o.CreateTable()
table.AddRow("APP", "VERSION", "DESIRED", "CURRENT", "UP-TO-DATE", "AVAILABLE", "AGE")
for _, d := range deps.Items {
replicas := ""
if d.Spec.Replicas != nil {
replicas = formatInt32(*d.Spec.Replicas)
}
table.AddRow(d.Name, kube.GetVersion(&d.ObjectMeta), replicas,
formatInt32(d.Status.ReadyReplicas), formatInt32(d.Status.UpdatedReplicas), formatInt32(d.Status.AvailableReplicas), "")
}
table.Render()
}
} else {
envs, err := client.JenkinsV1().Environments(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
if len(envs.Items) == 0 {
log.Infof("No environments found.\nTo create an environment use: jx create env\n")
return nil
}
environments := o.filterEnvironments(envs.Items)
kube.SortEnvironments(environments)
if o.Output != "" {
envs.Items = environments
return o.renderResult(envs, o.Output)
}
table := o.CreateTable()
if o.PreviewOnly {
table.AddRow("PULL REQUEST", "NAMESPACE", "APPLICATION")
} else {
table.AddRow("NAME", "LABEL", "KIND", "PROMOTE", "NAMESPACE", "ORDER", "CLUSTER", "SOURCE", "REF", "PR")
}
for _, env := range environments {
spec := &env.Spec
if o.PreviewOnly {
table.AddRow(spec.PullRequestURL, spec.Namespace, util.ColorInfo(spec.PreviewGitSpec.ApplicationURL))
} else {
table.AddRow(env.Name, spec.Label, kindString(spec), string(spec.PromotionStrategy), spec.Namespace, util.Int32ToA(spec.Order), spec.Cluster, spec.Source.URL, spec.Source.Ref, spec.PullRequestURL)
}
}
table.Render()
}
return nil
}
func kindString(spec *v1.EnvironmentSpec) string {
answer := string(spec.Kind)
if answer == "" {
return string(v1.EnvironmentKindTypePermanent)
}
return answer
}
func (o *GetEnvOptions) filterEnvironments(envs []v1.Environment) []v1.Environment {
answer := []v1.Environment{}
for _, e := range envs {
preview := e.Spec.Kind == v1.EnvironmentKindTypePreview
if o.matchesFilter(&e) && preview == o.PreviewOnly {
answer = append(answer, e)
}
}
return answer
}
func (o *GetEnvOptions) matchesFilter(env *v1.Environment) bool {
if o.PromotionStrategy == "" {
return true
}
return env.Spec.PromotionStrategy == v1.PromotionStrategyType(o.PromotionStrategy)
}