-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests to automate bundle and FBC image creation for plain bundles
Signed-off-by: jubittajohn <jujohn@redhat.com>
- Loading branch information
1 parent
fabd290
commit cab885b
Showing
11 changed files
with
444 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
schema: catalog-config | ||
packageName: plain | ||
channelData: | ||
- channelName: beta | ||
channelEntries: | ||
- entryVersion: 0.1.0 | ||
replaces: null | ||
skips: | ||
- 0.1.1 | ||
skipRange: null | ||
- entryVersion: 0.1.1 | ||
replaces: 0.1.0 | ||
skips: | ||
- null | ||
skipRange: null | ||
- channelName: foo | ||
channelEntries: | ||
- entryVersion: 0.1.0 | ||
replaces: null | ||
skips: | ||
- null | ||
skipRange: null | ||
- entryVersion: 0.1.1 | ||
replaces: 0.1.0 | ||
skips: | ||
- null | ||
skipRange: null | ||
bundleData: | ||
- bundleImage: localhost/testdata/bundles/plain-v0/plain:v0.1.0 | ||
bundleVersion: 0.1.0 | ||
- bundleImage: localhost/testdata/bundles/plain-v0/plain:v0.1.1 | ||
bundleVersion: 0.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
const ( | ||
schemaName = "catalog-config" | ||
) | ||
|
||
type ChannelEntry struct { | ||
EntryVersion string `json:"entryVersion"` | ||
Replaces string `json:"replaces,omitempty"` | ||
Skips []string `json:"skips,omitempty"` | ||
SkipRange string `json:"skipRange,omitempty"` | ||
} | ||
|
||
type ChannelData struct { | ||
ChannelName string `json:"channelName"` | ||
ChannelEntries []ChannelEntry `json:"channelEntries"` | ||
} | ||
|
||
type BundleData struct { | ||
BundleImage string `json:"bundleImage"` | ||
BundleVersion string `json:"bundleVersion"` | ||
} | ||
|
||
type Config struct { | ||
Schema string `json:"schema"` | ||
PackageName string `json:"packageName"` | ||
ChannelData []ChannelData `json:"channelData"` | ||
BundleData []BundleData `json:"bundleData"` | ||
} | ||
|
||
func ReadFile(f string) (*Config, error) { | ||
b, err := readFile(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var c Config | ||
err = yaml.Unmarshal(b, &c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if c.Schema != schemaName { | ||
return nil, fmt.Errorf("invalid schema: %q should be %q: %v", c.Schema, schemaName, err) | ||
} | ||
if c.PackageName == "" { | ||
return nil, errors.New("missing required package name") | ||
} | ||
if len(c.ChannelData) == 0 { | ||
return nil, errors.New("missing required channel information") | ||
} | ||
if len(c.BundleData) == 0 { | ||
return nil, errors.New("missing required bundle information") | ||
} | ||
|
||
return &c, nil | ||
} | ||
|
||
// overrideable func for mocking os.ReadFile | ||
var readFile = func(file string) ([]byte, error) { | ||
return os.ReadFile(file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package operatore2e | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/operator-framework/operator-registry/alpha/declcfg" | ||
"github.com/operator-framework/operator-registry/alpha/property" | ||
|
||
"github.com/operator-framework/operator-controller/test/operator-e2e/config" | ||
) | ||
|
||
const ( | ||
SchemaPackage = "olm.package" | ||
SchemaChannel = "olm.channel" | ||
SchemaBundle = "olm.bundle" | ||
SchemaBundleMediatype = "olm.bundle.mediatype" | ||
BundleMediatype = "plain+v0" | ||
) | ||
|
||
func CreateFBC(configFilePath string) (*declcfg.DeclarativeConfig, error) { | ||
config, err := config.ReadFile(configFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dPackage := formPackage(*config) | ||
dChannel := formChannel(*config) | ||
dBundle := formBundle(*config) | ||
|
||
fbc := declcfg.DeclarativeConfig{ | ||
Packages: []declcfg.Package{dPackage}, | ||
Channels: dChannel, | ||
Bundles: dBundle, | ||
} | ||
return &fbc, nil | ||
} | ||
|
||
func formPackage(config config.Config) declcfg.Package { | ||
packageFormed := declcfg.Package{ | ||
Schema: SchemaPackage, | ||
Name: config.PackageName, | ||
DefaultChannel: config.ChannelData[0].ChannelName, | ||
} | ||
return packageFormed | ||
} | ||
|
||
func formChannel(config config.Config) []declcfg.Channel { | ||
channelFormed := make([]declcfg.Channel, 0, len(config.ChannelData)) | ||
for _, channel := range config.ChannelData { | ||
channelEntries := formChannelEntries(config.PackageName, channel) | ||
channelFormed = append(channelFormed, declcfg.Channel{ | ||
Schema: SchemaChannel, | ||
Name: channel.ChannelName, | ||
Package: config.PackageName, | ||
Entries: channelEntries, | ||
}) | ||
} | ||
return channelFormed | ||
} | ||
|
||
func formChannelEntries(pkgName string, channel config.ChannelData) []declcfg.ChannelEntry { | ||
channelEntries := make([]declcfg.ChannelEntry, 0, len(channel.ChannelEntries)) | ||
for _, channelEntry := range channel.ChannelEntries { | ||
replace := "" | ||
if channelEntry.Replaces != "" { | ||
replace = pkgName + "." + channelEntry.Replaces | ||
} | ||
|
||
skip := []string{} | ||
if len(channelEntry.Skips) != 0 { | ||
for _, s := range channelEntry.Skips { | ||
if s != "" { | ||
skip = append(skip, s) | ||
} | ||
} | ||
} | ||
channelEntries = append(channelEntries, declcfg.ChannelEntry{ | ||
Name: pkgName + "." + channelEntry.EntryVersion, | ||
Replaces: replace, | ||
Skips: skip, | ||
SkipRange: channelEntry.SkipRange, | ||
}) | ||
} | ||
return channelEntries | ||
} | ||
|
||
func formBundle(config config.Config) []declcfg.Bundle { | ||
bundleFormed := make([]declcfg.Bundle, 0, len(config.BundleData)) | ||
for _, bundle := range config.BundleData { | ||
var properties []property.Property | ||
properties = append(properties, property.Property{ | ||
Type: SchemaPackage, | ||
Value: json.RawMessage(fmt.Sprintf(`{"packageName": "%s", "version": "%s"}`, config.PackageName, bundle.BundleVersion)), | ||
}) | ||
properties = append(properties, property.Property{ | ||
Type: SchemaBundleMediatype, | ||
Value: json.RawMessage(fmt.Sprintf(`"%s"`, BundleMediatype)), | ||
}) | ||
|
||
bundleFormed = append(bundleFormed, declcfg.Bundle{ | ||
Schema: SchemaBundle, | ||
Name: config.PackageName + "." + bundle.BundleVersion, | ||
Package: config.PackageName, | ||
Image: bundle.BundleImage, | ||
Properties: properties, | ||
}) | ||
} | ||
return bundleFormed | ||
} | ||
|
||
func WriteFBC(fbc declcfg.DeclarativeConfig, fbcFilePath string, fbcFileName string) error { | ||
var buf bytes.Buffer | ||
err := declcfg.WriteYAML(fbc, &buf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = os.Stat(fbcFilePath) | ||
if os.IsNotExist(err) { | ||
err := os.MkdirAll(fbcFilePath, 0755) | ||
if err != nil { | ||
fmt.Printf("Failed to create directory: %v\n", err) | ||
return err | ||
} | ||
} | ||
file, err := os.Create(fbcFilePath + "/" + fbcFileName) | ||
if err != nil { | ||
fmt.Printf("Failed to create file: %v\n", err) | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
err = os.WriteFile(fbcFilePath+"/"+fbcFileName, buf.Bytes(), 0600) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package operatore2e | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"text/template" | ||
) | ||
|
||
func generateDockerFile(dockerFilePath string, yamlFolderName string, dockerFileName string) error { | ||
t, err := template.New("dockerfile").Parse(dockerfileTmpl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
dockerFilePath = filepath.Join(dockerFilePath, dockerFileName) | ||
file, err := os.Create(dockerFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
_, err = file.WriteString("FROM scratch\n") | ||
if err != nil { | ||
return err | ||
} | ||
err = t.Execute(file, struct{ YamlDir string }{yamlFolderName}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
const dockerfileTmpl = `ADD {{.YamlDir}} /configs/{{.YamlDir}} | ||
` |
Oops, something went wrong.