Skip to content

Commit

Permalink
[release-4.15] OCPBUGS-27946: Capability to override default channel (#…
Browse files Browse the repository at this point in the history
…786)

* OCPBUGS-385: Capability to override default channel

* Change field to string and rename to setDefaultChannel

* Correct changes from review

* Add config validation for defaultChannel

* Fix typo

---------

Co-authored-by: Luigi Mario Zuccarelli <luzuccar@redhat.com>
  • Loading branch information
openshift-cherrypick-robot and lmzuccarelli committed Feb 2, 2024
1 parent 6ddf902 commit cae37aa
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 1,457 deletions.
9 changes: 8 additions & 1 deletion pkg/api/v1alpha2/types_include_config.go
Expand Up @@ -27,6 +27,13 @@ type IncludePackage struct {

// All channels containing these bundles are parsed for an upgrade graph.
IncludeBundle `json:",inline"`

// New field added due to the following filed cases for oc-mirror
// - CASE03657982
// - CASE03655018
// - CASE03676821
// Ability to override default channel.
DefaultChannel string `json:"defaultChannel,omitempty"`
}

// IncludeChannel contains a name (required) and versions (optional)
Expand Down Expand Up @@ -66,7 +73,7 @@ func (ic *IncludeConfig) ConvertToDiffIncludeConfig() (dic diff.DiffIncludeConfi
return dic, fmt.Errorf("package %s: %v", pkg.Name, err)
}

