Skip to content

Commit

Permalink
Remove host ports from the update demo and update tests (take 2).
Browse files Browse the repository at this point in the history
  • Loading branch information
roberthbailey committed Mar 2, 2015
1 parent 445fd56 commit 5799dd5
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 49 deletions.
1 change: 1 addition & 0 deletions cluster/gce/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ function test-setup {
detect-project

# Open up port 80 & 8080 so common containers on minions can be reached
# TODO(roberthbailey): Remove this once we are no longer relying on hostPorts.
gcloud compute firewall-rules create \
--project "${PROJECT}" \
--target-tags "${MINION_TAG}" \
Expand Down
16 changes: 4 additions & 12 deletions examples/update-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,20 @@ $ cd kubernetes
$ hack/dev-build-and-up.sh
```

If you are running your cluster on GCE (the default), you may need to open the firewall for port 8080 using the [console](https://console.developer.google.com) or the `gcloud` tool. The following command will allow traffic from any source to instances tagged `kubernetes-minion`:

```bash
$ gcloud compute firewall-rules create \
--allow tcp:8080 --target-tags=kubernetes-minion \
kubernetes-minion-8080
```

### Step One: Turn up the UX for the demo

You can use bash job control to run this in the background. This can sometimes spew to the output so you could also run it in a different terminal.
You can use bash job control to run this in the background (note that you must use the default port -- 8001 -- for the following demonstration to work properly). This can sometimes spew to the output so you could also run it in a different terminal.

```
$ ./cluster/kubectl.sh proxy --www=local/ &
+ ./cluster/kubectl.sh proxy --www=local/
$ ./cluster/kubectl.sh proxy --www=examples/update-demo/local/ &
+ ./cluster/kubectl.sh proxy --www=examples/update-demo/local/
I0218 15:18:31.623279 67480 proxy.go:36] Starting to serve on localhost:8001
```

Now visit the the [demo website](http://localhost:8001/static). You won't see anything much quite yet.

### Step Two: Run the controller
Now we will turn up two replicas of an image. They all serve on port 8080, mapped to internal port 80
Now we will turn up two replicas of an image. They all serve on internal port 80.

```bash
$ ./cluster/kubectl.sh create -f examples/update-demo/nautilus-rc.yaml
Expand Down
3 changes: 1 addition & 2 deletions examples/update-demo/kitten-rc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ desiredState:
- name: update-demo
image: kubernetes/update-demo:kitten
ports:
- hostPort: 8080
containerPort: 80
- containerPort: 80
protocol: TCP
labels:
name: update-demo
Expand Down
8 changes: 8 additions & 0 deletions examples/update-demo/local/angular.min.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/update-demo/local/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
</head>
<body ng-controller="ButtonsCtrl">
<div ng-repeat="server in servers" class="pod">
<img src="http://{{server.ip}}:8080/{{server.image}}" height="100px" width="100px" />
<b>ID:</b> {{server.id}}<br>
<b>Host:</b> <a href="http://{{server.ip}}:8080/data.json">{{server.host}}</a><br>
<img src="http://localhost:8001/api/v1beta1/proxy/pods/{{server.podId}}/{{server.image}}" height="100px" width="100px" />
<b>ID:</b> {{server.podId}}<br>
<b>Host:</b> <a href="http://localhost:8001/api/v1beta1/proxy/pods/{{server.podId}}/data.json">{{server.host}}</a><br>
<b>Status:</b> {{server.status}}<br>
<b>Image:</b> {{server.dockerImage}}<br>
<b>Labels:</b>
Expand Down
16 changes: 7 additions & 9 deletions examples/update-demo/local/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,25 @@ limitations under the License.
var base = "http://localhost:8001/api/v1beta1/";

var updateImage = function($http, server) {
$http.get("http://" + server.ip + ":8080/data.json")
$http.get(base + "proxy/pods/" + server.podId + "/data.json")
.success(function(data) {
server.image = data.image;
console.log(data);
server.image = data.image;
})
.error(function(data) {
server.image = "";
console.log(data);
server.image = "";
});
};

