From 9687c23e3bcfafa3034dc934ecc13ca39e0697b9 Mon Sep 17 00:00:00 2001 From: Leela Venkaiah G Date: Wed, 4 Oct 2023 13:34:55 +0530 Subject: [PATCH] controllers: send versions and client phase while reporting status Signed-off-by: Leela Venkaiah G --- config/rbac/status-reporter-clusterrole.yaml | 8 +++ config/rbac/status-reporter-role.yaml | 15 ++++++ config/rbac/status-reporter-role_binding.yaml | 12 +++++ service/status-report/main.go | 49 +++++++++++++++++-- 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 config/rbac/status-reporter-role.yaml create mode 100644 config/rbac/status-reporter-role_binding.yaml diff --git a/config/rbac/status-reporter-clusterrole.yaml b/config/rbac/status-reporter-clusterrole.yaml index 3748b9de2..7de4c30a0 100644 --- a/config/rbac/status-reporter-clusterrole.yaml +++ b/config/rbac/status-reporter-clusterrole.yaml @@ -18,3 +18,11 @@ rules: - get - list - update + - apiGroups: + - config.openshift.io + resources: + - clusterversions + verbs: + - get + - list + - watch diff --git a/config/rbac/status-reporter-role.yaml b/config/rbac/status-reporter-role.yaml new file mode 100644 index 000000000..4ba378bc7 --- /dev/null +++ b/config/rbac/status-reporter-role.yaml @@ -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 diff --git a/config/rbac/status-reporter-role_binding.yaml b/config/rbac/status-reporter-role_binding.yaml new file mode 100644 index 000000000..f3d013416 --- /dev/null +++ b/config/rbac/status-reporter-role_binding.yaml @@ -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 diff --git a/service/status-report/main.go b/service/status-report/main.go index cdb071125..43c561256 100644 --- a/service/status-report/main.go +++ b/service/status-report/main.go @@ -19,12 +19,14 @@ 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" @@ -35,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 { @@ -79,7 +85,42 @@ func main() { } csvList := opv1a1.ClusterServiceVersionList{} - _ = cl.List(ctx, &csvList, client.InNamespace(storageClientNamespace)) + if err = cl.List(ctx, &csvList, client.InNamespace(storageClientNamespace)); err != nil { + klog.Exitf("Failed to list csv resources: %v", err) + } + + var operatorVersion string + for idx := range csvList.Items { + candidate := &csvList.Items[idx] + if strings.HasPrefix(candidate.Name, csvPrefix) { + operatorVersion = candidate.Spec.Version.String() + break + } + } + + if operatorVersion == "" { + klog.Exitf("Unable to find csv with prefix %q", csvPrefix) + } + + clusterVersion := &configv1.ClusterVersion{} + clusterVersion.Name = "version" + if err = cl.Get(ctx, types.NamespacedName{Name: clusterVersion.Name}, clusterVersion); err != nil { + klog.Exitf("Failed to get clusterVersion: %v", err) + } + + var ocpVersion string + for idx := range clusterVersion.Status.History { + candidate := &clusterVersion.Status.History[idx] + if candidate.State == configv1.CompletedUpdate { + ocpVersion = candidate.Version + break + } + } + + if ocpVersion == "" { + klog.Exitf("Unable to find ocp version with completed update") + } + providerClient, err := providerclient.NewProviderClient( ctx, storageClient.Spec.StorageProviderEndpoint, @@ -90,9 +131,9 @@ func main() { } defer providerClient.Close() - temp := "Unknown" - if _, err = providerClient.ReportStatus(ctx, storageClient.Status.ConsumerID, temp, temp, string(storageClient.Status.Phase)); err != nil { - klog.Exitf("Failed to update lastHeartbeat of storageClient %v: %v", storageClient.Status.ConsumerID, err) + if _, err = providerClient.ReportStatus(ctx, storageClient.Status.ConsumerID, ocpVersion, + operatorVersion, string(storageClient.Status.Phase)); err != nil { + klog.Exitf("Failed to report status of storageClient %v: %v", storageClient.Status.ConsumerID, err) } var csiClusterConfigEntry = new(csi.ClusterConfigEntry)