Skip to content

Commit

Permalink
controllers: send platform and operator version while reporting status
Browse files Browse the repository at this point in the history
- send storage client operator version and version of platform to which
cluster last updated to successfully
- the status will be sent on top of existing heartbeat mechanism

Signed-off-by: Leela Venkaiah G <[email protected]>
  • Loading branch information
leelavg committed Oct 31, 2023
1 parent 1a6d644 commit cd87f93
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
8 changes: 8 additions & 0 deletions config/rbac/status-reporter-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ rules:
- get
- list
- update
- apiGroups:
- config.openshift.io
resources:
- clusterversions
verbs:
- get
- list
- watch
15 changes: 15 additions & 0 deletions config/rbac/status-reporter-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: null
name: status-reporter
rules:
- apiGroups:
- operators.coreos.com
resources:
- clusterserviceversions
verbs:
- get
- list
- watch
12 changes: 12 additions & 0 deletions config/rbac/status-reporter-role_binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: status-reporter
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: status-reporter
subjects:
- kind: ServiceAccount
name: status-reporter
namespace: system
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.27.9
github.com/openshift/api v0.0.0-20230816181854-a7ca92db022a
github.com/operator-framework/api v0.17.7-0.20230626210316-aa3e49803e7b
github.com/pkg/errors v0.9.1
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.67.1
github.com/red-hat-storage/ocs-operator/v4 v4.0.0-20230915072501-d290a4270291
Expand All @@ -31,6 +32,7 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
Expand Down Expand Up @@ -61,6 +63,7 @@ require (
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
Expand All @@ -86,3 +89,5 @@ require (
)

exclude github.com/openshift/api v3.9.1-0.20190924102528-32369d4db2ad+incompatible

replace github.com/red-hat-storage/ocs-operator/v4 => ../ocs-operator
28 changes: 28 additions & 0 deletions pkg/utils/slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2023 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

// Find returns the first entry matching the function "f" or else return nil
func Find[T any](list []T, f func(item *T) bool) *T {
for idx := range list {
ele := &list[idx]
if f(ele) {
return ele
}
}
return nil
}
47 changes: 45 additions & 2 deletions service/status-report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ package main
import (
"context"
"os"
"strings"
"time"

"github.com/red-hat-storage/ocs-client-operator/api/v1alpha1"
"github.com/red-hat-storage/ocs-client-operator/pkg/csi"
"github.com/red-hat-storage/ocs-client-operator/pkg/utils"

configv1 "github.com/openshift/api/config/v1"
opv1a1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
providerclient "github.com/red-hat-storage/ocs-operator/v4/services/provider/client"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -34,6 +37,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

const (
csvPrefix = "ocs-client-operator"
)

func main() {
scheme := runtime.NewScheme()
if err := v1alpha1.AddToScheme(scheme); err != nil {
Expand Down Expand Up @@ -77,6 +84,39 @@ func main() {
klog.Exitf("Failed to get storageClient %q/%q: %v", storageClient.Namespace, storageClient.Name, err)
}

var oprVersion string
csvList := opv1a1.ClusterServiceVersionList{}
if err = cl.List(ctx, &csvList, client.InNamespace(storageClientNamespace)); err != nil {
klog.Warningf("Failed to list csv resources: %v", err)
} else {
item := utils.Find(csvList.Items, func(csv *opv1a1.ClusterServiceVersion) bool {
return strings.HasPrefix(csv.Name, csvPrefix)
})
if item != nil {
oprVersion = item.Spec.Version.String()
}
}
if oprVersion == "" {
klog.Warningf("Unable to find csv with prefix %q", csvPrefix)
}

var pltVersion string
clusterVersion := &configv1.ClusterVersion{}
clusterVersion.Name = "version"
if err = cl.Get(ctx, types.NamespacedName{Name: clusterVersion.Name}, clusterVersion); err != nil {
klog.Warningf("Failed to get clusterVersion: %v", err)
} else {
item := utils.Find(clusterVersion.Status.History, func(record *configv1.UpdateHistory) bool {
return record.State == configv1.CompletedUpdate
})
if item != nil {
pltVersion = item.Version
}
}
if pltVersion == "" {
klog.Warningf("Unable to find ocp version with completed update")
}

providerClient, err := providerclient.NewProviderClient(
ctx,
storageClient.Spec.StorageProviderEndpoint,
Expand All @@ -87,8 +127,11 @@ func main() {
}
defer providerClient.Close()

if _, err = providerClient.ReportStatus(ctx, storageClient.Status.ConsumerID); err != nil {
klog.Exitf("Failed to update lastHeartbeat of storageClient %v: %v", storageClient.Status.ConsumerID, err)
status := providerclient.NewStorageClientStatus().
SetPlatformVersion(pltVersion).
SetOperatorVersion(oprVersion)
if _, err = providerClient.ReportStatus(ctx, storageClient.Status.ConsumerID, status); err != nil {
klog.Exitf("Failed to report status of storageClient %v: %v", storageClient.Status.ConsumerID, err)
}

var csiClusterConfigEntry = new(csi.ClusterConfigEntry)
Expand Down

0 comments on commit cd87f93

Please sign in to comment.