1 Star 0 Fork 0

zhuchance/kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
secret_for_docker_registry.go 3.86 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubectl
import (
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/credentialprovider"
)
// SecretForDockerRegistryGeneratorV1 supports stable generation of a docker registry secret
type SecretForDockerRegistryGeneratorV1 struct {
// Name of secret (required)
Name string
// Username for registry (required)
Username string
// Email for registry (optional)
Email string
// Password for registry (required)
Password string
// Server for registry (required)
Server string
}
// Ensure it supports the generator pattern that uses parameter injection
var _ Generator = &SecretForDockerRegistryGeneratorV1{}
// Ensure it supports the generator pattern that uses parameters specified during construction
var _ StructuredGenerator = &SecretForDockerRegistryGeneratorV1{}
// Generate returns a secret using the specified parameters
func (s SecretForDockerRegistryGeneratorV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
err := ValidateParams(s.ParamNames(), genericParams)
if err != nil {
return nil, err
}
params := map[string]string{}
for key, value := range genericParams {
strVal, isString := value.(string)
if !isString {
return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
}
params[key] = strVal
}
delegate := &SecretForDockerRegistryGeneratorV1{
Name: params["name"],
Username: params["docker-username"],
Email: params["docker-email"],
Password: params["docker-password"],
Server: params["docker-server"],
}
return delegate.StructuredGenerate()
}
// StructuredGenerate outputs a secret object using the configured fields
func (s SecretForDockerRegistryGeneratorV1) StructuredGenerate() (runtime.Object, error) {
if err := s.validate(); err != nil {
return nil, err
}
dockercfgContent, err := handleDockercfgContent(s.Username, s.Password, s.Email, s.Server)
if err != nil {
return nil, err
}
secret := &api.Secret{}
secret.Name = s.Name
secret.Type = api.SecretTypeDockercfg
secret.Data = map[string][]byte{}
secret.Data[api.DockerConfigKey] = dockercfgContent
return secret, nil
}
// ParamNames returns the set of supported input parameters when using the parameter injection generator pattern
func (s SecretForDockerRegistryGeneratorV1) ParamNames() []GeneratorParam {
return []GeneratorParam{
{"name", true},
{"docker-username", true},
{"docker-email", false},
{"docker-password", true},
{"docker-server", true},
}
}
// validate validates required fields are set to support structured generation
func (s SecretForDockerRegistryGeneratorV1) validate() error {
if len(s.Name) == 0 {
return fmt.Errorf("name must be specified")
}
if len(s.Username) == 0 {
return fmt.Errorf("username must be specified")
}
if len(s.Password) == 0 {
return fmt.Errorf("password must be specified")
}
if len(s.Server) == 0 {
return fmt.Errorf("server must be specified")
}
return nil
}
// handleDockercfgContent serializes a dockercfg json file
func handleDockercfgContent(username, password, email, server string) ([]byte, error) {
dockercfgAuth := credentialprovider.DockerConfigEntry{
Username: username,
Password: password,
Email: email,
}
dockerCfg := map[string]credentialprovider.DockerConfigEntry{server: dockercfgAuth}
return json.Marshal(dockerCfg)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v1.7.4

搜索帮助

0d507c66 1850385 C8b1a773 1850385