Skip to content

Commit 05b6aaa

Browse files
committed
fix(adm-upgrade-recommend-alerts): extend mockData to include featureGates's and infrastructure's
Signed-off-by: Nick Bottari <nbottari9@gmail.com> fix(adm-upgrade-recommend-alerts): add test for different featureGate version Signed-off-by: Nick Bottari <nbottari9@gmail.com> add comma Signed-off-by: Nick Bottari <nbottari9@gmail.com>
1 parent 55acfea commit 05b6aaa

4 files changed

Lines changed: 84 additions & 13 deletions

File tree

pkg/cli/admin/upgrade/recommend/alerts.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,19 +265,30 @@ func (o *options) alerts(ctx context.Context) ([]acceptableCondition, error) {
265265

266266
// alertsEvaluatedByCVO makes API calls to determine if we need to do client-side alert checking
267267
func (o *options) alertsEvaluatedByCVO(ctx context.Context) (bool, error) {
268-
featureGates, err := o.Client.ConfigV1().FeatureGates().Get(ctx, "cluster", metav1.GetOptions{})
269-
if err != nil {
270-
return false, err
271-
}
268+
var featureGates *configv1.FeatureGate
269+
var infrastructure *configv1.Infrastructure
270+
var cv *configv1.ClusterVersion
271+
272+
if o.mockData.cvPath != "" {
273+
featureGates = o.mockData.featureGate
274+
infrastructure = o.mockData.infrastructure
275+
cv = o.mockData.clusterVersion
276+
} else {
277+
var err error
278+
featureGates, err = o.Client.ConfigV1().FeatureGates().Get(ctx, "cluster", metav1.GetOptions{})
279+
if err != nil {
280+
return false, err
281+
}
272282

273-
infrastructure, err := o.Client.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{})
274-
if err != nil {
275-
return false, err
276-
}
283+
infrastructure, err = o.Client.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{})
284+
if err != nil {
285+
return false, err
286+
}
277287

278-
cv, err := o.Client.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{})
279-
if err != nil {
280-
return false, err
288+
cv, err = o.Client.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{})
289+
if err != nil {
290+
return false, err
291+
}
281292
}
282293

283294
// if the AcceptRisks feature gate AND hypershift is not enabled,

pkg/cli/admin/upgrade/recommend/alerts_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ func TestIsAcceptRisksEnabled(t *testing.T) {
6969
},
7070
expected: false,
7171
},
72+
{
73+
name: "ClusterUpdateAcceptRisks feature gate is enabled for a different cluster version",
74+
featureGateConfig: &configv1.FeatureGate{
75+
Status: configv1.FeatureGateStatus{
76+
FeatureGates: []configv1.FeatureGateDetails{
77+
{
78+
Version: "4.21.0",
79+
Enabled: []configv1.FeatureGateAttributes{
80+
{
81+
Name: features.FeatureGateClusterUpdateAcceptRisks,
82+
},
83+
},
84+
},
85+
},
86+
},
87+
},
88+
expected: false,
89+
},
7290
} {
7391
t.Run(testCase.name, func(t *testing.T) {
7492
actual := isAcceptRisksEnabled(testCase.featureGateConfig, "4.22.0")

pkg/cli/admin/upgrade/recommend/mockresources.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ import (
1313

1414
type mockData struct {
1515
// inputs
16-
cvPath string
17-
alertsPath string
16+
cvPath string
17+
alertsPath string
18+
featureGatePath string
19+
infrastructurePath string
1820

1921
// outputs
2022
clusterVersion *configv1.ClusterVersion
2123
alerts []byte
24+
featureGate *configv1.FeatureGate
25+
infrastructure *configv1.Infrastructure
2226
}
2327

2428
func asResourceList[T any](objects *corev1.List, decoder runtime.Decoder) ([]T, error) {
@@ -78,5 +82,41 @@ func (o *mockData) load() error {
7882
}
7983
}
8084

85+
if o.featureGatePath != "" {
86+
fgBytes, err := os.ReadFile(o.featureGatePath)
87+
if err != nil && !errors.Is(err, os.ErrNotExist) {
88+
return err
89+
}
90+
if fgBytes != nil {
91+
fgObj, err := runtime.Decode(decoder, fgBytes)
92+
if err != nil {
93+
return fmt.Errorf("error while parsing file %s: %w", o.featureGatePath, err)
94+
}
95+
fg, ok := fgObj.(*configv1.FeatureGate)
96+
if !ok {
97+
return fmt.Errorf("unexpected object type %T in %s content", fgObj, o.featureGatePath)
98+
}
99+
o.featureGate = fg
100+
}
101+
}
102+
103+
if o.infrastructurePath != "" {
104+
infraBytes, err := os.ReadFile(o.infrastructurePath)
105+
if err != nil && !errors.Is(err, os.ErrNotExist) {
106+
return err
107+
}
108+
if infraBytes != nil {
109+
infraObj, err := runtime.Decode(decoder, infraBytes)
110+
if err != nil {
111+
return fmt.Errorf("error while parsing file %s: %w", o.infrastructurePath, err)
112+
}
113+
infra, ok := infraObj.(*configv1.Infrastructure)
114+
if !ok {
115+
return fmt.Errorf("unexpected object type %T in %s content", infraObj, o.infrastructurePath)
116+
}
117+
o.infrastructure = infra
118+
}
119+
}
120+
81121
return nil
82122
}

pkg/cli/admin/upgrade/recommend/recommend.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ func (o *options) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string
111111
} else {
112112
cvSuffix := "-cv.yaml"
113113
o.mockData.alertsPath = strings.Replace(o.mockData.cvPath, cvSuffix, "-alerts.json", 1)
114+
o.mockData.featureGatePath = strings.Replace(o.mockData.cvPath, cvSuffix, "-featuregate.yaml", 1)
115+
o.mockData.infrastructurePath = strings.Replace(o.mockData.cvPath, cvSuffix, "-infrastructure.yaml", 1)
114116
err := o.mockData.load()
115117
if err != nil {
116118
return err

0 commit comments

Comments
 (0)