-
Notifications
You must be signed in to change notification settings - Fork 46
/
certificaterequest.go
134 lines (109 loc) · 4.13 KB
/
certificaterequest.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
/*
Copyright 2021 The cert-manager 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.
*/
package helper
import (
"context"
"fmt"
"time"
apiutil "github.com/cert-manager/cert-manager/pkg/api/util"
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/cert-manager/csi-driver/test/e2e/framework/log"
"github.com/cert-manager/csi-driver/test/e2e/util"
)
// WaitForCertificateRequestReady waits for the CertificateRequest resources to
// enter a Ready state.
func (h *Helper) WaitForCertificateRequestsReady(ctx context.Context, pod *corev1.Pod, timeout time.Duration) ([]*cmapi.CertificateRequest, error) {
var crs []*cmapi.CertificateRequest
err := wait.PollUntilContextTimeout(ctx, time.Second/4, timeout, true, func(ctx context.Context) (bool, error) {
crList, err := h.CMClient.CertmanagerV1().CertificateRequests(pod.Namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return false, err
}
crs, err = h.findCertificateRequests(crList.Items, pod.UID)
if err != nil {
log.Logf("Cannot find CertificateRequests for pod, waiting...")
return false, nil // nolint:nilerr // We want to ignore this error and wait for the CRs to be created
}
for _, cr := range crs {
isReady := apiutil.CertificateRequestHasCondition(cr, cmapi.CertificateRequestCondition{
Type: cmapi.CertificateRequestConditionReady,
Status: cmmeta.ConditionTrue,
})
if !isReady {
log.Logf("Expected CertificateRequest for Pod %s/%s to have Ready condition 'true' but it has: %v",
pod.Namespace, pod.Name, cr.Status.Conditions)
return false, nil
}
}
return true, nil
})
if err != nil {
return nil, err
}
return crs, nil
}
func (h *Helper) FindCertificateRequestsReady(crs []cmapi.CertificateRequest, pod *corev1.Pod) ([]*cmapi.CertificateRequest, error) {
podCRs, err := h.findCertificateRequests(crs, pod.GetUID())
if err != nil {
return nil, err
}
for _, cr := range podCRs {
if !util.CertificateRequestReady(cr) {
return nil, fmt.Errorf("CertificateRequest not ready: %+v", cr)
}
}
return podCRs, nil
}
func (h *Helper) WaitForCertificateRequestDeletion(ctx context.Context, namespace, name string, timeout time.Duration) error {
log.Logf("Waiting for CertificateRequest to be deleted %s/%s", namespace, name)
err := wait.PollUntilContextTimeout(ctx, time.Second/2, timeout, true, func(ctx context.Context) (bool, error) {
cr, err := h.CMClient.CertmanagerV1().CertificateRequests(namespace).Get(ctx, name, metav1.GetOptions{})
if k8sErrors.IsNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}
log.Logf("helper: CertificateRequest not deleted %s/%s: %v",
cr.Namespace, cr.Name, cr.Status.Conditions)
return false, nil
})
if err != nil {
if err := h.Kubectl(namespace).DescribeResource("certificaterequest", name); err != nil {
log.Logf("helper: failed to describe CertificateRequest %s/%s: %v", namespace, name, err)
}
return err
}
return nil
}
func (h *Helper) findCertificateRequests(crs []cmapi.CertificateRequest, podUID types.UID) ([]*cmapi.CertificateRequest, error) {
var podCRs []*cmapi.CertificateRequest
for i, cr := range crs {
if len(cr.OwnerReferences) == 0 {
continue
}
if cr.OwnerReferences[0].UID == podUID {
podCRs = append(podCRs, &crs[i])
}
}
if len(podCRs) == 0 {
return nil, fmt.Errorf("failed to find CertificateRequest owned by pod with UID %q", podUID)
}
return podCRs, nil
}