Skip to content

Commit

Permalink
Reverts deprecated ClusterClaims for odf-info
Browse files Browse the repository at this point in the history
Signed-off-by: raaizik <[email protected]>
  • Loading branch information
raaizik committed Aug 8, 2024
1 parent 2a54911 commit 35d3ec5
Show file tree
Hide file tree
Showing 23 changed files with 1,354 additions and 8 deletions.
10 changes: 10 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ rules:
- list
- update
- watch
- apiGroups:
- cluster.open-cluster-management.io
resources:
- clusterclaims
verbs:
- create
- get
- list
- update
- watch
- apiGroups:
- config.openshift.io
resources:
Expand Down
57 changes: 54 additions & 3 deletions controllers/ocsinitialization/ocsinitialization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/defaults"
"github.com/red-hat-storage/ocs-operator/v4/controllers/platform"
"github.com/red-hat-storage/ocs-operator/v4/controllers/storagecluster"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
"github.com/red-hat-storage/ocs-operator/v4/templates"
rookCephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
Expand All @@ -28,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
"open-cluster-management.io/api/cluster/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -45,6 +47,7 @@ const (
random30CharacterString = "KP7TThmSTZegSGmHuPKLnSaaAHSG3RSgqw6akBj0oVk"
PrometheusOperatorDeploymentName = "prometheus-operator"
PrometheusOperatorCSVNamePrefix = "odf-prometheus-operator"
ClusterClaimCrdName = "clusterclaims.cluster.open-cluster-management.io"
)

// InitNamespacedName returns a NamespacedName for the singleton instance that
Expand All @@ -66,6 +69,7 @@ type OCSInitializationReconciler struct {
SecurityClient secv1client.SecurityV1Interface
OperatorNamespace string
clusters *util.Clusters
availableCrds map[string]bool
}

// +kubebuilder:rbac:groups=ocs.openshift.io,resources=*,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -74,6 +78,7 @@ type OCSInitializationReconciler struct {
// +kubebuilder:rbac:groups="monitoring.coreos.com",resources={alertmanagers,prometheuses},verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="monitoring.coreos.com",resources=servicemonitors,verbs=get;list;watch;update;patch;create;delete
// +kubebuilder:rbac:groups=operators.coreos.com,resources=clusterserviceversions,verbs=get;list;watch;delete;update;patch
// +kubebuilder:rbac:groups=cluster.open-cluster-management.io,resources=clusterclaims,verbs=get;list;watch;create;update

// Reconcile reads that state of the cluster for a OCSInitialization object and makes changes based on the state read
// and what is in the OCSInitialization.Spec
Expand Down Expand Up @@ -142,6 +147,11 @@ func (r *OCSInitializationReconciler) Reconcile(ctx context.Context, request rec
}
}

r.availableCrds, err = util.MapCRDAvailability(r.ctx, r.Client, r.Log, ClusterClaimCrdName)
if err != nil {
return reconcile.Result{}, err
}

r.clusters, err = util.GetClusters(ctx, r.Client)
if err != nil {
r.Log.Error(err, "Failed to get clusters")
Expand Down Expand Up @@ -169,6 +179,12 @@ func (r *OCSInitializationReconciler) Reconcile(ctx context.Context, request rec
return reconcile.Result{}, err
}

err = r.ensureClusterClaimExists()
if err != nil {
r.Log.Error(err, "Failed to ensure odf-info namespacedname ClusterClaim")
return reconcile.Result{}, err
}

err = r.ensureRookCephOperatorConfigExists(instance)
if err != nil {
r.Log.Error(err, "Failed to ensure rook-ceph-operator-config ConfigMap")
Expand Down Expand Up @@ -252,7 +268,8 @@ func (r *OCSInitializationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return strings.HasPrefix(client.GetName(), PrometheusOperatorCSVNamePrefix)
},
)
return ctrl.NewControllerManagedBy(mgr).

ocsInitializationController := ctrl.NewControllerManagedBy(mgr).
For(&ocsv1.OCSInitialization{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Service{}).
Owns(&corev1.Secret{}).
Expand Down Expand Up @@ -333,8 +350,42 @@ func (r *OCSInitializationReconciler) SetupWithManager(mgr ctrl.Manager) error {
},
),
builder.WithPredicates(prometheusPredicate),
).
Complete(r)
)
return ocsInitializationController.Complete(r)
}

