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

[wip]OTA-1010: pull GetImplicitlyEnabledCapabilities from the cvo repo #1908

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
55 changes: 55 additions & 0 deletions pkg/capability/capability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package capability

import (
"k8s.io/apimachinery/pkg/util/sets"

configv1 "github.com/openshift/api/config/v1"
)

// ClusterCapabilities represents various types of ClusterVersionCapabilities
type ClusterCapabilities struct {
hongkailiu marked this conversation as resolved.
Show resolved Hide resolved
// Known is a set of all known ClusterVersionCapabilities. It may grow as OpenShift evolves.
Known sets.Set[configv1.ClusterVersionCapability]
// Enabled is a set of the ClusterVersionCapabilities that are enabled on a cluster.
Enabled sets.Set[configv1.ClusterVersionCapability]
// ImplicitlyEnabled is a set of the ClusterVersionCapabilities that are implicitly enabled on a cluster.
// For example, "ImageRegistry" is a new ClusterVersionCapability in 4.14.
// ImageRegistry becomes implicitly enabled on a 4.14 cluster upgraded from 4.13 with ImageRegistry running already.
// https://docs.openshift.com/container-platform/4.17/installing/overview/cluster-capabilities.html
ImplicitlyEnabled sets.Set[configv1.ClusterVersionCapability]
}

// GetImplicitlyEnabledCapabilities returns the capabilities from
// a set of capabilities that are not (even implicitly) enabled in ClusterCapabilities:
func GetImplicitlyEnabledCapabilities(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one seems too simple to be exported.
Let me think.

capabilities sets.Set[configv1.ClusterVersionCapability],
enabledCapabilities sets.Set[configv1.ClusterVersionCapability],
clusterCapabilities ClusterCapabilities,
) sets.Set[configv1.ClusterVersionCapability] {
if capabilities == nil {
return nil
}
caps := capabilities.Difference(enabledCapabilities).Difference(clusterCapabilities.Enabled).Difference(clusterCapabilities.ImplicitlyEnabled)
return caps
}

// SetCapabilities populates a ClusterCapabilities object from ClusterVersion clusterCapabilities spec. This method also
// ensures that each capability in others is still enabled: if not enabled then enabled implicitly.
func SetCapabilities(config *configv1.ClusterVersion,
others sets.Set[configv1.ClusterVersionCapability]) ClusterCapabilities {
var capabilities ClusterCapabilities
capabilities.Known = sets.New[configv1.ClusterVersionCapability]()
for _, s := range configv1.ClusterVersionCapabilitySets {
capabilities.Known.Insert(s...)
}
key := configv1.ClusterVersionCapabilitySetCurrent
if config.Spec.Capabilities != nil && config.Spec.Capabilities.BaselineCapabilitySet != "" {
key = config.Spec.Capabilities.BaselineCapabilitySet
}
capabilities.Enabled = sets.New[configv1.ClusterVersionCapability](configv1.ClusterVersionCapabilitySets[key]...)
if config.Spec.Capabilities != nil {
capabilities.Enabled.Insert(config.Spec.Capabilities.AdditionalEnabledCapabilities...)
}
capabilities.ImplicitlyEnabled = others.Difference(capabilities.Enabled)
return capabilities
}
224 changes: 224 additions & 0 deletions pkg/capability/capability_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package capability

import (
"k8s.io/apimachinery/pkg/util/sets"
"testing"

"github.com/google/go-cmp/cmp"

configv1 "github.com/openshift/api/config/v1"
)

