-
Notifications
You must be signed in to change notification settings - Fork 27
/
certificates.go
60 lines (51 loc) · 2.04 KB
/
certificates.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
package helpers
import (
"fmt"
"text/template"
"time"
"github.com/gruntwork-io/terratest/modules/k8s"
. "github.com/onsi/ginkgo/v2"
"github.com/pkg/errors"
)
// CreateCertificate creates a certificate resource in the specified namespace.
func CreateCertificate(namespace, host string, options *k8s.KubectlOptions) error {
tpl, err := TemplateFile("./fixtures/certificate.yaml.tmpl", "certificate.yaml.tmpl", template.FuncMap{
"certname": namespace,
"namespace": namespace,
"hostname": host,
"environment": "staging",
})
if err != nil {
return err
}
err = k8s.KubectlApplyFromStringE(GinkgoT(), options, tpl)
if err != nil {
return err
}
// Wait for the certificate 20 times, with a 10 second sleep between each check
err = WaitForCertificateToBeReady(namespace, options, 20, 10)
if err != nil {
return err
}
return nil
}
// WaitForCertificateToBeReady takes a certificate name and KubectlOptions arguments along with an along
// with an appropriate number of retries. It will wait until the certificate returns a status of Ready.
// The time between retries is specified by the retryInterval argument.
// If the certificate does not return a status of Ready after the specified number of retries, an error is returned.
func WaitForCertificateToBeReady(certName string, options *k8s.KubectlOptions, retries, retryInterval int) error {
fmt.Printf("Waiting for certificate %s to be ready %v times\n", certName, retries)
for i := 0; i < retries; i++ {
fmt.Println("Checking certificate status: attempt: " + fmt.Sprintf("%v", i+1))
status, err := k8s.RunKubectlAndGetOutputE(GinkgoT(), options, "get", "certificate", certName, "-o", "jsonpath='{.status.conditions[?(@.type==\"Ready\")].status}'")
if err != nil {
return errors.New("Certificate creation failed")
}
if status == "'True'" {
return nil
}
fmt.Printf("Failed to validate certificate %s, sleeping for %v seconds\n", certName, retryInterval)
time.Sleep(time.Duration(retryInterval) * time.Second)
}
return fmt.Errorf("certificate %s is not ready", certName)
}