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

Provide Skaffold user-agent on update-checks and downloads #4964

Merged
merged 1 commit into from
Oct 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions pkg/skaffold/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ package update

import (
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/blang/semver"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
)

Expand Down Expand Up @@ -100,18 +99,10 @@ func getLatestAndCurrentVersion() (semver.Version, semver.Version, error) {
}

func DownloadLatestVersion() (string, error) {
resp, err := http.Get(LatestVersionURL)
versionBytes, err := util.Download(LatestVersionURL)
if err != nil {
return "", fmt.Errorf("getting latest version info from GCS: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("http %d, error %q", resp.StatusCode, resp.Status)
}
versionBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("reading version file from GCS: %w", err)
}
return strings.TrimSuffix(string(versionBytes), "\n"), nil
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/skaffold/util/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2019 The Skaffold Authors
briandealwis marked this conversation as resolved.
Show resolved Hide resolved

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 util

import (
"fmt"
"io/ioutil"
"net/http"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
)

func Download(url string) ([]byte, error) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("creating http request: %w", err)
}
req.Header.Set("User-Agent", version.UserAgent())
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http %d, error %q", resp.StatusCode, resp.Status)
}
return ioutil.ReadAll(resp.Body)
}
42 changes: 42 additions & 0 deletions pkg/skaffold/util/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2020 The Skaffold Authors

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 util

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestDownload_UserAgent(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ua := r.Header.Get("User-Agent")
w.Write([]byte(ua))
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
}))
defer ts.Close()

// although we don't have a version number in tests, the user-agent string still
// has `skaffold/<GOOS>/<GOARCH>/`
testutil.CheckDeepEqual(t, true, strings.HasPrefix(version.UserAgent(), "skaffold/"))

v, err := Download(ts.URL)
testutil.CheckErrorAndDeepEqual(t, false, err, version.UserAgent(), string(v))
}
12 changes: 0 additions & 12 deletions pkg/skaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -149,16 +147,6 @@ func IsURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}

func Download(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

return ioutil.ReadAll(resp.Body)
}

// VerifyOrCreateFile checks if a file exists at the given path,
// and if not, creates all parent directories and creates the file.
func VerifyOrCreateFile(path string) error {
Expand Down
9 changes: 9 additions & 0 deletions pkg/skaffold/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,15 @@ func TestIsFileIsDir(t *testing.T) {
testutil.CheckDeepEqual(t, false, IsDir(filepath.Join(tmpDir.Root(), "nonexistent")))
}

func TestIsURL(t *testing.T) {
testutil.CheckDeepEqual(t, false, IsURL("foo"))
testutil.CheckDeepEqual(t, false, IsURL("http:bar"))
testutil.CheckDeepEqual(t, false, IsURL("https:bar"))

testutil.CheckDeepEqual(t, true, IsURL("http://bar"))
testutil.CheckDeepEqual(t, true, IsURL("https://bar"))
}

func stringPointer(s string) *string {
return &s
}