Skip to content

Commit f268b6d

Browse files
committed
Add support to EnsureServicePorts
1 parent b9a3ffa commit f268b6d

File tree

3 files changed

+436
-0
lines changed

3 files changed

+436
-0
lines changed

lib/resourceapply/core.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ func ApplyService(client coreclientv1.ServicesGetter, required *corev1.Service)
5555

5656
modified := pointer.BoolPtr(false)
5757
resourcemerge.EnsureObjectMeta(modified, &existing.ObjectMeta, required.ObjectMeta)
58+
resourcemerge.EnsureServicePorts(modified, &existing.Spec.Ports, required.Spec.Ports)
5859
selectorSame := equality.Semantic.DeepEqual(existing.Spec.Selector, required.Spec.Selector)
5960
typeSame := equality.Semantic.DeepEqual(existing.Spec.Type, required.Spec.Type)
61+
6062
if selectorSame && typeSame && !*modified {
6163
return nil, false, nil
6264
}

lib/resourcemerge/core.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,51 @@ func ensureContainerPort(modified *bool, existing *corev1.ContainerPort, require
202202
}
203203
}
204204

205+
func EnsureServicePorts(modified *bool, existing *[]corev1.ServicePort, required []corev1.ServicePort) {
206+
for i, existingServicePort := range *existing {
207+
var existingCurr *corev1.ServicePort
208+
for _, requiredServicePort := range required {
209+
if existingServicePort.Name == requiredServicePort.Name {
210+
existingCurr = &(*existing)[i]
211+
ensureServicePort(modified, existingCurr, requiredServicePort)
212+
break
213+
}
214+
}
215+
if existingCurr == nil {
216+
*modified = true
217+
*existing = append((*existing)[:i], (*existing)[i+1:]...)
218+
}
219+
}
220+
221+
for _, requiredServicePort := range required {
222+
match := false
223+
for _, existingServicePort := range *existing {
224+
if existingServicePort.Name == requiredServicePort.Name {
225+
match = true
226+
break
227+
}
228+
}
229+
if !match {
230+
*modified = true
231+
*existing = append(*existing, requiredServicePort)
232+
}
233+
}
234+
}
235+
236+
func ensureServicePort(modified *bool, existing *corev1.ServicePort, required corev1.ServicePort) {
237+
ensureServicePortDefaults(&required)
238+
if !equality.Semantic.DeepEqual(required, *existing) {
239+
*modified = true
240+
*existing = required
241+
}
242+
}
243+
244+
func ensureServicePortDefaults(servicePort *corev1.ServicePort) {
245+
if servicePort.Protocol == "" {
246+
servicePort.Protocol = corev1.ProtocolTCP
247+
}
248+
}
249+
205250
func ensureVolumeMount(modified *bool, existing *corev1.VolumeMount, required corev1.VolumeMount) {
206251
if !equality.Semantic.DeepEqual(required, *existing) {
207252
*modified = true

0 commit comments

Comments
 (0)