func (r *OCSInitializationReconciler) ensureClusterClaimExists() error {
if !r.availableCrds[ClusterClaimCrdName] && len(r.clusters.GetStorageClusters()) < 1 {
return nil
}
operatorNamespace, err := util.GetOperatorNamespace()
if err != nil {
r.Log.Error(err, "failed to get operator's namespace. retrying again")
return err
}

OdfInfoNamespacedName := types.NamespacedName{
Namespace: operatorNamespace,
Name: storagecluster.OdfInfoConfigMapName,
}.String()

cc := &v1alpha1.ClusterClaim{
ObjectMeta: metav1.ObjectMeta{
Name: util.OdfInfoNamespacedNameClaimName,
},
}

_, err = controllerutil.CreateOrUpdate(r.ctx, r.Client, cc, func() error {
cc.Spec.Value = OdfInfoNamespacedName
return nil
})
if err != nil {
r.Log.Error(err, "failed to create or update clusterclaim", "ClusterClaim", util.OdfInfoNamespacedNameClaimName)
return err
}
r.Log.Info("Created or updated clusterclaim", "ClusterClaim", cc.Name)

return err
}

// ensureRookCephOperatorConfigExists ensures that the rook-ceph-operator-config cm exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -113,6 +114,12 @@ func createFakeScheme(t *testing.T) *runtime.Scheme {
if err != nil {
assert.Fail(t, "failed to add v1alpha1 scheme")
}

err = extensionsv1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add extensionsv1 scheme")
}

return scheme
}

Expand Down
13 changes: 10 additions & 3 deletions controllers/storagecluster/odfinfoconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storagecluster

import (
"fmt"
"open-cluster-management.io/api/cluster/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"strings"
"sync"
Expand All @@ -26,7 +27,7 @@ const (
rookCephMonSecretName = "rook-ceph-mon"
fsidKey = "fsid"
ocsOperatorNamePrefix = "ocs-operator"
odfInfoConfigMapName = "odf-info"
OdfInfoConfigMapName = "odf-info"
odfInfoMapKind = "ConfigMap"
)

Expand All @@ -46,7 +47,7 @@ func (obj *odfInfoConfig) ensureCreated(r *StorageClusterReconciler, storageClus

odfInfoConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: odfInfoConfigMapName,
Name: OdfInfoConfigMapName,
Namespace: operatorNamespace,
},
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func (obj *odfInfoConfig) ensureDeleted(r *StorageClusterReconciler, storageClus
return reconcile.Result{}, err
}
odfInfoConfigMap := &corev1.ConfigMap{}
odfInfoConfigMap.Name = odfInfoConfigMapName
odfInfoConfigMap.Name = OdfInfoConfigMapName
odfInfoConfigMap.Namespace = operatorNamespace
if err = r.Client.Get(r.ctx, client.ObjectKeyFromObject(odfInfoConfigMap), odfInfoConfigMap); err != nil {
if errors.IsNotFound(err) {
Expand All @@ -114,6 +115,12 @@ func (obj *odfInfoConfig) ensureDeleted(r *StorageClusterReconciler, storageClus
r.Log.Error(err, "Failed to delete odf-info config map.", "ConfigMap", client.ObjectKeyFromObject(odfInfoConfigMap))
return reconcile.Result{}, fmt.Errorf("failed to delete odf-info %v: %v", odfInfoConfigMap.Name, err)
}
cc := &v1alpha1.ClusterClaim{}
cc.Name = util.OdfInfoNamespacedNameClaimName
if err = r.Client.Delete(r.ctx, cc); client.IgnoreNotFound(err) != nil {
r.Log.Error(err, "Failed to delete odf-info cluster claim.", "ClusterClaim", client.ObjectKeyFromObject(cc))
return reconcile.Result{}, fmt.Errorf("failed to delete odf-info cluster claim %v: %v", cc.Name, err)
}
}
return reconcile.Result{}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/storagecluster/odfinfoconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestOdfInfoConfig(t *testing.T) {

configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: odfInfoConfigMapName,
Name: OdfInfoConfigMapName,
Namespace: namespace,
},
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestOdfInfoConfig(t *testing.T) {
}
// get the output
err = r.Client.Get(r.ctx, client.ObjectKeyFromObject(configMap), configMap)
assert.NilError(t, err, "expected to find configmap %q: %+v", odfInfoConfigMapName, err)
assert.NilError(t, err, "expected to find configmap %q: %+v", OdfInfoConfigMapName, err)

// compare with the expected results
odfInfoDataOfStorageClusterKey := v1alpha1.OdfInfoData{}
Expand Down
5 changes: 5 additions & 0 deletions controllers/storagecluster/storagecluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
openv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1"
"os"
"regexp"
"testing"
Expand Down Expand Up @@ -1249,6 +1250,10 @@ func createFakeScheme(t *testing.T) *runtime.Scheme {
assert.Fail(t, "unable to add opv1a1 to scheme")
}

err = openv1alpha1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "unable to add openv1alpha1 to scheme")
}
return scheme
}

