diff --git a/apis/kubedb/constants.go b/apis/kubedb/constants.go index e216ca6ea2..e7ef88089c 100644 --- a/apis/kubedb/constants.go +++ b/apis/kubedb/constants.go @@ -37,6 +37,7 @@ const ( DatabasePodStandby = "standby" ComponentDatabase = "database" + ComponentSidekick = "sidekick" ComponentConnectionPooler = "connection-pooler" RoleStats = "stats" DefaultStatsPath = "/metrics" diff --git a/apis/kubedb/v1/helpers.go b/apis/kubedb/v1/helpers.go index 1a3fecfb44..514c607555 100644 --- a/apis/kubedb/v1/helpers.go +++ b/apis/kubedb/v1/helpers.go @@ -30,9 +30,18 @@ import ( ofstv1 "kmodules.xyz/offshoot-api/api/v1" petsetutil "kubeops.dev/petset/client/clientset/versioned/typed/apps/v1" pslister "kubeops.dev/petset/client/listers/apps/v1" + skapi "kubeops.dev/sidekick/apis/apps/v1alpha1" "sigs.k8s.io/controller-runtime/pkg/client" ) +func commonSidekickLabels() map[string]string { + return map[string]string{ + meta_util.NameLabelKey: fmt.Sprintf("%s.%s", skapi.ResourceSidekicks, skapi.SchemeGroupVersion.Group), + meta_util.ManagedByLabelKey: kubedb.GroupName, + meta_util.ComponentLabelKey: kubedb.ComponentSidekick, + } +} + func checkReplicas(lister pslister.PetSetNamespaceLister, selector labels.Selector, expectedItems int) (bool, string, error) { items, err := lister.List(selector) if err != nil { diff --git a/apis/kubedb/v1/mariadb_helpers.go b/apis/kubedb/v1/mariadb_helpers.go index 5ca57402de..1c97c29714 100644 --- a/apis/kubedb/v1/mariadb_helpers.go +++ b/apis/kubedb/v1/mariadb_helpers.go @@ -77,6 +77,13 @@ func (m MariaDB) PodControllerLabels() map[string]string { return m.offshootLabels(m.OffshootSelectors(), m.Spec.PodTemplate.Controller.Labels) } +func (m MariaDB) SidekickLabels() map[string]string { + return meta_util.OverwriteKeys(nil, commonSidekickLabels(), map[string]string{ + kubedb.ComponentDatabase: ResourceSingularMariaDB, + meta_util.InstanceLabelKey: m.Name, + }) +} + func (m MariaDB) offshootLabels(selector, override map[string]string) map[string]string { selector[meta_util.ComponentLabelKey] = kubedb.ComponentDatabase return meta_util.FilterKeys(kubedb.GroupName, selector, meta_util.OverwriteKeys(nil, m.Labels, override)) diff --git a/apis/kubedb/v1/mongodb_helpers.go b/apis/kubedb/v1/mongodb_helpers.go index 4422cccafe..d54ddd89bc 100644 --- a/apis/kubedb/v1/mongodb_helpers.go +++ b/apis/kubedb/v1/mongodb_helpers.go @@ -255,6 +255,13 @@ func (m MongoDB) PodControllerLabels(podControllerLabels map[string]string, extr return m.offshootLabels(meta_util.OverwriteKeys(m.OffshootSelectors(), extraLabels...), podControllerLabels) } +func (m MongoDB) SidekickLabels() map[string]string { + return meta_util.OverwriteKeys(nil, commonSidekickLabels(), map[string]string{ + kubedb.ComponentDatabase: ResourceSingularMongoDB, + meta_util.InstanceLabelKey: m.Name, + }) +} + func (m MongoDB) ServiceLabels(alias ServiceAlias, extraLabels ...map[string]string) map[string]string { svcTemplate := GetServiceTemplate(m.Spec.ServiceTemplates, alias) return m.offshootLabels(meta_util.OverwriteKeys(m.OffshootSelectors(), extraLabels...), svcTemplate.Labels) diff --git a/apis/kubedb/v1/mysql_helpers.go b/apis/kubedb/v1/mysql_helpers.go index 611674987c..efaf69704c 100644 --- a/apis/kubedb/v1/mysql_helpers.go +++ b/apis/kubedb/v1/mysql_helpers.go @@ -88,6 +88,13 @@ func (m MySQL) PodControllerLabels() map[string]string { return m.offshootLabels(m.OffshootSelectors(), m.Spec.PodTemplate.Controller.Labels) } +func (m MySQL) SidekickLabels() map[string]string { + return meta_util.OverwriteKeys(nil, commonSidekickLabels(), map[string]string{ + kubedb.ComponentDatabase: ResourceSingularMySQL, + meta_util.InstanceLabelKey: m.Name, + }) +} + func (m MySQL) RouterOffshootLabels() map[string]string { return m.offshootLabels(m.RouterOffshootSelectors(), nil) } diff --git a/apis/kubedb/v1/postgres_helpers.go b/apis/kubedb/v1/postgres_helpers.go index 3b1f9afe85..81c991844a 100644 --- a/apis/kubedb/v1/postgres_helpers.go +++ b/apis/kubedb/v1/postgres_helpers.go @@ -82,6 +82,13 @@ func (p Postgres) PodControllerLabels() map[string]string { return p.offshootLabels(p.OffshootSelectors(), p.Spec.PodTemplate.Controller.Labels) } +func (m Postgres) SidekickLabels() map[string]string { + return meta_util.OverwriteKeys(nil, commonSidekickLabels(), map[string]string{ + kubedb.ComponentDatabase: ResourceSingularPostgres, + meta_util.InstanceLabelKey: m.Name, + }) +} + func (p Postgres) ServiceLabels(alias ServiceAlias, extraLabels ...map[string]string) map[string]string { svcTemplate := GetServiceTemplate(p.Spec.ServiceTemplates, alias) return p.offshootLabels(meta_util.OverwriteKeys(p.OffshootSelectors(), extraLabels...), svcTemplate.Labels) diff --git a/apis/kubedb/v1alpha2/helpers.go b/apis/kubedb/v1alpha2/helpers.go index 2eceda7ce4..14213635a8 100644 --- a/apis/kubedb/v1alpha2/helpers.go +++ b/apis/kubedb/v1alpha2/helpers.go @@ -28,12 +28,22 @@ import ( "k8s.io/apimachinery/pkg/labels" appslister "k8s.io/client-go/listers/apps/v1" apps_util "kmodules.xyz/client-go/apps/v1" + meta_util "kmodules.xyz/client-go/meta" ofst "kmodules.xyz/offshoot-api/api/v1" petsetutil "kubeops.dev/petset/client/clientset/versioned/typed/apps/v1" pslister "kubeops.dev/petset/client/listers/apps/v1" + skapi "kubeops.dev/sidekick/apis/apps/v1alpha1" "sigs.k8s.io/controller-runtime/pkg/client" ) +func commonSidekickLabels() map[string]string { + return map[string]string{ + meta_util.NameLabelKey: fmt.Sprintf("%s.%s", skapi.ResourceSidekicks, skapi.SchemeGroupVersion.Group), + meta_util.ManagedByLabelKey: kubedb.GroupName, + meta_util.ComponentLabelKey: kubedb.ComponentSidekick, + } +} + func checkReplicas(lister appslister.StatefulSetNamespaceLister, selector labels.Selector, expectedItems int) (bool, string, error) { items, err := lister.List(selector) if err != nil { diff --git a/apis/kubedb/v1alpha2/mssqlserver_helpers.go b/apis/kubedb/v1alpha2/mssqlserver_helpers.go index 415e49f43e..28eafc7251 100644 --- a/apis/kubedb/v1alpha2/mssqlserver_helpers.go +++ b/apis/kubedb/v1alpha2/mssqlserver_helpers.go @@ -38,6 +38,7 @@ import ( kmapi "kmodules.xyz/client-go/api/v1" "kmodules.xyz/client-go/apiextensions" coreutil "kmodules.xyz/client-go/core/v1" + meta_util "kmodules.xyz/client-go/meta" metautil "kmodules.xyz/client-go/meta" "kmodules.xyz/client-go/policy/secomp" appcat "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1" @@ -225,6 +226,13 @@ func (m *MSSQLServer) AvailabilityGroupName() string { return availabilityGroupName } +func (m MSSQLServer) SidekickLabels() map[string]string { + return meta_util.OverwriteKeys(nil, commonSidekickLabels(), map[string]string{ + kubedb.ComponentDatabase: ResourceSingularMSSQLServer, + meta_util.InstanceLabelKey: m.Name, + }) +} + func (m *MSSQLServer) PodControllerLabels(extraLabels ...map[string]string) map[string]string { return m.offshootLabels(metautil.OverwriteKeys(m.OffshootSelectors(), extraLabels...), m.Spec.PodTemplate.Controller.Labels) }