-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pgpool db-client Signed-off-by: MobarakHsn <[email protected]> * Pgpool db client with latest pgpool api Signed-off-by: MobarakHsn <[email protected]> * Apimachinary updated Signed-off-by: MobarakHsn <[email protected]> * Pgpool auth problem Signed-off-by: MobarakHsn <[email protected]> * Add pgpool db client Signed-off-by: MobarakHsn <[email protected]> * Fixed pgpool db-client Signed-off-by: MobarakHsn <[email protected]> --------- Signed-off-by: MobarakHsn <[email protected]>
- Loading branch information
1 parent
6016257
commit 7f4d584
Showing
9 changed files
with
206 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
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 pgpool | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
type Client struct { | ||
*sql.DB | ||
} | ||
|
||
type XormClient struct { | ||
*xorm.Engine | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
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 pgpool | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
_ "github.com/lib/pq" | ||
core "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
appbinding "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1" | ||
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"xorm.io/xorm" | ||
) | ||
|
||
const ( | ||
DefaultBackendDBName = "postgres" | ||
DefaultPgpoolPort = 9999 | ||
TLSModeDisable = "disable" | ||
) | ||
|
||
type KubeDBClientBuilder struct { | ||
kc client.Client | ||
pgpool *api.Pgpool | ||
url string | ||
podName string | ||
backendDBName string | ||
ctx context.Context | ||
} | ||
|
||
func NewKubeDBClientBuilder(kc client.Client, pp *api.Pgpool) *KubeDBClientBuilder { | ||
return &KubeDBClientBuilder{ | ||
kc: kc, | ||
pgpool: pp, | ||
} | ||
} | ||
|
||
func (o *KubeDBClientBuilder) WithURL(url string) *KubeDBClientBuilder { | ||
o.url = url | ||
return o | ||
} | ||
|
||
func (o *KubeDBClientBuilder) WithPod(podName string) *KubeDBClientBuilder { | ||
o.podName = podName | ||
return o | ||
} | ||
|
||
func (o *KubeDBClientBuilder) WithPgpoolDB(pgDB string) *KubeDBClientBuilder { | ||
o.backendDBName = pgDB | ||
return o | ||
} | ||
|
||
func (o *KubeDBClientBuilder) WithContext(ctx context.Context) *KubeDBClientBuilder { | ||
o.ctx = ctx | ||
return o | ||
} | ||
|
||
func (o *KubeDBClientBuilder) GetPgpoolXormClient() (*XormClient, error) { | ||
if o.ctx == nil { | ||
o.ctx = context.Background() | ||
} | ||
|
||
connector, err := o.getConnectionString() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
engine, err := xorm.NewEngine("postgres", connector) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = engine.Query("SELECT 1") | ||
if err != nil { | ||
err = engine.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return nil, err | ||
} | ||
|
||
engine.SetDefaultContext(o.ctx) | ||
return &XormClient{ | ||
engine, | ||
}, nil | ||
} | ||
|
||
func (o *KubeDBClientBuilder) getURL() string { | ||
return fmt.Sprintf("%s.%s.%s.svc", o.podName, o.pgpool.GoverningServiceName(), o.pgpool.Namespace) | ||
} | ||
|
||
func (o *KubeDBClientBuilder) getBackendAuth() (string, string, error) { | ||
pp := o.pgpool | ||
var secretName string | ||
if pp.Spec.Backend != nil { | ||
apb := &appbinding.AppBinding{} | ||
err := o.kc.Get(o.ctx, types.NamespacedName{ | ||
Name: pp.Spec.Backend.Name, | ||
Namespace: pp.Namespace, | ||
}, apb) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
if apb.Spec.Secret == nil { | ||
return "", "", fmt.Errorf("backend database auth secret not found") | ||
} | ||
secretName = apb.Spec.Secret.Name | ||
} | ||
var secret core.Secret | ||
err := o.kc.Get(o.ctx, client.ObjectKey{Namespace: pp.Namespace, Name: secretName}, &secret) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
user, ok := secret.Data[core.BasicAuthUsernameKey] | ||
if !ok { | ||
return "", "", fmt.Errorf("error getting backend username") | ||
} | ||
pass, ok := secret.Data[core.BasicAuthPasswordKey] | ||
if !ok { | ||
return "", "", fmt.Errorf("error getting backend password") | ||
} | ||
return string(user), string(pass), nil | ||
} | ||
|
||
func (o *KubeDBClientBuilder) getConnectionString() (string, error) { | ||
user, pass, err := o.getBackendAuth() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if o.podName != "" { | ||
o.url = o.getURL() | ||
} | ||
|
||
if o.backendDBName == "" { | ||
o.backendDBName = DefaultBackendDBName | ||
} | ||
//TODO ssl mode is disable now need to work on this after adding tls support | ||
connector := fmt.Sprintf("user=%s password=%s host=%s port=%d connect_timeout=10 dbname=%s sslmode=%s", user, pass, o.url, DefaultPgpoolPort, o.backendDBName, TLSModeDisable) | ||
return connector, nil | ||
} |
4 changes: 2 additions & 2 deletions
4
vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/openapi_generated.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3531,6 +3531,7 @@ spec: | |
version: | ||
type: string | ||
required: | ||
- backend | ||
- version | ||
type: object | ||
status: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters