Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read architecture specific common templates file #1156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading