Skip to content

Commit

Permalink
api machinery branch updated
Browse files Browse the repository at this point in the history
Signed-off-by: Hiranmoy Das Chowdhury <[email protected]>
  • Loading branch information
HiranmoyChowdhury committed May 17, 2024
1 parent bcea661 commit 46523bf
Show file tree
Hide file tree
Showing 16 changed files with 902 additions and 801 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.1
kubedb.dev/apimachinery v0.45.2-0.20240515112316-c4861416c70e
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 @@ -609,8 +609,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.1 h1:J5hzWF5tXeI6uwTwz9Uyi0CJdLB2CCmUvoFMngKRnas=
kubedb.dev/apimachinery v0.45.1/go.mod h1:mVmsjSF2KmJzFrIx0rZo9KWee/zUcYmw8OqgcuvR2TA=
kubedb.dev/apimachinery v0.45.2-0.20240515112316-c4861416c70e h1:EfMR1D5mdhI7gL8AaIbMp8JXQsBy95tw/p0VKBUQJR8=
kubedb.dev/apimachinery v0.45.2-0.20240515112316-c4861416c70e/go.mod h1:n/8V16s0dvnNeeY0KuveptY5zDP0MWhPg8b+TsPKUwc=
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
2 changes: 1 addition & 1 deletion pgbouncer/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ func (l *XormClientList) addXormClient(kc client.Client, pb *api.PgBouncer, ctx
xormClient, err := NewKubeDBClientBuilder(kc, pb).WithContext(ctx).WithDatabaseRef(&pb.Spec.Database).WithPod(podName).WithAuth(auth).WithBackendDBType(dbType).WithPostgresDBName(dbName).GetPgBouncerXormClient()
l.Mutex.Lock()
defer l.Mutex.Unlock()
defer l.WG.Done()
if err != nil {
klog.V(5).ErrorS(err, fmt.Sprintf("failed to create xorm client for pgbouncer %s/%s ", pb.Namespace, pb.Name))
} else {
l.List = append(l.List, xormClient)
}
l.WG.Done()
}

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.

Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ const (
PgpoolExporterTlsVolumeName = "exporter-certs"
PgpoolExporterTlsVolumeMountPath = "/tls/certs"
PgpoolRootUser = "postgres"
PgpoolPrimaryServicePortName = "primary"
PgpoolDatabasePortName = "db"
// ========================================== ZooKeeper Constants =================================================//

KubeDBZooKeeperRoleName = "kubedb:zookeeper-version-reader"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@ import (
"fmt"

"kubedb.dev/apimachinery/apis"
catalog "kubedb.dev/apimachinery/apis/catalog/v1alpha1"
"kubedb.dev/apimachinery/apis/kubedb"
"kubedb.dev/apimachinery/crds"

promapi "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"gomodules.xyz/pointer"
core "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
appslister "k8s.io/client-go/listers/apps/v1"
"kmodules.xyz/client-go/apiextensions"
meta_util "kmodules.xyz/client-go/meta"
"kmodules.xyz/client-go/policy/secomp"
appcat "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
mona "kmodules.xyz/monitoring-agent-api/api/v1"
ofst "kmodules.xyz/offshoot-api/api/v1"
)

func (_ Memcached) CustomResourceDefinition() *apiextensions.CustomResourceDefinition {
Expand Down Expand Up @@ -166,11 +171,12 @@ func (m Memcached) StatsServiceLabels() map[string]string {
return m.ServiceLabels(StatsServiceAlias, map[string]string{LabelRole: RoleStats})
}

func (m *Memcached) SetDefaults() {
func (m *Memcached) SetDefaults(mcVersion *catalog.MemcachedVersion) {
if m == nil {
return
}

m.setDefaultContainerSecurityContext(mcVersion, &m.Spec.PodTemplate)
// perform defaulting
if m.Spec.TerminationPolicy == "" {
m.Spec.TerminationPolicy = TerminationPolicyDelete
Expand All @@ -196,6 +202,45 @@ func (m *Memcached) SetHealthCheckerDefaults() {
}
}

func (m *Memcached) setDefaultContainerSecurityContext(mcVersion *catalog.MemcachedVersion, podTemplate *ofst.PodTemplateSpec) {
if podTemplate == nil {
return
}
if podTemplate.Spec.ContainerSecurityContext == nil {
podTemplate.Spec.ContainerSecurityContext = &corev1.SecurityContext{}
}
if podTemplate.Spec.SecurityContext == nil {
podTemplate.Spec.SecurityContext = &corev1.PodSecurityContext{}
}
if podTemplate.Spec.SecurityContext.FSGroup == nil {
podTemplate.Spec.SecurityContext.FSGroup = mcVersion.Spec.SecurityContext.RunAsUser
}
m.assignDefaultContainerSecurityContext(mcVersion, podTemplate.Spec.ContainerSecurityContext)
}

func (m *Memcached) assignDefaultContainerSecurityContext(mcVersion *catalog.MemcachedVersion, sc *core.SecurityContext) {
if sc.AllowPrivilegeEscalation == nil {
sc.AllowPrivilegeEscalation = pointer.BoolP(false)
}
if sc.Capabilities == nil {
sc.Capabilities = &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
}
}
if sc.RunAsNonRoot == nil {
sc.RunAsNonRoot = pointer.BoolP(true)
}
if sc.RunAsUser == nil {
sc.RunAsUser = mcVersion.Spec.SecurityContext.RunAsUser
}
if sc.RunAsGroup == nil {
sc.RunAsGroup = mcVersion.Spec.SecurityContext.RunAsUser
}
if sc.SeccompProfile == nil {
sc.SeccompProfile = secomp.DefaultSeccompProfile()
}
}

func (m *MemcachedSpec) GetPersistentSecrets() []string {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha2
import (
"context"
"fmt"
"regexp"
"strings"
"time"

Expand All @@ -30,6 +31,7 @@ import (
"gomodules.xyz/pointer"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"kmodules.xyz/client-go/apiextensions"
Expand All @@ -38,6 +40,7 @@ import (
"kmodules.xyz/client-go/policy/secomp"
appcat "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
ofst "kmodules.xyz/offshoot-api/api/v2"
pslister "kubeops.dev/petset/client/listers/apps/v1"
)

type MSSQLServerApp struct {
Expand Down Expand Up @@ -119,7 +122,7 @@ func (m *MSSQLServer) OffshootSelectors(extraSelectors ...map[string]string) map
func (m *MSSQLServer) IsAvailabilityGroup() bool {
return m.Spec.Topology != nil &&
m.Spec.Topology.Mode != nil &&
*m.Spec.Topology.Mode == MSSQLModeAvailabilityGroup
*m.Spec.Topology.Mode == MSSQLServerModeAvailabilityGroup
}

func (m *MSSQLServer) IsStandalone() bool {
Expand Down Expand Up @@ -153,6 +156,32 @@ func (m *MSSQLServer) ServiceAccountName() string {
return m.OffshootName()
}

func (m *MSSQLServer) AvailabilityGroupName() string {
// Get the database name
dbName := m.Name

// Regular expression pattern to match allowed characters (alphanumeric only)
allowedPattern := regexp.MustCompile(`[a-zA-Z0-9]+`)

// Extract valid characters from the database name
validChars := allowedPattern.FindAllString(dbName, -1)

// Ensure that the availability group name is not empty
var availabilityGroupName string
if len(validChars) == 0 {
klog.Warningf("Database name '%s' contains no valid characters for the availability group name. Setting availability group name to 'DefaultGroupName'.", dbName)
availabilityGroupName = "DefaultGroupName" // Provide a default name if the database name contains no valid characters
} else {
// Concatenate the valid characters to form the availability group name
availabilityGroupName = ""
for _, char := range validChars {
availabilityGroupName += char
}
}

return availabilityGroupName
}

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 Expand Up @@ -225,7 +254,7 @@ func (m *MSSQLServer) SetDefaults() {
}
} else {
if m.Spec.LeaderElection == nil {
m.Spec.LeaderElection = &MSSQLLeaderElectionConfig{
m.Spec.LeaderElection = &MSSQLServerLeaderElectionConfig{
// The upper limit of election timeout is 50000ms (50s), which should only be used when deploying a
// globally-distributed etcd cluster. A reasonable round-trip time for the continental United States is around 130-150ms,
// and the time between US and Japan is around 350-400ms. If the network has uneven performance or regular packet
Expand Down Expand Up @@ -368,3 +397,9 @@ func (m *MSSQLServer) setDefaultContainerResourceLimits(podTemplate *ofst.PodTem
}
}
}

func (m *MSSQLServer) ReplicasAreReady(lister pslister.PetSetLister) (bool, string, error) {
// Desire number of petSets
expectedItems := 1
return checkReplicasOfPetSet(lister.PetSets(m.Namespace), labels.SelectorFromSet(m.OffshootLabels()), expectedItems)
}
24 changes: 12 additions & 12 deletions vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/mssql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ const (
)

// +kubebuilder:validation:Enum=AvailabilityGroup;RemoteReplica
type MSSQLMode string
type MSSQLServerMode string

const (
MSSQLModeAvailabilityGroup MSSQLMode = "AvailabilityGroup"
MSSQLModeRemoteReplica MSSQLMode = "RemoteReplica"
MSSQLServerModeAvailabilityGroup MSSQLServerMode = "AvailabilityGroup"
MSSQLServerModeRemoteReplica MSSQLServerMode = "RemoteReplica"
)

// MSSQLServer defines a MSSQLServer database.
Expand Down Expand Up @@ -68,7 +68,7 @@ type MSSQLServerSpec struct {

// MSSQLServer cluster topology
// +optional
Topology *MSSQLTopology `json:"topology,omitempty"`
Topology *MSSQLServerTopology `json:"topology,omitempty"`

// StorageType can be durable (default) or ephemeral
StorageType StorageType `json:"storageType,omitempty"`
Expand Down Expand Up @@ -111,7 +111,7 @@ type MSSQLServerSpec struct {

// Leader election configuration
// +optional
LeaderElection *MSSQLLeaderElectionConfig `json:"leaderElection,omitempty"`
LeaderElection *MSSQLServerLeaderElectionConfig `json:"leaderElection,omitempty"`

// HealthChecker defines attributes of the health checker
// +optional
Expand All @@ -130,18 +130,18 @@ type InternalAuthentication struct {
EndpointCert *kmapi.TLSConfig `json:"endpointCert"`
}

type MSSQLTopology struct {
type MSSQLServerTopology struct {
// If set to -
// "AvailabilityGroup", MSSQLAvailabilityGroupSpec is required and MSSQLServer servers will start an Availability Group
Mode *MSSQLMode `json:"mode,omitempty"`
Mode *MSSQLServerMode `json:"mode,omitempty"`

// AvailabilityGroup info for MSSQLServer
// +optional
AvailabilityGroup *MSSQLAvailabilityGroupSpec `json:"availabilityGroup,omitempty"`
AvailabilityGroup *MSSQLServerAvailabilityGroupSpec `json:"availabilityGroup,omitempty"`
}

// MSSQLAvailabilityGroupSpec defines the availability group spec for MSSQLServer
type MSSQLAvailabilityGroupSpec struct {
// MSSQLServerAvailabilityGroupSpec defines the availability group spec for MSSQLServer
type MSSQLServerAvailabilityGroupSpec struct {
// AvailabilityDatabases is an array of databases to be included in the availability group
// +optional
Databases []string `json:"databases"`
Expand All @@ -161,8 +161,8 @@ type MSSQLServerStatus struct {
Conditions []kmapi.Condition `json:"conditions,omitempty"`
}

// MSSQLLeaderElectionConfig contains essential attributes of leader election.
type MSSQLLeaderElectionConfig struct {
// MSSQLServerLeaderElectionConfig contains essential attributes of leader election.
type MSSQLServerLeaderElectionConfig struct {
// Period between Node.Tick invocations
// +kubebuilder:default="100ms"
// +optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (r *MSSQLServer) SetupWebhookWithManager(mgr ctrl.Manager) error {
Complete()
}

//+kubebuilder:webhook:path=/mutate-kubedb-com-v1alpha2-mssql,mutating=true,failurePolicy=fail,sideEffects=None,groups=kubedb.com,resources=mssqls,verbs=create;update,versions=v1alpha2,name=mmssql.kb.io,admissionReviewVersions=v1
//+kubebuilder:webhook:path=/mutate-kubedb-com-v1alpha2-mssqlserver,mutating=true,failurePolicy=fail,sideEffects=None,groups=kubedb.com,resources=mssqlservers,verbs=create;update,versions=v1alpha2,name=mmssqlserver.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &MSSQLServer{}

Expand All @@ -60,7 +60,7 @@ func (m *MSSQLServer) Default() {
m.SetDefaults()
}

//+kubebuilder:webhook:path=/validate-kubedb-com-v1alpha2-mssql,mutating=false,failurePolicy=fail,sideEffects=None,groups=kubedb.com,resources=mssqls,verbs=create;update,versions=v1alpha2,name=vmssql.kb.io,admissionReviewVersions=v1
//+kubebuilder:webhook:path=/validate-kubedb-com-v1alpha2-mssqlserver,mutating=false,failurePolicy=fail,sideEffects=None,groups=kubedb.com,resources=mssqlservers,verbs=create;update,versions=v1alpha2,name=vmssqlserver.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &MSSQLServer{}

Expand Down
Loading

0 comments on commit 46523bf

Please sign in to comment.