forked from derailed/k9s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkload.go
201 lines (175 loc) · 5.49 KB
/
workload.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package config
import (
"errors"
"fmt"
"os"
"path"
"github.com/derailed/k9s/internal/client"
"gopkg.in/yaml.v2"
)
var (
// Template represents the template of a new workload gvr
Template = []byte(`name: "test.com/v1alpha1/myCRD"
status:
cellName: "Status"
# na: true
readiness:
cellName: "Current"
# The cellExtraName will be shown as cellName/cellExtraName
cellExtraName: "Desired"
# na: true
validity:
replicas:
cellCurrentName: "Current"
cellDesiredName: "Desired"
# cellAllName: "Ready"
matchs:
- cellName: "State"
cellValue: "Ready"`)
)
var (
// defaultGvr represent the default values uses if a custom gvr is set without status, validity or readiness
defaultGvr = WorkloadGVR{
Status: &GVRStatus{CellName: "Status"},
Validity: &GVRValidity{Matchs: []Match{{CellName: "Ready", Value: "True"}}},
Readiness: &GVRReadiness{CellName: "Ready"},
}
// defaultConfigGVRs represents the default configurations
defaultConfigGVRs = map[string]WorkloadGVR{
"apps/v1/deployments": {
Name: "apps/v1/deployments",
Readiness: &GVRReadiness{CellName: "Ready"},
Validity: &GVRValidity{
Replicas: Replicas{CellAllName: "Ready"},
},
},
"apps/v1/daemonsets": {
Name: "apps/v1/daemonsets",
Readiness: &GVRReadiness{CellName: "Ready", CellExtraName: "Desired"},
Validity: &GVRValidity{
Replicas: Replicas{CellDesiredName: "Desired", CellCurrentName: "Ready"},
},
},
"apps/v1/replicasets": {
Name: "apps/v1/replicasets",
Readiness: &GVRReadiness{CellName: "Current", CellExtraName: "Desired"},
Validity: &GVRValidity{
Replicas: Replicas{CellDesiredName: "Desired", CellCurrentName: "Current"},
},
},
"apps/v1/statefulSets": {
Name: "apps/v1/statefulSets",
Status: &GVRStatus{CellName: "Ready"},
Readiness: &GVRReadiness{CellName: "Ready"},
Validity: &GVRValidity{
Replicas: Replicas{CellAllName: "Ready"},
},
},
"v1/pods": {
Name: "v1/pods",
Status: &GVRStatus{CellName: "Status"},
Readiness: &GVRReadiness{CellName: "Ready"},
Validity: &GVRValidity{
Matchs: []Match{
{CellName: "Status", Value: "Running"},
},
Replicas: Replicas{CellAllName: "Ready"},
},
},
}
)
type CellName string
type GVRStatus struct {
NA bool `json:"na" yaml:"na"`
CellName CellName `json:"cellName" yaml:"cellName"`
}
type GVRReadiness struct {
NA bool `json:"na" yaml:"na"`
CellName CellName `json:"cellName" yaml:"cellName"`
CellExtraName CellName `json:"cellExtraName" yaml:"cellExtraName"`
}
type Match struct {
CellName CellName `json:"cellName" yaml:"cellName"`
Value string `json:"cellValue" yaml:"cellValue"`
}
type Replicas struct {
CellCurrentName CellName `json:"cellCurrentName" yaml:"cellCurrentName"`
CellDesiredName CellName `json:"cellDesiredName" yaml:"cellDesiredName"`
CellAllName CellName `json:"cellAllName" yaml:"cellAllName"`
}
type GVRValidity struct {
NA bool `json:"na" yaml:"na"`
Matchs []Match `json:"matchs,omitempty" yaml:"matchs,omitempty"`
Replicas Replicas `json:"replicas" yaml:"replicas"`
}
type WorkloadGVR struct {
Name string `json:"name" yaml:"name"`
Status *GVRStatus `json:"status,omitempty" yaml:"status,omitempty"`
Readiness *GVRReadiness `json:"readiness,omitempty" yaml:"readiness,omitempty"`
Validity *GVRValidity `json:"validity,omitempty" yaml:"validity,omitempty"`
}
type WorkloadConfig struct {
GVRFilenames []string `yaml:"wkg"`
}
// NewWorkloadGVRs returns the default GVRs to use if no custom config is set
// The workloadDir represent the directory of the custom workloads, the gvrNames are the custom gvrs names
func NewWorkloadGVRs(workloadDir string, gvrNames []string) ([]WorkloadGVR, error) {
workloadGVRs := make([]WorkloadGVR, 0)
for _, gvr := range defaultConfigGVRs {
workloadGVRs = append(workloadGVRs, gvr)
}
var errs error
// Append custom GVRS
if len(gvrNames) != 0 {
for _, filename := range gvrNames {
wkgvr, err := GetWorkloadGVRFromFile(path.Join(workloadDir, fmt.Sprintf("%s.%s", filename, "yaml")))
if err != nil {
errs = errors.Join(errs, err)
continue
}
workloadGVRs = append(workloadGVRs, wkgvr)
}
}
return workloadGVRs, errs
}
// GetWorkloadGVRFromFile returns a gvr from a filepath
func GetWorkloadGVRFromFile(filepath string) (WorkloadGVR, error) {
yamlFile, err := os.ReadFile(filepath)
if err != nil {
return WorkloadGVR{}, err
}
var wkgvr WorkloadGVR
if err = yaml.Unmarshal(yamlFile, &wkgvr); err != nil {
return WorkloadGVR{}, err
}
return wkgvr, nil
}
// GetGVR will return the GVR defined by the WorkloadGVR's name
func (wgvr WorkloadGVR) GetGVR() client.GVR {
return client.NewGVR(wgvr.Name)
}
// ApplyDefault will complete the GVR with missing values
// If it's an existing GVR's name, it will apply their corresponding default values
// If it's an unknown resources without readiness, status or validity it will use the default ones
func (wkgvr *WorkloadGVR) ApplyDefault() {
// Apply default values
existingGvr, ok := defaultConfigGVRs[wkgvr.Name]
if ok {
wkgvr.applyDefaultValues(existingGvr)
} else {
wkgvr.applyDefaultValues(defaultGvr)
}
}
func (wkgvr *WorkloadGVR) applyDefaultValues(defaultGVR WorkloadGVR) {
if wkgvr.Status == nil {
wkgvr.Status = defaultGVR.Status
}
if wkgvr.Readiness == nil {
wkgvr.Readiness = defaultGVR.Readiness
}
if wkgvr.Validity == nil {
wkgvr.Validity = defaultGVR.Validity
}
}