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 Jul 15, 2024
1 parent 01bfbf8 commit b6bd338
Show file tree
Hide file tree
Showing 21 changed files with 1,385 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
103 changes: 100 additions & 3 deletions controllers/ocsinitialization/ocsinitialization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ocsinitialization
import (
"context"
"fmt"
"os"
"reflect"
"slices"
"strconv"
Expand All @@ -15,23 +16,27 @@ 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"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"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"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -45,6 +50,8 @@ const (
random30CharacterString = "KP7TThmSTZegSGmHuPKLnSaaAHSG3RSgqw6akBj0oVk"
PrometheusOperatorDeploymentName = "prometheus-operator"
PrometheusOperatorCSVNamePrefix = "odf-prometheus-operator"
ClusterClaimCrdName = "clusterclaims.cluster.open-cluster-management.io"
OdfInfoNamespacedNameClaimName = "odfinfo.odf.openshift.io"
)

// InitNamespacedName returns a NamespacedName for the singleton instance that
Expand All @@ -66,6 +73,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 +82,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 @@ -169,6 +178,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 +267,20 @@ func (r *OCSInitializationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return strings.HasPrefix(client.GetName(), PrometheusOperatorCSVNamePrefix)
},
)
return ctrl.NewControllerManagedBy(mgr).

clusterClaimCrdPredicate := predicate.Funcs{
CreateFunc: func(e event.TypedCreateEvent[client.Object]) bool {
klog.Errorf("ClusterClaim CRD was found. Restarting pod to initiate creation")
os.Exit(1)
return false
},
DeleteFunc: func(e event.TypedDeleteEvent[client.Object]) bool {
klog.Errorf("ClusterClaim CRD was found. Restarting pod to initiate deletion")
os.Exit(1)
return false
},
}
ocsInitializationController := ctrl.NewControllerManagedBy(mgr).
For(&ocsv1.OCSInitialization{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Service{}).
Owns(&corev1.Secret{}).
Expand Down Expand Up @@ -333,8 +361,77 @@ func (r *OCSInitializationReconciler) SetupWithManager(mgr ctrl.Manager) error {
},
),
builder.WithPredicates(prometheusPredicate),
).
Complete(r)
)
// Watcher for CRD of cluster claim
ocsInitializationController = ocsInitializationController.Watches(
&apiextensions.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: ClusterClaimCrdName,
},
},
handler.EnqueueRequestsFromMapFunc(
func(context context.Context, obj client.Object) []reconcile.Request {
return []reconcile.Request{{
NamespacedName: InitNamespacedName(),
}}
},
),
builder.WithPredicates(clusterClaimCrdPredicate),
)
// conditional watcher for cluster claim
var err error
r.availableCrds, err = util.MapCRDAvailability(r.ctx, r.Client, r.Log, ClusterClaimCrdName)
if err != nil {
os.Exit(1)
}
if r.availableCrds[ClusterClaimCrdName] {
ocsInitializationController = ocsInitializationController.Watches(
&v1alpha1.ClusterClaim{},
handler.EnqueueRequestsFromMapFunc(
func(context context.Context, obj client.Object) []reconcile.Request {
return []reconcile.Request{{
NamespacedName: InitNamespacedName(),
}}
},
),
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
)
}
return ocsInitializationController.Complete(r)
}

func (r *OCSInitializationReconciler) ensureClusterClaimExists() error {
if !r.availableCrds[ClusterClaimCrdName] {
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: 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", 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
6 changes: 3 additions & 3 deletions controllers/storagecluster/odfinfoconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
rookCephMonSecretName = "rook-ceph-mon"
fsidKey = "fsid"
ocsOperatorNamePrefix = "ocs-operator"
odfInfoConfigMapName = "odf-info"
OdfInfoConfigMapName = "odf-info"
odfInfoMapKind = "ConfigMap"
)

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

odfInfoConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: odfInfoConfigMapName,
Name: OdfInfoConfigMapName,
Namespace: operatorNamespace,
},
}
Expand Down Expand Up @@ -110,7 +110,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 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 := OdfInfoData{}
Expand Down
15 changes: 15 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"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -149,3 +150,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 := &apiextensions.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 @@ -247,6 +247,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 @@ -268,6 +268,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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ require (
k8s.io/client-go v0.30.2
k8s.io/klog/v2 v2.130.1
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
open-cluster-management.io/api v0.13.0
sigs.k8s.io/controller-runtime v0.18.4
sigs.k8s.io/yaml v1.4.0

)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,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-20240502163921-fe8a2dddb1d0 h1:jgGTlFYnhF1PM1Ax/lAlxUPE+KfCIXHaathvJg1C3ak=
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0/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
3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,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 b6bd338

Please sign in to comment.