Skip to content
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
60 changes: 60 additions & 0 deletions pkg/cli/admin/upgrade/recommend/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"fmt"
"strings"

configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/api/features"
routev1 "github.com/openshift/api/route/v1"
routev1client "github.com/openshift/client-go/route/clientset/versioned/typed/route/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
Comment thread
nbottari9 marked this conversation as resolved.

"github.com/openshift/oc/pkg/cli/admin/inspectalerts"
"github.com/openshift/oc/pkg/cli/admin/upgrade/status"
Expand All @@ -20,6 +23,12 @@ import (
// and Unknown when we do not have enough information to make a
// happy-or-sad determination.
func (o *options) alerts(ctx context.Context) ([]acceptableCondition, error) {
if skip, err := o.alertsEvaluatedByCVO(ctx); err != nil {
klog.Warningf("An error occured while determining if the CVO is evaluating alerts, so the client will check. %v", err)
} else if skip {
return nil, nil
}

var alertsBytes []byte
if o.mockData.alertsPath != "" {
if len(o.mockData.alerts) == 0 {
Expand Down Expand Up @@ -251,3 +260,54 @@ func (o *options) alerts(ctx context.Context) ([]acceptableCondition, error) {

return conditions, nil
}

// alertsEvaluatedByCVO makes API calls to determine if we need to do client-side alert checking
func (o *options) alertsEvaluatedByCVO(ctx context.Context) (bool, error) {
featureGates := o.mockData.featureGate
infrastructure := o.mockData.infrastructure
cv := o.mockData.clusterVersion
if cv == nil {
var err error
featureGates, err = o.Client.ConfigV1().FeatureGates().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
return false, err
}

infrastructure, err = o.Client.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
return false, err
}

cv, err = o.Client.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{})
if err != nil {
return false, err
}
}

// if the AcceptRisks feature gate is enabled AND oc is not running against a hosted cluster,
// the CVO is handling alerts and will generate the Recommended condition if needed
return isAcceptRisksEnabled(featureGates, cv.Status.Desired.Version) && !isHostedCluster(infrastructure), nil
}

// isAcceptRisksEnabled checks to see if the 'ClusterUpdateAcceptRisks' feature gate is enabled
// if so, return true to skip client-side alert checking
func isAcceptRisksEnabled(featureGate *configv1.FeatureGate, clusterVersion string) bool {
if featureGate == nil {
return false
}

for _, versionedGates := range featureGate.Status.FeatureGates {
if versionedGates.Version == clusterVersion {
for _, enabledGate := range versionedGates.Enabled {
if enabledGate.Name == features.FeatureGateClusterUpdateAcceptRisks {
return true
}
}
}
}
return false
}

func isHostedCluster(i *configv1.Infrastructure) bool {
return i != nil && i.Status.ControlPlaneTopology == configv1.ExternalTopologyMode
}
132 changes: 132 additions & 0 deletions pkg/cli/admin/upgrade/recommend/alerts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package recommend

import (
"testing"

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

func TestIsAcceptRisksEnabled(t *testing.T) {

for _, testCase := range []struct {
name string
featureGateConfig *configv1.FeatureGate
expected bool
}{
{
name: "no feature gates",
},
{
name: "ClusterUpdateAcceptRisks feature gate is enabled",
featureGateConfig: &configv1.FeatureGate{
Status: configv1.FeatureGateStatus{
FeatureGates: []configv1.FeatureGateDetails{
{
Version: "4.22.0",
Enabled: []configv1.FeatureGateAttributes{
{
Name: features.FeatureGateClusterUpdateAcceptRisks,
},
},
},
},
},
},
expected: true,
},
{
name: "ClusterUpdateAcceptRisks feature gate is explicitly disabled",
featureGateConfig: &configv1.FeatureGate{
Status: configv1.FeatureGateStatus{
FeatureGates: []configv1.FeatureGateDetails{
{
Version: "4.22.0",
Disabled: []configv1.FeatureGateAttributes{
{
Name: features.FeatureGateClusterUpdateAcceptRisks,
},
},
},
},
},
},
},
{
name: "ClusterUpdateAcceptRisks feature gate is not explicitly enabled or disabled",
featureGateConfig: &configv1.FeatureGate{
Status: configv1.FeatureGateStatus{
FeatureGates: []configv1.FeatureGateDetails{
{
Version: "4.22.0",
Enabled: []configv1.FeatureGateAttributes{},
Disabled: []configv1.FeatureGateAttributes{},
},
},
},
},
},
{
name: "ClusterUpdateAcceptRisks feature gate is enabled for a different cluster version",
featureGateConfig: &configv1.FeatureGate{
Status: configv1.FeatureGateStatus{
FeatureGates: []configv1.FeatureGateDetails{
{
Version: "4.21.0",
Enabled: []configv1.FeatureGateAttributes{
{
Name: features.FeatureGateClusterUpdateAcceptRisks,
},
},
},
},
},
},
},
} {
t.Run(testCase.name, func(t *testing.T) {
actual := isAcceptRisksEnabled(testCase.featureGateConfig, "4.22.0")

if actual != testCase.expected {
t.Errorf("%v != %v", actual, testCase.expected)
}
})
}
}

func TestIsHypershiftEnabled(t *testing.T) {
for _, testCase := range []struct {
name string
infrastructure *configv1.Infrastructure
expected bool
}{
{
name: "no infrastructure",
},
{
name: "hypershift enabled",
infrastructure: &configv1.Infrastructure{
Status: configv1.InfrastructureStatus{
ControlPlaneTopology: configv1.ExternalTopologyMode,
},
},
expected: true,
},
{
name: "hypershift not enabled",
infrastructure: &configv1.Infrastructure{
Status: configv1.InfrastructureStatus{
ControlPlaneTopology: configv1.HighlyAvailableTopologyMode,
},
},
},
} {
t.Run(testCase.name, func(t *testing.T) {
actual := isHostedCluster(testCase.infrastructure)

if actual != testCase.expected {
t.Errorf("%v != %v", actual, testCase.expected)
}
})
}
}
Loading