Skip to content

Commit

Permalink
Fix service nodeport issue (naively), add an integration test to guar…
Browse files Browse the repository at this point in the history
…d it.

Add vendor to .gitignore to allow experimenting with upstream patches locally
  • Loading branch information
pepov committed Jun 5, 2019
1 parent 824d23e commit 237aca9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
vendor
2 changes: 1 addition & 1 deletion pod_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestIntegration_Pod(t *testing.T) {
for _, test := range tests {
existingPod, err := testContext.Client.CoreV1().Pods(test.pod.Namespace).Create(test.pod)
defer func() {
testContext.Client.CoreV1().Pods("default").Delete(test.pod.Name, &metav1.DeleteOptions{
testContext.Client.CoreV1().Pods(test.pod.Namespace).Delete(test.pod.Name, &metav1.DeleteOptions{
GracePeriodSeconds: new(int64),
})
}()
Expand Down
24 changes: 20 additions & 4 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package objectmatch

import (
"encoding/json"
"reflect"

"github.com/goph/emperror"
corev1 "k8s.io/api/core/v1"
Expand All @@ -35,15 +34,32 @@ func NewServiceMatcher(objectMatcher ObjectMatcher) *serviceMatcher {
}

// Match compares two corev1.Service objects
func (m serviceMatcher) Match(old, new *corev1.Service) (bool, error) {
func (m serviceMatcher) Match(oldOrig, newOrig *corev1.Service) (bool, error) {

old := oldOrig.DeepCopy()
new := newOrig.DeepCopy()

type Service struct {
ObjectMeta
Spec corev1.ServiceSpec
}

if !reflect.DeepEqual(m.objectMatcher.GetObjectMeta(old.ObjectMeta), m.objectMatcher.GetObjectMeta(new.ObjectMeta)) {
return false, nil
// NodePort is a generated value, avoid the diff by removing it
tmpPorts := []corev1.ServicePort{}
for _, port := range old.Spec.Ports {
if port.NodePort > 0 {
port.NodePort = 0
tmpPorts = append(tmpPorts, corev1.ServicePort{
Name: port.Name,
Protocol: port.Protocol,
Port: port.Port,
TargetPort: port.TargetPort,
})
} else {
tmpPorts = append(tmpPorts, port)
}
}
old.Spec.Ports = tmpPorts

oldData, err := json.Marshal(Service{
ObjectMeta: m.objectMatcher.GetObjectMeta(old.ObjectMeta),
Expand Down
68 changes: 68 additions & 0 deletions service_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package objectmatch

import (
"testing"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/klog/klogr"
)

func TestIntegration_Service(t *testing.T) {

if !*integration {
t.Skip()
}

tests := []struct{
service *v1.Service
}{
{
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service",
Namespace: testContext.Namespace,
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Name: "http",
Protocol: v1.ProtocolTCP,
Port: 80,
TargetPort: intstr.FromInt(80),
},
},
Selector: map[string]string{
"app": "test",
},
Type: v1.ServiceTypeLoadBalancer,
},
},
},
}

for _, test := range tests {

existingService, err := testContext.Client.CoreV1().Services(test.service.Namespace).Create(test.service)
defer func() {
testContext.Client.CoreV1().Pods(test.service.Namespace).Delete(test.service.Name, &metav1.DeleteOptions{
GracePeriodSeconds: new(int64),
})
}()

if err != nil {
t.Fatalf("Failed to create pod: %s", err.Error())
}


matched, err := NewServiceMatcher(New(klogr.New())).Match(existingService, test.service)
if err != nil {
t.Fatalf("Failed to match objects: %s", err)
}

if !matched {
t.Fatalf("Objects did not match")
}
}
}

0 comments on commit 237aca9

Please sign in to comment.