Skip to content

Commit

Permalink
allowing on cluster build for go runtime (knative#1445)
Browse files Browse the repository at this point in the history
* allowing on cluster build for go runtime

* warning message added for go and rust builder

* gofmt

* fixups

Signed-off-by: Matej Vasek <mvasek@redhat.com>

---------

Signed-off-by: Matej Vasek <mvasek@redhat.com>
Co-authored-by: Matej Vasek <mvasek@redhat.com>
  • Loading branch information
Shashankft9 and matejvasek committed Sep 27, 2023
1 parent 3742481 commit 34fdfd8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
16 changes: 10 additions & 6 deletions docs/building-functions/on_cluster_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion pkg/pipelines/tekton/pipelines_pac_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions pkg/pipelines/tekton/pipelines_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 17 additions & 8 deletions pkg/pipelines/tekton/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/pipelines/tekton/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34fdfd8

Please sign in to comment.