diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index cc455127aa..b1c04c7136 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -23,6 +23,20 @@ jobs: with: tool_name: gofmt + goimports-reviser: + name: runner / suggester / goimports-reviser + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.1.1 + - uses: actions/setup-go@v5 + with: + go-version: '^1.22' + - run: go install -v github.com/incu6us/goimports-reviser/v3@latest + - run: $(go env GOPATH)/bin/goimports-reviser -imports-order "std,general,company,project" -company-prefixes "github.com/percona" ./... + - uses: reviewdog/action-suggester@v1 + with: + tool_name: goimports-reviser + shfmt: name: runner / suggester / shfmt runs-on: ubuntu-latest diff --git a/e2e-tests/tls-issue-cert-manager/compare/certificate_some-name-ssl-internal.yml b/e2e-tests/tls-issue-cert-manager/compare/certificate_some-name-ssl-internal.yml index 192cc65056..06c1844dbf 100644 --- a/e2e-tests/tls-issue-cert-manager/compare/certificate_some-name-ssl-internal.yml +++ b/e2e-tests/tls-issue-cert-manager/compare/certificate_some-name-ssl-internal.yml @@ -3,6 +3,11 @@ kind: Certificate metadata: annotations: {} generation: 1 + labels: + app.kubernetes.io/instance: some-name + app.kubernetes.io/managed-by: percona-server-mongodb-operator + app.kubernetes.io/name: percona-server-mongodb + app.kubernetes.io/part-of: percona-server-mongodb name: some-name-ssl-internal ownerReferences: - blockOwnerDeletion: true diff --git a/e2e-tests/tls-issue-cert-manager/compare/certificate_some-name-ssl.yml b/e2e-tests/tls-issue-cert-manager/compare/certificate_some-name-ssl.yml index 514a5ba4bf..4b0d14caf9 100644 --- a/e2e-tests/tls-issue-cert-manager/compare/certificate_some-name-ssl.yml +++ b/e2e-tests/tls-issue-cert-manager/compare/certificate_some-name-ssl.yml @@ -3,6 +3,11 @@ kind: Certificate metadata: annotations: {} generation: 1 + labels: + app.kubernetes.io/instance: some-name + app.kubernetes.io/managed-by: percona-server-mongodb-operator + app.kubernetes.io/name: percona-server-mongodb + app.kubernetes.io/part-of: percona-server-mongodb name: some-name-ssl ownerReferences: - blockOwnerDeletion: true diff --git a/e2e-tests/tls-issue-cert-manager/compare/issuer_some-name-psmdb-ca-issuer.yml b/e2e-tests/tls-issue-cert-manager/compare/issuer_some-name-psmdb-ca-issuer.yml index b44c57cbd8..a8ef61ad14 100644 --- a/e2e-tests/tls-issue-cert-manager/compare/issuer_some-name-psmdb-ca-issuer.yml +++ b/e2e-tests/tls-issue-cert-manager/compare/issuer_some-name-psmdb-ca-issuer.yml @@ -3,6 +3,11 @@ kind: Issuer metadata: annotations: {} generation: 1 + labels: + app.kubernetes.io/instance: some-name + app.kubernetes.io/managed-by: percona-server-mongodb-operator + app.kubernetes.io/name: percona-server-mongodb + app.kubernetes.io/part-of: percona-server-mongodb name: some-name-psmdb-ca-issuer ownerReferences: - blockOwnerDeletion: true diff --git a/e2e-tests/tls-issue-cert-manager/compare/issuer_some-name-psmdb-issuer.yml b/e2e-tests/tls-issue-cert-manager/compare/issuer_some-name-psmdb-issuer.yml index 6330059af5..5057173e6f 100644 --- a/e2e-tests/tls-issue-cert-manager/compare/issuer_some-name-psmdb-issuer.yml +++ b/e2e-tests/tls-issue-cert-manager/compare/issuer_some-name-psmdb-issuer.yml @@ -3,6 +3,11 @@ kind: Issuer metadata: annotations: {} generation: 1 + labels: + app.kubernetes.io/instance: some-name + app.kubernetes.io/managed-by: percona-server-mongodb-operator + app.kubernetes.io/name: percona-server-mongodb + app.kubernetes.io/part-of: percona-server-mongodb name: some-name-psmdb-issuer ownerReferences: - blockOwnerDeletion: true diff --git a/pkg/apis/psmdb/v1/psmdb_types.go b/pkg/apis/psmdb/v1/psmdb_types.go index e8c2e85321..6239314df1 100644 --- a/pkg/apis/psmdb/v1/psmdb_types.go +++ b/pkg/apis/psmdb/v1/psmdb_types.go @@ -25,6 +25,7 @@ import ( "github.com/percona/percona-backup-mongodb/pbm/compress" "github.com/percona/percona-backup-mongodb/pbm/defs" + "github.com/percona/percona-server-mongodb-operator/pkg/mcs" "github.com/percona/percona-server-mongodb-operator/pkg/util/numstr" "github.com/percona/percona-server-mongodb-operator/version" @@ -626,29 +627,6 @@ func (r ReplsetSpec) CustomReplsetName() (string, error) { return cfg.Replication.ReplSetName, nil } -func (r *ReplsetSpec) MongodLabels(cr *PerconaServerMongoDB) map[string]string { - return map[string]string{ - "app.kubernetes.io/name": "percona-server-mongodb", - "app.kubernetes.io/instance": cr.Name, - "app.kubernetes.io/replset": r.Name, - "app.kubernetes.io/managed-by": "percona-server-mongodb-operator", - "app.kubernetes.io/part-of": "percona-server-mongodb", - "app.kubernetes.io/component": "mongod", - } -} - -func (r *ReplsetSpec) ArbiterLabels(cr *PerconaServerMongoDB) map[string]string { - ls := r.MongodLabels(cr) - ls["app.kubernetes.io/component"] = "arbiter" - return ls -} - -func (r *ReplsetSpec) NonVotingLabels(cr *PerconaServerMongoDB) map[string]string { - ls := r.MongodLabels(cr) - ls["app.kubernetes.io/component"] = "nonVoting" - return ls -} - type LivenessProbeExtended struct { corev1.Probe `json:",inline"` StartupDelaySeconds int `json:"startupDelaySeconds,omitempty"` @@ -1144,33 +1122,6 @@ func (cr *PerconaServerMongoDB) MCSEnabled() bool { return mcs.IsAvailable() && cr.Spec.MultiCluster.Enabled } -func ClusterLabels(cr *PerconaServerMongoDB) map[string]string { - return map[string]string{ - "app.kubernetes.io/name": "percona-server-mongodb", - "app.kubernetes.io/instance": cr.Name, - "app.kubernetes.io/managed-by": "percona-server-mongodb-operator", - "app.kubernetes.io/part-of": "percona-server-mongodb", - } -} - -func MongodLabels(cr *PerconaServerMongoDB) map[string]string { - lbls := ClusterLabels(cr) - lbls["app.kubernetes.io/component"] = "mongod" - return lbls -} - -func ArbiterLabels(cr *PerconaServerMongoDB) map[string]string { - lbls := ClusterLabels(cr) - lbls["app.kubernetes.io/component"] = "arbiter" - return lbls -} - -func MongosLabels(cr *PerconaServerMongoDB) map[string]string { - lbls := ClusterLabels(cr) - lbls["app.kubernetes.io/component"] = "mongos" - return lbls -} - const ( FinalizerDeletePVC = "delete-psmdb-pvc" FinalizerDeletePSMDBPodsInOrder = "delete-psmdb-pods-in-order" diff --git a/pkg/controller/perconaservermongodb/backup.go b/pkg/controller/perconaservermongodb/backup.go index 4b77419658..a6e8f2e669 100644 --- a/pkg/controller/perconaservermongodb/backup.go +++ b/pkg/controller/perconaservermongodb/backup.go @@ -9,6 +9,7 @@ import ( "strconv" "time" + "github.com/pkg/errors" "github.com/robfig/cron/v3" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" @@ -23,9 +24,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" logf "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/pkg/errors" - api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/backup" ) @@ -103,7 +103,7 @@ func (r *ReconcilePerconaServerMongoDB) deleteOldBackupTasks(ctx context.Context log := logf.FromContext(ctx) if cr.CompareVersion("1.13.0") < 0 { - ls := backup.NewBackupCronJobLabels(cr.Name, cr.Spec.Backup.Labels) + ls := naming.NewBackupCronJobLabels(cr, cr.Spec.Backup.Labels) tasksList := &batchv1.CronJobList{} err := r.client.List(ctx, tasksList, diff --git a/pkg/controller/perconaservermongodb/connections_test.go b/pkg/controller/perconaservermongodb/connections_test.go index d5bfd60433..480187bdc5 100644 --- a/pkg/controller/perconaservermongodb/connections_test.go +++ b/pkg/controller/perconaservermongodb/connections_test.go @@ -19,6 +19,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo" mongoFake "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo/fake" @@ -117,8 +118,8 @@ func TestConnectionLeaks(t *testing.T) { obj := []client.Object{} obj = append(obj, cr, - fakeStatefulset(cr, cr.Spec.Replsets[0].Name, cr.Spec.Replsets[0].Size, updatedRevision), - fakeStatefulset(cr, "deleted-sts", 0, ""), + fakeStatefulset(cr, cr.Spec.Replsets[0], cr.Spec.Replsets[0].Size, updatedRevision), + fakeStatefulset(cr, &api.ReplsetSpec{Name: "deleted-sts"}, 0, ""), ) rsPods := fakePodsForRS(cr, cr.Spec.Replsets[0]) @@ -135,7 +136,7 @@ func TestConnectionLeaks(t *testing.T) { if err := cr.CheckNSetDefaults(version.PlatformKubernetes, logf.FromContext(ctx)); err != nil { t.Fatal(err) } - obj = append(obj, fakeStatefulset(cr, cr.Spec.Sharding.ConfigsvrReplSet.Name, cr.Spec.Sharding.ConfigsvrReplSet.Size, updatedRevision)) + obj = append(obj, fakeStatefulset(cr, cr.Spec.Sharding.ConfigsvrReplSet, cr.Spec.Sharding.ConfigsvrReplSet.Size, updatedRevision)) allPods = append(allPods, fakePodsForRS(cr, cr.Spec.Sharding.ConfigsvrReplSet)...) } @@ -216,7 +217,6 @@ func TestConnectionLeaks(t *testing.T) { } }) } - } func updateResource[T any](resource T, update func(T)) T { @@ -287,11 +287,11 @@ func updatePodsForSmartUpdate(ctx context.Context, cl client.Client, cr *api.Per func fakePodsForRS(cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec) []client.Object { pods := []client.Object{} - ls := psmdb.RSLabels(cr, rs.Name) + ls := naming.RSLabels(cr, rs) - ls["app.kubernetes.io/component"] = "mongod" + ls[naming.LabelKubernetesComponent] = "mongod" if rs.Name == api.ConfigReplSetName { - ls["app.kubernetes.io/component"] = api.ConfigReplSetName + ls[naming.LabelKubernetesComponent] = api.ConfigReplSetName } for i := 0; i < int(rs.Size); i++ { pods = append(pods, fakePod(fmt.Sprintf("%s-%s-%d", cr.Name, rs.Name, i), cr.Namespace, ls, "mongod")) @@ -301,7 +301,7 @@ func fakePodsForRS(cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec) []client.O func fakePodsForMongos(cr *api.PerconaServerMongoDB) []client.Object { pods := []client.Object{} - ls := psmdb.MongosLabels(cr) + ls := naming.MongosLabels(cr) ms := cr.Spec.Sharding.Mongos for i := 0; i < int(ms.Size); i++ { pods = append(pods, fakePod(fmt.Sprintf("%s-%s-%d", cr.Name, "mongos", i), cr.Namespace, ls, "mongos")) @@ -343,12 +343,12 @@ func fakePod(name, namespace string, ls map[string]string, containerName string) } } -func fakeStatefulset(cr *api.PerconaServerMongoDB, rsName string, size int32, updateRevision string) client.Object { +func fakeStatefulset(cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec, size int32, updateRevision string) client.Object { return &appsv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ - Name: fmt.Sprintf("%s-%s", cr.Name, rsName), + Name: fmt.Sprintf("%s-%s", cr.Name, rs.Name), Namespace: cr.Namespace, - Labels: psmdb.RSLabels(cr, rsName), + Labels: naming.RSLabels(cr, rs), }, Spec: appsv1.StatefulSetSpec{ Replicas: &size, @@ -371,12 +371,14 @@ func (g *fakeMongoClientProvider) Mongo(ctx context.Context, cr *api.PerconaServ fakeClient := mongoFake.NewClient() return &fakeMongoClient{pods: g.pods, cr: g.cr, connectionCount: g.connectionCount, Client: fakeClient}, nil } + func (g *fakeMongoClientProvider) Mongos(ctx context.Context, cr *api.PerconaServerMongoDB, role api.UserRole) (mongo.Client, error) { *g.connectionCount++ fakeClient := mongoFake.NewClient() return &fakeMongoClient{pods: g.pods, cr: g.cr, connectionCount: g.connectionCount, Client: fakeClient}, nil } + func (g *fakeMongoClientProvider) Standalone(ctx context.Context, cr *api.PerconaServerMongoDB, role api.UserRole, host string, tlsEnabled bool) (mongo.Client, error) { *g.connectionCount++ @@ -487,7 +489,6 @@ func (c *fakeMongoClient) RemoveShard(ctx context.Context, shard string) (mongo. OK: 1, }, }, nil - } func (c *fakeMongoClient) IsBalancerRunning(ctx context.Context) (bool, error) { diff --git a/pkg/controller/perconaservermongodb/external_service.go b/pkg/controller/perconaservermongodb/external_service.go index 950bb82850..8b2e91d121 100644 --- a/pkg/controller/perconaservermongodb/external_service.go +++ b/pkg/controller/perconaservermongodb/external_service.go @@ -4,16 +4,16 @@ import ( "context" "strconv" - k8serrors "k8s.io/apimachinery/pkg/api/errors" - + "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" "github.com/percona/percona-server-mongodb-operator/pkg/mcs" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" - "github.com/pkg/errors" ) func (r *ReconcilePerconaServerMongoDB) ensureExternalServices(ctx context.Context, cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec, podList *corev1.PodList) ([]corev1.Service, error) { @@ -45,7 +45,7 @@ func (r *ReconcilePerconaServerMongoDB) ensureExternalServices(ctx context.Conte } func (r *ReconcilePerconaServerMongoDB) exportService(ctx context.Context, cr *api.PerconaServerMongoDB, svc *corev1.Service) error { - ls := api.ClusterLabels(cr) + ls := naming.ClusterLabels(cr) if !cr.Spec.MultiCluster.Enabled { return nil } @@ -64,7 +64,7 @@ func (r *ReconcilePerconaServerMongoDB) exportServices(ctx context.Context, cr * return nil } - ls := api.ClusterLabels(cr) + ls := naming.ClusterLabels(cr) seList := mcs.ServiceExportList() err := r.client.List(ctx, diff --git a/pkg/controller/perconaservermongodb/finalizers_test.go b/pkg/controller/perconaservermongodb/finalizers_test.go index acbc8aec53..4906cc0f8e 100644 --- a/pkg/controller/perconaservermongodb/finalizers_test.go +++ b/pkg/controller/perconaservermongodb/finalizers_test.go @@ -20,7 +20,7 @@ func TestCheckFinalizers(t *testing.T) { defaultCR := readDefaultCR(t, crName, ns) obj := append( fakePodsForRS(defaultCR, defaultCR.Spec.Replsets[0]), - fakeStatefulset(defaultCR, defaultCR.Spec.Replsets[0].Name, defaultCR.Spec.Replsets[0].Size, ""), + fakeStatefulset(defaultCR, defaultCR.Spec.Replsets[0], defaultCR.Spec.Replsets[0].Size, ""), ) tests := []struct { diff --git a/pkg/controller/perconaservermongodb/getters.go b/pkg/controller/perconaservermongodb/getters.go index 5ef045eb6b..90711dd67d 100644 --- a/pkg/controller/perconaservermongodb/getters.go +++ b/pkg/controller/perconaservermongodb/getters.go @@ -13,6 +13,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" ) func (r *ReconcilePerconaServerMongoDB) getMongodPods(ctx context.Context, cr *api.PerconaServerMongoDB) (corev1.PodList, error) { @@ -21,7 +22,7 @@ func (r *ReconcilePerconaServerMongoDB) getMongodPods(ctx context.Context, cr *a &mongodPods, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(api.MongodLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.MongodLabels(cr, nil)), }, ) @@ -34,7 +35,6 @@ func (r *ReconcilePerconaServerMongoDB) getMongosDeployment(ctx context.Context, err := r.client.Get(ctx, cr.MongosNamespacedName(), &mongos) return mongos, err - } func (r *ReconcilePerconaServerMongoDB) getMongosPods(ctx context.Context, cr *api.PerconaServerMongoDB) (corev1.PodList, error) { @@ -43,24 +43,23 @@ func (r *ReconcilePerconaServerMongoDB) getMongosPods(ctx context.Context, cr *a &mongosPods, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(api.MongosLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.MongosLabels(cr)), }, ) return mongosPods, err } -func (r *ReconcilePerconaServerMongoDB) getArbiterStatefulset(ctx context.Context, cr *api.PerconaServerMongoDB, rs string) (appsv1.StatefulSet, error) { +func (r *ReconcilePerconaServerMongoDB) getArbiterStatefulset(ctx context.Context, cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec) (appsv1.StatefulSet, error) { list := appsv1.StatefulSetList{} - l := api.ArbiterLabels(cr) - l["app.kubernetes.io/replset"] = rs + ls := naming.ArbiterLabels(cr, rs) err := r.client.List(ctx, &list, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(l), + LabelSelector: labels.SelectorFromSet(ls), }, ) @@ -86,7 +85,7 @@ func (r *ReconcilePerconaServerMongoDB) getArbiterStatefulsets(ctx context.Conte &list, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(api.ArbiterLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.ArbiterLabels(cr, nil)), }, ) @@ -100,7 +99,7 @@ func (r *ReconcilePerconaServerMongoDB) getMongodStatefulsets(ctx context.Contex &list, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(api.MongodLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.MongodLabels(cr, nil)), }, ) @@ -110,9 +109,9 @@ func (r *ReconcilePerconaServerMongoDB) getMongodStatefulsets(ctx context.Contex func (r *ReconcilePerconaServerMongoDB) getStatefulsetsExceptMongos(ctx context.Context, cr *api.PerconaServerMongoDB) (appsv1.StatefulSetList, error) { list := appsv1.StatefulSetList{} - selectors := labels.SelectorFromSet(api.ClusterLabels(cr)) + selectors := labels.SelectorFromSet(naming.ClusterLabels(cr)) - req, err := labels.NewRequirement("app.kubernetes.io/component", selection.NotEquals, []string{"mongos"}) + req, err := labels.NewRequirement(naming.LabelKubernetesComponent, selection.NotEquals, []string{"mongos"}) if err != nil { return list, errors.Wrap(err, "get selector requirement") } @@ -137,7 +136,7 @@ func (r *ReconcilePerconaServerMongoDB) getAllstatefulsets(ctx context.Context, &list, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(api.ClusterLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.ClusterLabels(cr)), }, ) @@ -163,7 +162,7 @@ func (r *ReconcilePerconaServerMongoDB) getAllPVCs(ctx context.Context, cr *api. &list, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(api.ClusterLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.ClusterLabels(cr)), }, ) @@ -177,7 +176,7 @@ func (r *ReconcilePerconaServerMongoDB) getMongodPVCs(ctx context.Context, cr *a &list, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(api.MongodLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.MongodLabels(cr, nil)), }, ) diff --git a/pkg/controller/perconaservermongodb/mgo.go b/pkg/controller/perconaservermongodb/mgo.go index a1a1122f16..c4fb10044c 100644 --- a/pkg/controller/perconaservermongodb/mgo.go +++ b/pkg/controller/perconaservermongodb/mgo.go @@ -17,6 +17,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo" "github.com/percona/percona-server-mongodb-operator/pkg/util" @@ -303,7 +304,7 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context, member.Horizons = horizons } - switch pod.Labels["app.kubernetes.io/component"] { + switch pod.Labels[naming.LabelKubernetesComponent] { case "arbiter": member.ArbiterOnly = true member.Priority = 0 diff --git a/pkg/controller/perconaservermongodb/psmdb_controller.go b/pkg/controller/perconaservermongodb/psmdb_controller.go index 28d1745b6f..f2f19f3974 100644 --- a/pkg/controller/perconaservermongodb/psmdb_controller.go +++ b/pkg/controller/perconaservermongodb/psmdb_controller.go @@ -36,6 +36,7 @@ import ( "github.com/percona/percona-server-mongodb-operator/clientcmd" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/backup" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/secret" @@ -335,7 +336,7 @@ func (r *ReconcilePerconaServerMongoDB) Reconcile(ctx context.Context, request r } for _, sts := range removed { - rsName := sts.Labels["app.kubernetes.io/replset"] + rsName := sts.Labels[naming.LabelKubernetesReplset] log.Info("Deleting STS component from replst", "sts", sts.Name, "rs", rsName) @@ -344,7 +345,7 @@ func (r *ReconcilePerconaServerMongoDB) Reconcile(ctx context.Context, request r return reconcile.Result{}, errors.Wrapf(err, "check remove posibility for rs %s", rsName) } - if sts.Labels["app.kubernetes.io/component"] == "mongod" { + if sts.Labels[naming.LabelKubernetesComponent] == "mongod" { log.Info("Removing RS from shard", "rs", rsName) err = r.removeRSFromShard(ctx, cr, rsName) if err != nil { @@ -451,7 +452,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileReplsets(ctx context.Context, c return "", errors.Errorf("%s is reserved name for config server replset", api.ConfigReplSetName) } - matchLabels := replset.MongodLabels(cr) + matchLabels := naming.MongodLabels(cr, replset) pods, err := psmdb.GetRSPods(ctx, r.client, cr, replset.Name) if err != nil { @@ -466,7 +467,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileReplsets(ctx context.Context, c } if replset.Arbiter.Enabled { - matchLabels = replset.ArbiterLabels(cr) + matchLabels = naming.ArbiterLabels(cr, replset) _, err := r.reconcileStatefulSet(ctx, cr, replset, matchLabels) if err != nil { err = errors.Errorf("reconcile Arbiter StatefulSet for %s: %v", replset.Name, err) @@ -485,7 +486,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileReplsets(ctx context.Context, c } if replset.NonVoting.Enabled { - matchLabels = replset.NonVotingLabels(cr) + matchLabels = naming.NonVotingLabels(cr, replset) _, err := r.reconcileStatefulSet(ctx, cr, replset, matchLabels) if err != nil { err = errors.Errorf("reconcile nonVoting StatefulSet for %s: %v", replset.Name, err) @@ -683,7 +684,7 @@ func (r *ReconcilePerconaServerMongoDB) getSTSforRemoval(ctx context.Context, cr &client.ListOptions{ Namespace: cr.Namespace, LabelSelector: labels.SelectorFromSet(map[string]string{ - "app.kubernetes.io/instance": cr.Name, + naming.LabelKubernetesInstance: cr.Name, }), }, ); err != nil { @@ -697,12 +698,12 @@ func (r *ReconcilePerconaServerMongoDB) getSTSforRemoval(ctx context.Context, cr } for _, sts := range stsList.Items { - component := sts.Labels["app.kubernetes.io/component"] + component := sts.Labels[naming.LabelKubernetesComponent] if component == "mongos" || sts.Name == cr.Name+"-"+api.ConfigReplSetName { continue } - rsName := sts.Labels["app.kubernetes.io/replset"] + rsName := sts.Labels[naming.LabelKubernetesReplset] if _, ok := appliedRSNames[rsName]; ok { continue @@ -974,7 +975,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileMongodConfigMaps(ctx context.Co return errors.Wrap(err, "failed to delete mongod config map") } } else { - err := r.createOrUpdateConfigMap(ctx, cr, &corev1.ConfigMap{ + cm := &corev1.ConfigMap{ TypeMeta: metav1.TypeMeta{ APIVersion: "v1", Kind: "ConfigMap", @@ -982,11 +983,16 @@ func (r *ReconcilePerconaServerMongoDB) reconcileMongodConfigMaps(ctx context.Co ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: cr.Namespace, + Labels: naming.RSLabels(cr, rs), }, Data: map[string]string{ "mongod.conf": string(rs.Configuration), }, - }) + } + if cr.CompareVersion("1.17.0") < 0 { + cm.Labels = nil + } + err := r.createOrUpdateConfigMap(ctx, cr, cm) if err != nil { return errors.Wrap(err, "create or update config map") } @@ -1004,16 +1010,20 @@ func (r *ReconcilePerconaServerMongoDB) reconcileMongodConfigMaps(ctx context.Co continue } - - err := r.createOrUpdateConfigMap(ctx, cr, &corev1.ConfigMap{ + cm := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: cr.Namespace, + Labels: naming.RSLabels(cr, rs), }, Data: map[string]string{ "mongod.conf": string(rs.NonVoting.Configuration), }, - }) + } + if cr.CompareVersion("1.17.0") < 0 { + cm.Labels = nil + } + err := r.createOrUpdateConfigMap(ctx, cr, cm) if err != nil { return errors.Wrap(err, "create or update nonvoting config map") } @@ -1034,7 +1044,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileMongosConfigMap(ctx context.Con return nil } - err := r.createOrUpdateConfigMap(ctx, cr, &corev1.ConfigMap{ + cm := &corev1.ConfigMap{ TypeMeta: metav1.TypeMeta{ APIVersion: "v1", Kind: "ConfigMap", @@ -1042,11 +1052,16 @@ func (r *ReconcilePerconaServerMongoDB) reconcileMongosConfigMap(ctx context.Con ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: cr.Namespace, + Labels: naming.MongosLabels(cr), }, Data: map[string]string{ "mongos.conf": string(cr.Spec.Sharding.Mongos.Configuration), }, - }) + } + if cr.CompareVersion("1.17.0") < 0 { + cm.Labels = nil + } + err := r.createOrUpdateConfigMap(ctx, cr, cm) if err != nil { return err } @@ -1242,7 +1257,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileMongosStatefulset(ctx context.C return errors.Wrapf(err, "update or create mongos %s", sts) } - err = r.reconcilePDB(ctx, cr.Spec.Sharding.Mongos.PodDisruptionBudget, templateSpec.Labels, cr.Namespace, sts) + err = r.reconcilePDB(ctx, cr, cr.Spec.Sharding.Mongos.PodDisruptionBudget, templateSpec.Labels, cr.Namespace, sts) if err != nil { return errors.Wrap(err, "reconcile PodDisruptionBudget for mongos") } @@ -1425,7 +1440,7 @@ func (r *ReconcilePerconaServerMongoDB) getTLSHash(ctx context.Context, cr *api. return hash, nil } -func (r *ReconcilePerconaServerMongoDB) reconcilePDB(ctx context.Context, spec *api.PodDisruptionBudgetSpec, labels map[string]string, namespace string, owner client.Object) error { +func (r *ReconcilePerconaServerMongoDB) reconcilePDB(ctx context.Context, cr *api.PerconaServerMongoDB, spec *api.PodDisruptionBudgetSpec, labels map[string]string, namespace string, owner client.Object) error { if spec == nil { return nil } @@ -1448,6 +1463,9 @@ func (r *ReconcilePerconaServerMongoDB) reconcilePDB(ctx context.Context, spec * } pdb := psmdb.PodDisruptionBudget(spec, labels, namespace) + if cr.CompareVersion("1.17.0") < 0 { + pdb.Labels = nil + } err := setControllerReference(owner, pdb, r.scheme) if err != nil { return errors.Wrap(err, "set owner reference") diff --git a/pkg/controller/perconaservermongodb/secrets.go b/pkg/controller/perconaservermongodb/secrets.go index 3ea6f46c53..ef43e5c500 100644 --- a/pkg/controller/perconaservermongodb/secrets.go +++ b/pkg/controller/perconaservermongodb/secrets.go @@ -13,6 +13,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/secret" ) @@ -103,10 +104,14 @@ func (r *ReconcilePerconaServerMongoDB) reconcileUsersSecret(ctx context.Context ObjectMeta: metav1.ObjectMeta{ Name: cr.Spec.Secrets.Users, Namespace: cr.Namespace, + Labels: naming.ClusterLabels(cr), }, Data: data, Type: corev1.SecretTypeOpaque, } + if cr.CompareVersion("1.17.0") < 0 { + secretObj.Labels = nil + } err = r.client.Create(ctx, &secretObj) if err != nil { return fmt.Errorf("create Users secret: %v", err) diff --git a/pkg/controller/perconaservermongodb/smart.go b/pkg/controller/perconaservermongodb/smart.go index f545e0481a..86d534831e 100644 --- a/pkg/controller/perconaservermongodb/smart.go +++ b/pkg/controller/perconaservermongodb/smart.go @@ -18,6 +18,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/backup" ) @@ -34,17 +35,11 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api return nil } - matchLabels := map[string]string{ - "app.kubernetes.io/name": "percona-server-mongodb", - "app.kubernetes.io/instance": cr.Name, - "app.kubernetes.io/replset": replset.Name, - "app.kubernetes.io/managed-by": "percona-server-mongodb-operator", - "app.kubernetes.io/part-of": "percona-server-mongodb", - } + matchLabels := naming.RSLabels(cr, replset) - label, ok := sfs.Labels["app.kubernetes.io/component"] + label, ok := sfs.Labels[naming.LabelKubernetesComponent] if ok { - matchLabels["app.kubernetes.io/component"] = label + matchLabels[naming.LabelKubernetesComponent] = label } list := corev1.PodList{} @@ -144,8 +139,8 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api log.Info("apply changes to secondary pod", "pod", pod.Name) updateRevision := sfs.Status.UpdateRevision - if pod.Labels["app.kubernetes.io/component"] == "arbiter" { - arbiterSfs, err := r.getArbiterStatefulset(ctx, cr, pod.Labels["app.kubernetes.io/replset"]) + if pod.Labels[naming.LabelKubernetesComponent] == "arbiter" { + arbiterSfs, err := r.getArbiterStatefulset(ctx, cr, replset) if err != nil { return errors.Wrap(err, "failed to get arbiter statefulset") } @@ -160,7 +155,7 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api // If the primary is external, we can't match it with a running pod and // it'll have an empty name - if sfs.Labels["app.kubernetes.io/component"] != "nonVoting" && len(primaryPod.Name) > 0 { + if sfs.Labels[naming.LabelKubernetesComponent] != "nonVoting" && len(primaryPod.Name) > 0 { forceStepDown := replset.Size == 1 log.Info("doing step down...", "force", forceStepDown) client, err := r.mongoClientWithRole(ctx, cr, *replset, api.RoleClusterAdmin) @@ -449,7 +444,7 @@ func (r *ReconcilePerconaServerMongoDB) isAllSfsUpToDate(ctx context.Context, cr &k8sclient.ListOptions{ Namespace: cr.Namespace, LabelSelector: labels.SelectorFromSet(map[string]string{ - "app.kubernetes.io/instance": cr.Name, + naming.LabelKubernetesInstance: cr.Name, }), }, ); err != nil { @@ -512,7 +507,7 @@ func isSfsChanged(sfs *appsv1.StatefulSet, podList *corev1.PodList) bool { } for _, pod := range podList.Items { - if pod.Labels["app.kubernetes.io/component"] != sfs.Labels["app.kubernetes.io/component"] { + if pod.Labels[naming.LabelKubernetesComponent] != sfs.Labels[naming.LabelKubernetesComponent] { continue } if pod.ObjectMeta.Labels["controller-revision-hash"] != sfs.Status.UpdateRevision { diff --git a/pkg/controller/perconaservermongodb/ssl.go b/pkg/controller/perconaservermongodb/ssl.go index 9b4a381a54..7734d8343e 100644 --- a/pkg/controller/perconaservermongodb/ssl.go +++ b/pkg/controller/perconaservermongodb/ssl.go @@ -15,6 +15,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/tls" "github.com/percona/percona-server-mongodb-operator/pkg/util" ) @@ -122,7 +123,7 @@ func (r *ReconcilePerconaServerMongoDB) doAllStsHasLatestTLS(ctx context.Context &client.ListOptions{ Namespace: cr.Namespace, LabelSelector: labels.SelectorFromSet(map[string]string{ - "app.kubernetes.io/instance": cr.Name, + naming.LabelKubernetesInstance: cr.Name, }), }, ); err != nil { @@ -279,9 +280,13 @@ func (r *ReconcilePerconaServerMongoDB) updateCertManagerCerts(ctx context.Conte ObjectMeta: metav1.ObjectMeta{ Name: secret.Name + "-old", Namespace: secret.Namespace, + Labels: naming.ClusterLabels(cr), }, Data: secret.Data, } + if cr.CompareVersion("1.17.0") < 0 { + newSecret.Labels = nil + } if err := r.client.Create(ctx, newSecret); err != nil { return errors.Wrap(err, "create secret") @@ -457,10 +462,14 @@ func (r *ReconcilePerconaServerMongoDB) createSSLManually(ctx context.Context, c Name: api.SSLSecretName(cr), Namespace: cr.Namespace, OwnerReferences: ownerReferences, + Labels: naming.ClusterLabels(cr), }, Data: data, Type: corev1.SecretTypeTLS, } + if cr.CompareVersion("1.17.0") < 0 { + secretObj.Labels = nil + } err = r.createSSLSecret(ctx, &secretObj, certificateDNSNames) if err != nil { return errors.Wrap(err, "create TLS secret") @@ -478,10 +487,14 @@ func (r *ReconcilePerconaServerMongoDB) createSSLManually(ctx context.Context, c Name: api.SSLInternalSecretName(cr), Namespace: cr.Namespace, OwnerReferences: ownerReferences, + Labels: naming.ClusterLabels(cr), }, Data: data, Type: corev1.SecretTypeTLS, } + if cr.CompareVersion("1.17.0") < 0 { + secretObjInternal.Labels = nil + } err = r.createSSLSecret(ctx, &secretObjInternal, certificateDNSNames) if err != nil { return errors.Wrap(err, "create TLS internal secret") diff --git a/pkg/controller/perconaservermongodb/statefulset.go b/pkg/controller/perconaservermongodb/statefulset.go index fc0cf3f6f2..2d5cc97cc9 100644 --- a/pkg/controller/perconaservermongodb/statefulset.go +++ b/pkg/controller/perconaservermongodb/statefulset.go @@ -3,14 +3,16 @@ package perconaservermongodb import ( "context" - api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" - "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" logf "sigs.k8s.io/controller-runtime/pkg/log" + + api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" + "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" ) func (r *ReconcilePerconaServerMongoDB) reconcileStatefulSet(ctx context.Context, cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec, ls map[string]string) (*appsv1.StatefulSet, error) { @@ -20,10 +22,10 @@ func (r *ReconcilePerconaServerMongoDB) reconcileStatefulSet(ctx context.Context volumeSpec := rs.VolumeSpec if rs.ClusterRole == api.ClusterRoleConfigSvr { - ls["app.kubernetes.io/component"] = api.ConfigReplSetName + ls[naming.LabelKubernetesComponent] = api.ConfigReplSetName } - switch ls["app.kubernetes.io/component"] { + switch ls[naming.LabelKubernetesComponent] { case "arbiter": pdbspec = rs.Arbiter.PodDisruptionBudget case "nonVoting": @@ -60,7 +62,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileStatefulSet(ctx context.Context return nil, errors.Wrapf(err, "update StatefulSet %s", sfs.Name) } - err = r.reconcilePDB(ctx, pdbspec, ls, cr.Namespace, sfs) + err = r.reconcilePDB(ctx, cr, pdbspec, ls, cr.Namespace, sfs) if err != nil { return nil, errors.Wrapf(err, "PodDisruptionBudget for %s", sfs.Name) } @@ -77,10 +79,10 @@ func (r *ReconcilePerconaServerMongoDB) getStatefulsetFromReplset(ctx context.Co configName := psmdb.MongodCustomConfigName(cr.Name, rs.Name) if rs.ClusterRole == api.ClusterRoleConfigSvr { - ls["app.kubernetes.io/component"] = api.ConfigReplSetName + ls[naming.LabelKubernetesComponent] = api.ConfigReplSetName } - switch ls["app.kubernetes.io/component"] { + switch ls[naming.LabelKubernetesComponent] { case "arbiter": sfsName += "-arbiter" case "nonVoting": diff --git a/pkg/controller/perconaservermongodb/statefulset_test.go b/pkg/controller/perconaservermongodb/statefulset_test.go index ea961150c3..ec58701463 100644 --- a/pkg/controller/perconaservermongodb/statefulset_test.go +++ b/pkg/controller/perconaservermongodb/statefulset_test.go @@ -13,6 +13,7 @@ import ( "sigs.k8s.io/yaml" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/version" ) @@ -102,11 +103,11 @@ func TestReconcileStatefulSet(t *testing.T) { var ls map[string]string switch tt.component { case "mongod": - ls = rs.MongodLabels(tt.cr) + ls = naming.MongodLabels(tt.cr, rs) case "arbiter": - ls = rs.ArbiterLabels(tt.cr) + ls = naming.ArbiterLabels(tt.cr, rs) case "nonVoting": - ls = rs.NonVotingLabels(tt.cr) + ls = naming.NonVotingLabels(tt.cr, rs) default: t.Fatalf("unexpected component: %s", tt.component) } diff --git a/pkg/controller/perconaservermongodb/status.go b/pkg/controller/perconaservermongodb/status.go index 224917edb2..8a82e8a4ac 100644 --- a/pkg/controller/perconaservermongodb/status.go +++ b/pkg/controller/perconaservermongodb/status.go @@ -19,6 +19,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" ) @@ -392,14 +393,8 @@ func (r *ReconcilePerconaServerMongoDB) connectionEndpoint(ctx context.Context, err := r.client.List(ctx, &list, &client.ListOptions{ - Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(map[string]string{ - "app.kubernetes.io/name": "percona-server-mongodb", - "app.kubernetes.io/instance": cr.Name, - "app.kubernetes.io/replset": rs.Name, - "app.kubernetes.io/managed-by": "percona-server-mongodb-operator", - "app.kubernetes.io/part-of": "percona-server-mongodb", - }), + Namespace: cr.Namespace, + LabelSelector: labels.SelectorFromSet(naming.RSLabels(cr, rs)), }, ) if err != nil { diff --git a/pkg/controller/perconaservermongodb/users.go b/pkg/controller/perconaservermongodb/users.go index 99953d40f2..160da9ff70 100644 --- a/pkg/controller/perconaservermongodb/users.go +++ b/pkg/controller/perconaservermongodb/users.go @@ -17,6 +17,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo" ) @@ -62,6 +63,10 @@ func (r *ReconcilePerconaServerMongoDB) reconcileUsers(ctx context.Context, cr * internalSysUsersSecret.ObjectMeta = metav1.ObjectMeta{ Name: secretName, Namespace: cr.Namespace, + Labels: naming.ClusterLabels(cr), + } + if cr.CompareVersion("1.17.0") < 0 { + internalSysUsersSecret.Labels = nil } err = r.client.Create(ctx, internalSysUsersSecret) if err != nil { diff --git a/pkg/controller/perconaservermongodb/volumes.go b/pkg/controller/perconaservermongodb/volumes.go index a761b8c457..7e7e1516fb 100644 --- a/pkg/controller/perconaservermongodb/volumes.go +++ b/pkg/controller/perconaservermongodb/volumes.go @@ -21,6 +21,7 @@ import ( api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" "github.com/percona/percona-server-mongodb-operator/pkg/k8s" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/percona/percona-server-mongodb-operator/pkg/util" ) @@ -282,7 +283,7 @@ func (r *ReconcilePerconaServerMongoDB) revertVolumeTemplate(ctx context.Context orig := cr.DeepCopy() - replset, ok := sts.Labels["app.kubernetes.io/replset"] + replset, ok := sts.Labels[naming.LabelKubernetesReplset] if !ok { return errors.New("missing component label") } diff --git a/pkg/controller/perconaservermongodbrestore/physical.go b/pkg/controller/perconaservermongodbrestore/physical.go index 35d27c8ea6..d99736d53a 100644 --- a/pkg/controller/perconaservermongodbrestore/physical.go +++ b/pkg/controller/perconaservermongodbrestore/physical.go @@ -22,7 +22,9 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" "github.com/percona/percona-backup-mongodb/pbm/defs" + psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/backup" "github.com/percona/percona-server-mongodb-operator/version" @@ -812,11 +814,15 @@ func (r *ReconcilePerconaServerMongoDBRestore) createPBMConfigSecret(ctx context ObjectMeta: metav1.ObjectMeta{ Name: r.pbmConfigName(cluster), Namespace: cluster.Namespace, + Labels: naming.ClusterLabels(cluster), }, Data: map[string][]byte{ "pbm_config.yaml": confBytes, }, } + if cluster.CompareVersion("1.17.0") < 0 { + secret.Labels = nil + } if err := r.client.Create(ctx, &secret); err != nil { return errors.Wrap(err, "create PBM config secret") } @@ -827,8 +833,8 @@ func (r *ReconcilePerconaServerMongoDBRestore) createPBMConfigSecret(ctx context func (r *ReconcilePerconaServerMongoDBRestore) getReplsetPods(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB, rs *psmdbv1.ReplsetSpec) (corev1.PodList, error) { mongodPods := corev1.PodList{} - set := psmdbv1.MongodLabels(cluster) - set["app.kubernetes.io/replset"] = rs.Name + set := naming.MongodLabels(cluster, rs) + set[naming.LabelKubernetesReplset] = rs.Name err := r.client.List(ctx, &mongodPods, diff --git a/pkg/naming/labels.go b/pkg/naming/labels.go new file mode 100644 index 0000000000..13cbc947bf --- /dev/null +++ b/pkg/naming/labels.go @@ -0,0 +1,101 @@ +package naming + +import ( + api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/util" +) + +const ( + labelKubernetesPrefix = "app.kubernetes.io/" + + LabelKubernetesName = labelKubernetesPrefix + "name" + LabelKubernetesInstance = labelKubernetesPrefix + "instance" + LabelKubernetesManagedBy = labelKubernetesPrefix + "managed-by" + LabelKubernetesPartOf = labelKubernetesPrefix + "part-of" + LabelKubernetesComponent = labelKubernetesPrefix + "component" + LabelKubernetesReplset = labelKubernetesPrefix + "replset" +) + +const ( + LabelBackupAncestor = perconaPrefix + "backup-ancestor" + LabelBackupType = perconaPrefix + "backup-type" + LabelCluster = perconaPrefix + "cluster" +) + +func ClusterLabels(cr *api.PerconaServerMongoDB) map[string]string { + return map[string]string{ + LabelKubernetesName: "percona-server-mongodb", + LabelKubernetesInstance: cr.Name, + LabelKubernetesManagedBy: "percona-server-mongodb-operator", + LabelKubernetesPartOf: "percona-server-mongodb", + } +} + +func ServiceLabels(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) map[string]string { + return RSLabels(cr, replset) +} + +func ExternalServiceLabels(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) map[string]string { + ls := RSLabels(cr, replset) + ls[LabelKubernetesComponent] = "external-service" + + return ls +} + +func MongodLabels(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) map[string]string { + ls := RSLabels(cr, replset) + ls[LabelKubernetesComponent] = "mongod" + return ls +} + +func ArbiterLabels(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) map[string]string { + ls := RSLabels(cr, replset) + ls[LabelKubernetesComponent] = "arbiter" + return ls +} + +func NonVotingLabels(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) map[string]string { + ls := RSLabels(cr, replset) + ls[LabelKubernetesComponent] = "nonVoting" + return ls +} + +func MongosLabels(cr *api.PerconaServerMongoDB) map[string]string { + ls := ClusterLabels(cr) + ls[LabelKubernetesComponent] = "mongos" + return ls +} + +func RSLabels(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) map[string]string { + ls := ClusterLabels(cr) + if replset != nil { + ls[LabelKubernetesReplset] = replset.Name + } + return ls +} + +func ScheduledBackupLabels(cr *api.PerconaServerMongoDB, task *api.BackupTaskSpec) map[string]string { + if cr.CompareVersion("1.17.0") < 0 { + return map[string]string{ + "ancestor": task.Name, + "cluster": cr.Name, + "type": "cron", + } + } + ls := ClusterLabels(cr) + ls[LabelBackupAncestor] = task.Name + ls[LabelCluster] = cr.Name + ls[LabelBackupType] = "cron" + + return ls +} + +func NewBackupCronJobLabels(cr *api.PerconaServerMongoDB, labels map[string]string) map[string]string { + ls := ClusterLabels(cr) + ls[LabelKubernetesReplset] = "general" + ls[LabelKubernetesComponent] = "backup-schedule" + + ls = util.MapMerge(util.MapCopy(labels), ls) + + return ls +} diff --git a/pkg/naming/naming.go b/pkg/naming/naming.go new file mode 100644 index 0000000000..403491b9e7 --- /dev/null +++ b/pkg/naming/naming.go @@ -0,0 +1,3 @@ +package naming + +const perconaPrefix = "percona.com/" diff --git a/pkg/psmdb/backup/job.go b/pkg/psmdb/backup/job.go index de2cde2193..df8d246bd9 100644 --- a/pkg/psmdb/backup/job.go +++ b/pkg/psmdb/backup/job.go @@ -5,13 +5,15 @@ import ( "fmt" "time" + "github.com/pkg/errors" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/percona/percona-backup-mongodb/pbm/defs" + api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" - "github.com/pkg/errors" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" ) func BackupCronJob(cr *api.PerconaServerMongoDB, task *api.BackupTaskSpec) (batchv1.CronJob, error) { @@ -57,7 +59,7 @@ func BackupCronJob(cr *api.PerconaServerMongoDB, task *api.BackupTaskSpec) (batc ObjectMeta: metav1.ObjectMeta{ Name: task.Name, Namespace: cr.Namespace, - Labels: NewBackupCronJobLabels(cr.Name, backupSpec.Labels), + Labels: naming.NewBackupCronJobLabels(cr, backupSpec.Labels), Annotations: backupSpec.Annotations, }, Spec: batchv1.CronJobSpec{ @@ -65,13 +67,13 @@ func BackupCronJob(cr *api.PerconaServerMongoDB, task *api.BackupTaskSpec) (batc ConcurrencyPolicy: batchv1.ForbidConcurrent, JobTemplate: batchv1.JobTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: NewBackupCronJobLabels(cr.Name, backupSpec.Labels), + Labels: naming.NewBackupCronJobLabels(cr, backupSpec.Labels), Annotations: backupSpec.Annotations, }, Spec: batchv1.JobSpec{ Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: NewBackupCronJobLabels(cr.Name, backupSpec.Labels), + Labels: naming.NewBackupCronJobLabels(cr, backupSpec.Labels), Annotations: backupSpec.Annotations, }, Spec: backupPod, @@ -82,23 +84,6 @@ func BackupCronJob(cr *api.PerconaServerMongoDB, task *api.BackupTaskSpec) (batc }, nil } -func NewBackupCronJobLabels(crName string, labels map[string]string) map[string]string { - base := make(map[string]string) - - for k, v := range labels { - base[k] = v - } - - base["app.kubernetes.io/name"] = "percona-server-mongodb" - base["app.kubernetes.io/instance"] = crName - base["app.kubernetes.io/replset"] = "general" - base["app.kubernetes.io/managed-by"] = "percona-server-mongodb-operator" - base["app.kubernetes.io/component"] = "backup-schedule" - base["app.kubernetes.io/part-of"] = "percona-server-mongodb" - - return base -} - func BackupFromTask(cr *api.PerconaServerMongoDB, task *api.BackupTaskSpec) (*api.PerconaServerMongoDBBackup, error) { shortClusterName := cr.Name if len(shortClusterName) > 16 { @@ -116,11 +101,7 @@ func BackupFromTask(cr *api.PerconaServerMongoDB, task *api.BackupTaskSpec) (*ap ObjectMeta: metav1.ObjectMeta{ Finalizers: []string{"delete-backup"}, GenerateName: "cron-" + shortClusterName + "-" + time.Now().Format("20060102150405") + "-", - Labels: map[string]string{ - "ancestor": task.Name, - "cluster": cr.Name, - "type": "cron", - }, + Labels: naming.ScheduledBackupLabels(cr, task), }, Spec: api.PerconaServerMongoDBBackupSpec{ Type: backupType, diff --git a/pkg/psmdb/getters.go b/pkg/psmdb/getters.go index 23d7457d3c..f2ae58cdc3 100644 --- a/pkg/psmdb/getters.go +++ b/pkg/psmdb/getters.go @@ -5,6 +5,7 @@ import ( "sort" "time" + "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/labels" @@ -13,31 +14,10 @@ import ( api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" "github.com/percona/percona-server-mongodb-operator/pkg/mcs" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo" - "github.com/pkg/errors" ) -func clusterLabels(cr *api.PerconaServerMongoDB) map[string]string { - return map[string]string{ - "app.kubernetes.io/name": "percona-server-mongodb", - "app.kubernetes.io/instance": cr.Name, - "app.kubernetes.io/managed-by": "percona-server-mongodb-operator", - "app.kubernetes.io/part-of": "percona-server-mongodb", - } -} - -func RSLabels(cr *api.PerconaServerMongoDB, rsName string) map[string]string { - lbls := clusterLabels(cr) - lbls["app.kubernetes.io/replset"] = rsName - return lbls -} - -func MongosLabels(cr *api.PerconaServerMongoDB) map[string]string { - lbls := clusterLabels(cr) - lbls["app.kubernetes.io/component"] = "mongos" - return lbls -} - // GetRSPods returns truncated list of replicaset pods to the size of `rs.Size`. func GetRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServerMongoDB, rsName string) (corev1.PodList, error) { return getRSPods(ctx, k8sclient, cr, rsName, true) @@ -56,8 +36,8 @@ func getRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServ &client.ListOptions{ Namespace: cr.Namespace, LabelSelector: labels.SelectorFromSet(map[string]string{ - "app.kubernetes.io/instance": cr.Name, - "app.kubernetes.io/replset": rsName, + naming.LabelKubernetesInstance: cr.Name, + naming.LabelKubernetesReplset: rsName, }), }, ); err != nil { @@ -65,8 +45,10 @@ func getRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServ } for _, sts := range stsList.Items { - lbls := RSLabels(cr, rsName) - lbls["app.kubernetes.io/component"] = sts.Labels["app.kubernetes.io/component"] + rs := cr.Spec.Replset(rsName) + + lbls := naming.RSLabels(cr, rs) + lbls[naming.LabelKubernetesComponent] = sts.Labels[naming.LabelKubernetesComponent] pods := corev1.PodList{} err := k8sclient.List(ctx, &pods, @@ -79,7 +61,6 @@ func getRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServ return rsPods, errors.Wrap(err, "failed to list pods") } - rs := cr.Spec.Replset(rsName) if trimOutdated && rs != nil { // `k8sclient.List` returns unsorted list of pods // We should sort pods to truncate pods that are going to be deleted during resize @@ -93,7 +74,7 @@ func getRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServ // `replSetReconfig` call in the `updateConfigMembers` function. rsSize := 0 - switch lbls["app.kubernetes.io/component"] { + switch lbls[naming.LabelKubernetesComponent] { case "arbiter": rsSize = int(rs.Arbiter.Size) case "nonVoting": @@ -127,7 +108,7 @@ func GetMongosPods(ctx context.Context, cl client.Client, cr *api.PerconaServerM &pods, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(MongosLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.MongosLabels(cr)), }, ) @@ -140,7 +121,7 @@ func GetMongosServices(ctx context.Context, cl client.Client, cr *api.PerconaSer list, &client.ListOptions{ Namespace: cr.Namespace, - LabelSelector: labels.SelectorFromSet(MongosLabels(cr)), + LabelSelector: labels.SelectorFromSet(naming.MongosLabels(cr)), }, ) if err != nil { @@ -150,7 +131,7 @@ func GetMongosServices(ctx context.Context, cl client.Client, cr *api.PerconaSer } func GetExportedServices(ctx context.Context, cl client.Client, cr *api.PerconaServerMongoDB) (*mcsv1alpha1.ServiceExportList, error) { - ls := clusterLabels(cr) + ls := naming.ClusterLabels(cr) seList := mcs.ServiceExportList() err := cl.List(ctx, diff --git a/pkg/psmdb/mongos.go b/pkg/psmdb/mongos.go index 58299cc69b..246b647538 100644 --- a/pkg/psmdb/mongos.go +++ b/pkg/psmdb/mongos.go @@ -13,6 +13,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" ) func MongosStatefulset(cr *api.PerconaServerMongoDB) *appsv1.StatefulSet { @@ -24,7 +25,7 @@ func MongosStatefulset(cr *api.PerconaServerMongoDB) *appsv1.StatefulSet { ObjectMeta: metav1.ObjectMeta{ Name: cr.MongosNamespacedName().Name, Namespace: cr.MongosNamespacedName().Namespace, - Labels: MongosLabels(cr), + Labels: naming.MongosLabels(cr), }, } } @@ -46,7 +47,7 @@ func MongosStatefulsetSpec(cr *api.PerconaServerMongoDB, template corev1.PodTemp return appsv1.StatefulSetSpec{ Replicas: &cr.Spec.Sharding.Mongos.Size, Selector: &metav1.LabelSelector{ - MatchLabels: MongosLabels(cr), + MatchLabels: naming.MongosLabels(cr), }, Template: template, UpdateStrategy: updateStrategy, @@ -54,7 +55,7 @@ func MongosStatefulsetSpec(cr *api.PerconaServerMongoDB, template corev1.PodTemp } func MongosTemplateSpec(cr *api.PerconaServerMongoDB, initImage string, log logr.Logger, customConf CustomConfig, cfgInstances []string) (corev1.PodTemplateSpec, error) { - ls := MongosLabels(cr) + ls := naming.MongosLabels(cr) if cr.Spec.Sharding.Mongos.Labels != nil { for k, v := range cr.Spec.Sharding.Mongos.Labels { @@ -417,7 +418,7 @@ func MongosService(cr *api.PerconaServerMongoDB, name string) corev1.Service { }, } if cr.CompareVersion("1.12.0") >= 0 { - svc.Labels = MongosLabels(cr) + svc.Labels = naming.MongosLabels(cr) } if cr.Spec.Sharding.Mongos != nil { @@ -433,7 +434,7 @@ func MongosService(cr *api.PerconaServerMongoDB, name string) corev1.Service { } func MongosServiceSpec(cr *api.PerconaServerMongoDB, podName string) corev1.ServiceSpec { - ls := MongosLabels(cr) + ls := naming.MongosLabels(cr) if cr.Spec.Sharding.Mongos.Expose.ServicePerPod { ls["statefulset.kubernetes.io/pod-name"] = podName diff --git a/pkg/psmdb/pdb.go b/pkg/psmdb/pdb.go index 76a7f0bc34..27f31e370d 100644 --- a/pkg/psmdb/pdb.go +++ b/pkg/psmdb/pdb.go @@ -5,6 +5,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" ) func PodDisruptionBudget(spec *api.PodDisruptionBudgetSpec, labels map[string]string, namespace string) *policyv1.PodDisruptionBudget { @@ -14,8 +15,9 @@ func PodDisruptionBudget(spec *api.PodDisruptionBudgetSpec, labels map[string]st Kind: "PodDisruptionBudget", }, ObjectMeta: metav1.ObjectMeta{ - Name: labels["app.kubernetes.io/instance"] + "-" + labels["app.kubernetes.io/component"] + "-" + labels["app.kubernetes.io/replset"], + Name: labels[naming.LabelKubernetesInstance] + "-" + labels[naming.LabelKubernetesComponent] + "-" + labels[naming.LabelKubernetesReplset], Namespace: namespace, + Labels: labels, }, Spec: policyv1.PodDisruptionBudgetSpec{ MinAvailable: spec.MinAvailable, diff --git a/pkg/psmdb/service.go b/pkg/psmdb/service.go index f9233aa3a7..3e9e049b70 100644 --- a/pkg/psmdb/service.go +++ b/pkg/psmdb/service.go @@ -17,17 +17,12 @@ import ( mcsv1alpha1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" ) // Service returns a core/v1 API Service func Service(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) *corev1.Service { - ls := map[string]string{ - "app.kubernetes.io/name": "percona-server-mongodb", - "app.kubernetes.io/instance": cr.Name, - "app.kubernetes.io/replset": replset.Name, - "app.kubernetes.io/managed-by": "percona-server-mongodb-operator", - "app.kubernetes.io/part-of": "percona-server-mongodb", - } + ls := naming.ServiceLabels(cr, replset) svc := &corev1.Service{ TypeMeta: metav1.TypeMeta{ @@ -81,14 +76,7 @@ func ExternalService(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec, pod }, } - svc.Labels = map[string]string{ - "app.kubernetes.io/name": "percona-server-mongodb", - "app.kubernetes.io/instance": cr.Name, - "app.kubernetes.io/replset": replset.Name, - "app.kubernetes.io/managed-by": "percona-server-mongodb-operator", - "app.kubernetes.io/part-of": "percona-server-mongodb", - "app.kubernetes.io/component": "external-service", - } + svc.Labels = naming.ExternalServiceLabels(cr, replset) for k, v := range replset.Expose.ServiceLabels { if _, ok := svc.Labels[k]; !ok { diff --git a/pkg/psmdb/statefulset.go b/pkg/psmdb/statefulset.go index 0a57df94b4..468c63238f 100644 --- a/pkg/psmdb/statefulset.go +++ b/pkg/psmdb/statefulset.go @@ -12,6 +12,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" ) // NewStatefulSet returns a StatefulSet object configured for a name @@ -47,7 +48,7 @@ func StatefulSpec(ctx context.Context, cr *api.PerconaServerMongoDB, replset *ap readinessProbe := replset.ReadinessProbe configName := MongodCustomConfigName(cr.Name, replset.Name) - switch ls["app.kubernetes.io/component"] { + switch ls[naming.LabelKubernetesComponent] { case "arbiter": containerName += "-arbiter" size = replset.Arbiter.Size @@ -227,7 +228,7 @@ func StatefulSpec(ctx context.Context, cr *api.PerconaServerMongoDB, replset *ap ) } - if ls["app.kubernetes.io/component"] == "arbiter" { + if ls[naming.LabelKubernetesComponent] == "arbiter" { volumes = append(volumes, corev1.Volume{ Name: MongodDataVolClaimName, @@ -478,7 +479,7 @@ func PodAffinity(cr *api.PerconaServerMongoDB, af *api.PodAffinity, labels map[s } if cr.CompareVersion("1.6.0") < 0 { - delete(labelsCopy, "app.kubernetes.io/component") + delete(labelsCopy, naming.LabelKubernetesComponent) } switch { diff --git a/pkg/psmdb/tls/certmanager.go b/pkg/psmdb/tls/certmanager.go index ff087b3f89..aa0148114a 100644 --- a/pkg/psmdb/tls/certmanager.go +++ b/pkg/psmdb/tls/certmanager.go @@ -19,6 +19,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" + "github.com/percona/percona-server-mongodb-operator/pkg/naming" "github.com/percona/percona-server-mongodb-operator/pkg/util" ) @@ -131,6 +132,7 @@ func (c *certManagerController) ApplyIssuer(ctx context.Context, cr *api.Percona ObjectMeta: metav1.ObjectMeta{ Name: issuerName(cr), Namespace: cr.Namespace, + Labels: naming.ClusterLabels(cr), }, Spec: cm.IssuerSpec{ IssuerConfig: cm.IssuerConfig{ @@ -149,6 +151,10 @@ func (c *certManagerController) ApplyIssuer(ctx context.Context, cr *api.Percona } } + if cr.CompareVersion("1.17.0") < 0 { + issuer.Labels = nil + } + return c.createOrUpdate(ctx, cr, issuer) } @@ -157,6 +163,7 @@ func (c *certManagerController) ApplyCAIssuer(ctx context.Context, cr *api.Perco ObjectMeta: metav1.ObjectMeta{ Name: caIssuerName(cr), Namespace: cr.Namespace, + Labels: naming.ClusterLabels(cr), }, Spec: cm.IssuerSpec{ IssuerConfig: cm.IssuerConfig{ @@ -165,6 +172,10 @@ func (c *certManagerController) ApplyCAIssuer(ctx context.Context, cr *api.Perco }, } + if cr.CompareVersion("1.17.0") < 0 { + issuer.Labels = nil + } + return c.createOrUpdate(ctx, cr, issuer) } @@ -185,6 +196,7 @@ func (c *certManagerController) ApplyCertificate(ctx context.Context, cr *api.Pe ObjectMeta: metav1.ObjectMeta{ Name: certificateName(cr, internal), Namespace: cr.Namespace, + Labels: naming.ClusterLabels(cr), }, Spec: cm.CertificateSpec{ Subject: &cm.X509Subject{ @@ -203,6 +215,10 @@ func (c *certManagerController) ApplyCertificate(ctx context.Context, cr *api.Pe }, } + if cr.CompareVersion("1.17.0") < 0 { + certificate.Labels = nil + } + return c.createOrUpdate(ctx, cr, certificate) } @@ -236,6 +252,7 @@ func (c *certManagerController) ApplyCACertificate(ctx context.Context, cr *api. ObjectMeta: metav1.ObjectMeta{ Name: CACertificateSecretName(cr), Namespace: cr.Namespace, + Labels: naming.ClusterLabels(cr), }, Spec: cm.CertificateSpec{ SecretName: CACertificateSecretName(cr), @@ -249,6 +266,9 @@ func (c *certManagerController) ApplyCACertificate(ctx context.Context, cr *api. RenewBefore: &metav1.Duration{Duration: 730 * time.Hour}, }, } + if cr.CompareVersion("1.17.0") < 0 { + cert.Labels = nil + } return c.createOrUpdate(ctx, cr, cert) }