Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist mssql config secret, validate user envs #1322

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apis/kubedb/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ const (
EnvMSSQLAgentEnabled = "MSSQL_AGENT_ENABLED"
EnvMSSQLSAUsername = "MSSQL_SA_USERNAME"
EnvMSSQLSAPassword = "MSSQL_SA_PASSWORD"
EnvMSSQLVersion = "VERSION"

// container related
MSSQLContainerName = "mssql"
Expand Down
3 changes: 2 additions & 1 deletion apis/kubedb/v1alpha2/mssqlserver_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m *MSSQLServer) ServiceName() string {
}

func (m *MSSQLServer) SecondaryServiceName() string {
return metautil.NameWithPrefix(m.ServiceName(), "secondary")
return metautil.NameWithPrefix(m.ServiceName(), string(SecondaryServiceAlias))
}

func (m *MSSQLServer) GoverningServiceName() string {
Expand Down Expand Up @@ -245,6 +245,7 @@ func (m *MSSQLServer) GetPersistentSecrets() []string {
secrets = append(secrets, m.EndpointCertSecretName())
secrets = append(secrets, m.DbmLoginSecretName())
secrets = append(secrets, m.MasterKeySecretName())
secrets = append(secrets, m.ConfigSecretName())

return secrets
}
Expand Down
39 changes: 39 additions & 0 deletions apis/kubedb/v1alpha2/mssqlserver_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package v1alpha2
import (
"context"
"errors"
"fmt"

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

"gomodules.xyz/x/arrays"
core "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -156,6 +159,14 @@ func (m *MSSQLServer) ValidateCreateOrUpdate() field.ErrorList {
m.Name, "spec.tls.issuerRef' is missing"))
}

if m.Spec.PodTemplate != nil {
if err = ValidateMSSQLServerEnvVar(getMSSQLServerContainerEnvs(m), forbiddenMSSQLServerEnvVars, m.ResourceKind()); err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("podTemplate"),
m.Name,
err.Error()))
}
}

err = mssqlValidateVolumes(m.Spec.PodTemplate)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("podTemplate").Child("spec").Child("volumes"),
Expand Down Expand Up @@ -276,3 +287,31 @@ func mssqlValidateVolumesMountPaths(podTemplate *ofst.PodTemplateSpec) error {

return nil
}

var forbiddenMSSQLServerEnvVars = []string{
kubedb.EnvMSSQLSAUsername,
kubedb.EnvMSSQLSAPassword,
kubedb.EnvAcceptEula,
kubedb.EnvMSSQLEnableHADR,
kubedb.EnvMSSQLAgentEnabled,
kubedb.EnvMSSQLVersion,
}

func getMSSQLServerContainerEnvs(m *MSSQLServer) []core.EnvVar {
for _, container := range m.Spec.PodTemplate.Spec.Containers {
if container.Name == kubedb.MSSQLContainerName {
return container.Env
}
}
return []core.EnvVar{}
}

func ValidateMSSQLServerEnvVar(envs []core.EnvVar, forbiddenEnvs []string, resourceType string) error {
for _, env := range envs {
present, _ := arrays.Contains(forbiddenEnvs, env.Name)
if present {
return fmt.Errorf("environment variable %s is forbidden to use in %s spec", env.Name, resourceType)
}
}
return nil
}
3 changes: 2 additions & 1 deletion apis/kubedb/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ const (
TerminationPolicyDoNotTerminate TerminationPolicy = "DoNotTerminate"
)

// +kubebuilder:validation:Enum=primary;standby;stats;dashboard
// +kubebuilder:validation:Enum=primary;standby;stats;dashboard;secondary
Neaj-Morshad-101 marked this conversation as resolved.
Show resolved Hide resolved
type ServiceAlias string

const (
PrimaryServiceAlias ServiceAlias = "primary"
StandbyServiceAlias ServiceAlias = "standby"
StatsServiceAlias ServiceAlias = "stats"
DashboardServiceAlias ServiceAlias = "dashboard"
SecondaryServiceAlias ServiceAlias = "secondary"
)

// +kubebuilder:validation:Enum=DNS;IP;IPv4;IPv6
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_cassandras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3168,6 +3168,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_clickhouses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9720,6 +9720,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_druids.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_elasticsearches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43675,6 +43675,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_etcds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3917,6 +3917,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_ferretdbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3382,6 +3382,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_kafkas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19610,6 +19610,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_mariadbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8899,6 +8899,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_memcacheds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8033,6 +8033,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_mongodbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30038,6 +30038,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_mssqlservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4621,6 +4621,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_mysqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12182,6 +12182,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_perconaxtradbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8882,6 +8882,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_pgbouncers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6650,6 +6650,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_pgpools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3389,6 +3389,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_postgreses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9182,6 +9182,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_proxysqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6620,6 +6620,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_rabbitmqs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3384,6 +3384,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_redises.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8905,6 +8905,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_redissentinels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6584,6 +6584,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_singlestores.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4381,6 +4381,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_solrs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3385,6 +3385,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
1 change: 1 addition & 0 deletions crds/kubedb.com_zookeepers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3375,6 +3375,7 @@ spec:
- standby
- stats
- dashboard
- secondary
type: string
metadata:
properties:
Expand Down
Loading