diff --git a/Makefile b/Makefile index 461eaa79b2..217ebb6844 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,8 @@ KVER ?= $(shell git describe --tags --match 'knative-*') LDFLAGS := -X knative.dev/func/pkg/version.Vers=$(VERS) -X knative.dev/func/pkg/version.Kver=$(KVER) -X knative.dev/func/pkg/version.Hash=$(HASH) -FUNC_UTILS_IMG ?= ghcr.io/knative/func-utils:v2 +#FUNC_UTILS_IMG ?= ghcr.io/knative/func-utils:v2 +FUNC_UTILS_IMG ?= quay.io/dfridric/my-func-util LDFLAGS += -X knative.dev/func/pkg/k8s.SocatImage=$(FUNC_UTILS_IMG) LDFLAGS += -X knative.dev/func/pkg/k8s.TarImage=$(FUNC_UTILS_IMG) LDFLAGS += -X knative.dev/func/pkg/pipelines/tekton.FuncUtilImage=$(FUNC_UTILS_IMG) diff --git a/cmd/func-util/main.go b/cmd/func-util/main.go index e5fc4cc311..75106ee7fa 100644 --- a/cmd/func-util/main.go +++ b/cmd/func-util/main.go @@ -97,31 +97,38 @@ func scaffold(ctx context.Context) error { } appRoot := filepath.Join(f.Root, ".s2i", "builds", "last") + if f.Build.Builder != "s2i" { + // TODO: gauron99 - change this completely + // non-s2i override + appRoot = filepath.Join(f.Root, ".func", "builds", "last") + } + fmt.Printf("appRoot is '%s'\n", appRoot) _ = os.RemoveAll(appRoot) + // build step now includes scaffolding for go-pack err = scaffolding.Write(appRoot, f.Root, f.Runtime, f.Invoke, embeddedRepo.FS()) if err != nil { return fmt.Errorf("cannot write the scaffolding: %w", err) } - if err := os.MkdirAll(filepath.Join(f.Root, ".s2i", "bin"), 0755); err != nil { - return fmt.Errorf("unable to create .s2i bin dir. %w", err) - } - - var asm string - switch f.Runtime { - case "go": - asm = s2i.GoAssembler - case "python": - asm = s2i.PythonAssembler - default: - panic("unreachable") - } + if f.Build.Builder == "s2i" { + if err := os.MkdirAll(filepath.Join(f.Root, ".s2i", "bin"), 0755); err != nil { + return fmt.Errorf("unable to create .s2i bin dir. %w", err) + } + var asm string + switch f.Runtime { + case "go": + asm = s2i.GoAssembler + case "python": + asm = s2i.PythonAssembler + default: + panic("unreachable") + } - if err := os.WriteFile(filepath.Join(f.Root, ".s2i", "bin", "assemble"), []byte(asm), 0755); err != nil { - return fmt.Errorf("unable to write go assembler. %w", err) + if err := os.WriteFile(filepath.Join(f.Root, ".s2i", "bin", "assemble"), []byte(asm), 0755); err != nil { + return fmt.Errorf("unable to write go assembler. %w", err) + } } - return nil } diff --git a/hack/images.sh b/hack/images.sh index 53137c7715..65ac956e8a 100755 --- a/hack/images.sh +++ b/hack/images.sh @@ -5,6 +5,9 @@ set -o nounset set -o pipefail FUNC_UTILS_IMG="localhost:50000/knative/func-utils:v2" +if [ -n "$GITHUB_ENV" ]; then + echo "FUNC_UTILS_IMG=$FUNC_UTILS_IMG" >> $GITHUB_ENV +fi CGO_ENABLED=0 go build -o "func-util" -trimpath -ldflags '-w -s' ./cmd/func-util diff --git a/pkg/builders/buildpacks/builder.go b/pkg/builders/buildpacks/builder.go index a341b08226..ac6a3266e6 100644 --- a/pkg/builders/buildpacks/builder.go +++ b/pkg/builders/buildpacks/builder.go @@ -23,13 +23,14 @@ import ( "knative.dev/func/pkg/builders" "knative.dev/func/pkg/docker" fn "knative.dev/func/pkg/functions" + "knative.dev/func/pkg/scaffolding" ) // DefaultName when no WithName option is provided to NewBuilder const DefaultName = builders.Pack -var DefaultBaseBuilder = "ghcr.io/knative/builder-jammy-base:latest" -var DefaultTinyBuilder = "ghcr.io/knative/builder-jammy-tiny:latest" +var DefaultBaseBuilder = "ghcr.io/gauron99/builder-jammy-base:latest" +var DefaultTinyBuilder = "ghcr.io/gauron99/builder-jammy-tiny:latest" var ( DefaultBuilderImages = map[string]string{ @@ -46,6 +47,7 @@ var ( // Ensure that all entries in this list are terminated with a trailing "/" // See GHSA-5336-2g3f-9g3m for details trustedBuilderImagePrefixes = []string{ + "quay.io/dfridric/", "quay.io/boson/", "gcr.io/paketo-buildpacks/", "docker.io/paketobuildpacks/", @@ -116,7 +118,7 @@ func WithTimestamp(v bool) Option { } } -var DefaultLifecycleImage = "docker.io/buildpacksio/lifecycle:553c041" +var DefaultLifecycleImage = "docker.io/buildpacksio/lifecycle:3659764" // Build the Function at path. func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platform) (err error) { @@ -171,6 +173,16 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf Volumes []string }{Network: "", Volumes: nil}, } + + // NOTE: gauron99 - this might be even created into a Client function and + // ran before the client.Build() all together (in the CLI). There are gonna + // be commonalitites across the builders for scaffolding with some nuances + // which could be handled by each "scaffolder" - similarly to builders. + // scaffold + if err = scaffold(f); err != nil { + return + } + if b.withTimestamp { now := time.Now() opts.CreationTime = &now @@ -186,6 +198,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf opts.Env["BPE_DEFAULT_LISTEN_ADDRESS"] = "[::]:8080" } + // go specific workdir set to where main is + if f.Runtime == "go" { + if _, ok := opts.Env["BP_GO_WORKDIR"]; !ok { + opts.Env["BP_GO_WORKDIR"] = ".func/builds/last" + } + } var bindings = make([]string, 0, len(f.Build.Mounts)) for _, m := range f.Build.Mounts { bindings = append(bindings, fmt.Sprintf("%s:%s", m.Source, m.Destination)) @@ -312,3 +330,32 @@ type ErrRuntimeNotSupported struct { func (e ErrRuntimeNotSupported) Error() string { return fmt.Sprintf("Pack builder has no default builder image for the '%v' language runtime. Please provide one.", e.Runtime) } + +// TODO: gauron99 - unify this with other builders; temporary for the go pack +// +// scaffold the project +func scaffold(f fn.Function) error { + // Scafffolding is currently only supported by the Go runtime + // Python currently uses an injector instead of this + if f.Runtime != "go" { + return nil + } + + contextDir := filepath.Join(".func", "builds", "last") + appRoot := filepath.Join(f.Root, contextDir) + _ = os.RemoveAll(appRoot) + + // The embedded repository contains the scaffolding code itself which glues + // together the middleware and a function via main + embeddedRepo, err := fn.NewRepository("", "") // default is the embedded fs + if err != nil { + return fmt.Errorf("unable to load the embedded scaffolding. %w", err) + } + + // Write scaffolding to .func/builds/last + err = scaffolding.Write(appRoot, f.Root, f.Runtime, f.Invoke, embeddedRepo.FS()) + if err != nil { + return fmt.Errorf("unable to build due to a scaffold error. %w", err) + } + return nil +} diff --git a/pkg/builders/buildpacks/builder_test.go b/pkg/builders/buildpacks/builder_test.go index d2af0bc70d..58cdf13a47 100644 --- a/pkg/builders/buildpacks/builder_test.go +++ b/pkg/builders/buildpacks/builder_test.go @@ -86,11 +86,11 @@ func TestBuild_BuildpacksDefault(t *testing.T) { var ( i = &mockImpl{} b = NewBuilder(WithImpl(i)) - f = fn.Function{Runtime: "go"} + f = fn.Function{Runtime: "node"} ) i.BuildFn = func(ctx context.Context, opts pack.BuildOptions) error { - expected := defaultBuildpacks["go"] + expected := defaultBuildpacks["node"] if !reflect.DeepEqual(expected, opts.Buildpacks) { t.Fatalf("expected buildpacks '%v', got '%v'", expected, opts.Buildpacks) } @@ -141,7 +141,7 @@ func TestBuild_BuilderImageExclude(t *testing.T) { b = NewBuilder( // Func Builder logic WithName(builders.Pack), WithImpl(i)) f = fn.Function{ - Runtime: "go", + Runtime: "node", } ) funcIgnoreContent := []byte(`#testing comments diff --git a/pkg/k8s/dialer.go b/pkg/k8s/dialer.go index 0dff3c4773..f8549af3b9 100644 --- a/pkg/k8s/dialer.go +++ b/pkg/k8s/dialer.go @@ -275,6 +275,8 @@ func (c *contextDialer) startDialerPod(ctx context.Context) (err error) { } }() + fmt.Printf("[DEBUG] pkg/k8s/dialer.go: Using SocatImage = %s\n", SocatImage) + pod := &coreV1.Pod{ ObjectMeta: metaV1.ObjectMeta{ Name: c.podName, diff --git a/pkg/k8s/persistent_volumes.go b/pkg/k8s/persistent_volumes.go index c50ae15511..ed22c1c444 100644 --- a/pkg/k8s/persistent_volumes.go +++ b/pkg/k8s/persistent_volumes.go @@ -76,6 +76,7 @@ var TarImage = "ghcr.io/knative/func-utils:v2" // UploadToVolume uploads files (passed in form of tar stream) into volume. func UploadToVolume(ctx context.Context, content io.Reader, claimName, namespace string) error { + fmt.Printf("[DEBUG] pkg/k8s/persistent_volumes.go: Using TarImage = %s\n", TarImage) return runWithVolumeMounted(ctx, TarImage, []string{"sh", "-c", "umask 0000 && exec tar -xmf -"}, content, claimName, namespace) } diff --git a/pkg/pipelines/tekton/pipelines_provider.go b/pkg/pipelines/tekton/pipelines_provider.go index 540f995c0d..9ccceb6ab5 100644 --- a/pkg/pipelines/tekton/pipelines_provider.go +++ b/pkg/pipelines/tekton/pipelines_provider.go @@ -106,6 +106,7 @@ func NewPipelinesProvider(opts ...Opt) *PipelinesProvider { // Returned is the final url, and the input Function with the final results of the run populated // (f.Deploy.Image and f.Deploy.Namespace) or an error. func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) (string, fn.Function, error) { + fmt.Println("#### pp.Run") var err error // Checks builder and registry: diff --git a/pkg/pipelines/tekton/tasks.go b/pkg/pipelines/tekton/tasks.go index 4a9c584904..4e5fb34a3d 100644 --- a/pkg/pipelines/tekton/tasks.go +++ b/pkg/pipelines/tekton/tasks.go @@ -2,17 +2,21 @@ package tekton import ( "fmt" + "os" "strings" ) var ( - FuncUtilImage = "ghcr.io/knative/func-utils:v2" - DeployerImage string - ScaffoldImage string - S2IImage string + FuncUtilImage = utilsImage() + FuncUtilImageDefault = "ghcr.io/knative/func-utils:v2" + DeployerImage string + ScaffoldImage string + S2IImage string ) func init() { + fmt.Printf("[DEBUG] pkg/pipelines/tekton/tasks.go: Initial FuncUtilImage = %s\n", FuncUtilImage) + if DeployerImage == "" { DeployerImage = FuncUtilImage } @@ -22,6 +26,19 @@ func init() { if S2IImage == "" { S2IImage = FuncUtilImage } + + fmt.Printf("[DEBUG] pkg/pipelines/tekton/tasks.go: DeployerImage = %s\n", DeployerImage) + fmt.Printf("[DEBUG] pkg/pipelines/tekton/tasks.go: ScaffoldImage = %s\n", ScaffoldImage) + fmt.Printf("[DEBUG] pkg/pipelines/tekton/tasks.go: S2IImage = %s\n", S2IImage) +} + +func utilsImage() string { + if val := os.Getenv("FUNC_UTILS_IMG"); val != "" { + fmt.Printf("Using '%v' utils image\n", val) + return val + } + fmt.Printf("Using '%v' utils image\n", FuncUtilImageDefault) + return FuncUtilImageDefault } func getBuildpackTask() string { diff --git a/pkg/pipelines/tekton/templates.go b/pkg/pipelines/tekton/templates.go index 9410c5bac5..1ca2840180 100644 --- a/pkg/pipelines/tekton/templates.go +++ b/pkg/pipelines/tekton/templates.go @@ -342,6 +342,7 @@ func getTaskSpec(taskYaml string) (string, error) { // createAndApplyPipelineTemplate creates and applies Pipeline template for a standard on-cluster build // all resources are created on the fly, if there's a Pipeline defined in the project directory, it is used instead func createAndApplyPipelineTemplate(f fn.Function, namespace string, labels map[string]string) error { + fmt.Println("#### createAndApplyPipelineTemplate") // If Git is set up create fetch task and reference it from build task, // otherwise sources have been already uploaded to workspace PVC. gitCloneTaskRef := "" @@ -385,14 +386,16 @@ func createAndApplyPipelineTemplate(f fn.Function, namespace string, labels map[ default: return builders.ErrBuilderNotSupported{Builder: f.Build.Builder} } - + fmt.Printf("## BI: '%v'\n", data.BuilderImage) return createAndApplyResource(f.Root, pipelineFileName, template, "pipeline", getPipelineName(f), namespace, data) } // createAndApplyPipelineRunTemplate creates and applies PipelineRun template for a standard on-cluster build // all resources are created on the fly, if there's a PipelineRun defined in the project directory, it is used instead func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels map[string]string) error { + fmt.Println("createAndApplyPipelineRunTemplate") contextDir := f.Build.Git.ContextDir + fmt.Printf("contextDir: '%v'\n", contextDir) if contextDir == "" && f.Build.Builder == builders.S2I { // TODO(lkingland): could instead update S2I to interpret empty string // as cwd, such that builder-specific code can be kept out of here. @@ -413,6 +416,11 @@ func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels m } } + // add BP_GO_WORKDIR for go-build buildpack + if f.Runtime == "go" { + buildEnvs = append(buildEnvs, "BP_GO_WORKDIR=.func/builds/last") + } + s2iImageScriptsUrl := defaultS2iImageScriptsUrl if f.Runtime == "quarkus" { s2iImageScriptsUrl = quarkusS2iImageScriptsUrl @@ -445,7 +453,7 @@ func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels m RepoUrl: f.Build.Git.URL, Revision: pipelinesTargetBranch, } - + fmt.Printf("# data.BuilderImage: '%v'\n", data.BuilderImage) var template string switch f.Build.Builder { case builders.Pack: diff --git a/pkg/scaffolding/scaffold.go b/pkg/scaffolding/scaffold.go index 85c292ac2c..7b8e18f3f2 100644 --- a/pkg/scaffolding/scaffold.go +++ b/pkg/scaffolding/scaffold.go @@ -31,7 +31,7 @@ import ( // fs: filesytem which contains scaffolding at '[runtime]/scaffolding' // (exclusive with 'repo') func Write(out, src, runtime, invoke string, fs filesystem.Filesystem) (err error) { - + fmt.Println("#### scaffolding.Write") // detect the signature of the source code in the given location, presuming // a runtime and invocation hint (default "http") s, err := detectSignature(src, runtime, invoke)