Skip to content

Commit 9e19be1

Browse files
committed
Fix Openshift version check to work on ROSA
Signed-off-by: Mohamed Mahmoud <mmahmoud@redhat.com>
1 parent b2b9812 commit 9e19be1

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

bundle/manifests/netobserv-operator.clusterserviceversion.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,18 @@ spec:
10371037
- patch
10381038
- update
10391039
- watch
1040+
- apiGroups:
1041+
- config.openshift.io
1042+
resources:
1043+
- clusteroperators
1044+
verbs:
1045+
- create
1046+
- delete
1047+
- get
1048+
- list
1049+
- patch
1050+
- update
1051+
- watch
10401052
- apiGroups:
10411053
- config.openshift.io
10421054
resources:

config/rbac/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ rules:
108108
- patch
109109
- update
110110
- watch
111+
- apiGroups:
112+
- config.openshift.io
113+
resources:
114+
- clusteroperators
115+
verbs:
116+
- create
117+
- delete
118+
- get
119+
- list
120+
- patch
121+
- update
122+
- watch
111123
- apiGroups:
112124
- config.openshift.io
113125
resources:

pkg/cluster/cluster.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
operatorv1 "github.com/openshift/api/operator/v1"
1212
securityv1 "github.com/openshift/api/security/v1"
1313
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
14+
"k8s.io/apimachinery/pkg/types"
15+
kerrors "k8s.io/apimachinery/pkg/util/errors"
1416
"k8s.io/client-go/discovery"
1517
"sigs.k8s.io/controller-runtime/pkg/client"
1618
)
@@ -39,10 +41,17 @@ func NewInfo(dcl *discovery.DiscoveryClient) (Info, error) {
3941
}
4042

4143
func (c *Info) CheckClusterInfo(ctx context.Context, cl client.Client) error {
44+
var errs []error
4245
if c.IsOpenShift() && !c.fetchedClusterVersion {
4346
if err := c.fetchOpenShiftClusterVersion(ctx, cl); err != nil {
44-
return err
47+
errs = append(errs, err)
4548
}
49+
if err := c.fetchOpenShiftClusterID(ctx, cl); err != nil {
50+
errs = append(errs, err)
51+
}
52+
}
53+
if len(errs) != 0 {
54+
return kerrors.NewAggregate(errs)
4655
}
4756
return nil
4857
}
@@ -75,24 +84,35 @@ func (c *Info) fetchAvailableAPIs(client *discovery.DiscoveryClient) error {
7584
}
7685

7786
func (c *Info) fetchOpenShiftClusterVersion(ctx context.Context, cl client.Client) error {
78-
key := client.ObjectKey{Name: "version"}
79-
cversion := &configv1.ClusterVersion{}
80-
if err := cl.Get(ctx, key, cversion); err != nil {
81-
return fmt.Errorf("could not fetch ClusterVersion: %w", err)
87+
cno := &configv1.ClusterOperator{}
88+
err := cl.Get(ctx, types.NamespacedName{Name: "network"}, cno)
89+
if err != nil {
90+
return fmt.Errorf("error fetching OpenShift Cluster Network Operator: %w", err)
8291
}
83-
c.ID = string(cversion.Spec.ClusterID)
84-
// Get version; use the same method as via `oc get clusterversion`, where printed column uses jsonPath:
85-
// .status.history[?(@.state=="Completed")].version
86-
for _, history := range cversion.Status.History {
87-
if history.State == "Completed" {
88-
c.openShiftVersion = semver.New(history.Version)
92+
for _, v := range cno.Status.Versions {
93+
if v.Name == "operator" {
94+
ver, err := semver.NewVersion(v.Version)
95+
if err != nil {
96+
return fmt.Errorf("error parsing OpenShift Cluster Network Operator version: %w", err)
97+
}
98+
c.openShiftVersion = ver
8999
break
90100
}
91101
}
92102
c.fetchedClusterVersion = true
93103
return nil
94104
}
95105

106+
func (c *Info) fetchOpenShiftClusterID(ctx context.Context, cl client.Client) error {
107+
key := client.ObjectKey{Name: "version"}
108+
version := &configv1.ClusterVersion{}
109+
if err := cl.Get(ctx, key, version); err != nil {
110+
return fmt.Errorf("could not fetch ClusterVersion: %w", err)
111+
}
112+
c.ID = string(version.Spec.ClusterID)
113+
return nil
114+
}
115+
96116
// MockOpenShiftVersion shouldn't be used except for testing
97117
func (c *Info) MockOpenShiftVersion(v string) {
98118
if c.apisMap == nil {

pkg/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
//+kubebuilder:rbac:urls="/metrics",verbs=get
3939
//+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch
4040
//+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions/status,verbs=update;patch
41+
//+kubebuilder:rbac:groups=config.openshift.io,resources=clusteroperators,verbs=get;create;delete;update;patch;list;watch
4142

4243
type Registerer func(context.Context, *Manager) error
4344

pkg/test/envtest.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ func PrepareEnvTest(controllers []manager.Registerer, namespaces []string, baseP
132132
})
133133
Expect(err).NotTo(HaveOccurred())
134134

135+
err = k8sClient.Create(ctx, &configv1.ClusterOperator{
136+
ObjectMeta: metav1.ObjectMeta{Name: "network"},
137+
})
138+
Expect(err).NotTo(HaveOccurred())
135139
k8sManager, err := manager.NewManager(
136140
context.Background(),
137141
cfg,

0 commit comments

Comments
 (0)