Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registry namespace #662

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func (a *initFnCmd) BuildFuncFileV20180708(c *cli.Context, path string) error {
if err != nil {
return err
}
buildImage = common.AddContainerNamespace(buildImage)
a.ff.Build_image = buildImage
}
if helper.IsMultiStage() {
Expand All @@ -431,6 +432,7 @@ func (a *initFnCmd) BuildFuncFileV20180708(c *cli.Context, path string) error {
if err != nil {
return err
}
runImage = common.AddContainerNamespace(runImage)
a.ff.Run_image = runImage
}
}
Expand Down
36 changes: 29 additions & 7 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ import (

// Global docker variables.
const (
FunctionsDockerImage = "fnproject/fnserver"
FuncfileDockerRuntime = "docker"
MinRequiredDockerVersion = "17.5.0"
FunctionsDockerImage = "fnproject/fnserver"
FuncfileDockerRuntime = "docker"
MinRequiredDockerVersion = "17.5.0"
ContainerRegistryNamespace = ""
//Change the containerRegistryNamespace while migrating the images to a different repository.
// The values should ghcr.io/ for github container registry, container-registry.oracle.com/ for OCR and docker.io or blank space for dockerhub.
//Currently keeping the value as blank due to cli_runtime_fallback_build test failure.
)

var GlobalVerbose bool
Expand Down Expand Up @@ -176,6 +180,8 @@ func imageStampFuncFile(fpath string, funcfile *FuncFile) (*FuncFile, error) {
if err != nil {
return funcfile, err
}
bi = AddContainerNamespace(bi)

funcfile.BuildImage = bi

// In case of multistage build there will be a runtime image
Expand All @@ -184,6 +190,8 @@ func imageStampFuncFile(fpath string, funcfile *FuncFile) (*FuncFile, error) {
if err != nil {
return funcfile, err
}
ri = AddContainerNamespace(ri)

funcfile.RunImage = ri
}

Expand Down Expand Up @@ -225,6 +233,7 @@ func imageStampFuncFileV20180708(fpath string, funcfile *FuncFileV20180708) (*Fu
if err != nil {
return funcfile, err
}
bi = AddContainerNamespace(bi)
funcfile.Build_image = bi

// In case of multistage build there will be a runtime image
Expand All @@ -233,6 +242,7 @@ func imageStampFuncFileV20180708(fpath string, funcfile *FuncFileV20180708) (*Fu
if err != nil {
return funcfile, nil
}
ri = AddContainerNamespace(ri)
funcfile.Run_image = ri
}

Expand Down Expand Up @@ -539,6 +549,7 @@ func writeTmpDockerfile(helper langs.LangHelper, dir string, ff *FuncFile) (stri
if err != nil {
return "", err
}
bi = AddContainerNamespace(bi)
}
if helper.IsMultiStage() {
// build stage
Expand All @@ -562,6 +573,7 @@ func writeTmpDockerfile(helper langs.LangHelper, dir string, ff *FuncFile) (stri
if err != nil {
return "", err
}
ri = AddContainerNamespace(ri)
}
dfLines = append(dfLines, fmt.Sprintf("FROM %s", ri))
dfLines = append(dfLines, "WORKDIR /function")
Expand Down Expand Up @@ -600,6 +612,7 @@ func writeTmpDockerfileV20180708(helper langs.LangHelper, dir string, ff *FuncFi
if err != nil {
return "", err
}
bi = AddContainerNamespace(bi)
}
if helper.IsMultiStage() {
// build stage
Expand All @@ -617,6 +630,7 @@ func writeTmpDockerfileV20180708(helper langs.LangHelper, dir string, ff *FuncFi
if err != nil {
return "", err
}
ri = AddContainerNamespace(ri)
}
dfLines = append(dfLines, fmt.Sprintf("FROM %s", ri))
dfLines = append(dfLines, "WORKDIR /function")
Expand Down Expand Up @@ -848,7 +862,7 @@ func ListFnsAndTriggersInApp(c *cli.Context, client *fnclient.Fn, app *modelsv2.
return fns, trs, nil
}

//DeleteFunctions deletes all the functions provided to it. if provided nil it is a no-op
// DeleteFunctions deletes all the functions provided to it. if provided nil it is a no-op
func DeleteFunctions(c *cli.Context, client *fnclient.Fn, fns []*modelsv2.Fn) error {
if fns == nil {
return nil
Expand All @@ -865,7 +879,7 @@ func DeleteFunctions(c *cli.Context, client *fnclient.Fn, fns []*modelsv2.Fn) er
return nil
}

//DeleteTriggers deletes all the triggers provided to it. if provided nil it is a no-op
// DeleteTriggers deletes all the triggers provided to it. if provided nil it is a no-op
func DeleteTriggers(c *cli.Context, client *fnclient.Fn, triggers []*modelsv2.Trigger) error {
if triggers == nil {
return nil
Expand All @@ -882,7 +896,7 @@ func DeleteTriggers(c *cli.Context, client *fnclient.Fn, triggers []*modelsv2.Tr
return nil
}

//ListFnsInApp gets all the functions associated with an app
// ListFnsInApp gets all the functions associated with an app
func ListFnsInApp(c *cli.Context, client *fnclient.Fn, app *modelsv2.App) ([]*modelsv2.Fn, error) {
params := &apifns.ListFnsParams{
Context: context.Background(),
Expand Down Expand Up @@ -910,7 +924,7 @@ func ListFnsInApp(c *cli.Context, client *fnclient.Fn, app *modelsv2.App) ([]*mo
return resFns, nil
}

//ListTriggersInFunc gets all the triggers associated with a function
// ListTriggersInFunc gets all the triggers associated with a function
func ListTriggersInFunc(c *cli.Context, client *fnclient.Fn, fn *modelsv2.Fn) ([]*modelsv2.Trigger, error) {
params := &apitriggers.ListTriggersParams{
Context: context.Background(),
Expand Down Expand Up @@ -1025,3 +1039,11 @@ func untarStream(r io.Reader) error {
}
}
}

// func to add the container registry namespace before the image names
func AddContainerNamespace(imageName string) string {

registryNamespace := ContainerRegistryNamespace
fullImageName := fmt.Sprintf("%s%s", registryNamespace, imageName)
return fullImageName
}
20 changes: 20 additions & 0 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,23 @@ func Test_proxyArgs(t *testing.T) {
}
}
}

func Test_addNamespace(t *testing.T) {
testCases := []struct {
image string
fullImageName string
}{
{image: "fnproject/go", fullImageName: ContainerRegistryNamespace + "fnproject/go"},
{image: "fnproject/dotnet", fullImageName: ContainerRegistryNamespace + "fnproject/dotnet"},
}

for _, c := range testCases {
t.Run(c.image, func(t *testing.T) {
imageName := AddContainerNamespace(c.image)
if imageName != c.fullImageName {
t.Fatalf("expected %s but got %s", c.fullImageName, imageName)
}
t.Logf("Output %s", imageName)
})
}
}
21 changes: 14 additions & 7 deletions test/cli_runtime_fallback_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package test

import (
"fmt"
"github.com/fnproject/cli/langs"
"github.com/fnproject/cli/testharness"
"strings"
"testing"

"github.com/fnproject/cli/common"
"github.com/fnproject/cli/langs"
"github.com/fnproject/cli/testharness"
)

const (
Expand All @@ -45,11 +47,11 @@ entrypoint: ruby func.rb`
)

/*
This test case check for backwards compatibility with older cli func.yaml file
Cases Tested:
1. During `fn build` make sure Build_image and Run_image are stamped in func.yaml file
2. Function container is build using proper fallback runtime and dev image, check
by invoking container and fetching runtime version, should match with fallback version.
This test case check for backwards compatibility with older cli func.yaml file
Cases Tested:
1. During `fn build` make sure Build_image and Run_image are stamped in func.yaml file
2. Function container is build using proper fallback runtime and dev image, check
by invoking container and fetching runtime version, should match with fallback version.
*/
func TestFnBuildWithOlderRuntimeWithoutVersion(t *testing.T) {
t.Run("`fn invoke` should return the fallback ruby version", func(t *testing.T) {
Expand Down Expand Up @@ -85,10 +87,15 @@ func TestFnBuildWithOlderRuntimeWithoutVersion(t *testing.T) {
if err != nil {
panic(err)
}
bi = common.AddContainerNamespace(bi)
fmt.Println(bi)

ri, err := fallBackHandler.RunFromImage()
if err != nil {
panic(err)
}
ri = common.AddContainerNamespace(ri)
fmt.Println(ri)

updatedFuncFile := h.GetYamlFile("func.yaml")
if bi == "" || ri == "" {
Expand Down