Skip to content

Commit

Permalink
Merge pull request #346 from covexo/issue-344
Browse files Browse the repository at this point in the history
Issue 344
  • Loading branch information
Lukas Gentele authored Oct 31, 2018
2 parents faf9acf + 20a9ce9 commit 22651d5
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 34 deletions.
27 changes: 27 additions & 0 deletions pkg/devspace/builder/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var (
stdin, stdout, stderr = term.StdStreams()
)

var authConfigs = map[string]*types.AuthConfig{}

// Builder holds the necessary information to build and push docker images
type Builder struct {
RegistryURL string
Expand Down Expand Up @@ -188,6 +190,9 @@ func (b *Builder) Authenticate(user, password string, checkCredentialsStore bool

b.authConfig = authConfig

// Cache authConfig for GetAuthConfig
authConfigs[b.RegistryURL] = authConfig

return b.authConfig, nil
}

Expand Down Expand Up @@ -219,3 +224,25 @@ func (b *Builder) PushImage() error {

return nil
}

// GetAuthConfig returns the AuthConfig for a Docker registry from the Docker credential helper
func GetAuthConfig(registryURL string) (*types.AuthConfig, error) {
if registryURL == "hub.docker.com" || registryURL == "index.docker.io" {
registryURL = ""
}

authConfig, authConfigExists := authConfigs[registryURL]

if !authConfigExists {
dockerBuilder, err := NewBuilder(registryURL, "", "", false)
if err != nil {
return nil, err
}

authConfig, err = dockerBuilder.Authenticate("", "", true)
if err != nil {
return nil, err
}
}
return authConfig, nil
}
4 changes: 4 additions & 0 deletions pkg/devspace/deploy/helm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ func (d *DeployConfig) Deploy(generatedConfig *generated.Config, forceDeploy boo
}
}

for _, autoGeneratedPullSecret := range registry.GetPullSecretNames() {
overwritePullSecrets = append(overwritePullSecrets, autoGeneratedPullSecret)
}

overwriteValues["containers"] = overwriteContainerValues
overwriteValues["pullSecrets"] = overwritePullSecrets

Expand Down
112 changes: 79 additions & 33 deletions pkg/devspace/registry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"errors"
"fmt"

"github.com/covexo/devspace/pkg/devspace/builder/docker"
"github.com/covexo/devspace/pkg/devspace/config/v1"

"github.com/covexo/devspace/pkg/devspace/config/configutil"
"github.com/covexo/devspace/pkg/devspace/helm"
"github.com/covexo/devspace/pkg/util/log"
Expand Down Expand Up @@ -38,45 +41,88 @@ func InitRegistries(client *kubernetes.Clientset, log log.Logger) error {
log.Done("Internal registry started")
}

mustInitDockerHub := true

if registryMap != nil {
defaultNamespace, err := configutil.GetDefaultNamespace(config)
for registryName, registryConf := range registryMap {
if registryConf.URL == nil || *registryConf.URL == "hub.docker.com" || *registryConf.URL == "index.docker.io" {
mustInitDockerHub = false
}

log.StartWait("Creating image pull secret for registry: " + registryName)
err := initRegistry(client, registryConf)
log.StopWait()

if err != nil {
return fmt.Errorf("Failed to create pull secret for registry: %v", err)
}
}
}

if mustInitDockerHub {
log.StartWait("Creating image pull secret for registry: hub.docker.com")
err := initRegistry(client, &v1.RegistryConfig{
URL: configutil.String(""),
})
log.StopWait()

if err != nil {
return fmt.Errorf("Cannot get default namespace: %v", err)
return fmt.Errorf("Failed to create pull secret for registry: %v", err)
}
}

for registryName, registryConf := range registryMap {
if registryConf.Auth != nil && registryConf.Auth.Password != nil {
if config.DevSpace.Deployments != nil {
for _, deployConfig := range *config.DevSpace.Deployments {
username := ""
password := *registryConf.Auth.Password
email := "[email protected]"
registryURL := ""

if registryConf.Auth.Username != nil {
username = *registryConf.Auth.Username
}
if registryConf.URL != nil {
registryURL = *registryConf.URL
}

namespace := *deployConfig.Namespace
if namespace == "" {
namespace = defaultNamespace
}

log.StartWait("Creating image pull secret for registry: " + registryName)
err := CreatePullSecret(client, namespace, registryURL, username, password, email)
log.StopWait()

if err != nil {
return fmt.Errorf("Failed to create pull secret for registry: %v", err)
}
}
}
}
return nil
}

func initRegistry(client *kubernetes.Clientset, registryConf *v1.RegistryConfig) error {
config := configutil.GetConfig()
defaultNamespace, err := configutil.GetDefaultNamespace(config)
if err != nil {
return err
}

registryURL := ""
if registryConf.URL != nil {
registryURL = *registryConf.URL
}

username := ""
password := ""

if registryConf.Auth == nil || registryConf.Auth.Username == nil || registryConf.Auth.Password == nil {
authConfig, _ := docker.GetAuthConfig(registryURL)

if authConfig != nil {
username = authConfig.Username
password = authConfig.Password
}
}

if registryConf.Auth != nil {
if registryConf.Auth.Username != nil {
username = *registryConf.Auth.Username
}

if registryConf.Auth.Password != nil {
password = *registryConf.Auth.Password
}
}

if config.DevSpace.Deployments != nil {
for _, deployConfig := range *config.DevSpace.Deployments {
email := "[email protected]"

namespace := *deployConfig.Namespace
if namespace == "" {
namespace = defaultNamespace
}

err := CreatePullSecret(client, namespace, registryURL, username, password, email)

if err != nil {
return err
}
}
}
return nil
}
12 changes: 11 additions & 1 deletion pkg/devspace/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const InternalRegistryDeploymentName = "devspace-registry-docker-registry"
const registryAuthSecretNamePrefix = "devspace-registry-auth-"
const registryPort = 5000

var pullSecretNames = []string{}

// CreatePullSecret creates an image pull secret for a registry
func CreatePullSecret(kubectl *kubernetes.Clientset, namespace, registryURL, username, passwordOrToken, email string) error {
pullSecretName := GetRegistryAuthSecretName(registryURL)
Expand Down Expand Up @@ -72,8 +74,11 @@ func CreatePullSecret(kubectl *kubernetes.Clientset, namespace, registryURL, use
}

if err != nil {
return fmt.Errorf("Unable to update image pull secret: %s", err.Error())
return fmt.Errorf("Unable to create/update image pull secret: %s", err.Error())
}

pullSecretNames = append(pullSecretNames, pullSecretName)

return nil
}

Expand Down Expand Up @@ -187,3 +192,8 @@ func GetRegistryConfig(imageConfig *v1.ImageConfig) (*v1.RegistryConfig, error)

return registryConfig, nil
}

// GetPullSecretNames returns all names of auto-generated image pull secrets
func GetPullSecretNames() []string {
return pullSecretNames
}

0 comments on commit 22651d5

Please sign in to comment.