From 5e1596d81b8e3007f0d855f33ddbcab1a2de9ba4 Mon Sep 17 00:00:00 2001 From: Bhashkarjya Nath Date: Thu, 6 Apr 2023 12:41:24 +0530 Subject: [PATCH 1/2] add container namespace --- commands/init.go | 2 ++ common/common.go | 33 +++++++++++++++++++------ common/common_test.go | 20 +++++++++++++++ test/cli_runtime_fallback_build_test.go | 21 ++++++++++------ 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/commands/init.go b/commands/init.go index 23ec647d..baa96cb0 100644 --- a/commands/init.go +++ b/commands/init.go @@ -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() { @@ -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 } } diff --git a/common/common.go b/common/common.go index 94e5c76a..09107a95 100644 --- a/common/common.go +++ b/common/common.go @@ -50,9 +50,10 @@ import ( // Global docker variables. const ( - FunctionsDockerImage = "fnproject/fnserver" - FuncfileDockerRuntime = "docker" - MinRequiredDockerVersion = "17.5.0" + FunctionsDockerImage = "fnproject/fnserver" + FuncfileDockerRuntime = "docker" + MinRequiredDockerVersion = "17.5.0" + ContainerRegistryNamespace = "docker.io/" ) var GlobalVerbose bool @@ -176,6 +177,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 @@ -184,6 +187,8 @@ func imageStampFuncFile(fpath string, funcfile *FuncFile) (*FuncFile, error) { if err != nil { return funcfile, err } + ri = AddContainerNamespace(ri) + funcfile.RunImage = ri } @@ -225,6 +230,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 @@ -233,6 +239,7 @@ func imageStampFuncFileV20180708(fpath string, funcfile *FuncFileV20180708) (*Fu if err != nil { return funcfile, nil } + ri = AddContainerNamespace(ri) funcfile.Run_image = ri } @@ -539,6 +546,7 @@ func writeTmpDockerfile(helper langs.LangHelper, dir string, ff *FuncFile) (stri if err != nil { return "", err } + bi = AddContainerNamespace(bi) } if helper.IsMultiStage() { // build stage @@ -562,6 +570,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") @@ -600,6 +609,7 @@ func writeTmpDockerfileV20180708(helper langs.LangHelper, dir string, ff *FuncFi if err != nil { return "", err } + bi = AddContainerNamespace(bi) } if helper.IsMultiStage() { // build stage @@ -617,6 +627,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") @@ -848,7 +859,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 @@ -865,7 +876,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 @@ -882,7 +893,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(), @@ -910,7 +921,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(), @@ -1025,3 +1036,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 +} diff --git a/common/common_test.go b/common/common_test.go index 85f450dc..7bad9bcd 100644 --- a/common/common_test.go +++ b/common/common_test.go @@ -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) + }) + } +} diff --git a/test/cli_runtime_fallback_build_test.go b/test/cli_runtime_fallback_build_test.go index da36093f..857cf1dc 100644 --- a/test/cli_runtime_fallback_build_test.go +++ b/test/cli_runtime_fallback_build_test.go @@ -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 ( @@ -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) { @@ -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 == "" { From 97765b9b4c05aa7f920b2b13e9e6a25270668395 Mon Sep 17 00:00:00 2001 From: Bhashkarjya Nath Date: Thu, 6 Apr 2023 12:46:18 +0530 Subject: [PATCH 2/2] keep containerRegistryNamespace as blank to pull from dockerhub --- common/common.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/common/common.go b/common/common.go index 09107a95..341bd6a4 100644 --- a/common/common.go +++ b/common/common.go @@ -53,7 +53,10 @@ const ( FunctionsDockerImage = "fnproject/fnserver" FuncfileDockerRuntime = "docker" MinRequiredDockerVersion = "17.5.0" - ContainerRegistryNamespace = "docker.io/" + 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