Skip to content

Commit

Permalink
feat: read architecture specific common templates file
Browse files Browse the repository at this point in the history
This change prepares for architecture-specific delivery. The logic
will first attempt to locate the architecture-specific common
templates bundle. If it is not found, it will then fall back to the
generic bundle.

Signed-off-by: Nestor Acuna Blanco <nestor.acuna@ibm.com>
  • Loading branch information
nestoracunablanco committed Dec 3, 2024
1 parent 5abe053 commit 50f7390
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/controllers/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import (
"context"
"fmt"
"path/filepath"

"github.com/go-logr/logr"
v1 "github.com/openshift/api/config/v1"
Expand Down Expand Up @@ -50,8 +49,11 @@ func CreateControllers(ctx context.Context, apiReader client.Reader) ([]Controll
return nil, fmt.Errorf("failed to check if running on openshift: %w", err)
}

templatesFile := filepath.Join(templateBundleDir, "common-templates-"+common_templates.Version+".yaml")
templatesBundle, err := template_bundle.ReadBundle(templatesFile)
templatesBundleFile, err := template_bundle.RetrieveCommonTemplatesBundleFile(templateBundleDir)
if err != nil {
return nil, err
}
templatesBundle, err := template_bundle.ReadBundle(templatesBundleFile)
if err != nil {
return nil, fmt.Errorf("failed to read template bundle: %w", err)
}
Expand Down
27 changes: 27 additions & 0 deletions internal/template-bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"

templatev1 "github.com/openshift/api/template/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
cdiv1beta1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
common_templates "kubevirt.io/ssp-operator/internal/operands/common-templates"
)

const (
commonTemplatesFilePrefix = "common-templates-"
commonTemplatesFileSuffix = ".yaml"
)

type Bundle struct {
Expand Down Expand Up @@ -47,6 +54,26 @@ func ReadBundle(filename string) (Bundle, error) {
}, nil
}

func RetrieveCommonTemplatesBundleFile(templateBundleDir string) (string, error) {
archDependentFileName := filepath.Join(templateBundleDir, commonTemplatesFilePrefix+runtime.GOARCH+"-"+common_templates.Version+commonTemplatesFileSuffix)
archIndependentFileName := filepath.Join(templateBundleDir, commonTemplatesFilePrefix+common_templates.Version+commonTemplatesFileSuffix)
commonTemplatesExistingFiles := filterExistingTemplateFiles([]string{archDependentFileName, archIndependentFileName})
if len(commonTemplatesExistingFiles) == 0 {
return "", fmt.Errorf("failed to find common-templates bundles, none of the files were found: %s, %s", archDependentFileName, archIndependentFileName)
}
return commonTemplatesExistingFiles[0], nil
}

func filterExistingTemplateFiles(commonTemplatesBundleFiles []string) []string {
var filtered []string
for _, fileName := range commonTemplatesBundleFiles {
if _, err := os.Stat(fileName); err == nil {
filtered = append(filtered, fileName)
}
}
return filtered
}

func readTemplates(filename string) ([]templatev1.Template, error) {
var bundle []templatev1.Template
file, err := os.ReadFile(filename)
Expand Down
34 changes: 34 additions & 0 deletions internal/template-bundle/bundle_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package template_bundle

import (
"path/filepath"
"runtime"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
common_templates "kubevirt.io/ssp-operator/internal/operands/common-templates"
)

func retrieveSuffix() string {
Expand All @@ -21,13 +23,19 @@ var _ = Describe("Template bundle", Ordered, func() {
var (
testBundle Bundle
nameSuffix string
testDir string
)

BeforeAll(func() {
var err error
testBundle, err = ReadBundle("template-bundle-test.yaml")
Expect(err).ToNot(HaveOccurred())
nameSuffix = retrieveSuffix()
_, filename, _, ok := runtime.Caller(0)
Expect(ok).To(BeTrue())
fileAbsolutePath, err := filepath.Abs(filename)
Expect(err).NotTo(HaveOccurred())
testDir = filepath.Dir(fileAbsolutePath)
})

It("should correctly read templates", func() {
Expand Down Expand Up @@ -75,6 +83,32 @@ var _ = Describe("Template bundle", Ordered, func() {
Expect(ds2.Spec.Source.PVC.Name).To(Equal("win10"))
Expect(ds2.Spec.Source.PVC.Namespace).To(Equal("kubevirt-os-images"))
})

It("should throw an error retrieving the bundle file", func() {
_, err := RetrieveCommonTemplatesBundleFile(testDir + "/test-files/wrong-folder")
Expect(err.Error()).To(ContainSubstring("failed to find common-templates bundles, none of the files were found"))
})

It("should retrieve the bundle arch independent file", func() {
folderOnlyArchIndependent := testDir + "/test-files/arch-independent-only"
commonTemplatesBundleFile, err := RetrieveCommonTemplatesBundleFile(folderOnlyArchIndependent)
Expect(err).ToNot(HaveOccurred())
Expect(commonTemplatesBundleFile).To(Equal(folderOnlyArchIndependent + "/" + commonTemplatesFilePrefix + common_templates.Version + commonTemplatesFileSuffix))
})

It("should retrieve the bundle arch dependent file", func() {
folderOnlyArchDependent := testDir + "/test-files/arch-dependent-only"
commonTemplatesBundleFile, err := RetrieveCommonTemplatesBundleFile(folderOnlyArchDependent)
Expect(err).ToNot(HaveOccurred())
Expect(commonTemplatesBundleFile).To(Equal(folderOnlyArchDependent + "/" + commonTemplatesFilePrefix + runtime.GOARCH + "-" + common_templates.Version + commonTemplatesFileSuffix))
})

It("should retrieve the bundle arch dependent file when the generic one also exists", func() {
folderOnlyArchDependent := testDir + "/test-files/all-supported-archs"
commonTemplatesBundleFile, err := RetrieveCommonTemplatesBundleFile(folderOnlyArchDependent)
Expect(err).ToNot(HaveOccurred())
Expect(commonTemplatesBundleFile).To(Equal(folderOnlyArchDependent + "/" + commonTemplatesFilePrefix + runtime.GOARCH + "-" + common_templates.Version + commonTemplatesFileSuffix))
})
})

func TestTemplateBundle(t *testing.T) {
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 50f7390

Please sign in to comment.