Skip to content

Commit

Permalink
devdb: support sqlserver (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored May 21, 2024
1 parent f8dfadb commit 21f0a08
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 57 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ jobs:
sudo install skaffold /usr/local/bin/
- name: Run integration tests
run: |
echo "ATLAS_TOKEN=${{ secrets.ATLAS_TOKEN }}" > ./config/atlas-token/.env.secret
make integration-tests
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ Dockerfile.cross
# Kubernetes Generated files - skip generated files, except for vendored files

!vendor/**/zz_generated.*

# editor and IDE paraphernalia
.idea
*.swp
*.swo
*~

.kpt-pipeline
.kpt-pipeline
.env.secret
.env*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ cli-gen: generate manifests chart-manifests license

.PHONY: integration-tests
integration-tests:
skaffold run --wait-for-connection=true
skaffold run --wait-for-connection=true -p integration
$(MAKE) install
./scripts/integration-tests.sh
$(MAKE) undeploy ignore-not-found=true
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ To configure the operator, you can set the following values in the `values.yaml`
```yaml
extraEnvs: []
# extraEnvs:
# - name: FOO
# value: "foo"
# - name: MSSQL_ACCEPT_EULA
# value: "Y"
# - name: MSSQL_PID
# value: "Developer"
# - name: ATLAS_TOKEN
# valueFrom:
# secretKeyRef:
Expand All @@ -82,6 +84,8 @@ To configure the operator, you can set the following values in the `values.yaml`
# name: configmap-resource
```

> Note: The SQL Server driver requires the `MSSQL_ACCEPT_EULA` and `MSSQL_PID` environment variables to be set for acceptance of the [Microsoft EULA](https://go.microsoft.com/fwlink/?linkid=857698) and the product ID, respectively.
- `extraVolumes`: Used to mount additional volumes to the operator

```yaml
Expand Down
29 changes: 29 additions & 0 deletions api/v1alpha1/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"net/url"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -100,6 +101,13 @@ func (c *Credentials) URL() *url.URL {
Scheme: c.Scheme,
Path: c.Database,
}
if DriverBySchema(c.Scheme) == "sqlserver" && c.Database != "" {
u.Path = ""
if c.Parameters == nil {
c.Parameters = map[string]string{}
}
c.Parameters["database"] = c.Database
}
if c.User != "" || c.Password != "" {
u.User = url.UserPassword(c.User, c.Password)
}
Expand Down Expand Up @@ -131,3 +139,24 @@ func getSecrectValue(
}
return string(val.Data[ref.Key]), nil
}

// DriverBySchema returns the driver from the given schema.
// it remove the schema modifier if present.
// e.g. mysql+unix -> mysql
// it also handles aliases.
// e.g. mariadb -> mysql
func DriverBySchema(schema string) string {
p := strings.SplitN(schema, "+", 2)
switch drv := strings.ToLower(p[0]); drv {
case "libsql":
return "sqlite"
case "maria", "mariadb":
return "mysql"
case "postgresql":
return "postgres"
case "sqlserver", "azuresql", "mssql":
return "sqlserver"
default:
return drv
}
}
11 changes: 11 additions & 0 deletions api/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ func TestCredentials_URL(t *testing.T) {
},
exp: "mysql://user:pass@:3306/db",
},
{
c: v1alpha1.Credentials{
Scheme: "sqlserver",
User: "sa",
Password: "P@ssw0rd0995",
Host: "",
Port: 1433,
Database: "master",
},
exp: "sqlserver://sa:P%40ssw0rd0995@:1433?database=master",
},
} {
t.Run(tt.exp, func(t *testing.T) {
require.Equal(t, tt.exp, tt.c.URL().String())
Expand Down
42 changes: 42 additions & 0 deletions config/atlas-token/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2023 The Atlas Operator Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

namespace: atlas-operator-system
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../default
secretGenerator:
- name: atlas-token-secret
envs:
- .env.secret
patches:
- patch: |-
- op: add
path: "/spec/template/spec/containers/0/env/-"
value:
name: ATLAS_TOKEN
valueFrom:
secretKeyRef:
key: ATLAS_TOKEN
name: atlas-token-secret
- op: add
path: "/spec/template/spec/containers/0/env/-"
value:
name: MSSQL_ACCEPT_EULA
value: "Y"
target:
kind: Deployment
namespace: system
name: controller-manager
73 changes: 73 additions & 0 deletions config/integration/databases/sqlserver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2023 The Atlas Operator Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apps/v1
kind: Deployment
metadata:
name: sqlserver
spec:
selector:
matchLabels:
app: sqlserver
replicas: 1
template:
metadata:
labels:
app: sqlserver
spec:
containers:
- name: sqlserver
image: mcr.microsoft.com/mssql/server:2022-latest
env:
- name: ACCEPT_EULA
value: "Y"
- name: MSSQL_PID
value: "Developer"
- name: MSSQL_SA_PASSWORD
value: "P@ssw0rd0995"
ports:
- containerPort: 1433
name: sqlserver
readinessProbe:
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
exec:
command: [
"/opt/mssql-tools/bin/sqlcmd",
"-U", "sa", "-P","P@ssw0rd0995",
"-Q", "SELECT 1"
]
---
apiVersion: v1
kind: Service
metadata:
name: sqlserver
spec:
selector:
app: sqlserver
ports:
- name: sqlserver
port: 1433
targetPort: sqlserver
type: ClusterIP
---
apiVersion: v1
kind: Secret
metadata:
name: sqlserver-credentials
type: Opaque
stringData:
url: "sqlserver://sa:P%[email protected]:1433?database=master"
password: P@ssw0rd0995
41 changes: 41 additions & 0 deletions config/integration/schema/sqlserver_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2023 The Atlas Operator Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: db.atlasgo.io/v1alpha1
kind: AtlasSchema
metadata:
labels:
app.kubernetes.io/name: atlasschema
app.kubernetes.io/instance: atlasschema-sample
app.kubernetes.io/part-of: atlas-operator
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: atlas-operator
name: atlasschema-sqlserver
spec:
credentials:
scheme: sqlserver
host: sqlserver.default
user: sa
passwordFrom:
secretKeyRef:
key: password
name: sqlserver-credentials
database: master
port: 1433
schema:
sql: |
create table [t1] (
[id] int not null,
primary key ([id])
);
1 change: 1 addition & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ spec:
- --leader-elect
image: controller:latest
name: manager
env: []
securityContext:
runAsUser: 1000
allowPrivilegeEscalation: false
Expand Down
23 changes: 0 additions & 23 deletions controllers/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -694,27 +692,6 @@ func (m *mockClient) Create(ctx context.Context, obj client.Object, opts ...clie
return nil
}

func TestTemplateSanity(t *testing.T) {
var b bytes.Buffer
v := &devDB{
NamespacedName: types.NamespacedName{
Name: "test",
Namespace: "default",
},
}
for _, tt := range []string{"mysql", "postgres"} {
t.Run(tt, func(t *testing.T) {
v.Driver = tt
err := tmpl.ExecuteTemplate(&b, "devdb.tmpl", v)
require.NoError(t, err)
var d appsv1.Deployment
err = yaml.NewYAMLToJSONDecoder(&b).Decode(&d)
require.NoError(t, err)
b.Reset()
})
}
}

func (t *test) cond() metav1.Condition {
s := t.k8s.state[req().NamespacedName].(*dbv1alpha1.AtlasSchema)
return s.Status.Conditions[0]
Expand Down
Loading

0 comments on commit 21f0a08

Please sign in to comment.