Skip to content

Commit

Permalink
Update apimachinery module
Browse files Browse the repository at this point in the history
Signed-off-by: raihankhan <[email protected]>
  • Loading branch information
raihankhan committed Jun 3, 2024
1 parent f25da8e commit 8beb95c
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 74 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
k8s.io/klog/v2 v2.120.1
kmodules.xyz/client-go v0.29.14
kmodules.xyz/custom-resources v0.29.1
kubedb.dev/apimachinery v0.45.2-0.20240531110354-049490369476
kubedb.dev/apimachinery v0.45.2-0.20240531124004-74e7190f0509
sigs.k8s.io/controller-runtime v0.17.4
xorm.io/xorm v1.3.6
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,8 @@ kmodules.xyz/monitoring-agent-api v0.29.0 h1:gpFl6OZrlMLb/ySMHdREI9EwGtnJ91oZBn9
kmodules.xyz/monitoring-agent-api v0.29.0/go.mod h1:iNbvaMTgVFOI5q2LJtGK91j4Dmjv4ZRiRdasGmWLKQI=
kmodules.xyz/offshoot-api v0.29.2 h1:akXmvkNqFz1n9p1STVs9iP7ODYET0S7BhcYCMXEjK4A=
kmodules.xyz/offshoot-api v0.29.2/go.mod h1:Wv7Xo8wbvznI+8bhaylRFHFjkt30xRDOUOnqV8kOAxM=
kubedb.dev/apimachinery v0.45.2-0.20240531110354-049490369476 h1:suwRPKSUTPJXBLRopWmgxE4U9S7POo5iMROR4tnlQlU=
kubedb.dev/apimachinery v0.45.2-0.20240531110354-049490369476/go.mod h1:LBoGqbBHFeIRLv1KtkkmVxjK/g9CmQETVpwszFp9Ly0=
kubedb.dev/apimachinery v0.45.2-0.20240531124004-74e7190f0509 h1:fkvqASIcqzMnUvMCXK5SznLkk3sJdqVIM0TH4AhAG7w=
kubedb.dev/apimachinery v0.45.2-0.20240531124004-74e7190f0509/go.mod h1:LBoGqbBHFeIRLv1KtkkmVxjK/g9CmQETVpwszFp9Ly0=
kubeops.dev/petset v0.0.5 h1:VVXi39JhjondlbHyZ98z0MLp6VCmiCMinL59K48Y2zA=
kubeops.dev/petset v0.0.5/go.mod h1:ijtKT1HlAht2vBEZj5LW7C00XEs3B0d1VdCQgd5V4cA=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,6 @@ func (c *ClickHouse) ResourceSingular() string {
}

func (c *ClickHouse) SetDefaults() {
if c.Spec.Replicas == nil {
c.Spec.Replicas = pointer.Int32P(1)
}
if c.Spec.TerminationPolicy == "" {
c.Spec.TerminationPolicy = TerminationPolicyDelete
}
if c.Spec.StorageType == "" {
c.Spec.StorageType = StorageTypeDurable
}

var chVersion catalog.ClickHouseVersion
err := DefaultClient.Get(context.TODO(), types.NamespacedName{
Name: c.Spec.Version,
Expand Down Expand Up @@ -257,6 +247,16 @@ func (c *ClickHouse) SetDefaults() {
}
c.Spec.ClusterTopology.Cluster = clusters
} else {
if c.Spec.Replicas == nil {
c.Spec.Replicas = pointer.Int32P(1)
}
if c.Spec.TerminationPolicy == "" {
c.Spec.TerminationPolicy = TerminationPolicyDelete
}
if c.Spec.StorageType == "" {
c.Spec.StorageType = StorageTypeDurable
}

if c.Spec.PodTemplate == nil {
c.Spec.PodTemplate = &ofst.PodTemplateSpec{}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ func (r *ClickHouse) ValidateDelete() (admission.Warnings, error) {

func (r *ClickHouse) ValidateCreateOrUpdate() error {
var allErr field.ErrorList

if r.Spec.Version == "" {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"),
r.Name,
"spec.version' is missing"))
return apierrors.NewInvalid(schema.GroupKind{Group: "ClickHouse.kubedb.com", Kind: "ClickHouse"}, r.Name, allErr)
} else {
err := r.ValidateVersion(r)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"),
r.Spec.Version,
err.Error()))
return apierrors.NewInvalid(schema.GroupKind{Group: "ClickHouse.kubedb.com", Kind: "ClickHouse"}, r.Name, allErr)
}
}

if r.Spec.ClusterTopology != nil {
clusterName := map[string]bool{}
clusters := r.Spec.ClusterTopology.Cluster
Expand All @@ -99,13 +115,40 @@ func (r *ClickHouse) ValidateCreateOrUpdate() error {
"cluster name is duplicated, use different cluster name"))
}
clusterName[cluster.Name] = true

allErr = r.validateClusterStorageType(cluster, allErr)

err := r.validateVolumes(cluster.PodTemplate)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("clusterTopology").Child("podTemplate").Child("spec").Child("volumes"),
r.Name,
err.Error()))
}
err = r.validateVolumesMountPaths(cluster.PodTemplate)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("clusterTopology").Child("podTemplate").Child("spec").Child("volumeMounts"),
r.Name,
err.Error()))
}
}
if r.Spec.PodTemplate != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("podTemplate"),
r.Name,
"PodTemplate should be nil in clusterTopology"))
}

if r.Spec.Replicas != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("replica"),
r.Name,
"replica should be nil in clusterTopology"))
}

if r.Spec.StorageType != "" {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("storageType"),
r.Name,
"StorageType should be empty in clusterTopology"))
}

if r.Spec.Storage != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("storage"),
r.Name,
Expand All @@ -126,22 +169,6 @@ func (r *ClickHouse) ValidateCreateOrUpdate() error {
r.Name,
"number of replicas can't be greater than 1 in standalone mode"))
}

}

if r.Spec.Version == "" {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"),
r.Name,
"spec.version' is missing"))
} else {
err := r.ValidateVersion(r)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"),
r.Spec.Version,
err.Error()))
}
}
if r.Spec.ClusterTopology == nil {
err := r.validateVolumes(r.Spec.PodTemplate)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("podTemplate").Child("spec").Child("volumes"),
Expand All @@ -154,36 +181,10 @@ func (r *ClickHouse) ValidateCreateOrUpdate() error {
r.Name,
err.Error()))
}
}

if r.Spec.ClusterTopology != nil {
clusters := r.Spec.ClusterTopology.Cluster
for _, cluster := range clusters {
allErr = r.validateClusterStorageType(cluster, allErr)

err := r.validateVolumes(cluster.PodTemplate)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("clusterTopology").Child("podTemplate").Child("spec").Child("volumes"),
r.Name,
err.Error()))
}
err = r.validateVolumesMountPaths(cluster.PodTemplate)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("clusterTopology").Child("podTemplate").Child("spec").Child("volumeMounts"),
r.Name,
err.Error()))
}
}
} else {
allErr = r.validateStandaloneStorageType(r.Spec.StorageType, r.Spec.Storage, allErr)
}

//if r.Spec.ConfigSecret != nil && r.Spec.ConfigSecret.Name == "" {
// allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("configSecret").Child("name"),
// r.Name,
// "ConfigSecret Name can not be empty"))
//}

if len(allErr) == 0 {
return nil
}
Expand Down Expand Up @@ -302,6 +303,5 @@ func (r *ClickHouse) validateVolumesMountPaths(podTemplate *ofst.PodTemplateSpec
}
}
}

return nil
}
12 changes: 9 additions & 3 deletions vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,15 +1254,21 @@ const (
ClickHouseContainerName = "clickhouse"
ClickHouseInitContainerName = "clickhouse-init"

ClickHouseClusterConfigFileName = "cluster-config.yaml"
ClickHouseTempConfigDir = "/ch-tmp/config"
ClickHouseTempDir = "/ch-tmp"
ClickHouseClusterConfigFile = "cluster-config.yaml"
ClickHouseTempConfigDir = "/ch-tmp/config"
ClickHouseTempDir = "/ch-tmp"

ClickHouseUserConfigDir = "/etc/clickhouse-server/user.d"
ClickHouseMacrosFileName = "macros.yaml"

ClickHouseStandalone = "standalone"
ClickHouseCluster = "cluster"

ClickHouseHealthCheckerDatabase = "kubedb_system_db"
ClickHouseHealthCheckerTable = "kubedb_system_table"

ClickHouseServerConfigFile = "server-config.yaml"
ClickHouseKeeperFileConfig = "keeper-config.yaml"
)

// Resource kind related constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,24 +247,24 @@ func (m *MSSQLServer) EndpointCertSecretName() string {
}

// CertificateName returns the default certificate name and/or certificate secret name for a certificate alias
func (s *MSSQLServer) CertificateName(alias MSSQLServerCertificateAlias) string {
return metautil.NameWithSuffix(s.Name, fmt.Sprintf("%s-cert", string(alias)))
func (m *MSSQLServer) CertificateName(alias MSSQLServerCertificateAlias) string {
return metautil.NameWithSuffix(m.Name, fmt.Sprintf("%s-cert", string(alias)))
}

func (s *MSSQLServer) SecretName(alias MSSQLServerCertificateAlias) string {
return metautil.NameWithSuffix(s.Name, string(alias))
func (m *MSSQLServer) SecretName(alias MSSQLServerCertificateAlias) string {
return metautil.NameWithSuffix(m.Name, string(alias))
}

// GetCertSecretName returns the secret name for a certificate alias if any
// otherwise returns default certificate secret name for the given alias.
func (s *MSSQLServer) GetCertSecretName(alias MSSQLServerCertificateAlias) string {
if s.Spec.TLS != nil {
name, ok := kmapi.GetCertificateSecretName(s.Spec.TLS.Certificates, string(alias))
func (m *MSSQLServer) GetCertSecretName(alias MSSQLServerCertificateAlias) string {
if m.Spec.TLS != nil {
name, ok := kmapi.GetCertificateSecretName(m.Spec.TLS.Certificates, string(alias))
if ok {
return name
}
}
return s.CertificateName(alias)
return m.CertificateName(alias)
}

func (m *MSSQLServer) GetNameSpacedName() string {
Expand Down Expand Up @@ -330,6 +330,8 @@ func (m *MSSQLServer) SetDefaults() {

m.setDefaultContainerSecurityContext(&mssqlVersion, m.Spec.PodTemplate)

m.SetTLSDefaults()

m.SetHealthCheckerDefaults()

m.setDefaultContainerResourceLimits(m.Spec.PodTemplate)
Expand Down Expand Up @@ -436,6 +438,85 @@ func (m *MSSQLServer) setDefaultContainerResourceLimits(podTemplate *ofst.PodTem
}
}

func (m *MSSQLServer) SetTLSDefaults() {
m.SetTLSDefaultsForInternalAuth()

if m.Spec.TLS == nil || m.Spec.TLS.IssuerRef == nil {
return
}

// Server-cert
defaultServerOrg := []string{KubeDBOrganization}
defaultServerOrgUnit := []string{string(MSSQLServerServerCert)}
_, cert := kmapi.GetCertificate(m.Spec.TLS.Certificates, string(MSSQLServerServerCert))
if cert != nil && cert.Subject != nil {
if cert.Subject.Organizations != nil {
defaultServerOrg = cert.Subject.Organizations
}
if cert.Subject.OrganizationalUnits != nil {
defaultServerOrgUnit = cert.Subject.OrganizationalUnits
}
}

m.Spec.TLS.Certificates = kmapi.SetMissingSpecForCertificate(m.Spec.TLS.Certificates, kmapi.CertificateSpec{
Alias: string(MSSQLServerServerCert),
SecretName: m.GetCertSecretName(MSSQLServerServerCert),
Subject: &kmapi.X509Subject{
Organizations: defaultServerOrg,
OrganizationalUnits: defaultServerOrgUnit,
},
})

// Client-cert
defaultClientOrg := []string{KubeDBOrganization}
defaultClientOrgUnit := []string{string(MSSQLServerClientCert)}
_, cert = kmapi.GetCertificate(m.Spec.TLS.Certificates, string(MSSQLServerClientCert))
if cert != nil && cert.Subject != nil {
if cert.Subject.Organizations != nil {
defaultClientOrg = cert.Subject.Organizations
}
if cert.Subject.OrganizationalUnits != nil {
defaultClientOrgUnit = cert.Subject.OrganizationalUnits
}
}
m.Spec.TLS.Certificates = kmapi.SetMissingSpecForCertificate(m.Spec.TLS.Certificates, kmapi.CertificateSpec{
Alias: string(MSSQLServerClientCert),
SecretName: m.GetCertSecretName(MSSQLServerClientCert),
Subject: &kmapi.X509Subject{
Organizations: defaultClientOrg,
OrganizationalUnits: defaultClientOrgUnit,
},
})
}

func (m *MSSQLServer) SetTLSDefaultsForInternalAuth() {
if m.Spec.InternalAuth == nil || m.Spec.InternalAuth.EndpointCert == nil || m.Spec.InternalAuth.EndpointCert.IssuerRef == nil {
return
}

// Endpoint-cert
defaultServerOrg := []string{KubeDBOrganization}
defaultServerOrgUnit := []string{string(MSSQLServerEndpointCert)}
_, cert := kmapi.GetCertificate(m.Spec.InternalAuth.EndpointCert.Certificates, string(MSSQLServerEndpointCert))
if cert != nil && cert.Subject != nil {
if cert.Subject.Organizations != nil {
defaultServerOrg = cert.Subject.Organizations
}
if cert.Subject.OrganizationalUnits != nil {
defaultServerOrgUnit = cert.Subject.OrganizationalUnits
}
}

m.Spec.InternalAuth.EndpointCert.Certificates = kmapi.SetMissingSpecForCertificate(m.Spec.InternalAuth.EndpointCert.Certificates, kmapi.CertificateSpec{
Alias: string(MSSQLServerEndpointCert),
SecretName: m.GetCertSecretName(MSSQLServerEndpointCert),
Subject: &kmapi.X509Subject{
Organizations: defaultServerOrg,
OrganizationalUnits: defaultServerOrgUnit,
},
})
}

func (m *MSSQLServer) ReplicasAreReady(lister pslister.PetSetLister) (bool, string, error) {
// Desire number of petSets
expectedItems := 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const (
type MSSQLServerCertificateAlias string

const (
MSSQLServerServerCert MSSQLServerCertificateAlias = "server"
MSSQLServerClientCert MSSQLServerCertificateAlias = "client"
MSSQLServerEndpoint MSSQLServerCertificateAlias = "endpoint"
MSSQLServerServerCert MSSQLServerCertificateAlias = "server"
MSSQLServerClientCert MSSQLServerCertificateAlias = "client"
MSSQLServerEndpointCert MSSQLServerCertificateAlias = "endpoint"
)

// MSSQLServer defines a MSSQLServer database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ func (m *MSSQLServer) ValidateCreateOrUpdate() field.ErrorList {

if m.Spec.InternalAuth == nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("internalAuth"),
m.Name, "spec.internalAuth, spec.internalAuth.endpointCert, spec.internalAuth.endpointCert.issuerRef' is missing"))
m.Name, "spec.internalAuth is missing"))
} else if m.Spec.InternalAuth.EndpointCert == nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("internalAuth").Child("endpointCert"),
m.Name, "spec.internalAuth.endpointCert, spec.internalAuth.endpointCert.issuerRef' is missing"))
} else if m.Spec.InternalAuth.EndpointCert != nil {
m.Name, "spec.internalAuth.endpointCert is missing"))
} else {
if m.Spec.InternalAuth.EndpointCert.IssuerRef == nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("internalAuth").Child("endpointCert").Child("issuerRef"),
m.Name, "spec.internalAuth.endpointCert.issuerRef' is missing"))
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ kmodules.xyz/monitoring-agent-api/api/v1
## explicit; go 1.22.0
kmodules.xyz/offshoot-api/api/v1
kmodules.xyz/offshoot-api/api/v2
# kubedb.dev/apimachinery v0.45.2-0.20240531110354-049490369476
# kubedb.dev/apimachinery v0.45.2-0.20240531124004-74e7190f0509
## explicit; go 1.22.0
kubedb.dev/apimachinery/apis
kubedb.dev/apimachinery/apis/catalog
Expand Down

0 comments on commit 8beb95c

Please sign in to comment.