-
Notifications
You must be signed in to change notification settings - Fork 181
/
internal_service.go
179 lines (152 loc) · 5.83 KB
/
internal_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
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
package ingress
import (
"context"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/cluster-ingress-operator/pkg/manifests"
"github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
// Annotation used to inform the certificate generation service to
// generate a cluster-signed certificate and populate the secret.
ServingCertSecretAnnotation = "service.alpha.openshift.io/serving-cert-secret-name"
)
// ensureInternalRouterServiceForIngress ensures that an internal service exists
// for a given IngressController. Returns a Boolean indicating whether the
// service exists, the current service if it does exist, and an error value.
func (r *reconciler) ensureInternalIngressControllerService(ic *operatorv1.IngressController, deploymentRef metav1.OwnerReference) (bool, *corev1.Service, error) {
desired := desiredInternalIngressControllerService(ic, deploymentRef)
have, current, err := r.currentInternalIngressControllerService(ic)
if err != nil {
return false, nil, err
}
switch {
case !have:
if err := r.client.Create(context.TODO(), desired); err != nil {
return false, nil, fmt.Errorf("failed to create internal ingresscontroller service: %w", err)
}
log.Info("created internal ingresscontroller service", "service", desired)
return r.currentInternalIngressControllerService(ic)
case have:
if updated, err := r.updateInternalService(current, desired); err != nil {
return true, current, fmt.Errorf("failed to update internal service: %v", err)
} else if updated {
return r.currentInternalIngressControllerService(ic)
}
}
return true, current, nil
}
func (r *reconciler) currentInternalIngressControllerService(ic *operatorv1.IngressController) (bool, *corev1.Service, error) {
current := &corev1.Service{}
err := r.client.Get(context.TODO(), controller.InternalIngressControllerServiceName(ic), current)
if err != nil {
if errors.IsNotFound(err) {
return false, nil, nil
}
return false, nil, err
}
return true, current, nil
}
func desiredInternalIngressControllerService(ic *operatorv1.IngressController, deploymentRef metav1.OwnerReference) *corev1.Service {
s := manifests.InternalIngressControllerService()
name := controller.InternalIngressControllerServiceName(ic)
s.Namespace = name.Namespace
s.Name = name.Name
s.Labels = map[string]string{
manifests.OwningIngressControllerLabel: ic.Name,
}
s.Annotations = map[string]string{
// TODO: remove hard-coded name
ServingCertSecretAnnotation: fmt.Sprintf("router-metrics-certs-%s", ic.Name),
}
s.Spec.Selector = controller.IngressControllerDeploymentPodSelector(ic).MatchLabels
s.SetOwnerReferences([]metav1.OwnerReference{deploymentRef})
return s
}
// updateInternalService updates a ClusterIP service. Returns a Boolean
// indicating whether the service was updated, and an error value.
func (r *reconciler) updateInternalService(current, desired *corev1.Service) (bool, error) {
changed, updated := internalServiceChanged(current, desired)
if !changed {
return false, nil
}
// Diff before updating because the client may mutate the object.
diff := cmp.Diff(current, updated, cmpopts.EquateEmpty())
if err := r.client.Update(context.TODO(), updated); err != nil {
return false, err
}
log.Info("updated internal service", "namespace", updated.Namespace, "name", updated.Name, "diff", diff)
return true, nil
}
// managedInternalServiceAnnotations is a set of annotation keys for annotations
// that the operator manages for its internal service.
var managedInternalServiceAnnotations = sets.NewString(
ServingCertSecretAnnotation,
)
// internalServiceChanged checks if the current internal service annotations and
// spec match the expected annotations and spec and if not returns an updated
// service.
func internalServiceChanged(current, expected *corev1.Service) (bool, *corev1.Service) {
changed := false
serviceCmpOpts := []cmp.Option{
// Ignore fields that the API, other controllers, or user may
// have modified.
cmpopts.IgnoreFields(corev1.ServicePort{}, "NodePort"),
cmpopts.IgnoreFields(corev1.ServiceSpec{},
"ClusterIP", "ClusterIPs",
"ExternalIPs",
"HealthCheckNodePort",
"IPFamilies", "IPFamilyPolicy",
),
cmp.Comparer(cmpServiceAffinity),
cmpopts.EquateEmpty(),
}
if !cmp.Equal(current.Spec, expected.Spec, serviceCmpOpts...) {
changed = true
}
annotationCmpOpts := []cmp.Option{
cmpopts.IgnoreMapEntries(func(k, _ string) bool {
return !managedInternalServiceAnnotations.Has(k)
}),
cmpopts.EquateEmpty(),
}
if !cmp.Equal(current.Annotations, expected.Annotations, annotationCmpOpts...) {
changed = true
}
if !changed {
return false, nil
}
updated := current.DeepCopy()
updated.Spec = expected.Spec
if updated.Annotations == nil {
updated.Annotations = map[string]string{}
}
for annotation := range managedInternalServiceAnnotations {
currentVal, have := current.Annotations[annotation]
expectedVal, want := expected.Annotations[annotation]
if want && (!have || currentVal != expectedVal) {
updated.Annotations[annotation] = expected.Annotations[annotation]
} else if have && !want {
delete(updated.Annotations, annotation)
}
}
// Preserve fields that the API, other controllers, or user may have
// modified.
updated.Spec.ClusterIP = current.Spec.ClusterIP
updated.Spec.ExternalIPs = current.Spec.ExternalIPs
updated.Spec.HealthCheckNodePort = current.Spec.HealthCheckNodePort
for i, updatedPort := range updated.Spec.Ports {
for _, currentPort := range current.Spec.Ports {
if currentPort.Name == updatedPort.Name {
updated.Spec.Ports[i].TargetPort = currentPort.TargetPort
}
}
}
return true, updated
}