• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes / kompose / 4217081086

19 Feb 2023 04:20PM UTC coverage: 53.126%. Remained the same
4217081086

push

github

GitHub
Bump golang.org/x/net from 0.5.0 to 0.7.0 (#1592)

1963 of 3695 relevant lines covered (53.13%)

7.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/utils/docker/push.go
1
/*
2
Copyright 2016 The Kubernetes Authors All rights reserved
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package docker
18

19
import (
20
        "bytes"
21

22
        dockerlib "github.com/fsouza/go-dockerclient"
23
        "github.com/pkg/errors"
24
        log "github.com/sirupsen/logrus"
25
)
26

27
// Push will provide methods for interaction with API regarding pushing images
28
type Push struct {
29
        Client dockerlib.Client
30
}
31

32
/*
33
PushImage pushes a Docker image via the Docker API. Takes the image name,
34
parses the URL details and then push based on environment authentication
35
credentials.
36
*/
37
func (c *Push) PushImage(image Image) error {
×
38
        log.Infof("Pushing image '%s' to registry '%s'", image.Name, image.Registry)
×
39

×
40
        // Let's setup the push and authentication options
×
41
        outputBuffer := bytes.NewBuffer(nil)
×
42
        options := dockerlib.PushImageOptions{
×
43
                Tag:          image.Tag,
×
44
                Name:         image.Repository,
×
45
                Registry:     image.Registry,
×
46
                OutputStream: outputBuffer,
×
47
        }
×
48

×
49
        // Retrieve the authentication configuration file
×
50
        // Files checked as per https://godoc.org/github.com/fsouza/go-dockerclient#NewAuthConfigurationsFromFile
×
51
        // $DOCKER_CONFIG/config.json, $HOME/.docker/config.json , $HOME/.dockercfg
×
52
        credentials, err := dockerlib.NewAuthConfigurationsFromDockerCfg()
×
53
        if err != nil {
×
54
                log.Warn(errors.Wrap(err, "Unable to retrieve .docker/config.json authentication details. Check that 'docker login' works successfully on the command line."))
×
55
        } else {
×
56
                handleDockerRegistry(credentials)
×
57
        }
×
58

59
        // Find the authentication matched to registry
60
        auth, ok := credentials.Configs[image.Registry]
×
61
        if !ok {
×
62
                // Fallback to unauthenticated access in case if no auth credentials are retrieved
×
63
                log.Infof("Authentication credential of registry '%s' is not found. Will try push without authentication.", image.Registry)
×
64
                // Header X-Registry-Auth is required
×
65
                // Or API error (400): Bad parameters and missing X-Registry-Auth: EOF will throw
×
66
                // Just to make not empty struct
×
67
                auth = dockerlib.AuthConfiguration{Username: "docker"}
×
68
        }
×
69

70
        log.Debugf("Pushing image with options %+v", options)
×
71
        err = c.Client.PushImage(options, auth)
×
72
        if err != nil {
×
73
                log.Errorf("Unable to push image '%s' to registry '%s'. Error: %s", image.Name, image.Registry, err)
×
74
                return errors.New("unable to push docker image(s). Check that `docker login` works successfully on the command line")
×
75
        }
×
76

77
        log.Debugf("Image '%+v' push output:\n%s", image, outputBuffer)
×
78
        log.Infof("Successfully pushed image '%s' to registry '%s'", image.Name, image.Registry)
×
79
        return nil
×
80
}
81

82
// handleDockerRegistry adapt legacy docker registry address
83
// After docker login to docker.io, there must be https://index.docker.io/v1/ in config.json of authentication
84
// Reference: https://docs.docker.com/engine/api/v1.23/
85
//
86
//        > However (for legacy reasons) the “official” Docker, Inc. hosted registry
87
//        > must be specified with both a “https://” prefix and a “/v1/” suffix
88
//        > even though Docker will prefer to use the v2 registry API.
89
func handleDockerRegistry(auth *dockerlib.AuthConfigurations) {
×
90
        const address = "docker.io"
×
91
        const legacyAddress = "https://index.docker.io/v1/"
×
92

×
93
        if legacyAddressConfig, ok := auth.Configs[legacyAddress]; ok {
×
94
                auth.Configs[address] = legacyAddressConfig
×
95
        }
×
96
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc