Skip to content

Commit

Permalink
use gvk
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob kumar saha <[email protected]>
  • Loading branch information
ArnobKumarSaha committed Jan 31, 2024
1 parent 88a920c commit dea7aaf
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 76 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
kmodules.xyz/client-go v0.29.6
kmodules.xyz/custom-resources v0.29.1
kmodules.xyz/monitoring-agent-api v0.29.0
kubedb.dev/apimachinery v0.41.0-rc.1
kubedb.dev/apimachinery v0.41.0-rc.1.0.20240131123101-c711b3abb2a0
kubedb.dev/db-client-go v0.0.9
sigs.k8s.io/controller-runtime v0.17.0
sigs.k8s.io/yaml v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,8 @@ kmodules.xyz/prober v0.29.0 h1:Ex7m4F9rH7uWNNJlLgP63ROOM+nUATJkC2L5OQ7nwMg=
kmodules.xyz/prober v0.29.0/go.mod h1:UtK+HKyI1lFLEKX+HFLyOCVju6TO93zv3kwGpzqmKOo=
kmodules.xyz/resource-metadata v0.18.2-0.20240105072614-e92a8a48d400 h1:bmd9/fvbhE55xtMF9hXVzjoUZFLGjMfEoHSHu9Hw6Gc=
kmodules.xyz/resource-metadata v0.18.2-0.20240105072614-e92a8a48d400/go.mod h1:XsCdEKjfoulX29tMGviDhjT/jLl158uvMvXlKOhK1as=
kubedb.dev/apimachinery v0.41.0-rc.1 h1:+aaSLuMXcBlQXuaJMhHRalqOIKrE1pReBwyV3YwdpSo=
kubedb.dev/apimachinery v0.41.0-rc.1/go.mod h1:Z6vywQE35f+j6Vh24OaY2q5jnwAirDlx70nuKmlrrN0=
kubedb.dev/apimachinery v0.41.0-rc.1.0.20240131123101-c711b3abb2a0 h1:pgtLsEzFPYzRXCNHKiuczfbwhYi5XyRrvT9ijDB2nWI=
kubedb.dev/apimachinery v0.41.0-rc.1.0.20240131123101-c711b3abb2a0/go.mod h1:Z6vywQE35f+j6Vh24OaY2q5jnwAirDlx70nuKmlrrN0=
kubedb.dev/db-client-go v0.0.9 h1:oYfNBjZRLtF5jij1u83NW0yCjz1Is4zE9RI0tG0h5AU=
kubedb.dev/db-client-go v0.0.9/go.mod h1:h37/SUuec3Jnxusxv7JTs3Vl5SX9C/rNPG7qB7hQq4M=
kubeops.dev/sidekick v0.0.5-0.20231225214445-a15c70833046 h1:X1ieV+IXqNesmFwSH6NEVF1J2wO0vplC4k6v3Vmq0d0=
Expand Down
9 changes: 3 additions & 6 deletions pkg/monitor/alerts/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"time"

api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
"kubedb.dev/cli/pkg/monitor"

promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
Expand All @@ -44,11 +45,6 @@ type dbOpts struct {
resource string
}

const (
kubeDBGroup = "kubedb.com"
kubeDBVersion = "v1alpha2"
)

func Run(f cmdutil.Factory, args []string, prom monitor.PromSvc) {
if len(args) < 2 {
log.Fatal("Enter db object's name as an argument")
Expand Down Expand Up @@ -86,7 +82,8 @@ func newDBOpts(f cmdutil.Factory, dbName, namespace, resource string) (*dbOpts,
return nil, err
}

dbRes := schema.GroupVersionResource{Group: kubeDBGroup, Version: kubeDBVersion, Resource: resource}
gvk := api.SchemeGroupVersion
dbRes := schema.GroupVersionResource{Group: gvk.Group, Version: gvk.Version, Resource: resource}
db, err := dc.Resource(dbRes).Namespace(namespace).Get(context.TODO(), dbName, metav1.GetOptions{})
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"kubedb.dev/apimachinery/apis/kubedb"
"kubedb.dev/apimachinery/crds"

"github.com/Masterminds/semver/v3"
"gomodules.xyz/pointer"
v1 "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -316,66 +317,85 @@ func (d *Druid) SetDefaults() {
return
}

version, err := semver.NewVersion(druidVersion.Spec.Version)
if err != nil {
klog.Errorf("failed to parse druid version :%s\n", err.Error())
return
}

if d.Spec.Topology != nil {
if d.Spec.Topology.Coordinators != nil {
if d.Spec.Topology.Coordinators.Replicas == nil {
d.Spec.Topology.Coordinators.Replicas = pointer.Int32P(1)
}
if d.Spec.Topology.Coordinators.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Coordinators.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
if version.Major() > 25 {
if d.Spec.Topology.Coordinators.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Coordinators.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Coordinators.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Coordinators.PodTemplate)
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Coordinators.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Coordinators.PodTemplate)
}
if d.Spec.Topology.Overlords != nil {
if d.Spec.Topology.Overlords.Replicas == nil {
d.Spec.Topology.Overlords.Replicas = pointer.Int32P(1)
}
if d.Spec.Topology.Overlords.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Overlords.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
if version.Major() > 25 {
if d.Spec.Topology.Overlords.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Overlords.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Overlords.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Overlords.PodTemplate)
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Overlords.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Overlords.PodTemplate)
}
if d.Spec.Topology.MiddleManagers != nil {
if d.Spec.Topology.MiddleManagers.Replicas == nil {
d.Spec.Topology.MiddleManagers.Replicas = pointer.Int32P(1)
}
if d.Spec.Topology.MiddleManagers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.MiddleManagers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
if version.Major() > 25 {
if d.Spec.Topology.MiddleManagers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.MiddleManagers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.MiddleManagers.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.MiddleManagers.PodTemplate)
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.MiddleManagers.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.MiddleManagers.PodTemplate)
}
if d.Spec.Topology.Historicals != nil {
if d.Spec.Topology.Historicals.Replicas == nil {
d.Spec.Topology.Historicals.Replicas = pointer.Int32P(1)
}
if d.Spec.Topology.Historicals.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Historicals.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
if d.Spec.Version > "25.0.0" {
if d.Spec.Topology.Historicals.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Historicals.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Historicals.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Historicals.PodTemplate)
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Historicals.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Historicals.PodTemplate)
}
if d.Spec.Topology.Brokers != nil {
if d.Spec.Topology.Brokers.Replicas == nil {
d.Spec.Topology.Brokers.Replicas = pointer.Int32P(1)
}
if d.Spec.Topology.Brokers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Brokers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
if version.Major() > 25 {
if d.Spec.Topology.Brokers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Brokers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Brokers.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Brokers.PodTemplate)

}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Brokers.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Brokers.PodTemplate)
}
if d.Spec.Topology.Routers != nil {
if d.Spec.Topology.Routers.Replicas == nil {
d.Spec.Topology.Routers.Replicas = pointer.Int32P(1)
}
if d.Spec.Topology.Routers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Routers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
if version.Major() > 25 {
if d.Spec.Topology.Routers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Routers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Routers.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Routers.PodTemplate)
}
d.setDefaultContainerSecurityContext(&druidVersion, &d.Spec.Topology.Routers.PodTemplate)
d.setDefaultInitContainerSecurityContext(&druidVersion, &d.Spec.Topology.Routers.PodTemplate)
}
}
if d.Spec.MetadataStorage != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,15 @@ func (f *FerretDB) SetDefaults() {
}

