-
Notifications
You must be signed in to change notification settings - Fork 785
/
certificate.go
129 lines (119 loc) · 3.94 KB
/
certificate.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
package pki
import (
"context"
"fmt"
"strings"
"time"
kubeservices "github.com/jenkins-x/jx/pkg/kube/services"
certmng "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
certclient "github.com/jetstack/cert-manager/pkg/client/clientset/versioned"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)
const certSecretPrefix = "tls-"
// WaitCertificateIssuedReady wait for a certificate issued by cert-manager until is ready or the timeout is reached
func WaitCertificateIssuedReady(client certclient.Interface, name string, ns string, timeout time.Duration) error {
err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
cert, err := client.Certmanager().Certificates(ns).Get(name, metav1.GetOptions{})
if err != nil {
logrus.Warnf("Failed getting certificate %q: %v", name, err)
return false, nil
}
isReady := cert.HasCondition(certmng.CertificateCondition{
Type: certmng.CertificateConditionReady,
Status: certmng.ConditionTrue,
})
if !isReady {
return false, nil
}
return true, nil
})
if err != nil {
return errors.Wrapf(err, "waiting for certificate %q to be ready in namespace %q", name, ns)
}
return nil
}
// CleanCertSecrets removes all secrets which hold a TLS certificate issued by cert-manager
func CleanCertSecrets(client kubernetes.Interface, ns string) error {
// delete the tls related secrets so we dont reuse old ones when switching from http to https
secrets, err := client.CoreV1().Secrets(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, s := range secrets.Items {
if strings.HasPrefix(s.Name, certSecretPrefix) {
err := client.CoreV1().Secrets(ns).Delete(s.Name, &metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete tls secret %s: %v", s.Name, err)
}
}
}
return nil
}
// Certificate keeps some information related to a certificate issued by cert-manager
type Certificate struct {
// Name certificate name
Name string
//Namespace certificate namespace
Namespace string
}
// String returns the certificate information in a string format
func (c Certificate) String() string {
return fmt.Sprintf("%s/%s", c.Namespace, c.Name)
}
// WatchCertificatesIssuedReady starts watching for ready certificate in the given namespace.
// If the namespace is empty, it will watch the entire cluster. The caller can stop watching by cancelling the context.
func WatchCertificatesIssuedReady(ctx context.Context, client certclient.Interface, ns string) (<-chan Certificate, error) {
watcher, err := client.Certmanager().Certificates(ns).Watch(metav1.ListOptions{})
if err != nil {
return nil, errors.Wrapf(err, "watching certificates in namespace %q", ns)
}
results := make(chan Certificate)
go func() {
for {
select {
case <-ctx.Done():
watcher.Stop()
return
case e := <-watcher.ResultChan():
if e.Type == watch.Added || e.Type == watch.Modified {
cert, ok := e.Object.(*certmng.Certificate)
if ok {
isReady := cert.HasCondition(certmng.CertificateCondition{
Type: certmng.CertificateConditionReady,
Status: certmng.ConditionTrue,
})
if isReady {
result := Certificate{
Name: cert.GetName(),
Namespace: cert.GetNamespace(),
}
results <- result
}
}
}
}
}
}()
return results, nil
}
// ToCertificates converts a list of services into a list of certificates. The certificate name is built from
// the application label of the service.
func ToCertificates(services []*v1.Service) []Certificate {
result := make([]Certificate, 0)
for _, svc := range services {
app := kubeservices.ServiceAppName(svc)
cert := certSecretPrefix + app
ns := svc.GetNamespace()
result = append(result, Certificate{
Name: cert,
Namespace: ns,
})
}
return result
}