Skip to content

Commit

Permalink
devdb: add conntmpl-go to build DEV_URL using kubectl command
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Oct 9, 2024
1 parent 7579c1b commit 1c71271
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
33 changes: 23 additions & 10 deletions internal/controller/devdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"net/url"
"os"
"slices"
Expand All @@ -42,9 +43,10 @@ import (
)

const (
annoConnTmpl = "atlasgo.io/conntmpl"
labelEngine = "atlasgo.io/engine"
labelInstance = "app.kubernetes.io/instance"
annoConnTmpl = "atlasgo.io/conntmpl"
annoConnTmplGo = "atlasgo.io/conntmpl-go"
labelEngine = "atlasgo.io/engine"
labelInstance = "app.kubernetes.io/instance"
)

type (
Expand Down Expand Up @@ -197,8 +199,7 @@ type devDB struct {
SchemaBound bool
}

// ConnTmpl returns a connection template for the devDB.
func (d *devDB) ConnTmpl() string {
func (d *devDB) connAnnotations() map[string]string {
u := url.URL{
Scheme: d.Driver,
User: url.UserPassword(d.User, d.Pass),
Expand All @@ -216,11 +217,14 @@ func (d *devDB) ConnTmpl() string {
}
}
u.RawQuery = q.Encode()

return u.String()
s := u.String()
return map[string]string{
annoConnTmpl: s,
annoConnTmplGo: strings.Replace(s, "localhost", "%s", 1),
}
}

func (d *devDB) Render(w io.Writer) error {
func (d *devDB) render(w io.Writer) error {
return tmpl.ExecuteTemplate(w, "devdb.tmpl", d)
}

Expand Down Expand Up @@ -253,15 +257,16 @@ func deploymentDevDB(name types.NamespacedName, drv string, schemaBound bool) (*
return nil, fmt.Errorf("unsupported driver %q", v.Driver)
}
b := &bytes.Buffer{}
if err := v.Render(b); err != nil {
if err := v.render(b); err != nil {
return nil, err
}
d := &appsv1.Deployment{}
if err := yaml.NewYAMLToJSONDecoder(b).Decode(d); err != nil {
return nil, err
}
s := &d.Spec
if drv == "sqlserver" {
c := &d.Spec.Template.Spec.Containers[0]
c := &s.Template.Spec.Containers[0]
if v := os.Getenv("MSSQL_ACCEPT_EULA"); v != "" {
c.Env = append(c.Env, corev1.EnvVar{
Name: "ACCEPT_EULA",
Expand All @@ -275,6 +280,14 @@ func deploymentDevDB(name types.NamespacedName, drv string, schemaBound bool) (*
})
}
}
maps.Copy(s.Template.Labels, map[string]string{
labelEngine: drv,
labelInstance: name.Name,
})
s.Selector = &metav1.LabelSelector{
MatchLabels: s.Template.Labels,
}
s.Template.Annotations = v.connAnnotations()
return d, nil
}

Expand Down
11 changes: 0 additions & 11 deletions internal/controller/templates/devdb.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ metadata:
name: {{ .Name }}
namespace: {{ .Namespace }}
spec:
selector:
matchLabels:
"app.kubernetes.io/name": "atlas-dev-db"
"app.kubernetes.io/instance": "{{ .Name }}"
"app.kubernetes.io/part-of": "atlas-operator"
"app.kubernetes.io/created-by": "controller-manager"
"atlasgo.io/engine": "{{ .Driver }}"
replicas: 1
template:
metadata:
Expand All @@ -19,9 +12,6 @@ spec:
"app.kubernetes.io/instance": "{{ .Name }}"
"app.kubernetes.io/part-of": "atlas-operator"
"app.kubernetes.io/created-by": "controller-manager"
"atlasgo.io/engine": "{{ .Driver }}"
annotations:
"atlasgo.io/conntmpl": "{{ .ConnTmpl }}"
spec:
{{- if ne .Driver "sqlserver" }}
securityContext:
Expand Down Expand Up @@ -64,4 +54,3 @@ spec:
ports:
- containerPort: {{ .Port }}
name: {{ .Driver }}

36 changes: 33 additions & 3 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,27 @@ func TestOperator(t *testing.T) {
_, err = kind("kubectl", "rollout", "status",
"-n", nsController, controller, "--timeout", "2m")
require.NoError(t, err)
// Wait for the old pods to be deleted
_, err = kind("sleep", "5s")
require.NoError(t, err)
// Getting the controller-manager pod name
output, err := kind("kubectl", "wait", "pod",
"-n", nsController,
"-l", "control-plane=controller-manager",
"--for", "condition=Ready",
"-o", "go-template",
"--template", "{{.metadata.name}}",
)
require.NoError(t, err)
pods := strings.Split(output, "\n")
require.Len(t, pods, 1, "expected one controller pod")
// Running the test script
testscript.Run(t, testscript.Params{
Dir: filepath.Join("testdata", "atlas-schema"),
Setup: func(e *testscript.Env) (err error) {
e.Setenv("CONTROLLER_NS", nsController)
e.Setenv("CONTROLLER", controller)
e.Setenv("CONTROLLER_POD", pods[0])
// Sharing the atlas token with the test
e.Setenv("ATLAS_TOKEN", os.Getenv("ATLAS_TOKEN"))
// Ensure the test in running in the right kube context
Expand Down Expand Up @@ -123,9 +138,7 @@ func TestOperator(t *testing.T) {
},
// kubectl runs kubectl with the namespace set to the test namespace
"kubectl": func(ts *testscript.TestScript, neg bool, args []string) {
err := ts.Exec("kubectl", append([]string{
"--namespace", ts.Getenv("NAMESPACE"),
}, args...)...)
err := ts.Exec("kubectl", append([]string{"-n", ts.Getenv("NAMESPACE")}, args...)...)
if !neg {
ts.Check(err)
} else if err == nil {
Expand Down Expand Up @@ -157,6 +170,23 @@ func TestOperator(t *testing.T) {
ts.Setenv(vals[0], ts.ReadFile(vals[1]))
}
},
// devdb finds the devdb pod and prints the connection string
"devdb": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! devdb")
}
if len(args) != 1 {
ts.Fatalf("usage: devdb resource")
}
err := ts.Exec("kubectl", "get", "pods",
"-n", ts.Getenv("NAMESPACE"),
"-l", fmt.Sprintf("app.kubernetes.io/instance=%s-atlas-dev-db", args[0]),
"--field-selector", "status.phase=Running",
"-o", "go-template",
"--template", `{{with (index .items 0)}}{{printf (index .metadata.annotations "atlasgo.io/conntmpl-go") .status.podIP}}{{end}}`,
)
ts.Check(err)
},
},
})
}
Expand Down

0 comments on commit 1c71271

Please sign in to comment.