/
deployments.go
221 lines (198 loc) · 6.19 KB
/
deployments.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package apps
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/gmeghnag/omc/cmd/helpers"
"github.com/gmeghnag/omc/vars"
"github.com/spf13/cobra"
appsv1 "k8s.io/api/apps/v1"
"sigs.k8s.io/yaml"
)
type DeploymentsItems struct {
ApiVersion string `json:"apiVersion"`
Items []*appsv1.Deployment `json:"items"`
}
func GetDeployments(currentContextPath string, namespace string, resourceName string, allNamespacesFlag bool, outputFlag string, showLabels bool, jsonPathTemplate string, allResources bool) bool {
_headers := []string{"namespace", "name", "ready", "up-to-date", "available", "age", "containers", "images"}
var namespaces []string
if allNamespacesFlag == true {
namespace = "all"
_namespaces, _ := ioutil.ReadDir(currentContextPath + "/namespaces/")
for _, f := range _namespaces {
namespaces = append(namespaces, f.Name())
}
} else {
namespaces = append(namespaces, namespace)
}
var data [][]string
var _DeploymentsList = DeploymentsItems{ApiVersion: "v1"}
for _, _namespace := range namespaces {
var _Items DeploymentsItems
CurrentNamespacePath := currentContextPath + "/namespaces/" + _namespace
_file, err := ioutil.ReadFile(CurrentNamespacePath + "/apps/deployments.yaml")
if err != nil && !allNamespacesFlag {
fmt.Println("No resources found in " + _namespace + " namespace.")
os.Exit(1)
}
if err := yaml.Unmarshal([]byte(_file), &_Items); err != nil {
fmt.Println("Error when trying to unmarshal file " + CurrentNamespacePath + "/apps/deployments.yaml")
os.Exit(1)
}
for _, Deployment := range _Items.Items {
labels := helpers.ExtractLabels(Deployment.GetLabels())
if !helpers.MatchLabels(labels, vars.LabelSelectorStringVar) {
continue
}
if resourceName != "" && resourceName != Deployment.Name {
continue
}
if outputFlag == "name" {
_DeploymentsList.Items = append(_DeploymentsList.Items, Deployment)
fmt.Println("deployment.apps/" + Deployment.Name)
continue
}
if outputFlag == "yaml" {
_DeploymentsList.Items = append(_DeploymentsList.Items, Deployment)
continue
}
if outputFlag == "json" {
_DeploymentsList.Items = append(_DeploymentsList.Items, Deployment)
continue
}
if strings.HasPrefix(outputFlag, "jsonpath=") {
_DeploymentsList.Items = append(_DeploymentsList.Items, Deployment)
continue
}
//name
DeploymentName := Deployment.Name
if allResources {
DeploymentName = "deployment.apps/" + DeploymentName
}
//ready
ready := "??"
readyReplicas := Deployment.Status.ReadyReplicas
replicas := *Deployment.Spec.Replicas
ready = strconv.Itoa(int(readyReplicas)) + "/" + strconv.Itoa(int(replicas))
//up-to-date
upToDateReplicas := "??"
upToDateReplicas = strconv.Itoa(int(Deployment.Status.UpdatedReplicas))
//available
availableReplicas := "??"
availableReplicas = strconv.Itoa(int(Deployment.Status.AvailableReplicas))
//age
age := helpers.GetAge(CurrentNamespacePath+"/apps/deployments.yaml", Deployment.GetCreationTimestamp())
//containers
containers := ""
for _, c := range Deployment.Spec.Template.Spec.Containers {
containers += fmt.Sprint(c.Name) + ","
}
if containers == "" {
containers = "??"
} else {
containers = strings.TrimRight(containers, ",")
}
//images
images := ""
for _, i := range Deployment.Spec.Template.Spec.Containers {
images += fmt.Sprint(i.Image) + ","
}
if images == "" {
images = "??"
} else {
images = strings.TrimRight(images, ",")
}
_list := []string{Deployment.Namespace, DeploymentName, ready, upToDateReplicas, availableReplicas, age, containers, images}
data = helpers.GetData(data, allNamespacesFlag, showLabels, labels, outputFlag, 5, _list)
if resourceName != "" && resourceName == DeploymentName {
break
}
}
if namespace != "" && _namespace == namespace {
break
}
}
if (outputFlag == "" || outputFlag == "wide") && len(data) == 0 {
if !allResources {
fmt.Println("No resources found in " + namespace + " namespace.")
}
return true
}
var headers []string
if outputFlag == "" {
if allNamespacesFlag == true {
headers = _headers[0:5]
} else {
headers = _headers[1:5]
}
if showLabels {
headers = append(headers, "labels")
}
helpers.PrintTable(headers, data)
return false
}
if outputFlag == "wide" {
if allNamespacesFlag == true {
headers = _headers
} else {
headers = _headers[1:]
}
if showLabels {
headers = append(headers, "labels")
}
helpers.PrintTable(headers, data)
return false
}
if len(_DeploymentsList.Items) == 0 {
if !allResources {
fmt.Println("No resources found in " + namespace + " namespace.")
}
return true
}
var resource interface{}
if resourceName != "" {
resource = _DeploymentsList.Items[0]
} else {
resource = _DeploymentsList
}
if outputFlag == "yaml" {
y, _ := yaml.Marshal(resource)
fmt.Println(string(y))
}
if outputFlag == "json" {
j, _ := json.MarshalIndent(resource, "", " ")
fmt.Println(string(j))
}
if strings.HasPrefix(outputFlag, "jsonpath=") {
helpers.ExecuteJsonPath(resource, jsonPathTemplate)
}
return false
}
var Deployment = &cobra.Command{
Use: "deployment",
Aliases: []string{"deployments", "deployment.apps", "deploy"},
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
resourceName := ""
if len(args) == 1 {
resourceName = args[0]
}
jsonPathTemplate := helpers.GetJsonTemplate(vars.OutputStringVar)
GetDeployments(vars.MustGatherRootPath, vars.Namespace, resourceName, vars.AllNamespaceBoolVar, vars.OutputStringVar, vars.ShowLabelsBoolVar, jsonPathTemplate, false)
},
}