From 2e0c595d336562cebe39de0e75c89ef68cd21368 Mon Sep 17 00:00:00 2001 From: subhamkrai Date: Sun, 9 Jan 2022 19:22:56 +0530 Subject: [PATCH] mon: add annotation to mon secret and cm add annotations to secrets and configmaps to use different tools to sync secrets and configmap. Currently, these annotation is applied only on the `rook-ceph-mon-endpoints` configmap and the secrets `rook-ceph-mon` and `rook-ceph-admin-keyring`. Closes: https://github.com/rook/rook/issues/8999 Signed-off-by: subhamkrai (cherry picked from commit 02d43e82e04d080f7c98ec9aa96ed56a2a2673ad) --- Documentation/ceph-cluster-crd.md | 4 ++- deploy/examples/cluster.yaml | 4 +++ pkg/apis/ceph.rook.io/v1/annotations.go | 4 +++ pkg/apis/ceph.rook.io/v1/keys.go | 21 +++++------ pkg/operator/ceph/cluster/mon/mon.go | 9 ++++- pkg/operator/ceph/cluster/mon/mon_test.go | 13 ++++++- pkg/operator/ceph/config/keyring/admin.go | 35 +++++++++++++++++-- .../ceph/config/keyring/admin_test.go | 12 +++++-- 8 files changed, 84 insertions(+), 18 deletions(-) diff --git a/Documentation/ceph-cluster-crd.md b/Documentation/ceph-cluster-crd.md index f72625754..654f26ed0 100755 --- a/Documentation/ceph-cluster-crd.md +++ b/Documentation/ceph-cluster-crd.md @@ -516,13 +516,15 @@ Annotations and Labels can be specified so that the Rook components will have th You can set annotations / labels for Rook components for the list of key value pairs: -* `all`: Set annotations / labels for all components +* `all`: Set annotations / labels for all components except `clusterMetadata`. * `mgr`: Set annotations / labels for MGRs * `mon`: Set annotations / labels for mons * `osd`: Set annotations / labels for OSDs * `prepareosd`: Set annotations / labels for OSD Prepare Jobs * `monitoring`: Set annotations / labels for service monitor * `crashcollector`: Set annotations / labels for crash collectors +* `clusterMetadata`: Set annotations only to `rook-ceph-mon-endpoints` configmap and the `rook-ceph-mon` and `rook-ceph-admin-keyring` secrets. These annotations will not be merged with the `all` annotations. The common usage is for backing up these critical resources with `kubed`. +Note the clusterMetadata annotation will not be merged with the `all` annotation. When other keys are set, `all` will be merged together with the specific component. ### Placement Configuration Settings diff --git a/deploy/examples/cluster.yaml b/deploy/examples/cluster.yaml index 365908df3..2c626aaba 100644 --- a/deploy/examples/cluster.yaml +++ b/deploy/examples/cluster.yaml @@ -166,6 +166,10 @@ spec: # osd: # cleanup: # prepareosd: +# clusterMetadata annotations will be applied to only `rook-ceph-mon-endpoints` configmap and the `rook-ceph-mon` and `rook-ceph-admin-keyring` secrets. +# And clusterMetadata annotations will not be merged with `all` annotations. +# clusterMetadata: +# kubed.appscode.com/sync: "true" # If no mgr annotations are set, prometheus scrape annotations will be set by default. # mgr: labels: diff --git a/pkg/apis/ceph.rook.io/v1/annotations.go b/pkg/apis/ceph.rook.io/v1/annotations.go index d68ab9991..4f611abf4 100644 --- a/pkg/apis/ceph.rook.io/v1/annotations.go +++ b/pkg/apis/ceph.rook.io/v1/annotations.go @@ -57,6 +57,10 @@ func GetCleanupAnnotations(a AnnotationsSpec) Annotations { return mergeAllAnnotationsWithKey(a, KeyCleanup) } +func GetClusterMetadataAnnotations(a AnnotationsSpec) Annotations { + return a[KeyClusterMetadata] +} + func mergeAllAnnotationsWithKey(a AnnotationsSpec, name KeyType) Annotations { all := a.All() if all != nil { diff --git a/pkg/apis/ceph.rook.io/v1/keys.go b/pkg/apis/ceph.rook.io/v1/keys.go index 3257a312f..3c4bf633b 100644 --- a/pkg/apis/ceph.rook.io/v1/keys.go +++ b/pkg/apis/ceph.rook.io/v1/keys.go @@ -17,14 +17,15 @@ limitations under the License. package v1 const ( - KeyAll = "all" - KeyMds KeyType = "mds" - KeyMon KeyType = "mon" - KeyMonArbiter KeyType = "arbiter" - KeyMgr KeyType = "mgr" - KeyOSDPrepare KeyType = "prepareosd" - KeyOSD KeyType = "osd" - KeyCleanup KeyType = "cleanup" - KeyMonitoring KeyType = "monitoring" - KeyCrashCollector KeyType = "crashcollector" + KeyAll = "all" + KeyMds KeyType = "mds" + KeyMon KeyType = "mon" + KeyMonArbiter KeyType = "arbiter" + KeyMgr KeyType = "mgr" + KeyOSDPrepare KeyType = "prepareosd" + KeyOSD KeyType = "osd" + KeyCleanup KeyType = "cleanup" + KeyMonitoring KeyType = "monitoring" + KeyCrashCollector KeyType = "crashcollector" + KeyClusterMetadata KeyType = "clusterMetadata" ) diff --git a/pkg/operator/ceph/cluster/mon/mon.go b/pkg/operator/ceph/cluster/mon/mon.go index c0c30865f..070ce2edc 100644 --- a/pkg/operator/ceph/cluster/mon/mon.go +++ b/pkg/operator/ceph/cluster/mon/mon.go @@ -493,6 +493,11 @@ func (c *Cluster) initClusterInfo(cephVersion cephver.CephVersion, clusterName s return errors.Wrap(err, "failed to get cluster info") } + err = keyring.ApplyClusterMetadataToSecret(c.ClusterInfo, AppName, c.context, c.spec.Annotations) + if err != nil { + return errors.Wrap(err, "failed to apply annotation") + } + c.ClusterInfo.CephVersion = cephVersion c.ClusterInfo.OwnerInfo = c.ownerInfo c.ClusterInfo.Context = context @@ -509,7 +514,7 @@ func (c *Cluster) initClusterInfo(cephVersion cephver.CephVersion, clusterName s return errors.Wrap(err, "failed to save mon keyring secret") } // also store the admin keyring for other daemons that might need it during init - if err := k.Admin().CreateOrUpdate(c.ClusterInfo); err != nil { + if err := k.Admin().CreateOrUpdate(c.ClusterInfo, c.context, c.spec.Annotations); err != nil { return errors.Wrap(err, "failed to save admin keyring secret") } @@ -1052,6 +1057,8 @@ func (c *Cluster) persistExpectedMonDaemons() error { Finalizers: []string{DisasterProtectionFinalizerName}, }, } + cephv1.GetClusterMetadataAnnotations(c.spec.Annotations).ApplyToObjectMeta(&configMap.ObjectMeta) + err := c.ownerInfo.SetControllerReference(configMap) if err != nil { return errors.Wrapf(err, "failed to set owner reference mon configmap %q", configMap.Name) diff --git a/pkg/operator/ceph/cluster/mon/mon_test.go b/pkg/operator/ceph/cluster/mon/mon_test.go index 31938d307..14e6f37db 100644 --- a/pkg/operator/ceph/cluster/mon/mon_test.go +++ b/pkg/operator/ceph/cluster/mon/mon_test.go @@ -181,11 +181,21 @@ func TestStartMonPods(t *testing.T) { assert.NoError(t, err) c := newCluster(context, namespace, true, v1.ResourceRequirements{}) c.ClusterInfo = clienttest.CreateTestClusterInfo(1) + c.spec.Annotations = cephv1.AnnotationsSpec{ + cephv1.KeyClusterMetadata: cephv1.Annotations{ + "key": "value", + }, + } // start a basic cluster _, err = c.Start(c.ClusterInfo, c.rookVersion, cephver.Octopus, c.spec) assert.NoError(t, err) + // test annotations + secret, err := c.context.Clientset.CoreV1().Secrets(c.Namespace).Get(c.ClusterInfo.Context, AppName, metav1.GetOptions{}) + assert.NoError(t, err) + assert.Equal(t, map[string]string{"key": "value"}, secret.Annotations) + validateStart(t, c) // starting again should be a no-op @@ -263,7 +273,7 @@ func validateStart(t *testing.T, c *Cluster) { func TestPersistMons(t *testing.T) { clientset := test.New(t, 1) ownerInfo := cephclient.NewMinimumOwnerInfoWithOwnerRef() - c := New(&clusterd.Context{Clientset: clientset}, "ns", cephv1.ClusterSpec{}, ownerInfo, &sync.Mutex{}) + c := New(&clusterd.Context{Clientset: clientset}, "ns", cephv1.ClusterSpec{Annotations: cephv1.AnnotationsSpec{cephv1.KeyClusterMetadata: cephv1.Annotations{"key": "value"}}}, ownerInfo, &sync.Mutex{}) setCommonMonProperties(c, 1, cephv1.MonSpec{Count: 3, AllowMultiplePerNode: true}, "myversion") // Persist mon a @@ -273,6 +283,7 @@ func TestPersistMons(t *testing.T) { cm, err := c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Get(context.TODO(), EndpointConfigMapName, metav1.GetOptions{}) assert.NoError(t, err) assert.Equal(t, "a=1.2.3.1:6789", cm.Data[EndpointDataKey]) + assert.Equal(t, map[string]string{"key": "value"}, cm.Annotations) // Persist mon b, and remove mon a for simply testing the configmap is updated c.ClusterInfo.Monitors["b"] = &cephclient.MonInfo{Name: "b", Endpoint: "4.5.6.7:3300"} diff --git a/pkg/operator/ceph/config/keyring/admin.go b/pkg/operator/ceph/config/keyring/admin.go index fa793f6a7..cd3ac2d13 100644 --- a/pkg/operator/ceph/config/keyring/admin.go +++ b/pkg/operator/ceph/config/keyring/admin.go @@ -19,7 +19,11 @@ package keyring import ( "fmt" + "github.com/pkg/errors" + v1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" + "github.com/rook/rook/pkg/clusterd" cephclient "github.com/rook/rook/pkg/daemon/ceph/client" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( @@ -48,7 +52,34 @@ func (s *SecretStore) Admin() *AdminStore { } // CreateOrUpdate creates or updates the admin keyring secret with cluster information. -func (a *AdminStore) CreateOrUpdate(c *cephclient.ClusterInfo) error { +func (a *AdminStore) CreateOrUpdate(c *cephclient.ClusterInfo, context *clusterd.Context, annotation v1.AnnotationsSpec) error { keyring := fmt.Sprintf(adminKeyringTemplate, c.CephCred.Secret) - return a.secretStore.CreateOrUpdate(adminKeyringResourceName, keyring) + err := a.secretStore.CreateOrUpdate(adminKeyringResourceName, keyring) + if err != nil { + return err + } + err = ApplyClusterMetadataToSecret(c, keyringSecretName(adminKeyringResourceName), context, annotation) + if err != nil { + return errors.Errorf("failed to update admin secrets. %v", err) + } + + return nil +} + +func ApplyClusterMetadataToSecret(c *cephclient.ClusterInfo, secretName string, context *clusterd.Context, annotation v1.AnnotationsSpec) error { + // Get secret to update annotation + secret, err := context.Clientset.CoreV1().Secrets(c.Namespace).Get(c.Context, secretName, metav1.GetOptions{}) + if err != nil { + return errors.Wrapf(err, "failed to get %s secrets", secretName) + } + // We would need to reset the annotations back to empty, then reapply the annotations this is because the in some rook-ceph-mon secret is retrieved + // and then updated, instead of a new secret being generated. + secret.Annotations = map[string]string{} + v1.GetClusterMetadataAnnotations(annotation).ApplyToObjectMeta(&secret.ObjectMeta) + + _, err = context.Clientset.CoreV1().Secrets(c.Namespace).Update(c.Context, secret, metav1.UpdateOptions{}) + if err != nil { + return errors.Wrapf(err, "failed to update %s secret.", secretName) + } + return nil } diff --git a/pkg/operator/ceph/config/keyring/admin_test.go b/pkg/operator/ceph/config/keyring/admin_test.go index beee9a2e7..fa5261dbe 100644 --- a/pkg/operator/ceph/config/keyring/admin_test.go +++ b/pkg/operator/ceph/config/keyring/admin_test.go @@ -22,6 +22,7 @@ import ( "path" "testing" + v1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" "github.com/rook/rook/pkg/clusterd" cephclient "github.com/rook/rook/pkg/daemon/ceph/client" clienttest "github.com/rook/rook/pkg/daemon/ceph/client/test" @@ -53,13 +54,18 @@ func TestAdminKeyringStore(t *testing.T) { // create key clusterInfo.CephCred.Secret = "adminsecretkey" - err := k.Admin().CreateOrUpdate(clusterInfo) + err := k.Admin().CreateOrUpdate(clusterInfo, ctx, v1.AnnotationsSpec{v1.KeyClusterMetadata: v1.Annotations{"key": "value"}}) assert.NoError(t, err) assertKeyringData(fmt.Sprintf(adminKeyringTemplate, "adminsecretkey")) + // test annotation + secret, err := ctx.Clientset.CoreV1().Secrets(clusterInfo.Namespace).Get(clusterInfo.Context, keyringSecretName(adminKeyringResourceName), metav1.GetOptions{}) + assert.NoError(t, err) + assert.Equal(t, map[string]string{"key": "value"}, secret.Annotations) + // update key clusterInfo.CephCred.Secret = "differentsecretkey" - err = k.Admin().CreateOrUpdate(clusterInfo) + err = k.Admin().CreateOrUpdate(clusterInfo, ctx, v1.AnnotationsSpec{}) assert.NoError(t, err) assertKeyringData(fmt.Sprintf(adminKeyringTemplate, "differentsecretkey")) } @@ -74,7 +80,7 @@ func TestAdminVolumeAndMount(t *testing.T) { s := GetSecretStore(ctx, clusterInfo, ownerInfo) clusterInfo.CephCred.Secret = "adminsecretkey" - err := s.Admin().CreateOrUpdate(clusterInfo) + err := s.Admin().CreateOrUpdate(clusterInfo, ctx, v1.AnnotationsSpec{}) assert.NoError(t, err) v := Volume().Admin()