Merge pull request #9607 from rook/mergify/bp/release-1.8/pr-9547

mon: add annotation to mon secret and cm (backport #9547)
This commit is contained in:
mergify[bot] authored and GitHub committed 2022-01-19 15:15:29 +00:00
commit 960bb7fcc8
8 files changed
+84 -18

No files matched your search

+3 -1
View File
@@ -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
+4
View File
@@ -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:
+4
View File
@@ -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 {
+11 -10
View File
@@ -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"
)
+8 -1
View File
@@ -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)
+12 -1
View File
@@ -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"}
+33 -2
View File
@@ -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
}
@@ -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()