|
| 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