Skip to content

Commit

Permalink
api: add dev url from secret (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotemtam authored Aug 6, 2023
1 parent 617c178 commit 3cb77a8
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/v1alpha1/atlasschema_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ type (
// DevURL is the URL of the database to use for normalization and calculations.
// If not specified, the operator will spin up a temporary database container to use for these operations.
DevURL string `json:"devURL"`
// DevURLFrom is a reference to a secret containing the URL of the database to use for normalization and calculations.
// +optional
DevURLFrom URLFrom `json:"devURLFrom,omitempty"`
// Exclude a list of glob patterns used to filter existing resources being taken into account.
Exclude []string `json:"exclude,omitempty"`
// Policy defines the policies to apply when managing the schema change lifecycle.
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions charts/atlas-operator/templates/crds/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,31 @@ spec:
and calculations. If not specified, the operator will spin up a
temporary database container to use for these operations.
type: string
devURLFrom:
description: DevURLFrom is a reference to a secret containing the
URL of the database to use for normalization and calculations.
properties:
secretKeyRef:
description: SecretKeyRef references to the key of a secret in
the same namespace.
properties:
key:
description: The key of the secret to select from. Must be
a valid secret key.
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
optional:
description: Specify whether the Secret or its key must be
defined
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
exclude:
description: Exclude a list of glob patterns used to filter existing
resources being taken into account.
Expand Down
25 changes: 25 additions & 0 deletions config/crd/bases/db.atlasgo.io_atlasschemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ spec:
and calculations. If not specified, the operator will spin up a
temporary database container to use for these operations.
type: string
devURLFrom:
description: DevURLFrom is a reference to a secret containing the
URL of the database to use for normalization and calculations.
properties:
secretKeyRef:
description: SecretKeyRef references to the key of a secret in
the same namespace.
properties:
key:
description: The key of the secret to select from. Must be
a valid secret key.
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
optional:
description: Specify whether the Secret or its key must be
defined
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
exclude:
description: Exclude a list of glob patterns used to filter existing
resources being taken into account.
Expand Down
25 changes: 24 additions & 1 deletion controllers/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ func (r *AtlasSchemaReconciler) watch(sc *dbv1alpha1.AtlasSchema) {
sc.NamespacedName(),
)
}
if s := sc.Spec.DevURLFrom.SecretKeyRef; s != nil {
r.secretWatcher.Watch(
types.NamespacedName{Name: s.Name, Namespace: sc.Namespace},
sc.NamespacedName(),
)
}
}

// Clean up any resources created by the controller
Expand Down Expand Up @@ -434,11 +440,21 @@ func (r *AtlasSchemaReconciler) extractManaged(ctx context.Context, sc *dbv1alph
r.recorder.Eventf(sc, corev1.EventTypeWarning, "DatabaseURL", err.Error())
return nil, transient(err)
}
if sc.Spec.DevURL != "" {
switch spec := sc.Spec; {
case spec.DevURL != "":
devURL, err = url.Parse(sc.Spec.DevURL)
if err != nil {
return nil, err
}
case spec.DevURLFrom.SecretKeyRef != nil:
v, err := getSecretValue(ctx, r, sc.Namespace, *spec.DevURLFrom.SecretKeyRef)
if err != nil {
return nil, err
}
devURL, err = url.Parse(v)
if err != nil {
return nil, err
}
}
return &managed{
desired: string(data),
Expand Down Expand Up @@ -479,6 +495,13 @@ func (d *managed) schemaBound() bool {
// This value is false if the target database is sqlite or if the the user explicitly
// provided a url to a dev database.
func (d *managed) needsDevDB() bool {
switch {
case d.driver == "sqlite":
return false
case d.devURL != nil:
return false

}
if d.driver == "sqlite" {
return false
}
Expand Down
29 changes: 29 additions & 0 deletions controllers/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,35 @@ func TestReconcile_CustomDevURL(t *testing.T) {
require.EqualValues(t, "mysql://dev", runs[1].DevURL)
}

func TestReconcile_CustomDevURL_Secret(t *testing.T) {
tt := newTest(t)
sc := conditionReconciling()
tt.k8s.put(&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "devdb",
Namespace: "test",
},
Data: map[string][]byte{
"url": []byte("mysql://dev"),
},
})
sc.Spec.DevURLFrom = dbv1alpha1.URLFrom{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "url",
LocalObjectReference: corev1.LocalObjectReference{
Name: "devdb",
},
},
}
tt.k8s.put(sc)
request := req()
_, err := tt.r.Reconcile(context.Background(), request)
require.NoError(t, err)
runs := tt.mockCLI().applyRuns
require.EqualValues(t, "mysql://dev", runs[0].DevURL)
require.EqualValues(t, "mysql://dev", runs[1].DevURL)
}

func TestReconcile_HasSchemaAndDB(t *testing.T) {
tt := newTest(t)
sc := conditionReconciling()
Expand Down

0 comments on commit 3cb77a8

Please sign in to comment.