Skip to content

Commit cbc9776

Browse files
committed
feature: support environment variable auth
1 parent 6ed0c85 commit cbc9776

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

Diff for: .github/ISSUE_TEMPLATE/bug-report.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,5 @@ Type: *bug report*
2525
### Environment
2626
- image-syncer version:
2727
- OS (e.g. `cat /etc/os-release`):
28-
- Kernel (e.g. `uname -a`):
29-
- Kubernetes version:
30-
- Install tools:
28+
- Registry version (e.g. `habor`):
3129
- Others:

Diff for: README-zh_CN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ ACR(Ali Container Registry) 是阿里云提供的容器镜像服务,ACR企业
7575

7676
"quay.io": { // 支持 "registry" 和 "registry/namespace"(v1.0.3之后的版本) 的形式,需要跟下面images中的registry(registry/namespace)对应
7777
// images中被匹配到的的url会使用对应账号密码进行镜像同步, 优先匹配 "registry/namespace" 的形式
78-
"username": "xxx", // 用户名,可选
79-
"password": "xxxxxxxxx", // 密码,可选
78+
"username": "xxx", // 用户名,可选,(v1.3.1 之后支持)valuse 使用 "${env}" 或者 "$env" 类型的字符串可以引用环境变量
79+
"password": "xxxxxxxxx", // 密码,可选,(v1.3.1 之后支持)valuse 使用 "${env}" 或者 "$env" 类型的字符串可以引用环境变量
8080
"insecure": true // registry是否是http服务,如果是,insecure 字段需要为true,默认是false,可选,支持这个选项需要image-syncer版本 > v1.0.1
8181
},
8282
"registry.cn-beijing.aliyuncs.com": {

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ Authentication file holds all the authentication information for each registry,
6262

6363
"quay.io": { // This "registry" or "registry/namespace" string should be the same as registry or registry/namespace used below in "images" field.
6464
// The format of "registry/namespace" will be more prior matched than "registry"
65-
"username": "xxx",
66-
"password": "xxxxxxxxx",
65+
"username": "xxx", // Optional, if the value is a string of "${env}" or "$env", image-syncer will try to find the value in environment variables, after v1.3.1
66+
"password": "xxxxxxxxx", // Optional, if the value is a string of "${env}" or "$env", image-syncer will try to find the value in environment variables, after v1.3.1
6767
"insecure": true // "insecure" field needs to be true if this registry is a http service, default value is false, version of image-syncer need to be later than v1.0.1 to support this field
6868
},
6969
"registry.cn-beijing.aliyuncs.com": {

Diff for: pkg/client/config.go

+19
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func NewSyncConfig(configFile, authFilePath, imageFilePath, defaultDestRegistry,
5555
return nil, fmt.Errorf("decode auth file %v error: %v", authFilePath, err)
5656
}
5757
}
58+
config.AuthList = expandEnv(config.AuthList)
5859

5960
if err := openAndDecode(imageFilePath, &config.ImageList); err != nil {
6061
return nil, fmt.Errorf("decode image file %v error: %v", imageFilePath, err)
@@ -116,3 +117,21 @@ func (c *Config) GetAuth(registry string, namespace string) (Auth, bool) {
116117
func (c *Config) GetImageList() map[string]string {
117118
return c.ImageList
118119
}
120+
121+
func expandEnv(authMap map[string]Auth) map[string]Auth {
122+
123+
result := make(map[string]Auth)
124+
125+
for registry, auth := range authMap {
126+
pwd := os.ExpandEnv(auth.Password)
127+
name := os.ExpandEnv(auth.Username)
128+
newAuth := Auth{
129+
Username: name,
130+
Password: pwd,
131+
Insecure: auth.Insecure,
132+
}
133+
result[registry] = newAuth
134+
}
135+
136+
return result
137+
}

0 commit comments

Comments
 (0)