/
client.go
76 lines (62 loc) · 1.89 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package registry
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/estahn/k8s-image-swapper/pkg/config"
"github.com/estahn/k8s-image-swapper/pkg/types"
ctypes "github.com/containers/image/v5/types"
)
// Client provides methods required to be implemented by the various target registry clients, e.g. ECR, Docker, Quay.
type Client interface {
CreateRepository(ctx context.Context, name string) error
RepositoryExists() bool
CopyImage(ctx context.Context, src ctypes.ImageReference, srcCreds string, dest ctypes.ImageReference, destCreds string) error
PullImage() error
PutImage() error
ImageExists(ctx context.Context, ref ctypes.ImageReference) bool
// Endpoint returns the domain of the registry
Endpoint() string
Credentials() string
// IsOrigin returns true if the imageRef originates from this registry
IsOrigin(imageRef ctypes.ImageReference) bool
}
type DockerConfig struct {
AuthConfigs map[string]AuthConfig `json:"auths"`
}
type AuthConfig struct {
Auth string `json:"auth,omitempty"`
}
// NewClient returns a registry client ready for use without the need to specify an implementation
func NewClient(r config.Registry) (Client, error) {
if err := config.CheckRegistryConfiguration(r); err != nil {
return nil, err
}
registry, err := types.ParseRegistry(r.Type)
if err != nil {
return nil, err
}
switch registry {
case types.RegistryAWS:
return NewECRClient(r.AWS)
case types.RegistryGCP:
return NewGARClient(r.GCP)
default:
return nil, fmt.Errorf(`registry of type "%s" is not supported`, r.Type)
}
}
func GenerateDockerConfig(c Client) ([]byte, error) {
dockerConfig := DockerConfig{
AuthConfigs: map[string]AuthConfig{
c.Endpoint(): {
Auth: base64.StdEncoding.EncodeToString([]byte(c.Credentials())),
},
},
}
dockerConfigJson, err := json.Marshal(dockerConfig)
if err != nil {
return []byte{}, err
}
return dockerConfigJson, nil
}