From 34fdfd85988c21c1501987ba22e8674c20482560 Mon Sep 17 00:00:00 2001 From: Shashank Sharma <48708039+Shashankft9@users.noreply.github.com> Date: Fri, 28 Jul 2023 17:32:10 +0530 Subject: [PATCH] allowing on cluster build for go runtime (#1445) * allowing on cluster build for go runtime * warning message added for go and rust builder * gofmt * fixups Signed-off-by: Matej Vasek --------- Signed-off-by: Matej Vasek Co-authored-by: Matej Vasek --- docs/building-functions/on_cluster_build.md | 16 +++++++----- .../tekton/pipelines_pac_provider.go | 7 +++++- pkg/pipelines/tekton/pipelines_provider.go | 8 ++++-- pkg/pipelines/tekton/validate.go | 25 +++++++++++++------ pkg/pipelines/tekton/validate_test.go | 2 +- 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/docs/building-functions/on_cluster_build.md b/docs/building-functions/on_cluster_build.md index 097ac4e976..77f429f30b 100644 --- a/docs/building-functions/on_cluster_build.md +++ b/docs/building-functions/on_cluster_build.md @@ -34,12 +34,16 @@ git remote add origin git@github.com:my-repo/my-function.git ``` 4. Update the Function configuration in `func.yaml` to enable on cluster builds for the Git repository: ```yaml -build: git # required, specify `git` build type -git: - url: https://github.com/my-repo/my-function.git # required, git repository with the function source code - revision: main # optional, git revision to be used (branch, tag, commit) - # contextDir: myfunction # optional, needed only if the function is not located - # in the repository root folder +build: + git: + url: https://github.com/my-repo/my-function.git # required, git repository with the function source code + revision: main # optional, git revision to be used (branch, tag, commit) + # contextDir: myfunction # optional, needed only if the function is not located in the repository root folder + # builderImages: # optional, needed only if the runtime is golang + # pack: ghcr.io/boson-project/go-function-builder:tip + buildpacks: [] + builder: "" + buildEnvs: [] ``` 5. Implement the business logic of your Function, then commit and push changes ```bash diff --git a/pkg/pipelines/tekton/pipelines_pac_provider.go b/pkg/pipelines/tekton/pipelines_pac_provider.go index b5b1447418..caad5bf94a 100644 --- a/pkg/pipelines/tekton/pipelines_pac_provider.go +++ b/pkg/pipelines/tekton/pipelines_pac_provider.go @@ -30,9 +30,14 @@ func (pp *PipelinesProvider) ConfigurePAC(ctx context.Context, f fn.Function, me return fmt.Errorf("incorrect type of pipelines metadata: %T", metadata) } - if err := validatePipeline(f); err != nil { + var warningMsg string + var err error + if warningMsg, err = validatePipeline(f); err != nil { return err } + if warningMsg != "" { + pp.progressListener.Increment(warningMsg) + } if data.ConfigureLocalResources { if err := pp.createLocalPACResources(ctx, f); err != nil { diff --git a/pkg/pipelines/tekton/pipelines_provider.go b/pkg/pipelines/tekton/pipelines_provider.go index 9d01c7ab19..a053214fc1 100644 --- a/pkg/pipelines/tekton/pipelines_provider.go +++ b/pkg/pipelines/tekton/pipelines_provider.go @@ -118,10 +118,14 @@ func NewPipelinesProvider(opts ...Opt) *PipelinesProvider { // After the PipelineRun is being initialized, the progress of the PipelineRun is being watched and printed to the output. func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error { pp.progressListener.Increment("Creating Pipeline resources") - - if err := validatePipeline(f); err != nil { + var warningMsg string + var err error + if warningMsg, err = validatePipeline(f); err != nil { return err } + if warningMsg != "" { + pp.progressListener.Increment(warningMsg) + } client, namespace, err := NewTektonClientAndResolvedNamespace(pp.namespace) if err != nil { diff --git a/pkg/pipelines/tekton/validate.go b/pkg/pipelines/tekton/validate.go index 16f4670291..9551654c12 100644 --- a/pkg/pipelines/tekton/validate.go +++ b/pkg/pipelines/tekton/validate.go @@ -5,6 +5,7 @@ import ( "fmt" "knative.dev/func/pkg/builders" + "knative.dev/func/pkg/builders/buildpacks" "knative.dev/func/pkg/builders/s2i" fn "knative.dev/func/pkg/functions" ) @@ -21,28 +22,36 @@ type ErrRuntimeNotSupported struct { } func (e ErrRuntimeNotSupported) Error() string { - return fmt.Sprintf("runtime %q is not supported for on cluster build", e.Runtime) + return fmt.Sprintf("runtime %q is not supported for on cluster build with default builders", e.Runtime) } -func validatePipeline(f fn.Function) error { +func validatePipeline(f fn.Function) (string, error) { + var warningMsg string if f.Build.Builder == builders.Pack { if f.Runtime == "" { - return ErrRuntimeRequired + return "", ErrRuntimeRequired } if f.Runtime == "go" || f.Runtime == "rust" { - return ErrRuntimeNotSupported{f.Runtime} + builder := f.Build.BuilderImages[builders.Pack] + defaultBuilder := buildpacks.DefaultBuilderImages[f.Runtime] + if builder != "" && builder != defaultBuilder { + warningMsg = fmt.Sprintf("runtime %q is not supported for on cluster build with default builders, "+ + "continuing with the custom builder provided", f.Runtime) + } else { + return "", ErrRuntimeNotSupported{f.Runtime} + } } if len(f.Build.Buildpacks) > 0 { - return ErrBuilpacksNotSupported + return "", ErrBuilpacksNotSupported } } else if f.Build.Builder == builders.S2I { _, err := s2i.BuilderImage(f, builders.S2I) - return err + return "", err } else { - return builders.ErrUnknownBuilder{Name: f.Build.Builder} + return "", builders.ErrUnknownBuilder{Name: f.Build.Builder} } - return nil + return warningMsg, nil } diff --git a/pkg/pipelines/tekton/validate_test.go b/pkg/pipelines/tekton/validate_test.go index 72f1b2a3db..2983bd6367 100644 --- a/pkg/pipelines/tekton/validate_test.go +++ b/pkg/pipelines/tekton/validate_test.go @@ -108,7 +108,7 @@ func Test_validatePipeline(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := validatePipeline(tt.function) + _, err := validatePipeline(tt.function) if (err != nil) != tt.wantErr { t.Errorf("validatePipeline() error = %v, wantErr %v", err, tt.wantErr) return