Skip to content

Commit

Permalink
Extract databaseInfo from mssql (#1203)
Browse files Browse the repository at this point in the history

Signed-off-by: Arnob kumar saha <[email protected]>
Signed-off-by: Neaj Morshad <[email protected]>
  • Loading branch information
ArnobKumarSaha authored May 8, 2024
1 parent 945f983 commit 7fb9b8c
Show file tree
Hide file tree
Showing 6 changed files with 808 additions and 762 deletions.
39 changes: 37 additions & 2 deletions apis/kubedb/v1alpha2/mssql_helpers.go
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 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
4 changes: 2 additions & 2 deletions apis/kubedb/v1alpha2/mssql_webhook.go
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 7fb9b8c

Please sign in to comment.