defaultVersion := "13.13"
if !f.Spec.Backend.ExternallyManaged && f.Spec.Backend.Postgres == nil {
f.Spec.Backend.Postgres = &PostgresRef{
Version: &defaultVersion,
if !f.Spec.Backend.ExternallyManaged {
if f.Spec.Backend.Postgres == nil {
f.Spec.Backend.Postgres = &PostgresRef{
Version: &defaultVersion,
}
} else {
if f.Spec.Backend.Postgres.Version == nil {
f.Spec.Backend.Postgres.Version = &defaultVersion
}
}
}
f.SetTLSDefaults()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ type PostgresRef struct {
}

type PostgresServiceRef struct {
Name *string `json:"name"`
Namespace *string `json:"namespace"`
// +optional
Name string `json:"name,omitempty"`
// +optional
Namespace string `json:"namespace,omitempty"`
// PgPort is used because the service referred to the
// pg pod can have any port between 1 and 65535, inclusive
// but targetPort is fixed to 5432
PgPort *string `json:"pgPort"`
// +optional
PgPort int32 `json:"pgPort,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (f *FerretDB) ValidateCreateOrUpdate() field.ErrorList {
`'spec.authSecret.name' needs to specify when auth secret is externally managed`))
}

// Termination policy related
if f.Spec.StorageType == StorageTypeEphemeral && f.Spec.TerminationPolicy == TerminationPolicyHalt {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("storageType"),
f.Name,
Expand All @@ -173,22 +174,26 @@ func (f *FerretDB) ValidateCreateOrUpdate() field.ErrorList {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("backend"),
f.Name,
`'spec.postgres' is missing when backend is externally managed`))
} else {
if f.Spec.Backend.Postgres.URL == nil {
err := f.validateServiceRef(f.Spec.Backend.Postgres.Service)
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("backend"),
f.Name,
err.Error()))
}
}
if f.Spec.Backend.Postgres != nil && f.Spec.Backend.Postgres.URL == nil && f.Spec.Backend.Postgres.Service == nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("backend"),
f.Name,
`Have to provide 'backend.postgres.url' or 'backend.postgres.service' when backend is externally managed`))
}
}
if !f.Spec.Backend.ExternallyManaged && f.Spec.Backend.Postgres != nil && f.Spec.Backend.Postgres.Version != nil {
err := f.validatePostgresVersion()
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("backend"),
f.Name,
err.Error()))
} else {
if f.Spec.Backend.Postgres != nil && f.Spec.Backend.Postgres.Version != nil {
err := f.validatePostgresVersion()
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("backend"),
f.Name,
err.Error()))
}
}
}

// TLS related
if f.Spec.SSLMode == SSLModeAllowSSL || f.Spec.SSLMode == SSLModePreferSSL {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("sslMode"),
f.Name,
Expand Down Expand Up @@ -239,3 +244,14 @@ func (f *FerretDB) validatePostgresVersion() error {
}
return nil
}

func (f *FerretDB) validateServiceRef(ref *PostgresServiceRef) error {
if ref == nil {
return errors.New(`have to provide 'backend.postgres.url' or 'backend.postgres.service' when backend is externally managed`)
}
// port needs to be 0 < x < 65536
if ref.Namespace == "" || ref.Name == "" || ref.PgPort <= 0 || ref.PgPort >= 65536 {
return errors.New("pg service reference name, namespace and port(0<x<65536) needs to specify properly")
}
return nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions vendor/kubedb.dev/apimachinery/crds/kubedb.com_ferretdbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ spec:
namespace:
type: string
pgPort:
type: string
required:
- name
- namespace
- pgPort
format: int32
type: integer
type: object
url:
type: string
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,7 @@ kmodules.xyz/prober/api/v1
kmodules.xyz/resource-metadata/apis/node
kmodules.xyz/resource-metadata/apis/node/v1alpha1
kmodules.xyz/resource-metadata/crds
# kubedb.dev/apimachinery v0.41.0-rc.1
# kubedb.dev/apimachinery v0.41.0-rc.1.0.20240131123101-c711b3abb2a0
## explicit; go 1.21.5
kubedb.dev/apimachinery/apis
kubedb.dev/apimachinery/apis/archiver/v1alpha1
Expand Down

0 comments on commit dea7aaf

Please sign in to comment.