Expand Down
17 changes: 17 additions & 0 deletions controllers/util/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -42,6 +43,8 @@ const (

// This is the name for the OwnerUID FieldIndex
OwnerUIDIndexName = "ownerUID"

OdfInfoNamespacedNameClaimName = "odfinfo.odf.openshift.io"
)

// GetWatchNamespace returns the namespace the operator should be watching for changes
Expand Down Expand Up @@ -149,3 +152,17 @@ func GenerateNameForNonResilientCephBlockPoolSC(initData *ocsv1.StorageCluster)
}
return fmt.Sprintf("%s-ceph-non-resilient-rbd", initData.Name)
}

func MapCRDAvailability(ctx context.Context, clnt client.Client, log logr.Logger, crdNames ...string) (map[string]bool, error) {
crdExist := map[string]bool{}
for _, crdName := range crdNames {
crd := &apiextensionsv1.CustomResourceDefinition{}
crd.Name = crdName
if err := clnt.Get(ctx, client.ObjectKeyFromObject(crd), crd); client.IgnoreNotFound(err) != nil {
log.Error(err, fmt.Sprintf("Error getting CRD for %s", crdName))
return nil, fmt.Errorf("error getting CRD, %v", err)
}
crdExist[crdName] = crd.UID != ""
}
return crdExist, nil
}
10 changes: 10 additions & 0 deletions deploy/csv-templates/ocs-operator.csv.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ spec:
- list
- update
- watch
- apiGroups:
- cluster.open-cluster-management.io
resources:
- clusterclaims
verbs:
- create
- get
- list
- update
- watch
- apiGroups:
- config.openshift.io
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ spec:
- list
- update
- watch
- apiGroups:
- cluster.open-cluster-management.io
resources:
- clusterclaims
verbs:
- create
- get
- list
- update
- watch
- apiGroups:
- config.openshift.io
resources:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
k8s.io/client-go v0.30.3
k8s.io/klog/v2 v2.130.1
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
open-cluster-management.io/api v0.13.0
sigs.k8s.io/controller-runtime v0.18.4
sigs.k8s.io/yaml v1.4.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,8 @@ k8s.io/utils v0.0.0-20221107191617-1a15be271d1d/go.mod h1:OLgZIPagt7ERELqWJFomSt
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1J8+AsQnQCKsi8A=
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
open-cluster-management.io/api v0.13.0 h1:dlcJEZlNlE0DmSDctK2s7iWKg9l+Tgb0V78Z040nMuk=
open-cluster-management.io/api v0.13.0/go.mod h1:CuCPEzXDvOyxBB0H1d1eSeajbHqaeGEKq9c63vQc63w=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/util/retry"
clusterv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
apiclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -90,6 +91,7 @@ func init() {
utilruntime.Must(routev1.AddToScheme(scheme))
utilruntime.Must(quotav1.AddToScheme(scheme))
utilruntime.Must(ocsv1alpha1.AddToScheme(scheme))
utilruntime.Must(clusterv1alpha1.AddToScheme(scheme))
utilruntime.Must(operatorsv1alpha1.AddToScheme(scheme))
utilruntime.Must(nadscheme.AddToScheme(scheme))
utilruntime.Must(ocsclientv1a1.AddToScheme(scheme))
Expand Down
3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,9 @@ k8s.io/utils/pointer
k8s.io/utils/ptr
k8s.io/utils/strings/slices
k8s.io/utils/trace
# open-cluster-management.io/api v0.13.0
## explicit; go 1.21
open-cluster-management.io/api/cluster/v1alpha1
# sigs.k8s.io/container-object-storage-interface-api v0.1.0
## explicit; go 1.18
sigs.k8s.io/container-object-storage-interface-api/apis
Expand Down
Loading

0 comments on commit 35d3ec5

Please sign in to comment.