Set a kaniko user agent. (#262)

This commit is contained in:
dlorenc 2018-07-30 13:03:25 -07:00 committed by GitHub
parent cac00b9cb2
commit e43968f02f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 1 deletions

View File

@ -42,6 +42,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/snapshot" "github.com/GoogleContainerTools/kaniko/pkg/snapshot"
"github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/GoogleContainerTools/kaniko/pkg/version"
) )
// KanikoBuildArgs contains all the args required to build the image // KanikoBuildArgs contains all the args required to build the image
@ -181,7 +182,17 @@ func DoBuild(k KanikoBuildArgs) (v1.Image, error) {
return nil, err return nil, err
} }
type withUserAgent struct {
t http.RoundTripper
}
func (w *withUserAgent) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("User-Agent", fmt.Sprintf("kaniko/%s", version.Version()))
return w.t.RoundTrip(r)
}
func DoPush(image v1.Image, destinations []string, tarPath string) error { func DoPush(image v1.Image, destinations []string, tarPath string) error {
// continue pushing unless an error occurs // continue pushing unless an error occurs
for _, destination := range destinations { for _, destination := range destinations {
// Push the image // Push the image
@ -204,7 +215,10 @@ func DoPush(image v1.Image, destinations []string, tarPath string) error {
return err return err
} }
if err := remote.Write(destRef, image, pushAuth, http.DefaultTransport, remote.WriteOptions{}); err != nil { // Create a transport to set our user-agent.
rt := &withUserAgent{t: http.DefaultTransport}
if err := remote.Write(destRef, image, pushAuth, rt, remote.WriteOptions{}); err != nil {
logrus.Error(fmt.Errorf("Failed to push to destination %s", destination)) logrus.Error(fmt.Errorf("Failed to push to destination %s", destination))
return err return err
} }