This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
drain.go
70 lines (60 loc) · 1.81 KB
/
drain.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2018 Datadog, Inc.
package options
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/fatih/structs"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/sets"
)
var drainOptions = sets.NewString(getOptionNames(&Drain{})...) // does not include "all" or "none"
// Drain is a convenient key / value bool struct to store which components
// should be drained.
type Drain struct {
common
Pods bool `json:"pods,omitempty"`
KubeletGC bool `json:"kubeletgc,omitempty"`
Iptables bool `json:"iptables,omitempty"`
}
// NewDrainOptions instantiate a new Drain from the drainString
// The drain string is lowercase drain options comma separated like: pods,iptables ...
func NewDrainOptions(drainString string) *Drain {
opts := newOptions(drainString, drainOptions)
glog.V(3).Infof("Drain options are %q", opts.UnsortedList())
return &Drain{
common: common{opts.Has("all"), opts.Has("none")},
Pods: opts.Has("pods"),
KubeletGC: opts.Has("kubeletgc"),
Iptables: opts.Has("iptables"),
}
}
// StringJSON represents the clean options as a JSON
func (c *Drain) StringJSON() string {
b, err := json.Marshal(c)
if err != nil {
glog.Errorf("Cannot marshal the drain options: %v", err)
return ""
}
return string(b)
}
// StringCLI returns the drain options as a command line representation
func (c *Drain) StringCLI() string {
m := structs.Map(c)
var cli []string
for k, v := range m {
b, ok := v.(bool)
if ok && b {
cli = append(cli, strings.ToLower(fmt.Sprintf("%v", k)))
}
}
if len(cli) == len(m) {
return "all"
}
sort.Strings(cli)
return strings.Join(cli, ",")
}