forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crd_checks.go
210 lines (184 loc) · 7.66 KB
/
crd_checks.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
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// crdpolling contains functions which poll Knative Serving CRDs until they
// get into the state desired by the caller or time out.
package test
import (
"context"
"fmt"
"time"
pkgTest "github.com/knative/pkg/test"
"github.com/knative/pkg/test/logging"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
k8styped "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
interval = 1 * time.Second
timeout = 10 * time.Minute
)
// WaitForRouteState polls the status of the Route called name from client every
// interval until inState returns `true` indicating it is done, returns an
// error or timeout. desc will be used to name the metric that is emitted to
// track how long it took for name to get into the state checked by inState.
func WaitForRouteState(client *ServingClients, name string, inState func(r *v1alpha1.Route) (bool, error), desc string) error {
span := logging.GetEmitableSpan(context.Background(), fmt.Sprintf("WaitForRouteState/%s/%s", name, desc))
defer span.End()
var lastState *v1alpha1.Route
waitErr := wait.PollImmediate(interval, timeout, func() (bool, error) {
var err error
lastState, err = client.Routes.Get(name, metav1.GetOptions{})
if err != nil {
return true, err
}
return inState(lastState)
})
if waitErr != nil {
return errors.Wrapf(waitErr, "route %q is not in desired state, got: %+v", name, lastState)
}
return nil
}
// CheckRouteState verifies the status of the Route called name from client
// is in a particular state by calling `inState` and expecting `true`.
// This is the non-polling variety of WaitForRouteState
func CheckRouteState(client *ServingClients, name string, inState func(r *v1alpha1.Route) (bool, error)) error {
r, err := client.Routes.Get(name, metav1.GetOptions{})
if err != nil {
return err
}
if done, err := inState(r); err != nil {
return err
} else if !done {
return fmt.Errorf("route %q is not in desired state, got: %+v", name, r)
}
return nil
}
// WaitForConfigurationState polls the status of the Configuration called name
// from client every interval until inState returns `true` indicating it
// is done, returns an error or timeout. desc will be used to name the metric
// that is emitted to track how long it took for name to get into the state checked by inState.
func WaitForConfigurationState(client *ServingClients, name string, inState func(c *v1alpha1.Configuration) (bool, error), desc string) error {
span := logging.GetEmitableSpan(context.Background(), fmt.Sprintf("WaitForConfigurationState/%s/%s", name, desc))
defer span.End()
var lastState *v1alpha1.Configuration
waitErr := wait.PollImmediate(interval, timeout, func() (bool, error) {
var err error
lastState, err = client.Configs.Get(name, metav1.GetOptions{})
if err != nil {
return true, err
}
return inState(lastState)
})
if waitErr != nil {
return errors.Wrapf(waitErr, "configuration %q is not in desired state, got: %+v", name, lastState)
}
return nil
}
// CheckConfigurationState verifies the status of the Configuration called name from client
// is in a particular state by calling `inState` and expecting `true`.
// This is the non-polling variety of WaitForConfigurationState
func CheckConfigurationState(client *ServingClients, name string, inState func(r *v1alpha1.Configuration) (bool, error)) error {
c, err := client.Configs.Get(name, metav1.GetOptions{})
if err != nil {
return err
}
if done, err := inState(c); err != nil {
return err
} else if !done {
return fmt.Errorf("configuration %q is not in desired state, got: %+v", name, c)
}
return nil
}
// WaitForRevisionState polls the status of the Revision called name
// from client every `interval` until `inState` returns `true` indicating it
// is done, returns an error or timeout. desc will be used to name the metric
// that is emitted to track how long it took for name to get into the state checked by inState.
func WaitForRevisionState(client *ServingClients, name string, inState func(r *v1alpha1.Revision) (bool, error), desc string) error {
span := logging.GetEmitableSpan(context.Background(), fmt.Sprintf("WaitForRevision/%s/%s", name, desc))
defer span.End()
var lastState *v1alpha1.Revision
waitErr := wait.PollImmediate(interval, timeout, func() (bool, error) {
var err error
lastState, err = client.Revisions.Get(name, metav1.GetOptions{})
if err != nil {
return true, err
}
return inState(lastState)
})
if waitErr != nil {
return errors.Wrapf(waitErr, "revision %q is not in desired state, got: %+v", name, lastState)
}
return nil
}
// CheckRevisionState verifies the status of the Revision called name from client
// is in a particular state by calling `inState` and expecting `true`.
// This is the non-polling variety of WaitForRevisionState
func CheckRevisionState(client *ServingClients, name string, inState func(r *v1alpha1.Revision) (bool, error)) error {
r, err := client.Revisions.Get(name, metav1.GetOptions{})
if err != nil {
return err
}
if done, err := inState(r); err != nil {
return err
} else if !done {
return fmt.Errorf("revision %q is not in desired state, got: %+v", name, r)
}
return nil
}
// WaitForServiceState polls the status of the Service called name
// from client every `interval` until `inState` returns `true` indicating it
// is done, returns an error or timeout. desc will be used to name the metric
// that is emitted to track how long it took for name to get into the state checked by inState.
func WaitForServiceState(client *ServingClients, name string, inState func(s *v1alpha1.Service) (bool, error), desc string) error {
span := logging.GetEmitableSpan(context.Background(), fmt.Sprintf("WaitForServiceState/%s/%s", name, desc))
defer span.End()
var lastState *v1alpha1.Service
waitErr := wait.PollImmediate(interval, timeout, func() (bool, error) {
var err error
lastState, err = client.Services.Get(name, metav1.GetOptions{})
if err != nil {
return true, err
}
return inState(lastState)
})
if waitErr != nil {
return errors.Wrapf(waitErr, "service %q is not in desired state, got: %+v", name, lastState)
}
return nil
}
// CheckServiceState verifies the status of the Service called name from client
// is in a particular state by calling `inState` and expecting `true`.
// This is the non-polling variety of WaitForServiceState.
func CheckServiceState(client *ServingClients, name string, inState func(s *v1alpha1.Service) (bool, error)) error {
s, err := client.Services.Get(name, metav1.GetOptions{})
if err != nil {
return err
}
if done, err := inState(s); err != nil {
return err
} else if !done {
return fmt.Errorf("service %q is not in desired state, got: %+v", name, s)
}
return nil
}
// GetConfigMap gets the knative serving config map.
func GetConfigMap(client *pkgTest.KubeClient) k8styped.ConfigMapInterface {
return client.Kube.CoreV1().ConfigMaps("knative-serving")
}
// DeploymentScaledToZeroFunc returns a func that evaluates if a deployment has scaled to 0 pods.
func DeploymentScaledToZeroFunc(d *appsv1.Deployment) (bool, error) {
return d.Status.ReadyReplicas == 0, nil
}