Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit e024707

Browse files
authored
Merge pull request #189 from triggermesh/registry-project
"project" value added to registry configs
2 parents 9236507 + fbb938b commit e024707

File tree

8 files changed

+35
-11
lines changed

8 files changed

+35
-11
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,20 @@ script:
201201
After this, you may pass `--registry-secret gitlab-registry` parameter to `tm deploy` command (or in [serverless.yml](https://gitlab.com/knative-examples/functions/blob/master/serverless.yaml#L6)) so that Knative could authenticate against Gitlab registry.
202202
Gitlab registry doesn't provide permanent read-write token that can be used in CI, but it has job-specific `CI_JOB_TOKEN` with "write" permission which is valid only while CI job running and `CI_DEPLOY_PASSWORD` with read permission which we created before. Considering this, we can see that CLI `set registry-auth` command supports `--push` and `--pull` flags that indicates which secret must be used to push image and which for "pull" operations only. Resulting images will be stored under `registry.gitlab.com/username/project/function_name` path
203203

204+
### Custom registry name
205+
206+
While using a username as a registry identifier (docker.io/username) is a common practice, in some cases we must be able to use different values for an authentication and in destination URL (for example, [gcr.io](https://cloud.google.com/container-registry/docs/advanced-authentication#linux-macos)). Triggermesh CLI `set registry-auth` command provides such ability by exposing an optional `--project` argument which will be used as a part of the image URL instead of the username:
207+
208+
```
209+
TOKEN=$(gcloud auth print-access-token)
210+
tm set registry-auth gcr --registry eu.gcr.io --project my-org/my-project --username oauth2accesstoken --password $TOKEN
211+
tm generate python
212+
tm deploy -f python --registry-secret gcr --wait
213+
```
214+
215+
As a result, Knative service image will be pushed to `eu.gcr.io/my-org/my-project` registry
216+
217+
204218
#### Unauthenticated registry
205219

206220
Besides hosted registries, triggermesh CLI may work with unauthenticated registries which does not require setting access credentials. For such cases, you may simply add `--registry-host` argument to deployment command with registry domain name parameter and resulting image will be pushed to `registry-host/namespace/service_name` URL

cmd/set.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func cmdSetRegistryCreds(clientset *client.ConfigSet) *cobra.Command {
4949
}
5050

5151
setRegistryCredsCmd.Flags().StringVar(&rc.Host, "registry", "", "Registry host address")
52+
setRegistryCredsCmd.Flags().StringVar(&rc.ProjectID, "project", "", "If set, use this value instead of the username in image names. Example: gcr.io/<project>")
5253
setRegistryCredsCmd.Flags().StringVar(&rc.Username, "username", "", "Registry username")
5354
setRegistryCredsCmd.Flags().StringVar(&rc.Password, "password", "", "Registry password")
5455
setRegistryCredsCmd.Flags().BoolVar(&rc.Pull, "pull", false, "Indicates if this token must be used for pull operations only")

pkg/resources/build/create.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,14 @@ func (b *Build) imageName(clientset *client.ConfigSet) (string, error) {
404404
if len(config.Auths) > 1 {
405405
return "", errors.New("credentials with multiple registries not supported")
406406
}
407-
for k, v := range config.Auths {
408-
if url, ok := gitlabEnv(); ok {
409-
return fmt.Sprintf("%s/%s", url, b.Name), nil
407+
if url, ok := gitlabEnv(); ok {
408+
return fmt.Sprintf("%s/%s", url, b.Name), nil
409+
}
410+
for host, creds := range config.Auths {
411+
if config.Project != "" {
412+
return fmt.Sprintf("%s/%s/%s", host, config.Project, b.Name), nil
410413
}
411-
return fmt.Sprintf("%s/%s/%s", k, v.Username, b.Name), nil
414+
return fmt.Sprintf("%s/%s/%s", host, creds.Username, b.Name), nil
412415
}
413416
return "", errors.New("empty registry credentials")
414417
}

pkg/resources/build/types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ type Build struct {
2828
}
2929

3030
type registryAuths struct {
31-
Auths registry
31+
Project string
32+
Auths registry
3233
}
3334

3435
type registry map[string]credentials

pkg/resources/credential/registry-credentials.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (c *RegistryCreds) CreateRegistryCreds(clientset *client.ConfigSet) error {
3434
return err
3535
}
3636
}
37-
secret := fmt.Sprintf("{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\"}}}", c.Host, c.Username, c.Password)
37+
secret := fmt.Sprintf("{\"project\":%q,\"auths\":{%q:{\"username\":%q,\"password\":%q}}}", c.ProjectID, c.Host, c.Username, c.Password)
3838
if s, err := clientset.Core.CoreV1().Secrets(c.Namespace).Get(c.Name, metav1.GetOptions{}); err == nil {
3939
for k, v := range s.Data {
4040
secrets[k] = string(v)

pkg/resources/credential/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type RegistryCreds struct {
2525
Name string
2626
Namespace string
2727
Host string
28+
ProjectID string
2829
Username string
2930
Password string
3031
Pull bool

pkg/resources/taskrun/create.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,14 @@ func (tr *TaskRun) imageName(clientset *client.ConfigSet) (string, error) {
310310
if len(config.Auths) > 1 {
311311
return "", errors.New("credentials with multiple registries not supported")
312312
}
313-
for k, v := range config.Auths {
314-
if url, ok := gitlabEnv(); ok {
315-
return fmt.Sprintf("%s/%s", url, tr.Name), nil
313+
if url, ok := gitlabEnv(); ok {
314+
return fmt.Sprintf("%s/%s", url, tr.Name), nil
315+
}
316+
for host, creds := range config.Auths {
317+
if config.Project != "" {
318+
return fmt.Sprintf("%s/%s/%s", host, config.Project, tr.Name), nil
316319
}
317-
return fmt.Sprintf("%s/%s/%s", k, v.Username, tr.Name), nil
320+
return fmt.Sprintf("%s/%s/%s", host, creds.Username, tr.Name), nil
318321
}
319322
return "", errors.New("empty registry credentials")
320323
}

pkg/resources/taskrun/types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type Source struct {
4141
}
4242

4343
type registryAuths struct {
44-
Auths registry
44+
Project string
45+
Auths registry
4546
}
4647

4748
type credentials struct {

0 commit comments

Comments
 (0)