Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K8SPSMDB-899: add labels to all objects #1585

Merged
merged 18 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 2 additions & 52 deletions pkg/apis/psmdb/v1/psmdb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
"github.com/go-logr/logr"
v "github.com/hashicorp/go-version"
"github.com/percona/percona-backup-mongodb/pbm/compress"
"github.com/percona/percona-backup-mongodb/pbm/defs"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -23,8 +25,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/percona/percona-backup-mongodb/pbm/compress"
"github.com/percona/percona-backup-mongodb/pbm/defs"
pooknull marked this conversation as resolved.
Show resolved Hide resolved
"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"
Expand Down Expand Up @@ -626,29 +626,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"`
Expand Down Expand Up @@ -1144,33 +1121,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"
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/perconaservermongodb/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
25 changes: 13 additions & 12 deletions pkg/controller/perconaservermongodb/connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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])
Expand All @@ -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)...)
}

Expand Down Expand Up @@ -216,7 +217,6 @@ func TestConnectionLeaks(t *testing.T) {
}
})
}

}

func updateResource[T any](resource T, update func(T)) T {
Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down Expand Up @@ -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,
Expand All @@ -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++

Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/perconaservermongodb/external_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/perconaservermongodb/finalizers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading
Loading