/
target.go
133 lines (126 loc) · 3.9 KB
/
target.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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.
package v1alpha1
import (
"context"
"fmt"
"net/url"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type (
// TargetSpec defines the target database to manage.
TargetSpec struct {
// URL of the target database schema.
URL string `json:"url,omitempty"`
// URLs may be defined as a secret key reference.
URLFrom Secret `json:"urlFrom,omitempty"`
// Credentials defines the credentials to use when connecting to the database.
// Used instead of URL or URLFrom.
Credentials Credentials `json:"credentials,omitempty"`
}
// Credentials defines the credentials to use when connecting to the database.
Credentials struct {
Scheme string `json:"scheme,omitempty"`
User string `json:"user,omitempty"`
UserFrom Secret `json:"userFrom,omitempty"`
Password string `json:"password,omitempty"`
PasswordFrom Secret `json:"passwordFrom,omitempty"`
Host string `json:"host,omitempty"`
HostFrom Secret `json:"hostFrom,omitempty"`
Port int `json:"port,omitempty"`
Database string `json:"database,omitempty"`
Parameters map[string]string `json:"parameters,omitempty"`
}
// Secret defines a secret key reference.
Secret struct {
// SecretKeyRef defines the secret key reference to use for the user.
SecretKeyRef *corev1.SecretKeySelector `json:"secretKeyRef,omitempty"`
}
)
// DatabaseURL returns the database url.
func (s TargetSpec) DatabaseURL(ctx context.Context, r client.Reader, ns string) (*url.URL, error) {
if s.URLFrom.SecretKeyRef != nil {
val, err := getSecrectValue(ctx, r, ns, s.URLFrom.SecretKeyRef)
if err != nil {
return nil, err
}
return url.Parse(val)
}
if s.URL != "" {
return url.Parse(s.URL)
}
if s.Credentials.UserFrom.SecretKeyRef != nil {
val, err := getSecrectValue(ctx, r, ns, s.Credentials.UserFrom.SecretKeyRef)
if err != nil {
return nil, err
}
s.Credentials.User = val
}
if s.Credentials.PasswordFrom.SecretKeyRef != nil {
val, err := getSecrectValue(ctx, r, ns, s.Credentials.PasswordFrom.SecretKeyRef)
if err != nil {
return nil, err
}
s.Credentials.Password = val
}
if s.Credentials.HostFrom.SecretKeyRef != nil {
val, err := getSecrectValue(ctx, r, ns, s.Credentials.HostFrom.SecretKeyRef)
if err != nil {
return nil, err
}
s.Credentials.Host = val
}
if s.Credentials.Host != "" {
return s.Credentials.URL(), nil
}
return nil, fmt.Errorf("no target database defined")
}
// URL returns the URL for the database.
func (c *Credentials) URL() *url.URL {
u := &url.URL{
Scheme: c.Scheme,
Path: c.Database,
}
if c.User != "" || c.Password != "" {
u.User = url.UserPassword(c.User, c.Password)
}
if len(c.Parameters) > 0 {
qs := url.Values{}
for k, v := range c.Parameters {
qs.Set(k, v)
}
u.RawQuery = qs.Encode()
}
host := c.Host
if c.Port > 0 {
host = fmt.Sprintf("%s:%d", host, c.Port)
}
u.Host = host
return u
}
func getSecrectValue(
ctx context.Context,
r client.Reader,
ns string,
ref *corev1.SecretKeySelector,
) (string, error) {
val := &corev1.Secret{}
err := r.Get(ctx, types.NamespacedName{Name: ref.Name, Namespace: ns}, val)
if err != nil {
return "", err
}
return string(val.Data[ref.Key]), nil
}