Skip to content

Commit 8f85204

Browse files
Add db2 client go (#206)
Signed-off-by: obaydullahmhs <obaydullah@appscode.com>
1 parent 0b61304 commit 8f85204

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

db2/client.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package db2
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/go-resty/resty/v2"
7+
"k8s.io/klog/v2"
8+
)
9+
10+
type Client struct {
11+
*resty.Client
12+
Config *Config
13+
}
14+
15+
type Config struct {
16+
host string
17+
api string
18+
username string
19+
password string
20+
connectionScheme string
21+
transport *http.Transport
22+
}
23+
24+
func (cc *Client) PingDB2() (bool, error) {
25+
req := cc.Client.R().SetDoNotParseResponse(true)
26+
res, err := req.Get("/ready")
27+
if err != nil {
28+
klog.Error(err, "Failed to send http request")
29+
return false, err
30+
}
31+
32+
return res.StatusCode() == http.StatusOK, nil
33+
}
34+
35+
func (cc *Client) ReadWriteCheck() (bool, error) {
36+
req := cc.Client.R().SetDoNotParseResponse(true)
37+
res, err := req.Get("/test")
38+
if err != nil {
39+
klog.Error(err, "Failed to send http request")
40+
return false, err
41+
}
42+
43+
return res.StatusCode() == http.StatusOK, nil
44+
}

db2/kubedb_client_builder.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package db2
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net"
7+
"net/http"
8+
"time"
9+
10+
"github.com/go-resty/resty/v2"
11+
"github.com/pkg/errors"
12+
core "k8s.io/api/core/v1"
13+
"k8s.io/apimachinery/pkg/types"
14+
"k8s.io/klog/v2"
15+
dbapi "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
16+
"sigs.k8s.io/controller-runtime/pkg/client"
17+
)
18+
19+
type KubeDBClientBuilder struct {
20+
kc client.Client
21+
db2 *dbapi.DB2
22+
url string
23+
path string
24+
podName string
25+
ctx context.Context
26+
}
27+
28+
func NewKubeDBClientBuilder(kc client.Client, db2 *dbapi.DB2) *KubeDBClientBuilder {
29+
return &KubeDBClientBuilder{
30+
kc: kc,
31+
db2: db2,
32+
}
33+
}
34+
35+
func (o *KubeDBClientBuilder) WithPod(podName string) *KubeDBClientBuilder {
36+
o.podName = podName
37+
return o
38+
}
39+
40+
func (o *KubeDBClientBuilder) WithURL(url string) *KubeDBClientBuilder {
41+
o.url = url
42+
return o
43+
}
44+
45+
func (o *KubeDBClientBuilder) WithPath(path string) *KubeDBClientBuilder {
46+
o.path = path
47+
return o
48+
}
49+
50+
func (o *KubeDBClientBuilder) WithContext(ctx context.Context) *KubeDBClientBuilder {
51+
o.ctx = ctx
52+
return o
53+
}
54+
55+
func (o *KubeDBClientBuilder) GetDB2Client() (*Client, error) {
56+
config := Config{
57+
host: o.url,
58+
api: o.path,
59+
transport: &http.Transport{
60+
IdleConnTimeout: time.Second * 3,
61+
DialContext: (&net.Dialer{
62+
Timeout: time.Second * 30,
63+
}).DialContext,
64+
},
65+
connectionScheme: "http",
66+
}
67+
68+
var username, password string
69+
// if security is enabled set database credentials in clientConfig
70+
if o.db2.Spec.AuthSecret != nil && o.db2.Spec.AuthSecret.Name != "" {
71+
secret := &core.Secret{}
72+
err := o.kc.Get(o.ctx, types.NamespacedName{
73+
Name: o.db2.Spec.AuthSecret.Name,
74+
Namespace: o.db2.GetNamespace(),
75+
}, secret)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
if value, ok := secret.Data[core.BasicAuthUsernameKey]; ok {
81+
username = string(value)
82+
} else {
83+
klog.Info(fmt.Sprintf("Failed for secret: %s/%s, username is missing", secret.Namespace, secret.Name))
84+
return nil, errors.New("username is missing")
85+
}
86+
87+
if value, ok := secret.Data[core.BasicAuthPasswordKey]; ok {
88+
password = string(value)
89+
} else {
90+
klog.Info(fmt.Sprintf("Failed for secret: %s/%s, password is missing", secret.Namespace, secret.Name))
91+
return nil, errors.New("password is missing")
92+
}
93+
94+
config.username = username
95+
config.password = password
96+
}
97+
98+
newClient := resty.New()
99+
newClient.SetTransport(config.transport).SetScheme(config.connectionScheme).SetBaseURL(config.host)
100+
newClient.SetHeader("Accept", "application/json")
101+
newClient.SetBasicAuth(config.username, config.password)
102+
newClient.SetTimeout(time.Second * 30)
103+
newClient.SetDisableWarn(true)
104+
105+
return &Client{
106+
Client: newClient,
107+
Config: &config,
108+
}, nil
109+
}

0 commit comments

Comments
 (0)