Skip to content

Commit

Permalink
Allow usage of shared public Docker Repository
Browse files Browse the repository at this point in the history
For easier usage on Kubernetes and cloud Docker Repositories a
single account can be used and specified via repository_url i.e.
docker.io/getfaas/ vs registry:5000/

Tested on Kubernetes with shared Docker Hub account.

Signed-off-by: Alex Ellis (VMware) <[email protected]>
  • Loading branch information
alexellis committed May 25, 2018
1 parent 8768321 commit fa166b3
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 26 deletions.
17 changes: 14 additions & 3 deletions buildshiprun/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ func Handle(req []byte) string {
imageName := strings.TrimSpace(string(buildStatus))

repositoryURL := os.Getenv("repository_url")
pushRepositoryURL := os.Getenv("push_repository_url")

if len(repositoryURL) == 0 {
fmt.Fprintf(os.Stderr, "repository_url env-var not set")
os.Exit(1)
}

if len(pushRepositoryURL) == 0 {
fmt.Fprintf(os.Stderr, "push_repository_url env-var not set")
os.Exit(1)
}

serviceValue := ""

log.Printf("buildshiprun: image '%s'\n", imageName)
Expand All @@ -75,7 +81,7 @@ func Handle(req []byte) string {
gatewayURL := os.Getenv("gateway_url")

// Replace image name for "localhost" for deployment
imageName = getImageName(repositoryURL, imageName)
imageName = getImageName(repositoryURL, pushRepositoryURL, imageName)

serviceValue = fmt.Sprintf("%s-%s", event.owner, event.service)

Expand All @@ -96,6 +102,8 @@ func Handle(req []byte) string {
"Git-Repo": event.repository,
"Git-DeployTime": strconv.FormatInt(time.Now().Unix(), 10), //Unix Epoch string
"Git-SHA": event.sha,
"faas_function": serviceValue,
"app": serviceValue,
},
Limits: Limits{
Memory: defaultMemoryLimit,
Expand Down Expand Up @@ -331,8 +339,11 @@ func buildStatus(status string, desc string, context string, url string) *github
return &github.RepoStatus{State: &status, TargetURL: &url, Description: &desc, Context: &context}
}

func getImageName(repositoryURL, imageName string) string {
return repositoryURL + imageName[strings.Index(imageName, "/"):]
func getImageName(repositoryURL, pushRepositoryURL, imageName string) string {

return strings.Replace(imageName, pushRepositoryURL, repositoryURL, 1)

// return repositoryURL + imageName[strings.Index(imageName, "/"):]
}

type eventInfo struct {
Expand Down
29 changes: 21 additions & 8 deletions buildshiprun/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,48 @@ func TestGetEvent_EmptyEnvVars(t *testing.T) {
func Test_GetImageName(t *testing.T) {

var imageNameTestcases = []struct {
Name string
RepositoryURL string
ImageName string
Output string
Name string
PushRepositoryURL string
RepositoryURL string
ImageName string
Output string
}{
{
"Test Docker Hub with user-prefix",
"docker.io/of-community/",
"docker.io/of-community/",
"docker.io/of-community/function-name/",
"docker.io/of-community/function-name/",
},
{
"Testcase1",
"registry:5000",
"127.0.0.1:5000",
"registry:5000/username/function-name/",
"127.0.0.1:5000/username/function-name/",
},
{
"Testcase2",
"registry:31115",
"127.0.0.1:31115",
"registry:31115/username/function-name/",
"127.0.0.1:31115/username/function-name/",
},
{
"Testcase3",
"registry:31115",
"127.0.0.1",
"registry:31115/username/function-name/",
"127.0.0.1/username/function-name/",
},
}

for _, testcase := range imageNameTestcases {
output := getImageName(testcase.RepositoryURL, testcase.ImageName)
if output != testcase.Output {
t.Errorf("%s failed!. got: %s, want: %s", testcase.Name, output, testcase.Output)
}
t.Run(testcase.Name, func(t *testing.T) {
output := getImageName(testcase.RepositoryURL, testcase.PushRepositoryURL, testcase.ImageName)
if output != testcase.Output {
t.Errorf("%s failed!. got: %s, want: %s", testcase.Name, output, testcase.Output)
}
})
}
}
62 changes: 53 additions & 9 deletions git-tar/function/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,70 @@ import (
"github.com/openfaas/faas-cli/stack"
)

func TestImageNameSuffix_WithoutLatestTag(t *testing.T) {
func Test_FormatImageShaTag_PrivateRepo_WithTag_NoStackPrefix(t *testing.T) {
function := &stack.Function{
Image: "func:0.2",
}

owner := "alexellis"
repo := "go-fns-tester"
sha := "04b8e44988"

name := formatImageShaTag("registry:5000", function, sha, owner, repo)

want := "registry:5000/" + owner + "/" + repo + "-func:0.2-04b8e44988"
if name != want {
t.Errorf("Want \"%s\", got \"%s\"", want, name)
}
}

func Test_FormatImageShaTag_PrivateRepo_WithTag(t *testing.T) {
function := &stack.Function{
Image: "alexellis2/func:0.2",
}

owner := "alexellis"
repo := "go-fns-tester"
sha := "04b8e44988"

name := formatImageShaTag("registry:5000", function, sha, owner, repo)

want := "registry:5000/" + owner + "/" + repo + "-func:0.2-04b8e44988"
if name != want {
t.Errorf("Want \"%s\", got \"%s\"", want, name)
}
}

func Test_FormatImageShaTag_PrivateRepo_NoTag(t *testing.T) {
function := &stack.Function{
Image: "alexellis2/func",
}

name := formatImageShaTag("registry:5000", function, "thesha")
want := "registry:5000/alexellis2/func:latest-thesha"
owner := "alexellis"
repo := "go-fns-tester"
sha := "04b8e44988"

name := formatImageShaTag("registry:5000", function, sha, owner, repo)

want := "registry:5000/" + owner + "/" + repo + "-func:latest-04b8e44988"
if name != want {
t.Errorf("Want %s, got %s.", want, name)
t.Errorf("Want \"%s\", got \"%s\"", want, name)
}
}

func TestImageNameSuffix_WithSpecificTag(t *testing.T) {
func Test_FormatImageShaTag_SharedRepo_NoTag(t *testing.T) {
function := &stack.Function{
Image: "alexellis2/func:0.1",
Image: "alexellis2/func",
}

name := formatImageShaTag("registry:5000", function, "thesha")
want := "registry:5000/alexellis2/func:0.1-thesha"
owner := "alexellis"
repo := "go-fns-tester"
sha := "04b8e44988"

name := formatImageShaTag("docker.io/of-community/", function, sha, owner, repo)

want := "docker.io/of-community/" + owner + "-" + repo + "-func:latest-04b8e44988"
if name != want {
t.Errorf("Want %s, got %s.", want, name)
t.Errorf("Want \"%s\", got \"%s\"", want, name)
}
}
24 changes: 20 additions & 4 deletions git-tar/function/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func makeTar(pushEvent sdk.PushEvent, filePath string, services *stack.Services)
os.Exit(1)
}

imageName := formatImageShaTag(pushRepositoryURL, &v, pushEvent.AfterCommitID)
imageName := formatImageShaTag(pushRepositoryURL, &v, pushEvent.AfterCommitID,
pushEvent.Repository.Owner.Login, pushEvent.Repository.Name)

config := cfg{
Ref: imageName,
}
Expand Down Expand Up @@ -135,17 +137,31 @@ func makeTar(pushEvent sdk.PushEvent, filePath string, services *stack.Services)
return tars, nil
}

func formatImageShaTag(registry string, function *stack.Function, sha string) string {
func formatImageShaTag(registry string, function *stack.Function, sha string, owner string, repo string) string {
tag := ":latest"

imageName := function.Image
tagIndex := strings.LastIndex(function.Image, ":")

if tagIndex > 0 {
tag = function.Image[tagIndex:]
imageName = function.Image[:tagIndex]
}

imageName = registry + "/" + imageName + tag + "-" + sha
return imageName
repoIndex := strings.LastIndex(imageName, "/")
if repoIndex > -1 {
imageName = imageName[repoIndex+1:]
}

var imageRef string
sharedRepo := strings.HasSuffix(registry, "/")
if sharedRepo {
imageRef = registry[:len(registry)-1] + "/" + owner + "-" + repo + "-" + imageName + tag + "-" + sha
} else {
imageRef = registry + "/" + owner + "/" + repo + "-" + imageName + tag + "-" + sha
}

return imageRef
}

func clone(pushEvent sdk.PushEvent) (string, error) {
Expand Down
Loading

0 comments on commit fa166b3

Please sign in to comment.