forked from estahn/k8s-image-swapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(registry): Add generic registry for source, not target
This allows auth to a generic source registry, such as dockerhub. This partially solves estahn#50
- Loading branch information
Showing
9 changed files
with
215 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package registry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
ctypes "github.com/containers/image/v5/types" | ||
"github.com/estahn/k8s-image-swapper/pkg/config" | ||
) | ||
|
||
type GenericClient struct { | ||
options config.GenericOptions | ||
} | ||
|
||
func NewGenericClient(clientConfig config.Generic) (*GenericClient, error) { | ||
client := GenericClient{} | ||
|
||
client.options = clientConfig.GenericOptions | ||
|
||
return &client, nil | ||
} | ||
|
||
func (g *GenericClient) CreateRepository(ctx context.Context, name string) error { | ||
return nil | ||
} | ||
|
||
func (g *GenericClient) RepositoryExists() bool { | ||
return true | ||
} | ||
|
||
func (g *GenericClient) CopyImage(ctx context.Context, src ctypes.ImageReference, srcCreds string, dest ctypes.ImageReference, destCreds string) error { | ||
panic("implement me") | ||
} | ||
|
||
func (g *GenericClient) PullImage() error { | ||
panic("implement me") | ||
} | ||
|
||
func (g *GenericClient) PutImage() error { | ||
panic("implement me") | ||
} | ||
|
||
func (g *GenericClient) ImageExists(ctx context.Context, ref ctypes.ImageReference) bool { | ||
return true | ||
} | ||
|
||
// Endpoint returns the domain of the registry | ||
func (g *GenericClient) Endpoint() string { | ||
return g.options.Domain | ||
} | ||
|
||
func (g *GenericClient) Credentials() string { | ||
return fmt.Sprintf("%s:%s", g.options.Username, g.options.Password) | ||
} | ||
|
||
// IsOrigin returns true if the imageRef originates from this registry | ||
func (g *GenericClient) IsOrigin(imageRef ctypes.ImageReference) bool { | ||
return true | ||
} | ||
|
||
// For testing purposes | ||
func NewDummyGenericClient(domain string, options config.GenericOptions) *GenericClient { | ||
return &GenericClient{ | ||
options: options, | ||
} | ||
} |
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,26 @@ | ||
package registry | ||
|
||
import ( | ||
"encoding/base64" | ||
"testing" | ||
|
||
"github.com/estahn/k8s-image-swapper/pkg/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenericDockerConfig(t *testing.T) { | ||
fakeToken := []byte("username:password") | ||
fakeBase64Token := base64.StdEncoding.EncodeToString(fakeToken) | ||
|
||
expected := []byte("{\"auths\":{\"docker.io\":{\"auth\":\"" + fakeBase64Token + "\"}}}") | ||
|
||
fakeRegistry := NewDummyGenericClient("docker.io", config.GenericOptions{ | ||
Domain: "docker.io", | ||
Username: "username", | ||
Password: "password", | ||
}) | ||
|
||
r, _ := GenerateDockerConfig(fakeRegistry) | ||
|
||
assert.Equal(t, r, expected) | ||
} |
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