Skip to content

Commit

Permalink
Support unnamed ports for annotation defaults
Browse files Browse the repository at this point in the history
Currently, a default port is only set if it has an associated name.
This uses the first Pod's ContainerPort directly even if it doesn't
have an associated name.

All of the documentation already supports this design.
  • Loading branch information
adilyse committed Nov 19, 2018
1 parent 0c6dede commit 2d7d0b1
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 1 deletion.
6 changes: 5 additions & 1 deletion connect-inject/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ func (h *Handler) defaultAnnotations(pod *corev1.Pod) error {
if _, ok := pod.ObjectMeta.Annotations[annotationPort]; !ok {
if cs := pod.Spec.Containers; len(cs) > 0 {
if ps := cs[0].Ports; len(ps) > 0 {
pod.ObjectMeta.Annotations[annotationPort] = ps[0].Name
if ps[0].Name != "" {
pod.ObjectMeta.Annotations[annotationPort] = ps[0].Name
} else {
pod.ObjectMeta.Annotations[annotationPort] = strconv.Itoa(int(ps[0].ContainerPort))
}
}
}
}
Expand Down
114 changes: 114 additions & 0 deletions connect-inject/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,33 @@ func TestHandlerDefaultAnnotations(t *testing.T) {
},
"",
},

{
"basic pod, with unnamed ports",
&corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
corev1.Container{
Name: "web",
Ports: []corev1.ContainerPort{
corev1.ContainerPort{
ContainerPort: 8080,
},
},
},

corev1.Container{
Name: "web-side",
},
},
},
},
map[string]string{
annotationService: "web",
annotationPort: "8080",
},
"",
},
}

for _, tt := range cases {
Expand All @@ -360,6 +387,93 @@ func TestHandlerDefaultAnnotations(t *testing.T) {
}
}

// Test portValue function
func TestHandlerPortValue(t *testing.T) {
cases := []struct {
Name string
Pod *corev1.Pod
Value string
Expected int32
Err string
}{
{
"empty",
&corev1.Pod{},
"",
0,
"strconv.ParseInt: parsing \"\": invalid syntax",
},

{
"basic pod, with ports",
&corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
corev1.Container{
Name: "web",
Ports: []corev1.ContainerPort{
corev1.ContainerPort{
Name: "http",
ContainerPort: 8080,
},
},
},

corev1.Container{
Name: "web-side",
},
},
},
},
"http",
int32(8080),
"",
},

{
"basic pod, with unnamed ports",
&corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
corev1.Container{
Name: "web",
Ports: []corev1.ContainerPort{
corev1.ContainerPort{
ContainerPort: 8080,
},
},
},

corev1.Container{
Name: "web-side",
},
},
},
},
"8080",
int32(8080),
"",
},
}

for _, tt := range cases {
t.Run(tt.Name, func(t *testing.T) {
require := require.New(t)

port, err := portValue(tt.Pod, tt.Value)
if (tt.Err != "") != (err != nil) {
t.Fatalf("actual: %v, expected err: %v", err, tt.Err)
}
if tt.Err != "" {
require.Contains(err.Error(), tt.Err)
return
}

require.Equal(port, tt.Expected)
})
}
}

// encodeRaw is a helper to encode some data into a RawExtension.
func encodeRaw(t *testing.T, input interface{}) runtime.RawExtension {
data, err := json.Marshal(input)
Expand Down

0 comments on commit 2d7d0b1

Please sign in to comment.