Skip to content

Commit

Permalink
Merge pull request #1156 from nestoracunablanco/feat/selectArchFileIf…
Browse files Browse the repository at this point in the history
…Exists

feat: read architecture specific common templates file
  • Loading branch information
kubevirt-bot authored Dec 9, 2024
2 parents f4e12b2 + ca7e46f commit 5b3a952
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 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
14 changes: 14 additions & 0 deletions internal/template-bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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"
)

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

func RetrieveCommonTemplatesBundleFile(templateBundleDir string) (string, error) {
archDependentFileName := filepath.Join(templateBundleDir, fmt.Sprintf("common-templates-%s-%s.yaml", runtime.GOARCH, common_templates.Version))
if _, err := os.Stat(archDependentFileName); err == nil {
return archDependentFileName, nil
}
archIndependentFileName := filepath.Join(templateBundleDir, fmt.Sprintf("common-templates-%s.yaml", common_templates.Version))
if _, err := os.Stat(archIndependentFileName); err == nil {
return archIndependentFileName, nil
}
return "", fmt.Errorf("failed to find common-templates bundles, none of the files were found: %s, %s", archDependentFileName, archIndependentFileName)
}

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

import (
"fmt"
"os"
"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 @@ -19,8 +23,11 @@ func retrieveSuffix() string {

var _ = Describe("Template bundle", Ordered, func() {
var (
testBundle Bundle
nameSuffix string
testBundle Bundle
nameSuffix string
tmpDir string
archIndependentFile string
archDependentFile string
)

BeforeAll(func() {
Expand All @@ -30,6 +37,12 @@ var _ = Describe("Template bundle", Ordered, func() {
nameSuffix = retrieveSuffix()
})

BeforeEach(func() {
tmpDir = GinkgoT().TempDir()
archIndependentFile = filepath.Join(tmpDir, fmt.Sprintf("common-templates-%s.yaml", common_templates.Version))
archDependentFile = filepath.Join(tmpDir, fmt.Sprintf("common-templates-%s-%s.yaml", runtime.GOARCH, common_templates.Version))
})

It("should correctly read templates", func() {
templates := testBundle.Templates
Expect(templates).To(HaveLen(4))
Expand Down Expand Up @@ -75,6 +88,37 @@ 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(tmpDir)
Expect(err).To(MatchError(ContainSubstring("failed to find common-templates bundles, none of the files were found")))
})

It("should retrieve the bundle arch independent file", func() {
err := os.WriteFile(archIndependentFile, []byte(""), 0644)
Expect(err).ToNot(HaveOccurred())
commonTemplatesBundleFile, err := RetrieveCommonTemplatesBundleFile(tmpDir)
Expect(err).ToNot(HaveOccurred())
Expect(commonTemplatesBundleFile).To(Equal(archIndependentFile))
})

It("should retrieve the bundle arch dependent file", func() {
err := os.WriteFile(archDependentFile, []byte(""), 0644)
Expect(err).ToNot(HaveOccurred())
commonTemplatesBundleFile, err := RetrieveCommonTemplatesBundleFile(tmpDir)
Expect(err).ToNot(HaveOccurred())
Expect(commonTemplatesBundleFile).To(Equal(archDependentFile))
})

It("should retrieve the bundle arch dependent file when the generic one also exists", func() {
err := os.WriteFile(archIndependentFile, []byte(""), 0644)
Expect(err).ToNot(HaveOccurred())
err = os.WriteFile(archDependentFile, []byte(""), 0644)
Expect(err).ToNot(HaveOccurred())
commonTemplatesBundleFile, err := RetrieveCommonTemplatesBundleFile(tmpDir)
Expect(err).ToNot(HaveOccurred())
Expect(commonTemplatesBundleFile).To(Equal(archDependentFile))
})
})

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

0 comments on commit 5b3a952

Please sign in to comment.