Skip to content

Commit

Permalink
Add helpers to use different labels in sidekick
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob kumar saha <[email protected]>
  • Loading branch information
ArnobKumarSaha committed Nov 28, 2024
1 parent cfbad6a commit 2164ae8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions apis/kubedb/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
DatabasePodStandby = "standby"

ComponentDatabase = "database"
ComponentSidekick = "sidekick"
ComponentConnectionPooler = "connection-pooler"
RoleStats = "stats"
DefaultStatsPath = "/metrics"
Expand Down
9 changes: 9 additions & 0 deletions apis/kubedb/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions apis/kubedb/v1/mariadb_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions apis/kubedb/v1/mongodb_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions apis/kubedb/v1/mysql_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions apis/kubedb/v1/postgres_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions apis/kubedb/v1alpha2/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions apis/kubedb/v1alpha2/mssqlserver_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 2164ae8

Please sign in to comment.