This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
tolerations.go
76 lines (59 loc) · 2.21 KB
/
tolerations.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
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"github.com/pkg/errors"
"github.com/vmware/octant/pkg/view/component"
)
func printTolerations(podSpec corev1.PodSpec) (component.Component, error) {
td := &tolerationDescriber{
podSpec: podSpec,
}
return td.Create()
}
type tolerationDescriber struct {
podSpec corev1.PodSpec
}
func (td *tolerationDescriber) Create() (*component.Table, error) {
cols := component.NewTableCols("Description")
table := component.NewTable("Taints and Tolerations", "There are no taints or tolerations!", cols)
for _, toleration := range td.podSpec.Tolerations {
msg, err := td.describe(toleration)
if err != nil {
return nil, err
}
if evictSecs := toleration.TolerationSeconds; evictSecs != nil {
msg = fmt.Sprintf("%s Evict after %d seconds.", msg, *evictSecs)
}
table.Add(component.TableRow{"Description": component.NewText(msg)})
}
return table, nil
}
const (
taintFmt = "Schedule on nodes with %s taint."
wildcardFmt = "Schedule on all nodes."
)
func (td *tolerationDescriber) describe(toleration corev1.Toleration) (string, error) {
msgFmt := taintFmt
var a []interface{}
if toleration.Effect != "" && toleration.Key == "" && toleration.Value == "" {
a = append(a, string(toleration.Effect))
} else if toleration.Effect != "" && toleration.Key != "" && toleration.Value == "" {
a = append(a, fmt.Sprintf("%s:%s", toleration.Key, toleration.Effect))
} else if toleration.Key != "" && toleration.Value != "" && toleration.Effect == "" {
a = append(a, fmt.Sprintf("%s:%s", toleration.Key, toleration.Value))
} else if toleration.Key != "" && toleration.Value != "" && toleration.Effect != "" {
a = append(a, fmt.Sprintf("%s:%s:%s", toleration.Key, toleration.Value, toleration.Effect))
} else if toleration.Key != "" && toleration.Operator == corev1.TolerationOpExists {
a = append(a, toleration.Key)
} else if toleration.Key == "" && toleration.Operator == corev1.TolerationOpExists {
msgFmt = wildcardFmt
} else {
return "", errors.Errorf("unable to describe toleration: %v", toleration)
}
return fmt.Sprintf(msgFmt, a...), nil
}