dpkg := diff.DiffIncludePackage{Name: pkg.Name}
dpkg := diff.DiffIncludePackage{Name: pkg.Name, DefaultChannel: pkg.DefaultChannel}
switch {
case pkg.MinVersion != "" && pkg.MaxVersion != "":
dpkg.Range = fmt.Sprintf(">=%s <=%s", pkg.MinVersion, pkg.MaxVersion)
Expand Down
22 changes: 22 additions & 0 deletions pkg/cli/mirror/mirror.go
Expand Up @@ -285,11 +285,33 @@ func (o *MirrorOptions) Validate() error {
if err != nil {
return fmt.Errorf("unable to read the configuration file provided with --config: %v", err)
}

for _, op := range cfg.Mirror.Operators {
if op.IsFBCOCI() {
break
}
}

// check for defaultChannel
for _, op := range cfg.Mirror.Operators {
for _, pkg := range op.Packages {
if len(pkg.DefaultChannel) > 0 {
valid := false
for _, ch := range pkg.Channels {
// check that it's set in the channel stanza
if pkg.DefaultChannel == ch.Name {
valid = true
break
}
}
if !valid {
// if we get here it means that the channel has not been set with the same value as defaultChannel
return fmt.Errorf("defaultChannel has been set with '%s', please ensure that '%s' is declared in the channels section for the package '%s' in the config ", pkg.DefaultChannel, pkg.DefaultChannel, pkg.Name)
}
}
}
}

}

if o.SkipPruning {
Expand Down
8 changes: 8 additions & 0 deletions pkg/operator/diff/diff.go
Expand Up @@ -128,6 +128,13 @@ type DiffIncludePackage struct {
// and cannot combined with `Range` in `DiffIncludeChannel`.
// Range setting is mutually exclusive with channel versions/bundles/range settings.
Range string `json:"range,omitempty" yaml:"range,omitempty"`
// New field added due to the following filed cases for oc-mirror
// - CASE03657982
// - CASE03655018
// - CASE03676821
// - OCPBUGS-385
// Ability to override default channel.
DefaultChannel string `json:"defaultChannel,omitempty" yaml:"defaultChannel,omitempty"`
}

// DiffIncludeChannel contains a name (required) and versions (optional)
Expand Down Expand Up @@ -201,6 +208,7 @@ func convertIncludeConfigToIncluder(c DiffIncludeConfig) (includer diffInternal.
for pkgI, cpkg := range c.Packages {
pkg := &includer.Packages[pkgI]
pkg.Name = cpkg.Name
pkg.DefaultChannel = cpkg.DefaultChannel
pkg.AllChannels.Versions = cpkg.Versions
pkg.AllChannels.Bundles = cpkg.Bundles
if cpkg.Range != "" {
Expand Down
35 changes: 26 additions & 9 deletions pkg/operator/diff/internal/diff.go
Expand Up @@ -174,7 +174,6 @@ func (g *DiffGenerator) Run(oldModel, newModel model.Model) (model.Model, error)
}

}

}

if !g.SkipDependencies {
Expand All @@ -185,22 +184,40 @@ func (g *DiffGenerator) Run(oldModel, newModel model.Model) (model.Model, error)
}

// Default channel may not have been copied, so set it to the new default channel here.
for _, outputPkg := range outputModel {
for idx, outputPkg := range outputModel {
overrideSet := false
newPkg, found := newModel[outputPkg.Name]
if !found {
return nil, fmt.Errorf("package %s not present in the diff new model", outputPkg.Name)
}
var outputHasDefault bool
outputPkg.DefaultChannel, outputHasDefault = outputPkg.Channels[newPkg.DefaultChannel.Name]
if !outputHasDefault {
// Set the defaultChannel using the priority of a channel when the default got filtered out
// If no channels with the Priority property, raise an error
if err := setDefaultChannel(outputPkg, newPkg.DefaultChannel.Name); err != nil {
return nil, err
if !outputHasDefault && !overrideSet {
// OCPBUGS-385
// check if selectedDefaultChannel is the same as the channel being looked at
// if yes the then set it, this ensures we select the correct override for the selected
// package
for _, pkg := range g.Includer.Packages {
// check if the selectedDefaultChannel is valid
if len(pkg.DefaultChannel) > 0 {
overrideDefaultChannel, isValid := newPkg.Channels[pkg.DefaultChannel]
if pkg.Name == newPkg.Name && isValid {
outputModel[idx].DefaultChannel = overrideDefaultChannel
overrideSet = true
// no use continuing
break
}
}
}
if !overrideSet {
// Set the defaultChannel using the priority of a channel when the default got filtered out
// If no channels with the Priority property, raise an error
if err := setDefaultChannel(outputPkg, newPkg.DefaultChannel.Name); err != nil {
return nil, err
}
}
}
}

return outputModel, nil
}

Expand Down Expand Up @@ -262,7 +279,7 @@ This can be resolved by one of the following changes:
3) by changing the ImageSetConfiguration to filter channels or packages in such a way that it will include a package version that exists in the current default channel`)

// include a short message that does not mention any of the above to keep things simple
return fmt.Errorf("the current default channel %q for package %q could not be determined... ensure that your ImageSetConfiguration filtering criteria results in a package version that exists in the current default channel", newPackageDefaultChannelName, outputPkg.Name)
return fmt.Errorf("the current default channel %q for package %q could not be determined... ensure that your ImageSetConfiguration filtering criteria results in a package version that exists in the current default channel or use the 'defaultChannel' field ", newPackageDefaultChannelName, outputPkg.Name)
}

// pruneOldFromNewPackage prune any bundles and channels from newPkg that
Expand Down
6 changes: 6 additions & 0 deletions pkg/operator/diff/internal/diff_include.go
Expand Up @@ -51,6 +51,12 @@ type DiffIncludePackage struct {
// HeadsOnly is the mode that selects the head of the channels only.
// This setting will be overridden by any versions or bundles in the channels.
HeadsOnly bool
// New field added due to the following filed cases for oc-mirror
// - CASE03657982
// - CASE03655018
// - CASE03676821
// ability to override default channel
DefaultChannel string `json:"defaultChannel,omitempty"`
}

// DiffIncludeChannel specifies a channel, and optionally bundles and bundle versions
Expand Down
105 changes: 101 additions & 4 deletions pkg/operator/diff/internal/diff_test.go
Expand Up @@ -3805,7 +3805,108 @@ func TestSetDefaultChannelRange(t *testing.T) {
property.MustBuildPackage("ibm-mq", "1.7.0"),
},
},
},
},
},

{
name: "ibm-mq-test/Valid/OverrideDefaultChannel",
oldCfg: declcfg.DeclarativeConfig{},
newCfg: declcfg.DeclarativeConfig{
Packages: []declcfg.Package{
{Schema: declcfg.SchemaPackage, Name: "ibm-mq", DefaultChannel: "v1.8"},
},
Channels: []declcfg.Channel{
{Schema: declcfg.SchemaChannel, Name: "v1.8", Package: "ibm-mq", Entries: []declcfg.ChannelEntry{
{Name: "ibm-mq.v1.8.1"}},
Properties: []property.Property{
property.MustBuildChannelPriority("v1.8", 3),
},
},
{Schema: declcfg.SchemaChannel, Name: "v1.7", Package: "ibm-mq", Entries: []declcfg.ChannelEntry{
{Name: "ibm-mq.v1.7.0"}},
Properties: []property.Property{
property.MustBuildChannelPriority("v1.7", 1),
},
},
{Schema: declcfg.SchemaChannel, Name: "v1.6", Package: "ibm-mq", Entries: []declcfg.ChannelEntry{
{Name: "ibm-mq.v1.6.0"}},
Properties: []property.Property{
property.MustBuildChannelPriority("v1.6", 2),
},
},
},
Bundles: []declcfg.Bundle{
{
Schema: declcfg.SchemaBundle,
Name: "ibm-mq.v1.6.0",
Package: "ibm-mq",
Image: "reg/ibm-mq:latest",
Properties: []property.Property{
property.MustBuildPackage("ibm-mq", "1.6.0"),
},
},
{
Schema: declcfg.SchemaBundle,
Name: "ibm-mq.v1.7.0",
Package: "ibm-mq",
Image: "reg/ibm-mq:latest",
Properties: []property.Property{
property.MustBuildPackage("ibm-mq", "1.7.0"),
},
},
{
Schema: declcfg.SchemaBundle,
Name: "ibm-mq.v1.8.1",
Package: "ibm-mq",
Image: "reg/ibm-mq:latest",
Properties: []property.Property{
property.MustBuildPackage("ibm-mq", "1.8.1"),
},
},
}},
g: &DiffGenerator{
IncludeAdditively: false,
HeadsOnly: false,
SkipDependencies: true,
Includer: DiffIncluder{
Packages: []DiffIncludePackage{
{
Name: "ibm-mq",
Channels: []DiffIncludeChannel{
{
Name: "v1.7",
},
},
DefaultChannel: "v1.7",
},
},
},
},
expCfg: declcfg.DeclarativeConfig{
Packages: []declcfg.Package{
{Schema: declcfg.SchemaPackage, Name: "ibm-mq", DefaultChannel: "v1.7"},
},
Channels: []declcfg.Channel{
{Schema: declcfg.SchemaChannel, Name: "v1.7", Package: "ibm-mq", Entries: []declcfg.ChannelEntry{
{Name: "ibm-mq.v1.7.0"}},
Properties: []property.Property{
property.MustBuildChannelPriority("v1.7", 1),
},
},
},
Bundles: []declcfg.Bundle{
{
Schema: declcfg.SchemaBundle,
Name: "ibm-mq.v1.7.0",
Package: "ibm-mq",
Image: "reg/ibm-mq:latest",
Properties: []property.Property{
property.MustBuildPackage("ibm-mq", "1.7.0"),
},
},
},
},
},
}

Expand All @@ -3815,9 +3916,6 @@ func TestSetDefaultChannelRange(t *testing.T) {
s.assertion = require.NoError
}

//oldModel, err := declcfg.ConvertToModel(s.oldCfg)
//require.NoError(t, err)

newModel, err := declcfg.ConvertToModel(s.newCfg)
require.NoError(t, err)

Expand All @@ -3826,7 +3924,6 @@ func TestSetDefaultChannelRange(t *testing.T) {

if err := outputModel.Validate(); err != nil {
fmt.Println(err)
//return nil, err
}

outputCfg := declcfg.ConvertFromModel(outputModel)
Expand Down

0 comments on commit cae37aa

Please sign in to comment.