/
table_kubernetes_daemonset.go
317 lines (275 loc) · 9.97 KB
/
table_kubernetes_daemonset.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package kubernetes
import (
"context"
"strings"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
func tableKubernetesDaemonset(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "kubernetes_daemonset",
Description: "A DaemonSet ensures that all (or some) Nodes run a copy of a Pod.",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "namespace"}),
Hydrate: getK8sDaemonSet,
},
List: &plugin.ListConfig{
Hydrate: listK8sDaemonSets,
KeyColumns: getCommonOptionalKeyQuals(),
},
Columns: k8sCommonColumns([]*plugin.Column{
//// DaemonSetSpec Columns
{
Name: "min_ready_seconds",
Type: proto.ColumnType_INT,
Description: "The minimum number of seconds for which a newly created DaemonSet pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0",
Transform: transform.FromField("Spec.MinReadySeconds"),
},
{
Name: "revision_history_limit",
Type: proto.ColumnType_INT,
Description: "The number of old history to retain to allow rollback. This is a pointer to distinguish between explicit zero and not specified. Defaults to 10.",
Transform: transform.FromField("Spec.RevisionHistoryLimit"),
},
{
Name: "selector_query",
Type: proto.ColumnType_STRING,
Description: "A query string representation of the selector.",
Transform: transform.FromField("Spec.Selector").Transform(labelSelectorToString),
},
{
Name: "selector",
Type: proto.ColumnType_JSON,
Description: "A label query over pods that are managed by the daemon set.",
Transform: transform.FromField("Spec.Volumes"),
},
{
Name: "template",
Type: proto.ColumnType_JSON,
Description: "An object that describes the pod that will be created.",
Transform: transform.FromField("Spec.Template"),
},
{
Name: "update_strategy",
Type: proto.ColumnType_JSON,
Description: "An update strategy to replace existing DaemonSet pods with new pods.",
Transform: transform.FromField("Spec.UpdateStrategy"),
},
//// DaemonSetStatus Columns
{
Name: "current_number_scheduled",
Type: proto.ColumnType_INT,
Description: "The number of nodes that are running at least 1 daemon pod and are supposed to run the daemon pod.",
Transform: transform.FromField("Status.CurrentNumberScheduled"),
},
{
Name: "number_misscheduled",
Type: proto.ColumnType_INT,
Description: "The number of nodes that are running the daemon pod, but are not supposed to run the daemon pod.",
Transform: transform.FromField("Status.NumberMisscheduled"),
},
{
Name: "desired_number_scheduled",
Type: proto.ColumnType_INT,
Description: "The total number of nodes that should be running the daemon pod (including nodes correctly running the daemon pod).",
Transform: transform.FromField("Status.DesiredNumberScheduled"),
},
{
Name: "number_ready",
Type: proto.ColumnType_INT,
Description: "The number of nodes that should be running the daemon pod and have one or more of the daemon pod running and ready.",
Transform: transform.FromField("Status.NumberReady"),
},
{
Name: "observed_generation",
Type: proto.ColumnType_INT,
Description: "The most recent generation observed by the daemon set controller.",
Transform: transform.FromField("Status.ObservedGeneration"),
},
{
Name: "updated_number_scheduled",
Type: proto.ColumnType_INT,
Description: "The total number of nodes that are running updated daemon pod.",
Transform: transform.FromField("Status.UpdatedNumberScheduled"),
},
{
Name: "number_available",
Type: proto.ColumnType_INT,
Description: "The number of nodes that should be running the daemon pod and have one or more of the daemon pod running and available (ready for at least spec.minReadySeconds).",
Transform: transform.FromField("Status.NumberAvailable"),
},
{
Name: "number_unavailable",
Type: proto.ColumnType_INT,
Description: "The number of nodes that should be running the daemon pod and have none of the daemon pod running and available (ready for at least spec.minReadySeconds).",
Transform: transform.FromField("Status.NumberUnavailable"),
},
{
Name: "collision_count",
Type: proto.ColumnType_INT,
Description: "Count of hash collisions for the DaemonSet. The DaemonSet controller uses this field as a collision avoidance mechanism when it needs to create the name for the newest ControllerRevision.",
Transform: transform.FromField("Status.CollisionCount"),
},
{
Name: "conditions",
Type: proto.ColumnType_JSON,
Description: "Represents the latest available observations of a DaemonSet's current state.",
Transform: transform.FromField("Status.Conditions"),
},
{
Name: "context_name",
Type: proto.ColumnType_STRING,
Description: "Kubectl config context name.",
Hydrate: getDaemonSetResourceContext,
},
{
Name: "source_type",
Type: proto.ColumnType_STRING,
Description: "The source of the resource. Possible values are: deployed and manifest. If the resource is fetched from the spec file the value will be manifest.",
},
//// Steampipe Standard Columns
{
Name: "title",
Type: proto.ColumnType_STRING,
Description: ColumnDescriptionTitle,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Type: proto.ColumnType_JSON,
Description: ColumnDescriptionTags,
Transform: transform.From(transformDaemonSetTags),
},
}),
}
}
type DaemonSet struct {
v1.DaemonSet
parsedContent
}
//// HYDRATE FUNCTIONS
func listK8sDaemonSets(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
logger.Trace("listK8sDaemonSets")
// Get the client for querying the K8s APIs for the provided context.
// If the connection is configured for the manifest files, the client will return nil.
clientset, err := GetNewClientset(ctx, d)
if err != nil {
return nil, err
}
// Check for manifest files
parsedContents, err := fetchResourceFromManifestFileByKind(ctx, d, "DaemonSet")
if err != nil {
return nil, err
}
for _, content := range parsedContents {
daemonSet := content.ParsedData.(*v1.DaemonSet)
d.StreamListItem(ctx, DaemonSet{*daemonSet, content})
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
// Check for deployed resources
if clientset == nil {
return nil, nil
}
input := metav1.ListOptions{
Limit: 500,
}
// Limiting the results
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < input.Limit {
if *limit < 1 {
input.Limit = 1
} else {
input.Limit = *limit
}
}
}
commonFieldSelectorValue := getCommonOptionalKeyQualsValueForFieldSelector(d)
if len(commonFieldSelectorValue) > 0 {
input.FieldSelector = strings.Join(commonFieldSelectorValue, ",")
}
var response *v1.DaemonSetList
pageLeft := true
for pageLeft {
response, err = clientset.AppsV1().DaemonSets(d.EqualsQualString("namespace")).List(ctx, input)
if err != nil {
return nil, err
}
if response.GetContinue() != "" {
input.Continue = response.Continue
} else {
pageLeft = false
}
for _, daemonSet := range response.Items {
d.StreamListItem(ctx, DaemonSet{daemonSet, parsedContent{SourceType: "deployed"}})
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, nil
}
func getK8sDaemonSet(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
logger.Trace("getK8sDaemonSet")
// Get the client for querying the K8s APIs for the provided context.
// If the connection is configured for the manifest files, the client will return nil.
clientset, err := GetNewClientset(ctx, d)
if err != nil {
return nil, err
}
name := d.EqualsQuals["name"].GetStringValue()
namespace := d.EqualsQuals["namespace"].GetStringValue()
// return if namespace or name is empty
if namespace == "" || name == "" {
return nil, nil
}
// Get the manifest resource
parsedContents, err := fetchResourceFromManifestFileByKind(ctx, d, "DaemonSet")
if err != nil {
return nil, err
}
for _, content := range parsedContents {
daemonSet := content.ParsedData.(*v1.DaemonSet)
if daemonSet.Name == name && daemonSet.Namespace == namespace {
return DaemonSet{*daemonSet, content}, nil
}
}
// Get the deployed resource
if clientset == nil {
return nil, nil
}
daemonSet, err := clientset.AppsV1().DaemonSets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil && !isNotFoundError(err) {
return nil, err
}
return DaemonSet{*daemonSet, parsedContent{SourceType: "deployed"}}, nil
}
func getDaemonSetResourceContext(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
obj := h.Item.(DaemonSet)
// Set the context_name as nil
data := map[string]interface{}{}
if obj.Path != "" {
return data, nil
}
// Else, set the current context as context_name
currentContext, err := getKubectlContext(ctx, d, nil)
if err != nil {
return data, nil
}
data["ContextName"] = currentContext.(string)
return data, nil
}
//// TRANSFORM FUNCTIONS
func transformDaemonSetTags(_ context.Context, d *transform.TransformData) (interface{}, error) {
obj := d.HydrateItem.(DaemonSet)
return mergeTags(obj.Labels, obj.Annotations), nil
}