Skip to content
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

Improve error detection for URL manifests, and skip parsing if content unchanged #1343

Merged
merged 1 commit into from
Sep 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/kubelet/config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package config

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -34,12 +35,14 @@ import (
type SourceURL struct {
url string
updates chan<- interface{}
data []byte
}

func NewSourceURL(url string, period time.Duration, updates chan<- interface{}) *SourceURL {
config := &SourceURL{
url: url,
updates: updates,
data: nil,
}
glog.Infof("Watching URL %s", url)
go util.Forever(config.run, period)
Expand Down Expand Up @@ -68,6 +71,11 @@ func (s *SourceURL) extractFromURL() error {
if len(data) == 0 {
return fmt.Errorf("zero-length data received from %v", s.url)
}
// Short circuit if the manifest has not changed since the last time it was read.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do a hash comparison elsewhere:

https://github.com/GoogleCloudPlatform/kubernetes/blob/master/pkg/kubelet/kubelet.go#L495

Not sure if this check is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you; I just noticed that I was getting "Setting pods for source http : {...}" in my kubelet.log every 20 seconds, and it seemed like it could potentially be a lot of log output for something that may never change. It seemed preferable to me if the logs have output only on error or on change.

if bytes.Compare(data, s.data) == 0 {
return nil
}
s.data = data

// First try as if it's a single manifest
var manifest api.ContainerManifest
Expand Down Expand Up @@ -101,6 +109,13 @@ func (s *SourceURL) extractFromURL() error {
}
}
if multiErr == nil {
// A single manifest that did not pass semantic validation will yield an empty
// array of manifests (and no error) when unmarshaled as such. In that case,
// if the single manifest at least had a Version, we return the single-manifest
// error (if any).
if len(manifests) == 0 && manifest.Version != "" {
return singleErr
}
pods := []kubelet.Pod{}
for i, manifest := range manifests {
pod := kubelet.Pod{Name: manifest.ID, Manifest: manifest}
Expand Down
24 changes: 21 additions & 3 deletions pkg/kubelet/config/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestURLErrorNotExistNoUpdate(t *testing.T) {

func TestExtractFromHttpBadness(t *testing.T) {
ch := make(chan interface{}, 1)
c := SourceURL{"http://localhost:49575/_not_found_", ch}
c := SourceURL{"http://localhost:49575/_not_found_", ch, nil}
if err := c.extractFromURL(); err == nil {
t.Errorf("Expected error")
}
Expand Down Expand Up @@ -75,6 +75,24 @@ func TestExtractInvalidManifest(t *testing.T) {
},
},
},
{
desc: "Unspecified container name",
manifests: []api.ContainerManifest{
{
Version: "v1beta1",
Containers: []api.Container{{Name: ""}},
},
},
},
{
desc: "Invalid container name",
manifests: []api.ContainerManifest{
{
Version: "v1beta1",
Containers: []api.Container{{Name: "_INVALID_"}},
},
},
},
}
for _, testCase := range testCases {
data, err := json.Marshal(testCase.manifests)
Expand All @@ -87,7 +105,7 @@ func TestExtractInvalidManifest(t *testing.T) {
}
testServer := httptest.NewServer(&fakeHandler)
ch := make(chan interface{}, 1)
c := SourceURL{testServer.URL, ch}
c := SourceURL{testServer.URL, ch, nil}
if err := c.extractFromURL(); err == nil {
t.Errorf("%s: Expected error", testCase.desc)
}
Expand Down Expand Up @@ -146,7 +164,7 @@ func TestExtractFromHTTP(t *testing.T) {
}
testServer := httptest.NewServer(&fakeHandler)
ch := make(chan interface{}, 1)
c := SourceURL{testServer.URL, ch}
c := SourceURL{testServer.URL, ch, nil}
if err := c.extractFromURL(); err != nil {
t.Errorf("%s: Unexpected error: %v", testCase.desc, err)
}
Expand Down