func TestGetImplicitlyEnabledCapabilities(t *testing.T) {
tests := []struct {
name string
capabilities sets.Set[configv1.ClusterVersionCapability]
enabledCaps sets.Set[configv1.ClusterVersionCapability]
clusterCapabilities ClusterCapabilities
expected sets.Set[configv1.ClusterVersionCapability]
}{
{name: "implicitly enable capability",
capabilities: sets.New[configv1.ClusterVersionCapability]("cap2"),
enabledCaps: sets.New[configv1.ClusterVersionCapability]("cap1", "cap3"),
clusterCapabilities: ClusterCapabilities{
Enabled: sets.New[configv1.ClusterVersionCapability]("cap1"),
},
expected: sets.New[configv1.ClusterVersionCapability]("cap2"),
},
{name: "no prior caps, implicitly enabled capability",
capabilities: sets.New[configv1.ClusterVersionCapability]("cap2"),
expected: sets.New[configv1.ClusterVersionCapability]("cap2"),
},
{name: "multiple implicitly enable capability",
capabilities: sets.New[configv1.ClusterVersionCapability]("cap4", "cap5", "cap6"),
enabledCaps: sets.New[configv1.ClusterVersionCapability]("cap1", "cap2", "cap3"),
expected: sets.New[configv1.ClusterVersionCapability]("cap4", "cap5", "cap6"),
},
{name: "no implicitly enable capability",
enabledCaps: sets.New[configv1.ClusterVersionCapability]("cap1", "cap3"),
capabilities: sets.New[configv1.ClusterVersionCapability]("cap1"),
clusterCapabilities: ClusterCapabilities{
Enabled: sets.New[configv1.ClusterVersionCapability]("cap1"),
},
},
{name: "prior cap, no updated caps, no implicitly enabled capability",
enabledCaps: sets.New[configv1.ClusterVersionCapability]("cap1"),
},
{name: "no implicitly enable capability, already enabled",
enabledCaps: sets.New[configv1.ClusterVersionCapability]("cap1", "cap2"),
capabilities: sets.New[configv1.ClusterVersionCapability]("cap2"),
clusterCapabilities: ClusterCapabilities{
Enabled: sets.New[configv1.ClusterVersionCapability]("cap1", "cap2"),
},
},
{name: "no implicitly enable capability, new cap but already enabled",
enabledCaps: sets.New[configv1.ClusterVersionCapability]("cap1"),
capabilities: sets.New[configv1.ClusterVersionCapability]("cap2"),
clusterCapabilities: ClusterCapabilities{
Enabled: sets.New[configv1.ClusterVersionCapability]("cap2"),
},
},
{name: "no implicitly enable capability, already implicitly enabled",
enabledCaps: sets.New[configv1.ClusterVersionCapability]("cap1"),
capabilities: sets.New[configv1.ClusterVersionCapability]("cap2"),
clusterCapabilities: ClusterCapabilities{
Enabled: sets.New[configv1.ClusterVersionCapability]("cap2"),
ImplicitlyEnabled: sets.New[configv1.ClusterVersionCapability]("cap2"),
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
caps := GetImplicitlyEnabledCapabilities(test.capabilities, test.enabledCaps, test.clusterCapabilities)
if diff := cmp.Diff(test.expected, caps); diff != "" {
t.Errorf("%s: Returned capacities differ from expected:\n%s", test.name, diff)
}
})
}
}

