Skip to content

Commit

Permalink
Merge pull request #2568 from GoogleContainerTools/gke_load_balancer_svc
Browse files Browse the repository at this point in the history
Bring back applying labels to services deployed with helm
  • Loading branch information
balopat authored Jul 30, 2019
2 parents f3d560d + cbc124c commit 54d1a23
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 18 deletions.
95 changes: 84 additions & 11 deletions integration/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package integration

import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -106,17 +107,7 @@ func TestDevAPITriggers(t *testing.T) {
client, shutdown := setupRPCClient(t, rpcAddr)
defer shutdown()

// read the event log stream from the skaffold grpc server
var stream proto.SkaffoldService_EventLogClient
var err error
for i := 0; i < readRetries; i++ {
stream, err = client.EventLog(context.Background())
if err != nil {
t.Logf("waiting for connection...")
time.Sleep(waitTime)
continue
}
}
stream, err := readEventAPIStream(client, t, readRetries)
if stream == nil {
t.Fatalf("error retrieving event log: %v\n", err)
}
Expand Down Expand Up @@ -235,6 +226,72 @@ func TestDevPortForward(t *testing.T) {
testutil.CheckError(t, false, err)
}

func TestDevPortForwardGKELoadBalancer(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
if !ShouldRunGCPOnlyTests() {
t.Skip("skipping test that is not gcp only")
}

// Run skaffold build first to fail quickly on a build failure
skaffold.Build().InDir("testdata/gke_loadbalancer").RunOrFail(t)

ns, _, deleteNs := SetupNamespace(t)
defer deleteNs()

rpcAddr := randomPort()
env := []string{fmt.Sprintf("TEST_NS=%s", ns.Name)}
cmd := skaffold.Dev("--port-forward", "--rpc-port", rpcAddr).InDir("testdata/gke_loadbalancer").InNs(ns.Name).WithEnv(env)
stop := cmd.RunBackground(t)
defer stop()

client, shutdown := setupRPCClient(t, rpcAddr)
defer shutdown()

// create a grpc connection. Increase number of reties for helm.
stream, err := readEventAPIStream(client, t, 20)
if stream == nil {
t.Fatalf("error retrieving event log: %v\n", err)
}

// read entries from the log
entries := make(chan *proto.LogEntry)
go func() {
for {
entry, _ := stream.Recv()
if entry != nil {
entries <- entry
}
}
}()

body := []byte{}
err = wait.PollImmediate(time.Millisecond*500, 5*time.Minute, func() (bool, error) {
e := <-entries
switch e.Event.GetEventType().(type) {
case *proto.Event_PortEvent:
if e.Event.GetPortEvent().ResourceName == "gke-loadbalancer" &&
e.Event.GetPortEvent().ResourceType == "service" {
port := e.Event.GetPortEvent().LocalPort
t.Logf("Detected service/gke-loadbalancer is forwarded to port %d", port)
resp, err := http.Get(fmt.Sprintf("http://localhost:%d", port))
if err != nil {
t.Errorf("could not get service/gke-loadbalancer due to %s", err)
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
return true, err
}
return false, nil
default:
return false, nil
}
})

testutil.CheckErrorAndDeepEqual(t, false, err, string(body), "hello!!\n")
}

func replaceInFile(target, replacement, filepath string) ([]byte, os.FileMode, error) {
fInfo, err := os.Stat(filepath)
if err != nil {
Expand All @@ -251,3 +308,19 @@ func replaceInFile(target, replacement, filepath string) ([]byte, os.FileMode, e

return original, fInfo.Mode(), err
}

func readEventAPIStream(client proto.SkaffoldServiceClient, t *testing.T, retries int) (proto.SkaffoldService_EventLogClient, error) {
t.Helper()
// read the event log stream from the skaffold grpc server
var stream proto.SkaffoldService_EventLogClient
var err error
for i := 0; i < retries; i++ {
stream, err = client.EventLog(context.Background())
if err != nil {
t.Logf("waiting for connection...")
time.Sleep(waitTime)
continue
}
}
return stream, err
}
7 changes: 7 additions & 0 deletions integration/testdata/gke_loadbalancer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:1.12.6-alpine3.9 as builder
COPY main.go .
RUN go build -o /main .

FROM alpine:3.9
CMD ["./main"]
COPY --from=builder /main .
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
description: A Helm chart for Kubernetes
name: loadbalancer-helm
version: 0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: gke-loadbalancer
labels:
app: gke-loadbalancer
spec:
replicas: 1
selector:
matchLabels:
app: gke-loadbalancer
template:
metadata:
labels:
app: gke-loadbalancer
spec:
containers:
- name: gke-container
image: {{ .Values.image }}
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: gke-loadbalancer
labels:
app: gke-loadbalancer
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 3000
protocol: TCP
name: http
selector:
app: "gke-loadbalancer"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
image: gcr.io/k8s-skaffold/gke-loadbalancer


17 changes: 17 additions & 0 deletions integration/testdata/gke_loadbalancer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"log"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello!!\n")
}

func main() {
log.Print("gke loadbalancer server ready")
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}
14 changes: 14 additions & 0 deletions integration/testdata/gke_loadbalancer/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: skaffold/v1beta13
kind: Config
build:
artifacts:
- image: gcr.io/k8s-skaffold/gke-loadbalancer
context: ./
deploy:
helm:
releases:
# seed test namespace in the release name.
- name: skaffold-helm-{{.TEST_NS}}
chartPath: loadbalancer-helm
values:
image: gcr.io/k8s-skaffold/gke-loadbalancer
7 changes: 0 additions & 7 deletions pkg/skaffold/deploy/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package deploy
import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
Expand Down Expand Up @@ -111,12 +110,6 @@ func updateRuntimeObject(client dynamic.Interface, disco discovery.DiscoveryInte
}
name := accessor.GetName()

kind := modifiedObj.GetObjectKind().GroupVersionKind().Kind
if strings.EqualFold(kind, "Service") {
logrus.Debugf("Labels are not applied to service [%s] because of issue: https://github.com/GoogleContainerTools/skaffold/issues/887", name)
return nil
}

addLabels(labels, accessor)

modifiedJSON, _ := json.Marshal(modifiedObj)
Expand Down

0 comments on commit 54d1a23

Please sign in to comment.