var updateServer = function($http, server) {
$http.get(base + "pods/" + server.id)
$http.get(base + "pods/" + server.podId)
.success(function(data) {
console.log(data);
server.ip = data.currentState.hostIP;
server.labels = data.labels;
server.host = data.currentState.host.split('.')[0];
server.status = data.currentState.status;

server.dockerImage = data.currentState.info["update-demo"].Image;
server.dockerImage = data.currentState.info["update-demo"].image;
updateImage($http, server);
})
.error(function(data) {
Expand All @@ -62,7 +60,7 @@ var ButtonsCtrl = function ($scope, $http, $interval) {
var getServer = function($scope, id) {
var servers = $scope.servers;
for (var i = 0; i < servers.length; ++i) {
if (servers[i].id == id) {
if (servers[i].podId == id) {
return servers[i];
}
}
Expand All @@ -89,7 +87,7 @@ var update = function($scope, $http) {
}
var server = getServer($scope, pod.id);
if (server == null) {
server = { "id": pod.id };
server = { "podId": pod.id };
}
newServers.push(server);
}
Expand Down
3 changes: 1 addition & 2 deletions examples/update-demo/nautilus-rc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ desiredState:
- name: update-demo
image: kubernetes/update-demo:nautilus
ports:
- hostPort: 8080
containerPort: 80
- containerPort: 80
protocol: TCP
labels:
name: update-demo
Expand Down
81 changes: 60 additions & 21 deletions test/e2e/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -36,13 +37,50 @@ const (
updateDemoSelector = "name=update-demo"
updateDemoContainer = "update-demo"
validateTimeout = 60 * time.Second
kubectlProxyPort = 8011
)

var _ = Describe("kubectl", func() {

updateDemoRoot := filepath.Join(testContext.repoRoot, "examples/update-demo")
nautilusPath := filepath.Join(updateDemoRoot, "nautilus-rc.yaml")
kittenPath := filepath.Join(updateDemoRoot, "kitten-rc.yaml")
// Constants.
var (
updateDemoRoot = filepath.Join(testContext.repoRoot, "examples/update-demo")
nautilusPath = filepath.Join(updateDemoRoot, "nautilus-rc.yaml")
kittenPath = filepath.Join(updateDemoRoot, "kitten-rc.yaml")
)

var cmd *exec.Cmd

BeforeEach(func() {
cmd = kubectlCmd("proxy", fmt.Sprintf("--port=%d", kubectlProxyPort))
if err := cmd.Start(); err != nil {
Failf("Unable to start kubectl proxy: %v", err)
}
})

AfterEach(func() {
if cmd.Process == nil {
Logf("No process associated with the kubectl proxy")
return
}
// Kill the proxy
errChan := make(chan error, 1)
go func() {
if err := cmd.Process.Signal(os.Interrupt); err != nil {
errChan <- err
}
_, err := cmd.Process.Wait()
errChan <- err
}()
select {
case <-time.After(10 * time.Second):
Fail("Timed out waiting for kubectl proxy process to exit")
case err := <-errChan:
if err != nil {
Failf("Unable to kill kubectl proxy: %v", err)
}
}
})

It("should create and stop a replication controller", func() {
defer cleanup(nautilusPath)
Expand Down Expand Up @@ -107,8 +145,6 @@ func validateController(image string, replicas int) {

getImageTemplate := fmt.Sprintf(`--template={{(index .currentState.info "%s").image}}`, updateDemoContainer)

getHostIPTemplate := "--template={{.currentState.hostIP}}"

By(fmt.Sprintf("waiting for all containers in %s pods to come up.", updateDemoSelector))
for start := time.Now(); time.Since(start) < validateTimeout; time.Sleep(5 * time.Second) {
getPodsOutput := runKubectl("get", "pods", "-o", "template", getPodsTemplate, "-l", updateDemoSelector)
Expand All @@ -118,32 +154,31 @@ func validateController(image string, replicas int) {
continue
}
var runningPods []string
for _, podId := range pods {
running := runKubectl("get", "pods", podId, "-o", "template", getContainerStateTemplate)
for _, podID := range pods {
running := runKubectl("get", "pods", podID, "-o", "template", getContainerStateTemplate)
if running == "false" {
By(fmt.Sprintf("%s is created but not running", podId))
Logf("%s is created but not running", podID)
continue
}

currentImage := runKubectl("get", "pods", podId, "-o", "template", getImageTemplate)
currentImage := runKubectl("get", "pods", podID, "-o", "template", getImageTemplate)
if currentImage != image {
By(fmt.Sprintf("%s is created but running wrong image; expected: %s, actual: %s", podId, image, currentImage))
Logf("%s is created but running wrong image; expected: %s, actual: %s", podID, image, currentImage)
continue
}

hostIP := runKubectl("get", "pods", podId, "-o", "template", getHostIPTemplate)
data, err := getData(hostIP)
data, err := getData(podID)
if err != nil {
By(fmt.Sprintf("%s is running right image but fetching data failed: %v", podId, err))
Logf("%s is running right image but fetching data failed: %v", podID, err)
continue
}
if strings.Contains(data.image, image) {
By(fmt.Sprintf("%s is running right image but fetched data has the wrong info: %s", podId, data))
Logf("%s is running right image but fetched data has the wrong info: %s", podID, data)
continue
}

Logf("%s is verified up and running", podId)
runningPods = append(runningPods, podId)
Logf("%s is verified up and running", podID)
runningPods = append(runningPods, podID)
}
if len(runningPods) == replicas {
return
Expand All @@ -156,9 +191,8 @@ type updateDemoData struct {
image string `json:"image"`
}

func getData(hostIP string) (*updateDemoData, error) {
addr := fmt.Sprintf("http://%s:8080/data.json", hostIP)
resp, err := http.Get(fmt.Sprintf(addr))
func getData(podID string) (*updateDemoData, error) {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/api/v1beta1/proxy/pods/%s/data.json", kubectlProxyPort, podID))
if err != nil || resp.StatusCode != 200 {
return nil, err
}
Expand All @@ -173,7 +207,7 @@ func getData(hostIP string) (*updateDemoData, error) {
return &data, err
}

func runKubectl(args ...string) string {
func kubectlCmd(args ...string) *exec.Cmd {
defaultArgs := []string{"--auth-path=" + testContext.authConfig}
if testContext.certDir != "" {
defaultArgs = append(defaultArgs,
Expand All @@ -186,9 +220,14 @@ func runKubectl(args ...string) string {
if testContext.provider == "gke" {
kubectlArgs = append(kubectlArgs, "--server="+testContext.host)
}
Logf("Running 'kubectl %v'", strings.Join(kubectlArgs, " "))
cmd := exec.Command("kubectl", kubectlArgs...)
Logf("Running '%s %s'", cmd.Path, strings.Join(cmd.Args, " "))
return cmd
}

func runKubectl(args ...string) string {
var stdout, stderr bytes.Buffer
cmd := kubectlCmd(args...)
cmd.Stdout, cmd.Stderr = &stdout, &stderr

if err := cmd.Run(); err != nil {
Expand Down

0 comments on commit 5799dd5

Please sign in to comment.