slice mirroring controller mirror annotations

Add support to the endpoint slice mirroring controller to mirror
annotations, in addition to labels, but don´t mirror endpoint
triggertime annotation.

Also, fix a bug in the endpointslice mirroring controller, that
wasn't updating the mirrored slice with the new labels, in case
that only the endpoint labels were modified.
This commit is contained in:
Antonio Ojea
2021-01-16 19:30:48 +01:00
committed by Antonio Ojea
parent 7c702138f9
commit c421e22e2c
5 changed files with 462 additions and 25 deletions

View File

@@ -69,6 +69,7 @@ func newEndpointSlice(endpoints *corev1.Endpoints, ports []discovery.EndpointPor
epSlice := &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{},
Annotations: map[string]string{},
OwnerReferences: []metav1.OwnerReference{*ownerRef},
Namespace: endpoints.Namespace,
},
@@ -77,13 +78,23 @@ func newEndpointSlice(endpoints *corev1.Endpoints, ports []discovery.EndpointPor
Endpoints: []discovery.Endpoint{},
}
// clone all labels
for label, val := range endpoints.Labels {
epSlice.Labels[label] = val
}
// overwrite specific labels
epSlice.Labels[discovery.LabelServiceName] = endpoints.Name
epSlice.Labels[discovery.LabelManagedBy] = controllerName
// clone all annotations but EndpointsLastChangeTriggerTime
for annotation, val := range endpoints.Annotations {
if annotation == corev1.EndpointsLastChangeTriggerTime {
continue
}
epSlice.Annotations[annotation] = val
}
if sliceName == "" {
epSlice.GenerateName = getEndpointSlicePrefix(endpoints.Name)
} else {
@@ -228,3 +239,22 @@ func hasLeaderElection(annotations map[string]string) bool {
_, ok := annotations[resourcelock.LeaderElectionRecordAnnotationKey]
return ok
}
// cloneAndRemoveKeys is a copy of CloneAndRemoveLabels
// it is used here for annotations and labels
func cloneAndRemoveKeys(a map[string]string, keys ...string) map[string]string {
if len(keys) == 0 {
// Don't need to remove a key.
return a
}
// Clone.
newMap := map[string]string{}
for k, v := range a {
newMap[k] = v
}
// remove keys
for _, key := range keys {
delete(newMap, key)
}
return newMap
}