forked from cert-manager/cert-manager
/
service.go
140 lines (122 loc) · 4.46 KB
/
service.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
/*
Copyright 2019 The Jetstack cert-manager contributors.
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 http
import (
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/intstr"
"github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
"k8s.io/klog"
)
func (s *Solver) ensureService(issuer v1alpha1.GenericIssuer, ch *v1alpha1.Challenge) (*corev1.Service, error) {
existingServices, err := s.getServicesForChallenge(ch)
if err != nil {
return nil, err
}
if len(existingServices) == 1 {
return existingServices[0], nil
}
if len(existingServices) > 1 {
errMsg := fmt.Sprintf("multiple challenge solver services found for certificate '%s/%s'. Cleaning up existing services.", ch.Namespace, ch.Name)
klog.Infof(errMsg)
err := s.cleanupServices(ch)
if err != nil {
return nil, err
}
return nil, fmt.Errorf(errMsg)
}
klog.Infof("No existing HTTP01 challenge solver service found for Certificate %q. One will be created.", ch.Namespace+"/"+ch.Name)
return s.createService(issuer, ch)
}
// getServicesForChallenge returns a list of services that were created to solve
// http challenges for the given domain
func (s *Solver) getServicesForChallenge(ch *v1alpha1.Challenge) ([]*corev1.Service, error) {
podLabels := podLabels(ch)
selector := labels.NewSelector()
for key, val := range podLabels {
req, err := labels.NewRequirement(key, selection.Equals, []string{val})
if err != nil {
return nil, err
}
selector = selector.Add(*req)
}
serviceList, err := s.serviceLister.Services(ch.Namespace).List(selector)
if err != nil {
return nil, err
}
var relevantServices []*corev1.Service
for _, service := range serviceList {
if !metav1.IsControlledBy(service, ch) {
klog.Infof("Found service %q with acme-order-url annotation set to that of Certificate %q"+
"but it is not owned by the Certificate resource, so skipping it.", service.Namespace+"/"+service.Name, ch.Namespace+"/"+ch.Name)
continue
}
relevantServices = append(relevantServices, service)
}
return relevantServices, nil
}
// createService will create the service required to solve this challenge
// in the target API server.
func (s *Solver) createService(issuer v1alpha1.GenericIssuer, ch *v1alpha1.Challenge) (*corev1.Service, error) {
return s.Client.CoreV1().Services(ch.Namespace).Create(buildService(issuer, ch))
}
func buildService(issuer v1alpha1.GenericIssuer, ch *v1alpha1.Challenge) *corev1.Service {
podLabels := podLabels(ch)
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "cm-acme-http-solver-",
Namespace: ch.Namespace,
Labels: podLabels,
Annotations: map[string]string{
"auth.istio.io/8089": "NONE",
},
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(ch, challengeGvk)},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "http",
Port: acmeSolverListenPort,
TargetPort: intstr.FromInt(acmeSolverListenPort),
},
},
Selector: podLabels,
},
}
// checking for presence of http01 config and if set serviceType is set, override our default (NodePort)
service.Spec.Type = corev1.ServiceTypeNodePort
if issuer.GetSpec().ACME.HTTP01 != nil && issuer.GetSpec().ACME.HTTP01.ServiceType != "" {
service.Spec.Type = issuer.GetSpec().ACME.HTTP01.ServiceType
}
return service
}
func (s *Solver) cleanupServices(ch *v1alpha1.Challenge) error {
services, err := s.getServicesForChallenge(ch)
if err != nil {
return err
}
var errs []error
for _, service := range services {
// TODO: should we call DeleteCollection here? We'd need to somehow
// also ensure ownership as part of that request using a FieldSelector.
err := s.Client.CoreV1().Services(service.Namespace).Delete(service.Name, nil)
if err != nil {
errs = append(errs, err)
}
}
return utilerrors.NewAggregate(errs)
}