-
Notifications
You must be signed in to change notification settings - Fork 788
/
certificate.go
199 lines (184 loc) · 6.44 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
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
package pki
import (
"context"
"fmt"
"github.com/jenkins-x/jx/pkg/util"
"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"
"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"
)
// CertSecretPrefix used as prefix for all certificate object names
const CertSecretPrefix = "tls-"
// 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
}
// 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.CertmanagerV1alpha1().Certificates(ns).Get(name, metav1.GetOptions{})
if err != nil {
return false, nil
}
isReady := cert.HasCondition(certmng.CertificateCondition{
Type: certmng.CertificateConditionReady,
Status: certmng.ConditionTrue,
})
if !isReady {
return false, nil
}
logrus.Infof("Ready Cert: %s\n", util.ColorInfo(name))
return true, nil
})
if err != nil {
return errors.Wrapf(err, "waiting for certificate %q to be ready in namespace %q.", name, ns)
}
return nil
}
// WaitCertificateExists waits until the timeout for the certificate with the provided name to be available in the certificates list
func WaitCertificateExists(client certclient.Interface, name string, ns string, timeout time.Duration) error {
err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
_, err := client.CertmanagerV1alpha1().Certificates(ns).Get(name, metav1.GetOptions{})
if err != nil {
return false, nil
}
return true, nil
})
if err != nil {
return errors.Wrapf(err, "waiting for certificate %q to be created in namespace %q.", name, ns)
}
return nil
}
// CleanAllCerts removes all certs and their associated secrets which hold a TLS certificated issued by cert-manager
func CleanAllCerts(client kubernetes.Interface, certclient certclient.Interface, ns string) error {
return cleanCerts(client, certclient, ns, func(cert string) bool {
return strings.HasPrefix(cert, CertSecretPrefix)
})
}
// CleanCerts removes the certs and their associated secrets which hold a TLS certificate issued by cert-manager
func CleanCerts(client kubernetes.Interface, certclient certclient.Interface, ns string, filter []Certificate) error {
allowed := make(map[string]bool)
for _, cert := range filter {
allowed[cert.Name] = true
}
return cleanCerts(client, certclient, ns, func(cert string) bool {
_, ok := allowed[cert]
return ok
})
}
func cleanCerts(client kubernetes.Interface, certclient certclient.Interface, ns string, allow func(string) bool) error {
certsClient := certclient.Certmanager().Certificates(ns)
certsList, err := certsClient.List(metav1.ListOptions{})
if err != nil {
// there are no certificates to clean
return nil
}
for _, c := range certsList.Items {
if allow(c.GetName()) {
err := certsClient.Delete(c.GetName(), &metav1.DeleteOptions{})
if err != nil {
return errors.Wrapf(err, "deleting the cert %s/%s", ns, c.GetName())
}
}
}
// 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 errors.Wrapf(err, "listing the secrets in namespace %q", ns)
}
for _, s := range secrets.Items {
if allow(s.GetName()) {
err := client.CoreV1().Secrets(ns).Delete(s.Name, &metav1.DeleteOptions{})
if err != nil {
return errors.Wrapf(err, "deleteing the tls secret %s/%s", ns, s.GetName())
}
}
}
return nil
}
// 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 {
if isCertReady(cert) {
result := Certificate{
Name: cert.GetName(),
Namespace: cert.GetNamespace(),
}
results <- result
}
}
}
}
}
}()
return results, nil
}
func isCertReady(cert *certmng.Certificate) bool {
return cert.HasCondition(certmng.CertificateCondition{
Type: certmng.CertificateConditionReady,
Status: certmng.ConditionTrue,
})
}
// GetIssuedReadyCertificates returns the current ready certificates in the given namespace
func GetIssuedReadyCertificates(client certclient.Interface, ns string) ([]Certificate, error) {
certs := make([]Certificate, 0)
certsList, err := client.Certmanager().Certificates(ns).List(metav1.ListOptions{})
if err != nil {
return certs, errors.Wrapf(err, "listing certificates in namespace %q", ns)
}
for _, cert := range certsList.Items {
if isCertReady(&cert) {
certs = append(certs, Certificate{
Name: cert.GetName(),
Namespace: cert.GetNamespace(),
})
}
}
return certs, 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
}