Skip to content

Commit

Permalink
Fix Openshift version check to work on ROSA
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Mahmoud <mmahmoud@redhat.com>
  • Loading branch information
msherif1234 committed Jan 24, 2025
1 parent b2b9812 commit 228b019
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (r *FlowCollector) validateAgent(_ context.Context, fc *FlowCollectorSpec)
slices.Contains(fc.Agent.EBPF.Features, EbpfManager) {
// Make sure required version of ocp is installed
if CurrentClusterInfo != nil && CurrentClusterInfo.IsOpenShift() {
b, err := CurrentClusterInfo.OpenShiftVersionIsAtLeast("4.18.0")
b, err := CurrentClusterInfo.OpenShiftVersionIsAtLeast("4.18.0-0")
if err != nil {
warnings = append(warnings, fmt.Sprintf("Could not detect OpenShift cluster version: %s", err.Error()))
} else if !b {
Expand All @@ -105,7 +105,7 @@ func (r *FlowCollector) validateAgent(_ context.Context, fc *FlowCollectorSpec)
}
if slices.Contains(fc.Agent.EBPF.Features, PacketDrop) {
if CurrentClusterInfo != nil && CurrentClusterInfo.IsOpenShift() {
b, err := CurrentClusterInfo.OpenShiftVersionIsAtLeast("4.14.0")
b, err := CurrentClusterInfo.OpenShiftVersionIsAtLeast("4.14.0-0")
if err != nil {
warnings = append(warnings, fmt.Sprintf("Could not detect OpenShift cluster version: %s", err.Error()))
} else if !b {
Expand Down
12 changes: 12 additions & 0 deletions bundle/manifests/netobserv-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,18 @@ spec:
- patch
- update
- watch
- apiGroups:
- config.openshift.io
resources:
- clusteroperators
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- config.openshift.io
resources:
Expand Down
12 changes: 12 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- config.openshift.io
resources:
- clusteroperators
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- config.openshift.io
resources:
Expand Down
44 changes: 32 additions & 12 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
operatorv1 "github.com/openshift/api/operator/v1"
securityv1 "github.com/openshift/api/security/v1"
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -39,10 +41,18 @@ func NewInfo(dcl *discovery.DiscoveryClient) (Info, error) {
}

func (c *Info) CheckClusterInfo(ctx context.Context, cl client.Client) error {
var errs []error
if c.IsOpenShift() && !c.fetchedClusterVersion {
if err := c.fetchOpenShiftClusterVersion(ctx, cl); err != nil {
return err
errs = append(errs, err)
}
if err := c.fetchOpenShiftClusterID(ctx, cl); err != nil {
errs = append(errs, err)
}
if len(errs) != 0 {
return kerrors.NewAggregate(errs)
}
c.fetchedClusterVersion = true
}
return nil
}
Expand Down Expand Up @@ -75,21 +85,31 @@ func (c *Info) fetchAvailableAPIs(client *discovery.DiscoveryClient) error {
}

func (c *Info) fetchOpenShiftClusterVersion(ctx context.Context, cl client.Client) error {
key := client.ObjectKey{Name: "version"}
cversion := &configv1.ClusterVersion{}
if err := cl.Get(ctx, key, cversion); err != nil {
return fmt.Errorf("could not fetch ClusterVersion: %w", err)
cno := &configv1.ClusterOperator{}
err := cl.Get(ctx, types.NamespacedName{Name: "network"}, cno)
if err != nil {
return fmt.Errorf("error fetching OpenShift Cluster Network Operator: %w", err)
}
c.ID = string(cversion.Spec.ClusterID)
// Get version; use the same method as via `oc get clusterversion`, where printed column uses jsonPath:
// .status.history[?(@.state=="Completed")].version
for _, history := range cversion.Status.History {
if history.State == "Completed" {
c.openShiftVersion = semver.New(history.Version)
for _, v := range cno.Status.Versions {
if v.Name == "operator" {
ver, err := semver.NewVersion(v.Version)
if err != nil {
return fmt.Errorf("error parsing OpenShift Cluster Network Operator version: %w", err)
}
c.openShiftVersion = ver
break
}
}
c.fetchedClusterVersion = true
return nil
}

func (c *Info) fetchOpenShiftClusterID(ctx context.Context, cl client.Client) error {
key := client.ObjectKey{Name: "version"}
version := &configv1.ClusterVersion{}
if err := cl.Get(ctx, key, version); err != nil {
return fmt.Errorf("could not fetch ClusterVersion: %w", err)
}
c.ID = string(version.Spec.ClusterID)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
//+kubebuilder:rbac:urls="/metrics",verbs=get
//+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch
//+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions/status,verbs=update;patch
//+kubebuilder:rbac:groups=config.openshift.io,resources=clusteroperators,verbs=get;create;delete;update;patch;list;watch

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

Expand Down
4 changes: 4 additions & 0 deletions pkg/test/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func PrepareEnvTest(controllers []manager.Registerer, namespaces []string, baseP
})
Expect(err).NotTo(HaveOccurred())

err = k8sClient.Create(ctx, &configv1.ClusterOperator{
ObjectMeta: metav1.ObjectMeta{Name: "network"},
})
Expect(err).NotTo(HaveOccurred())
k8sManager, err := manager.NewManager(
context.Background(),
cfg,
Expand Down

0 comments on commit 228b019

Please sign in to comment.