Skip to content

Commit 2f84f99

Browse files
authored
Merge pull request #309 from covexo/issue-307
Issue 307
2 parents 63e4e9c + bd1325c commit 2f84f99

6 files changed

Lines changed: 54 additions & 8 deletions

File tree

docs/docs/configuration/config.yaml.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ DockerConfig:
109109
KanikoConfig:
110110
- `cache` *bool* if true the last image build is used as cache repository
111111
- `namespace` *string* specifies the namespace where the build pod should be started
112+
- `pullSecret` *string* mount this pullSecret instead of creating one to authenticate to the registry
112113

113114
### images[].build.options
114115
BuildOptions:

examples/kaniko/.devspace/config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ images:
2626
build:
2727
kaniko:
2828
cache: true
29-
namespace: ""
3029
name: yourdockername/kaniko

examples/kaniko/README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ This example shows how kaniko can be used instead of docker to build and push an
44

55
# Step 0: Prerequisites
66

7-
In order for this example to work you need access to a docker registry, where you can push images to (e.g. hub.docker.com, gcr.io etc.). There are two options how you can push images to registries with devspace.
7+
In order for this example to work you need access to a docker registry, where you can push images to (e.g. hub.docker.com, gcr.io etc.). There are three options how you can push images to registries with devspace.
88

99
## Option 1: Use docker credentials store
1010
If you have docker installed, devspace can take the required auth information directly out of the docker credentials store and will create the needed secret for you in the target cluster automatically. Make sure you are logged in the registry with `docker login`.
1111

1212
## Option 2: Provide auth information yourself
13-
As a second option you can provide your credentials directly in the config.yaml. See example below:
13+
As a second option you can provide your credentials directly in the config.yaml and devspace cli will create a pull secret for you automatically. See example below:
1414

1515
```yaml
1616
images:
1717
default:
1818
build:
1919
kaniko:
2020
cache: true
21-
namespace: ""
2221
# Don't prefix image name with registry url
2322
name: name/devspace
2423
registry: myRegistry
@@ -33,6 +32,37 @@ registries:
3332
3433
devspace will then automatically create a secret for you which kaniko can use to push to that registry.
3534
35+
## Option 3: Provide kaniko pull secret yourself
36+
As a third option you can provide the pullSecret to use for kaniko yourself. Make sure the pull secret has the following form:
37+
38+
```yaml
39+
apiVersion: v1
40+
kind: Secret
41+
data:
42+
# .dockerconfigjson encoded in base64 e.g.:
43+
# {
44+
# "auths": {
45+
# "myRegistryUrl": {
46+
# "auth": "base64Encoded(user:password/token)",
47+
# "email": "myemail@test.de"
48+
# }
49+
# }
50+
# }
51+
.dockerconfigjson: BASE64EncodedDockerConfigJson
52+
```
53+
54+
Now specify the pullsecret name as the pull secret to use for kaniko in the .devspace/config:
55+
56+
```yaml
57+
images:
58+
default:
59+
build:
60+
kaniko:
61+
cache: true
62+
name: registryName/name/devspace
63+
pullSecret: myPullSecretName
64+
```
65+
3666
## Optional: Use self hosted cluster (minikube, GKE etc.) instead of devspace-cloud
3767
3868
If you want to use your own cluster instead of the devspace-cloud as deployment target, make sure `kubectl` is configured correctly to access the target cluster. Then just erase the `cluster` section in the `.devspace/config.yaml` and devspace will use your current kubectl context as deployment target.

pkg/devspace/builder/kaniko/kaniko.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
// Builder holds the necessary information to build and push docker images
2424
type Builder struct {
2525
RegistryURL string
26+
PullSecretName string
2627
ImageName string
2728
ImageTag string
2829
PreviousImageTag string
@@ -33,9 +34,10 @@ type Builder struct {
3334
}
3435

3536
// NewBuilder creates a new kaniko.Builder instance
36-
func NewBuilder(registryURL, imageName, imageTag, lastImageTag, buildNamespace string, kubectl *kubernetes.Clientset, allowInsecureRegistry bool) (*Builder, error) {
37+
func NewBuilder(registryURL, pullSecretName, imageName, imageTag, lastImageTag, buildNamespace string, kubectl *kubernetes.Clientset, allowInsecureRegistry bool) (*Builder, error) {
3738
return &Builder{
3839
RegistryURL: registryURL,
40+
PullSecretName: pullSecretName,
3941
ImageName: imageName,
4042
ImageTag: imageTag,
4143
PreviousImageTag: lastImageTag,
@@ -47,6 +49,10 @@ func NewBuilder(registryURL, imageName, imageTag, lastImageTag, buildNamespace s
4749

4850
// Authenticate authenticates kaniko for pushing to the RegistryURL (if username == "", it will try to get login data from local docker daemon)
4951
func (b *Builder) Authenticate(username, password string, checkCredentialsStore bool) (*types.AuthConfig, error) {
52+
if b.PullSecretName != "" {
53+
return nil, nil
54+
}
55+
5056
email := "noreply@devspace-cloud.com"
5157

5258
if len(username) == 0 {
@@ -74,6 +80,10 @@ func (b *Builder) Authenticate(username, password string, checkCredentialsStore
7480
// BuildImage builds a dockerimage within a kaniko pod
7581
func (b *Builder) BuildImage(contextPath, dockerfilePath string, options *types.ImageBuildOptions) error {
7682
pullSecretName := registry.GetRegistryAuthSecretName(b.RegistryURL)
83+
if b.PullSecretName != "" {
84+
pullSecretName = b.PullSecretName
85+
}
86+
7787
randString, _ := randutil.GenerateRandomString(12)
7888
buildID := strings.ToLower(randString)
7989
buildPod := &k8sv1.Pod{

pkg/devspace/config/v1/image.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ type BuildConfig struct {
1919

2020
// KanikoConfig tells the DevSpace CLI to build with Docker on Minikube or on localhost
2121
type KanikoConfig struct {
22-
Cache *bool `yaml:"cache"`
23-
Namespace *string `yaml:"namespace,omitempty"`
22+
Cache *bool `yaml:"cache"`
23+
Namespace *string `yaml:"namespace,omitempty"`
24+
PullSecret *string `yaml:"pullSecret,omitempty"`
2425
}
2526

2627
// DockerConfig tells the DevSpace CLI to build with Docker on Minikube or on localhost

pkg/devspace/image/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ func Build(client *kubernetes.Clientset, generatedConfig *generated.Config, imag
107107
allowInsecurePush = *registryConf.Insecure
108108
}
109109

110-
imageBuilder, err = kaniko.NewBuilder(registryURL, imageName, imageTag, (*generatedConfig).ImageTags[imageName], buildNamespace, client, allowInsecurePush)
110+
pullSecret := ""
111+
if imageConf.Build.Kaniko.PullSecret != nil {
112+
pullSecret = *imageConf.Build.Kaniko.PullSecret
113+
}
114+
115+
imageBuilder, err = kaniko.NewBuilder(registryURL, pullSecret, imageName, imageTag, (*generatedConfig).ImageTags[imageName], buildNamespace, client, allowInsecurePush)
111116
if err != nil {
112117
log.Fatalf("Error creating kaniko builder: %v", err)
113118
}

0 commit comments

Comments
 (0)