-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(private-containers): private registries
refs akash-network/support#54 Signed-off-by: Adam Bozanich <[email protected]>
- Loading branch information
Showing
6 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package builder | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
mani "github.com/akash-network/akash-api/go/manifest/v2beta2" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type ServiceCredentials interface { | ||
NS() string | ||
Name() string | ||
Create() (*corev1.Secret, error) | ||
Update(obj *corev1.Secret) (*corev1.Secret, error) | ||
} | ||
|
||
type serviceCredentials struct { | ||
ns string | ||
serviceName string | ||
credentials *mani.ServiceImageCredentials | ||
// TODO: labels for deleting | ||
// labels map[string]string | ||
} | ||
|
||
func NewServiceCredentials(ns string, serviceName string, credentials *mani.ServiceImageCredentials) ServiceCredentials { | ||
return serviceCredentials{ | ||
ns: ns, | ||
serviceName: serviceName, | ||
credentials: credentials, | ||
} | ||
} | ||
|
||
func (b serviceCredentials) NS() string { | ||
return b.ns | ||
} | ||
|
||
func (b serviceCredentials) Name() string { | ||
return fmt.Sprintf("docker-creds-%v", b.serviceName) | ||
} | ||
|
||
func (b serviceCredentials) Create() (*corev1.Secret, error) { | ||
// see https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/create/create_secret_docker.go#L280-L298 | ||
|
||
data, err := b.encodeSecret() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
obj := &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: b.ns, | ||
Name: b.Name(), | ||
}, | ||
Data: map[string][]byte{ | ||
corev1.DockerConfigJsonKey: data, | ||
}, | ||
Type: corev1.SecretTypeDockerConfigJson, | ||
} | ||
|
||
return obj, nil | ||
} | ||
|
||
func (b serviceCredentials) Update(obj *corev1.Secret) (*corev1.Secret, error) { | ||
// see https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/create/create_secret_docker.go#L280-L298 | ||
|
||
data, err := b.encodeSecret() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
obj.Data = map[string][]byte{ | ||
corev1.DockerConfigJsonKey: data, | ||
} | ||
obj.Type = corev1.SecretTypeDockerConfigJson | ||
return obj, nil | ||
} | ||
|
||
type dockerCredentialsEntry struct { | ||
Username string `json:"username,omitempty"` | ||
Password string `json:"password,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
Auth string `json:"auth,omitempty"` | ||
} | ||
|
||
type dockerCredentials struct { | ||
Auths map[string]dockerCredentialsEntry `json:"auths"` | ||
} | ||
|
||
func (b serviceCredentials) encodeSecret() ([]byte, error) { | ||
entry := dockerCredentialsEntry{ | ||
Username: strings.TrimSpace(b.credentials.Username), | ||
Password: strings.TrimSpace(b.credentials.Password), | ||
Email: strings.TrimSpace(b.credentials.Email), | ||
Auth: encodeAuth(strings.TrimSpace(b.credentials.Username), strings.TrimSpace(b.credentials.Password)), | ||
} | ||
creds := dockerCredentials{ | ||
Auths: map[string]dockerCredentialsEntry{ | ||
b.credentials.Host: entry, | ||
}, | ||
} | ||
return json.Marshal(creds) | ||
} | ||
|
||
func encodeAuth(username, password string) string { | ||
value := username + ":" + password | ||
return base64.StdEncoding.EncodeToString([]byte(value)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters