Skip to content

Commit

Permalink
Do not return error when remote file is fetched (Fixes #126)
Browse files Browse the repository at this point in the history
This commit moves some code away from the build command, that manages
the loading of the base pipeline, task and the pipeline tasks into a
separate piece of code that lives in the same directory as the Tekton
backend. Some tests are also added to the codebase testing the correct
returning of errors. Finally, it fixes the issue where the
httpFileLoader returns an error wrapping a nil error even if the file
has been fetched from the remote source correctly.

Signed-off-by: Pavlos Tzianos <[email protected]>
  • Loading branch information
Pavlos Tzianos committed Apr 8, 2024
1 parent 8a2d76f commit 4d9fee8
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 89 deletions.
42 changes: 5 additions & 37 deletions cmd/draconctl/pipelines/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"path"
"strings"

"github.com/ocurity/dracon/pkg/components"
"github.com/ocurity/dracon/pkg/manifests"
"github.com/ocurity/dracon/pkg/pipelines"
"github.com/spf13/cobra"
tektonV1Beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"sigs.k8s.io/kustomize/api/types"
kustomizeType "sigs.k8s.io/kustomize/api/types"
)

var buildSubCmd = &cobra.Command{
Expand Down Expand Up @@ -46,7 +44,7 @@ func buildPipeline(cmd *cobra.Command, args []string) error {
}

// Parse Pipeline kustomization
kustomization := &types.Kustomization{}
kustomization := &kustomizeType.Kustomization{}
if err = kustomization.Unmarshal(fileContents); err != nil {
return fmt.Errorf("%s: could not unmarshal YAML file: %w", kustomizationPath, err)
}
Expand All @@ -61,40 +59,10 @@ func buildPipeline(cmd *cobra.Command, args []string) error {
return fmt.Errorf("you need to specify the base pipeline and task in the resources field of the kustomization")
}

var baseTaskPath string
var basePipeline *tektonV1Beta1.Pipeline

if basePipeline, err = manifests.LoadTektonV1Beta1Pipeline(cmd.Context(), kustomizationDir, kustomization.Resources[0]); err != nil {
if basePipeline, err = manifests.LoadTektonV1Beta1Pipeline(cmd.Context(), kustomizationDir, kustomization.Resources[1]); err != nil {
return err
}
baseTaskPath = kustomization.Resources[0]
} else {
baseTaskPath = kustomization.Resources[1]
}

baseTask, err := manifests.LoadTektonV1Beta1Task(cmd.Context(), kustomizationDir, baseTaskPath)
pipelineKustomization := &pipelines.Kustomization{Kustomization: kustomization, KustomizationDir: kustomizationDir}
basePipeline, taskList, err := pipelineKustomization.ResolveKustomizationResources(cmd.Context())
if err != nil {
return err
}

if len(kustomization.Components) == 0 {
return fmt.Errorf("%s: no components are listed in the kustomization", kustomizationPath)
}

taskList := []*tektonV1Beta1.Task{baseTask}
for _, pathOrURI := range kustomization.Components {
newTask, err := manifests.LoadTektonV1Beta1Task(cmd.Context(), kustomizationDir, pathOrURI)
if err != nil {
return err
}

if err = components.ValidateTask(newTask); err != nil {
return fmt.Errorf("%s: invalid task found: %w", newTask.Name, err)
}

newTask.Namespace = kustomization.Namespace
taskList = append(taskList, newTask)
return fmt.Errorf("%s: could not resolve base pipeline and tasks: %w", kustomizationDir, err)
}

k8sBackend, err := pipelines.NewTektonV1Beta1Backend(basePipeline, taskList, kustomization.NamePrefix, kustomization.NameSuffix)
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/avast/retry-go/v4 v4.3.3
github.com/aws/aws-sdk-go v1.17.7
github.com/elastic/go-elasticsearch/v8 v8.3.0
github.com/go-errors/errors v1.4.2
github.com/golang-migrate/migrate/v4 v4.15.1
github.com/google/uuid v1.6.0
github.com/hairyhenderson/go-codeowners v0.4.0
Expand Down Expand Up @@ -56,7 +57,6 @@ require (
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
Expand Down Expand Up @@ -132,7 +132,6 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.61.1 // indirect
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
Expand Down Expand Up @@ -167,11 +166,11 @@ require (
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v3 v3.0.1
gopkg.in/yaml.v3 v3.0.1 // indirect
)
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1757,9 +1757,6 @@ google.golang.org/grpc v1.41.0/go.mod h1:U3l9uK9J0sini8mHphKoXyaqDA/8VyGnDee1zzI
google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY=
google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 h1:rNBFJjBCOgVr9pWD7rs/knKL4FRTKgpZmsRfV214zcA=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0/go.mod h1:Dk1tviKTvMCz5tvh7t+fh94dhmQVHuCt2OzJB3CTW9Y=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.33.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
7 changes: 5 additions & 2 deletions pkg/manifests/httpfileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ func (hfl *httpFileLoader) Load(ctx context.Context) (content []byte, err error)
}()

if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("%s: could not fetch file: %w", hfl.uri, err)
return nil, fmt.Errorf("%s: could not fetch file: %d", hfl.uri, resp.StatusCode)
}

content, err = io.ReadAll(resp.Body)
return content, fmt.Errorf("%s: could not read body of response: %w", hfl.uri, err)
if err != nil {
return nil, fmt.Errorf("%s: could not read body of response: %w", hfl.uri, err)
}
return content, nil
}

func (hfl *httpFileLoader) Path() string {
Expand Down
Loading

0 comments on commit 4d9fee8

Please sign in to comment.