Skip to content

Commit

Permalink
Parse Build Strategies with Manifestival
Browse files Browse the repository at this point in the history
Use manifestival to parse the build strategies in the kodata/
buildstrategies directory. This will make it easier for tests to decode
YAML data for build strategies.

Signed-off-by: Adam Kaplan <[email protected]>
  • Loading branch information
adambkaplan committed Jun 4, 2024
1 parent db1f9a2 commit 0b2c93d
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions test/common.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package test

import (
"bytes"
"context"
"io/fs"
"os"
"fmt"
"path/filepath"
"time"

"github.com/manifestival/manifestival"
o "github.com/onsi/gomega"

apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/operator/pkg/common"
)

Expand Down Expand Up @@ -108,29 +105,18 @@ func ParseBuildStrategyNames() ([]string, error) {
}
strategyPath := filepath.Join(koDataPath, "samples", "buildstrategy")
sampleNames := []string{}
err = filepath.WalkDir(strategyPath, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
clusterBuildStrategy := &v1alpha1.ClusterBuildStrategy{}
decodeErr := decodeYaml(path, clusterBuildStrategy)
if decodeErr != nil {
return decodeErr
}
sampleNames = append(sampleNames, clusterBuildStrategy.Name)
return nil
})
manifest, err := manifestival.ManifestFrom(manifestival.Recursive(strategyPath))
if err != nil {
return nil, err
return sampleNames, err
}
return sampleNames, nil
}
for _, obj := range manifest.Resources() {
if obj.GetKind() == "ClusterBuildStrategy" {
sampleNames = append(sampleNames, obj.GetName())
}

func decodeYaml(path string, obj *v1alpha1.ClusterBuildStrategy) error {
yaml, err := os.ReadFile(path)
if err != nil {
return err
}
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(yaml), 16)
return decoder.Decode(obj)
if len(sampleNames) == 0 {
return sampleNames, fmt.Errorf("no ClusterBuildStrategy objects found in %s", strategyPath)
}
return sampleNames, nil
}

0 comments on commit 0b2c93d

Please sign in to comment.