func TestSetCapabilities(t *testing.T) {
tests := []struct {
name string
config *configv1.ClusterVersion
others sets.Set[configv1.ClusterVersionCapability]
expected ClusterCapabilities
}{
{name: "capabilities nil",
config: &configv1.ClusterVersion{},
expected: ClusterCapabilities{
Known: sets.New[configv1.ClusterVersionCapability](configv1.KnownClusterVersionCapabilities...),
Enabled: sets.New[configv1.ClusterVersionCapability](configv1.ClusterVersionCapabilitySets[configv1.ClusterVersionCapabilitySetCurrent]...),
},
},
{name: "capabilities set not set",
config: &configv1.ClusterVersion{
Spec: configv1.ClusterVersionSpec{
Capabilities: &configv1.ClusterVersionCapabilitiesSpec{},
},
},
expected: ClusterCapabilities{
Known: sets.New[configv1.ClusterVersionCapability](configv1.KnownClusterVersionCapabilities...),
Enabled: sets.New[configv1.ClusterVersionCapability](configv1.ClusterVersionCapabilitySets[configv1.ClusterVersionCapabilitySetCurrent]...),
},
},
{name: "set capabilities None",
config: &configv1.ClusterVersion{
Spec: configv1.ClusterVersionSpec{
Capabilities: &configv1.ClusterVersionCapabilitiesSpec{
BaselineCapabilitySet: configv1.ClusterVersionCapabilitySetNone,
},
},
},
expected: ClusterCapabilities{
Known: sets.New[configv1.ClusterVersionCapability](configv1.KnownClusterVersionCapabilities...),
},
},
{name: "set capabilities 4_11",
config: &configv1.ClusterVersion{
Spec: configv1.ClusterVersionSpec{
Capabilities: &configv1.ClusterVersionCapabilitiesSpec{
BaselineCapabilitySet: configv1.ClusterVersionCapabilitySet4_11,
AdditionalEnabledCapabilities: []configv1.ClusterVersionCapability{},
},
},
},
expected: ClusterCapabilities{
Known: sets.New[configv1.ClusterVersionCapability](configv1.KnownClusterVersionCapabilities...),
Enabled: sets.New[configv1.ClusterVersionCapability](
configv1.ClusterVersionCapabilityBaremetal,
configv1.ClusterVersionCapabilityMarketplace,
configv1.ClusterVersionCapabilityOpenShiftSamples,
configv1.ClusterVersionCapabilityMachineAPI,
),
},
},
{name: "set capabilities vCurrent",
config: &configv1.ClusterVersion{
Spec: configv1.ClusterVersionSpec{
Capabilities: &configv1.ClusterVersionCapabilitiesSpec{
BaselineCapabilitySet: configv1.ClusterVersionCapabilitySetCurrent,
AdditionalEnabledCapabilities: []configv1.ClusterVersionCapability{},
},
},
},
expected: ClusterCapabilities{
Known: sets.New[configv1.ClusterVersionCapability](configv1.KnownClusterVersionCapabilities...),
Enabled: sets.New[configv1.ClusterVersionCapability](configv1.ClusterVersionCapabilitySets[configv1.ClusterVersionCapabilitySetCurrent]...),
},
},
{name: "set capabilities None with additional",
config: &configv1.ClusterVersion{
Spec: configv1.ClusterVersionSpec{
Capabilities: &configv1.ClusterVersionCapabilitiesSpec{
BaselineCapabilitySet: configv1.ClusterVersionCapabilitySetNone,
AdditionalEnabledCapabilities: []configv1.ClusterVersionCapability{"cap1", "cap2", "cap3"},
},
},
},
expected: ClusterCapabilities{
Known: sets.New[configv1.ClusterVersionCapability](configv1.KnownClusterVersionCapabilities...),
Enabled: sets.New[configv1.ClusterVersionCapability]("cap1", "cap2", "cap3"),
},
},
{name: "set capabilities 4_11 with additional",
config: &configv1.ClusterVersion{
Spec: configv1.ClusterVersionSpec{
Capabilities: &configv1.ClusterVersionCapabilitiesSpec{
BaselineCapabilitySet: configv1.ClusterVersionCapabilitySet4_11,
AdditionalEnabledCapabilities: []configv1.ClusterVersionCapability{"cap1", "cap2", "cap3"},
},
},
},
expected: ClusterCapabilities{
Known: sets.New[configv1.ClusterVersionCapability](configv1.KnownClusterVersionCapabilities...),
Enabled: sets.New[configv1.ClusterVersionCapability](
configv1.ClusterVersionCapabilityBaremetal,
configv1.ClusterVersionCapabilityMarketplace,
configv1.ClusterVersionCapabilityOpenShiftSamples,
configv1.ClusterVersionCapabilityMachineAPI,
"cap1",
"cap2",
"cap3"),
},
},
{name: "set capabilities 4_11 with additional and others",
config: &configv1.ClusterVersion{
Spec: configv1.ClusterVersionSpec{
Capabilities: &configv1.ClusterVersionCapabilitiesSpec{
BaselineCapabilitySet: configv1.ClusterVersionCapabilitySet4_11,
AdditionalEnabledCapabilities: []configv1.ClusterVersionCapability{"cap1", "cap2", "cap3"},
},
},
},
others: sets.New[configv1.ClusterVersionCapability](
configv1.ClusterVersionCapabilityMachineAPI,
"cap1",
"cap-implicitly",
),
expected: ClusterCapabilities{
Known: sets.New[configv1.ClusterVersionCapability](configv1.KnownClusterVersionCapabilities...),
Enabled: sets.New[configv1.ClusterVersionCapability](
configv1.ClusterVersionCapabilityBaremetal,
configv1.ClusterVersionCapabilityMarketplace,
configv1.ClusterVersionCapabilityOpenShiftSamples,
configv1.ClusterVersionCapabilityMachineAPI,
"cap1",
"cap2",
"cap3"),
ImplicitlyEnabled: sets.New[configv1.ClusterVersionCapability]("cap-implicitly"),
},
},
}
for _, test := range tests {
if test.name != "set capabilities 4_11 with additional and others" {
continue
}
t.Run(test.name, func(t *testing.T) {
caps := SetCapabilities(test.config, test.others)
if diff := cmp.Diff(test.expected, caps); diff != "" {
t.Errorf("%s: Returned capacities differ from expected:\n%s", test.name, diff)
}
})
}
}