-
Notifications
You must be signed in to change notification settings - Fork 40.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kubelet: add --runonce flag #1534
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
Copyright 2014 Google Inc. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kubelet | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools" | ||
"github.com/golang/glog" | ||
) | ||
|
||
const ( | ||
RunOnceManifestDelay = 1 * time.Second | ||
RunOnceMaxRetries = 10 | ||
RunOnceRetryDelay = 1 * time.Second | ||
RunOnceRetryDelayBackoff = 2 | ||
) | ||
|
||
type RunPodResult struct { | ||
Pod *Pod | ||
Info api.PodInfo | ||
Err error | ||
} | ||
|
||
// RunOnce polls from one configuration update and run the associated pods. | ||
func (kl *Kubelet) RunOnce(updates <-chan PodUpdate) ([]RunPodResult, error) { | ||
select { | ||
case u := <-updates: | ||
glog.Infof("processing manifest with %d pods", len(u.Pods)) | ||
result, err := kl.runOnce(u.Pods) | ||
glog.Infof("finished processing %d pods", len(u.Pods)) | ||
return result, err | ||
case <-time.After(RunOnceManifestDelay): | ||
return nil, fmt.Errorf("no pod manifest update after %v", RunOnceManifestDelay) | ||
} | ||
} | ||
|
||
// runOnce runs a given set of pods and returns their status. | ||
func (kl *Kubelet) runOnce(pods []Pod) (results []RunPodResult, err error) { | ||
if kl.dockerPuller == nil { | ||
kl.dockerPuller = dockertools.NewDockerPuller(kl.dockerClient, kl.pullQPS, kl.pullBurst) | ||
} | ||
pods = filterHostPortConflicts(pods) | ||
|
||
ch := make(chan RunPodResult) | ||
for _, pod := range pods { | ||
go func() { | ||
info, err := kl.runPod(pod) | ||
ch <- RunPodResult{&pod, info, err} | ||
}() | ||
} | ||
|
||
glog.Infof("waiting for %d pods", len(pods)) | ||
failedPods := []string{} | ||
for i := 0; i < len(pods); i++ { | ||
res := <-ch | ||
results = append(results, res) | ||
if res.Err != nil { | ||
glog.Infof("failed to start pod %q: %v", res.Pod.Name, res.Err) | ||
failedPods = append(failedPods, res.Pod.Name) | ||
} else { | ||
glog.Infof("started pod %q: %#v", res.Pod.Name, res.Info) | ||
} | ||
} | ||
if len(failedPods) > 0 { | ||
return results, fmt.Errorf("error running pods: %v", failedPods) | ||
} | ||
glog.Infof("%d pods started", len(pods)) | ||
return results, err | ||
} | ||
|
||
// Run a single pod and wait until all containers are running. | ||
func (kl *Kubelet) runPod(pod Pod) (api.PodInfo, error) { | ||
dockerContainers, err := dockertools.GetKubeletDockerContainers(kl.dockerClient, false) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get kubelet docker containers: %v", err) | ||
} | ||
|
||
delay := RunOnceRetryDelay | ||
retry := 0 | ||
for { | ||
glog.Infof("syncing pod") | ||
err := kl.syncPod(&pod, dockerContainers) | ||
if err != nil { | ||
return nil, fmt.Errorf("error syncing pod: %v", err) | ||
} | ||
info, err := kl.GetPodInfo(GetPodFullName(&pod), pod.Manifest.UUID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting pod info: %v", err) | ||
} | ||
if podInfo(info).isRunning() { | ||
return info, nil | ||
} | ||
if retry >= RunOnceMaxRetries { | ||
return nil, fmt.Errorf("timeout error: pod %q containers not running after %d retries", pod.Name, RunOnceMaxRetries) | ||
} | ||
glog.Infof("pod %q containers not running, waiting for %v", pod.Name, delay) | ||
<-time.After(delay) | ||
retry++ | ||
delay *= RunOnceRetryDelayBackoff | ||
} | ||
} | ||
|
||
// Alias PodInfo for internal usage. | ||
type podInfo api.PodInfo | ||
|
||
// Check if all containers of a pod are running. | ||
func (info podInfo) isRunning() bool { | ||
for _, container := range info { | ||
if container.State.Running == nil { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
Copyright 2014 Google Inc. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kubelet | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools" | ||
docker "github.com/fsouza/go-dockerclient" | ||
) | ||
|
||
type listContainersResult struct { | ||
label string | ||
containers []docker.APIContainers | ||
err error | ||
} | ||
|
||
type inspectContainersResult struct { | ||
label string | ||
container docker.Container | ||
err error | ||
} | ||
|
||
type testDocker struct { | ||
listContainersResults []listContainersResult | ||
inspectContainersResults []inspectContainersResult | ||
dockertools.FakeDockerClient | ||
t *testing.T | ||
} | ||
|
||
func (d *testDocker) ListContainers(options docker.ListContainersOptions) ([]docker.APIContainers, error) { | ||
if len(d.listContainersResults) > 0 { | ||
result := d.listContainersResults[0] | ||
d.listContainersResults = d.listContainersResults[1:] | ||
d.t.Logf("ListContainers: %q, returning: (%v, %v)", result.label, result.containers, result.err) | ||
return result.containers, result.err | ||
} | ||
return nil, fmt.Errorf("ListContainers error: no more test results") | ||
} | ||
|
||
func (d *testDocker) InspectContainer(id string) (*docker.Container, error) { | ||
if len(d.inspectContainersResults) > 0 { | ||
result := d.inspectContainersResults[0] | ||
d.inspectContainersResults = d.inspectContainersResults[1:] | ||
d.t.Logf("InspectContainers: %q, returning: (%v, %v)", result.label, result.container, result.err) | ||
return &result.container, result.err | ||
} | ||
return nil, fmt.Errorf("InspectContainer error: no more test results") | ||
} | ||
|
||
func TestRunOnce(t *testing.T) { | ||
kb := &Kubelet{} | ||
container := api.Container{Name: "bar"} | ||
kb.dockerClient = &testDocker{ | ||
listContainersResults: []listContainersResult{ | ||
{label: "pre syncPod", containers: []docker.APIContainers{}}, | ||
{label: "syncPod #1", containers: []docker.APIContainers{}}, | ||
{label: "syncPod #2", containers: []docker.APIContainers{}}, | ||
{label: "post syncPod", containers: []docker.APIContainers{ | ||
{ | ||
Names: []string{"/k8s_bar." + strconv.FormatUint(dockertools.HashContainer(&container), 16) + "_foo.test"}, | ||
ID: "1234", | ||
}, | ||
{ | ||
Names: []string{"/k8s_net_foo.test_"}, | ||
ID: "9876", | ||
}, | ||
}}, | ||
}, | ||
inspectContainersResults: []inspectContainersResult{ | ||
{label: "syncPod", container: docker.Container{State: docker.State{Running: true}}}, | ||
{label: "syncPod", container: docker.Container{State: docker.State{Running: true}}}, | ||
}, | ||
t: t, | ||
} | ||
kb.dockerPuller = &dockertools.FakeDockerPuller{} | ||
results, err := kb.runOnce([]Pod{ | ||
{ | ||
Name: "foo", | ||
Namespace: "test", | ||
Manifest: api.ContainerManifest{ | ||
ID: "foo", | ||
Containers: []api.Container{ | ||
{Name: "bar"}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
if results[0].Err != nil { | ||
t.Errorf("unexpected run pod error: %v", results[0].Err) | ||
} | ||
if results[0].Pod.Name != "foo" { | ||
t.Errorf("unexpected pod: %q", results[0].Pod.Name) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like there's some overlap with the main path - can it be avoided? or is it just not enough to worry about? I'm concerned that we will fix bugs in the main path and not this, since this has no e2e test (ahem)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is actually reusing most of the element of the main path: it's just a retry loop around
syncPod
.The only difference is that it calls
GetKubeletDockerContainers
for each pod, instead of having a syncLoop for all the pod like the main path.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But agreed this need some tests, I'm currently fighting with
kubelet_test.go
to figure out what I can reuse.