/
approve_csr_handler.go
158 lines (137 loc) · 3.81 KB
/
approve_csr_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
150
151
152
153
154
155
156
157
158
package actions
import (
"context"
"errors"
"fmt"
"reflect"
"time"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"github.com/castai/cluster-controller/castai"
"github.com/castai/cluster-controller/csr"
"github.com/castai/cluster-controller/waitext"
)
const (
approveCSRTimeout = 4 * time.Minute
)
func newApproveCSRHandler(log logrus.FieldLogger, clientset kubernetes.Interface) ActionHandler {
return &approveCSRHandler{
log: log,
clientset: clientset,
initialCSRFetchTimeout: 5 * time.Minute,
csrFetchInterval: 5 * time.Second,
}
}
type approveCSRHandler struct {
log logrus.FieldLogger
clientset kubernetes.Interface
initialCSRFetchTimeout time.Duration
csrFetchInterval time.Duration
}
func (h *approveCSRHandler) Handle(ctx context.Context, action *castai.ClusterAction) error {
req, ok := action.Data().(*castai.ActionApproveCSR)
if !ok {
return fmt.Errorf("unexpected type %T for approve csr handler", action.Data())
}
log := h.log.WithFields(logrus.Fields{
"node_name": req.NodeName,
"node_id": req.NodeID,
"type": reflect.TypeOf(action.Data().(*castai.ActionApproveCSR)).String(),
actionIDLogField: action.ID,
})
cert, err := h.getInitialNodeCSR(ctx, log, req.NodeName)
if err != nil {
return fmt.Errorf("getting initial csr: %w", err)
}
if cert.Approved() {
log.Debug("csr is already approved")
return nil
}
ctx, cancel := context.WithTimeout(ctx, approveCSRTimeout)
defer cancel()
b := newApproveCSRExponentialBackoff()
return waitext.Retry(
ctx,
b,
waitext.Forever,
func(ctx context.Context) (bool, error) {
return true, h.handle(ctx, log, cert)
},
func(err error) {
log.Warnf("csr approval failed, will retry: %v", err)
},
)
}
func (h *approveCSRHandler) handle(ctx context.Context, log logrus.FieldLogger, cert *csr.Certificate) (reterr error) {
// Since this new csr may be denied we need to delete it.
log.Debug("deleting old csr")
if err := csr.DeleteCertificate(ctx, h.clientset, cert); err != nil {
return fmt.Errorf("deleting csr: %w", err)
}
// Create new csr with the same request data as original csr.
log.Debug("requesting new csr")
newCert, err := csr.RequestCertificate(
ctx,
h.clientset,
cert,
)
if err != nil {
return fmt.Errorf("requesting new csr: %w", err)
}
// Approve new csr.
log.Debug("approving new csr")
resp, err := csr.ApproveCertificate(ctx, h.clientset, newCert)
if err != nil {
return fmt.Errorf("approving csr: %w", err)
}
if resp.Approved() {
return nil
}
return errors.New("certificate signing request was not approved")
}
func (h *approveCSRHandler) getInitialNodeCSR(ctx context.Context, log logrus.FieldLogger, nodeName string) (*csr.Certificate, error) {
log.Debug("getting initial csr")
ctx, cancel := context.WithTimeout(ctx, h.initialCSRFetchTimeout)
defer cancel()
poll := func() (*csr.Certificate, error) {
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(h.csrFetchInterval):
cert, err := csr.GetCertificateByNodeName(ctx, h.clientset, nodeName)
if err != nil && !errors.Is(err, csr.ErrNodeCertificateNotFound) {
return nil, err
}
if cert != nil {
return cert, nil
}
}
}
}
var cert *csr.Certificate
var err error
b := waitext.DefaultExponentialBackoff()
err = waitext.Retry(
ctx,
b,
3,
func(ctx context.Context) (bool, error) {
cert, err = poll()
if errors.Is(err, context.DeadlineExceeded) {
return false, err
}
return true, err
},
func(err error) {
log.Warnf("getting initial csr, will retry: %v", err)
},
)
return cert, err
}
func newApproveCSRExponentialBackoff() wait.Backoff {
b := waitext.DefaultExponentialBackoff()
b.Factor = 2
return b
}