/
handler.go
149 lines (130 loc) · 4.56 KB
/
handler.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
package handler
import (
"fmt"
"time"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
navarchosv1alpha1 "github.com/pusher/navarchos/pkg/apis/navarchos/v1alpha1"
"github.com/pusher/navarchos/pkg/controller/nodereplacement/status"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Options are used to configure the NodeReplacementHandler
type Options struct {
// EvictionGracePeriod determines how long the controller should attempt to
// evict a pod before marking it a failed eviction
EvictionGracePeriod *time.Duration
// DrainTimeout determines how long the controller should attempt to drain a
// node before timing out. Zero means infinite
DrainTimeout *time.Duration
// IgnoreAllDaemonSets instructs the controller to ignore all DaemonSet
// managed pods. Defaults true
IgnoreAllDaemonSets *bool
// DeleteLocalData instructs the controller to delete local data belonging
// to pods (emptyDir). Defaults true
DeleteLocalData *bool
// ForcePodDeletion instructs the controller to continue even if there are
// pods not managed by a ReplicationController, ReplicaSet, Job, DaemonSet
// or StatefulSet. Defaults false
ForcePodDeletion *bool
// Config is used to construct a kubernetes client
Config *rest.Config
// k8sClient is the typed client interface for all standard groups in
// Kubernetes
k8sClient kubernetes.Interface
}
// Complete defaults any values that are not explicitly set
func (o *Options) Complete() {
if o.EvictionGracePeriod == nil {
grace := -1 * time.Second
o.EvictionGracePeriod = &grace
}
if o.DrainTimeout == nil {
timeout := 15 * time.Minute
o.DrainTimeout = &timeout
}
if o.IgnoreAllDaemonSets == nil {
o.IgnoreAllDaemonSets = boolPtr(true)
}
if o.DeleteLocalData == nil {
o.DeleteLocalData = boolPtr(true)
}
if o.ForcePodDeletion == nil {
o.ForcePodDeletion = boolPtr(false)
}
if o.Config != nil {
o.k8sClient = kubernetes.NewForConfigOrDie(o.Config)
}
}
// NodeReplacementHandler handles the business logic within the NodeReplacement
// controller.
type NodeReplacementHandler struct {
client client.Client
k8sClient kubernetes.Interface
evictionGracePeriod time.Duration
drainTimeout time.Duration
ignoreAllDaemonSets bool
deleteLocalData bool
forcePodDeletion bool
}
// NewNodeReplacementHandler creates a new NodeReplacementHandler
func NewNodeReplacementHandler(c client.Client, opts *Options) *NodeReplacementHandler {
opts.Complete()
return &NodeReplacementHandler{
client: c,
k8sClient: opts.k8sClient,
evictionGracePeriod: *opts.EvictionGracePeriod,
drainTimeout: *opts.DrainTimeout,
ignoreAllDaemonSets: *opts.IgnoreAllDaemonSets,
deleteLocalData: *opts.DeleteLocalData,
forcePodDeletion: *opts.ForcePodDeletion,
}
}
// Handle performs the business logic of a NodeReplacement and returns
// information in a Result. The use of fallthrough is to ensure that one
// instance of a NodeReplacement can be handled in full without interruption
func (h *NodeReplacementHandler) Handle(instance *navarchosv1alpha1.NodeReplacement) (*status.Result, error) {
var result = &status.Result{}
var err error
switch instance.Status.Phase {
default:
newPhase := navarchosv1alpha1.ReplacementPhaseNew
// Update status before starting next phase. This updates the instance
// phase too, it is mutated in place...
err = status.UpdateStatus(h.client, instance, &status.Result{
Phase: &newPhase,
})
if err != nil {
// This is an API error which means other errors are also likely,
// bail and requeue We will reattempt the status update anyway where
// this is called
return result, fmt.Errorf("error updating status: %v", err)
}
fallthrough // This is important, we want one instance to be handled to completion without a requeue if possible
case navarchosv1alpha1.ReplacementPhaseNew:
result, err = h.handleNew(instance)
if err != nil {
return result, err
}
if result.Requeue {
return result, nil
}
// Update status before starting next phase
err = status.UpdateStatus(h.client, instance, result)
if err != nil {
return result, fmt.Errorf("error updating status: %v", err)
}
fallthrough // This is important, we want one instance to be handled to completion without a requeue if possible
case navarchosv1alpha1.ReplacementPhaseInProgress:
result, err = h.handleInProgress(instance)
if err != nil {
return result, err
}
// Nothing left to do
return result, nil
case navarchosv1alpha1.ReplacementPhaseCompleted:
return &status.Result{}, nil
}
}
func boolPtr(b bool) *bool {
return &b
}