Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/go-sql-driver/mysql v1.9.0
github.com/gocql/gocql v1.6.0
github.com/grafadruid/go-druid v0.0.6
github.com/kubedb/gomemcache v0.0.0-20240930104810-7694d68c3e21
github.com/lib/pq v1.10.7
github.com/michaelklishin/rabbit-hole/v3 v3.1.0
github.com/microsoft/go-mssqldb v1.6.0
Expand All @@ -34,7 +35,7 @@ require (
k8s.io/klog/v2 v2.130.1
kmodules.xyz/client-go v0.32.1
kmodules.xyz/custom-resources v0.32.0
kubedb.dev/apimachinery v0.53.0
kubedb.dev/apimachinery v0.53.1-0.20250325061150-ba449cf3bc49
sigs.k8s.io/controller-runtime v0.20.3
xorm.io/xorm v1.3.6
)
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubedb/gomemcache v0.0.0-20240930104810-7694d68c3e21 h1:5DhgzM4z088xe+G6+PS+LzjOFyjis+aVZelglkMlZp0=
github.com/kubedb/gomemcache v0.0.0-20240930104810-7694d68c3e21/go.mod h1:FSZMy/DY5gr7zUXkxt66DdA9dUtbjlB0aJb+sYwRaEg=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down Expand Up @@ -659,8 +661,8 @@ kmodules.xyz/monitoring-agent-api v0.32.0 h1:cMQbWvbTc4JWeLI/zYE0HLefsdFYBzqvATL
kmodules.xyz/monitoring-agent-api v0.32.0/go.mod h1:zgRKiJcuK7FOHy0Y1TsONRbJfgnPCs8t4Zh/6Afr+yU=
kmodules.xyz/offshoot-api v0.32.0 h1:gogc5scSZe2JoXtZof72UGRl3Tit0kFaFRMkLLT1D8o=
kmodules.xyz/offshoot-api v0.32.0/go.mod h1:tled7OxYZ3SkUJcrVFVVYyd+zXjsRSEm1R6Q3k4gcx0=
kubedb.dev/apimachinery v0.53.0 h1:uxgf/Kc27/A87/oZd+TjddAC2IS5C3ubZGiRBVqu0cc=
kubedb.dev/apimachinery v0.53.0/go.mod h1:CSK+s+3FJcqJv7tx8R1VKDOUMa7bbruOQ9yk7U9dmMo=
kubedb.dev/apimachinery v0.53.1-0.20250325061150-ba449cf3bc49 h1:NYtX/Dh/PeEC7WWGEc+QFF3vo7onKGmxIaytB3dfvYk=
kubedb.dev/apimachinery v0.53.1-0.20250325061150-ba449cf3bc49/go.mod h1:CSK+s+3FJcqJv7tx8R1VKDOUMa7bbruOQ9yk7U9dmMo=
kubeops.dev/petset v0.0.10 h1:sNaqmHrD9bW7pcrWnwPoiQrKvdRwRX0BaRQc5QA78Bg=
kubeops.dev/petset v0.0.10/go.mod h1:uHL83kggwmtSxdlIfxNbY2isV22iYV6YjADv0y+Z7YA=
kubeops.dev/sidekick v0.0.11 h1:OydXdIH6cYSiWxKIWvrywk95WhhHSERkc7RNPOmTekc=
Expand Down
9 changes: 9 additions & 0 deletions memcached/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package memcached

import (
"github.com/kubedb/gomemcache/memcache"
)

type Client struct {
*memcache.Client
}
152 changes: 152 additions & 0 deletions memcached/kubedb_client_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
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 memcached

import (
"context"
"crypto/tls"
"crypto/x509"
"net"
"strings"
"time"

"github.com/pkg/errors"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"kubedb.dev/apimachinery/apis/kubedb"
dbapi "kubedb.dev/apimachinery/apis/kubedb/v1"

"github.com/kubedb/gomemcache/memcache"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type KubeDBClientBuilder struct {
kc client.Client
Client kubernetes.Interface
db *dbapi.Memcached
podName string
url string
database int
}

func NewKubeDBClientBuilder(kc client.Client, db *dbapi.Memcached) *KubeDBClientBuilder {
return &KubeDBClientBuilder{
kc: kc,
db: db,
}
}

func (o *KubeDBClientBuilder) WithPod(podName string) *KubeDBClientBuilder {
o.podName = podName
return o
}

func (o *KubeDBClientBuilder) WithURL(url string) *KubeDBClientBuilder {
o.url = url
return o
}

func (o *KubeDBClientBuilder) WithDatabase(database int) *KubeDBClientBuilder {
o.database = database
return o
}

func (o *KubeDBClientBuilder) GetMemcachedClient() (*Client, error) {
mcClient := memcache.New(o.db.Address())
if o.db.Spec.TLS != nil {
// Secret for Memcached Client Certs
secret, err := o.GetSecret()
if err != nil {
klog.Error(err, "Failed to get auth-secret")
return nil, errors.New("secret is not found")
}

if secret.Data["ca.crt"] == nil || secret.Data["tls.crt"] == nil || secret.Data["tls.key"] == nil {
return nil, errors.New("invalid auth-secret. Certificates not found.")
}

caCert := secret.Data["ca.crt"]
clientCert := secret.Data["tls.crt"]
clientKey := secret.Data["tls.key"]

caCertPool := x509.NewCertPool()
if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
klog.Infoln("Failed to append CA certificate to the pool")
}

// Load client certificate
clientCertificate, err := tls.X509KeyPair(clientCert, clientKey)
if err != nil {
klog.Errorf("Failed to load client certificate: %v", err)
}
// Create TLS configuration
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{clientCertificate},
RootCAs: caCertPool,
InsecureSkipVerify: false, // Ensure server's cert is verified
}
// Override the dialer to use TLS by setting the DialContext function
mcClient.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return tls.DialWithDialer(&net.Dialer{
Timeout: 10 * time.Second,
}, "tcp", o.db.Address(), tlsConfig)
}
}

return &Client{
mcClient,
}, nil
}

func (o *KubeDBClientBuilder) SetAuth(mcClient *Client) error {
secret, err := o.GetSecret()
if err != nil {
klog.Error(err, "Failed to get auth-secret")
return errors.New("secret is not found")
}

authData := string(secret.Data[kubedb.AuthDataKey])
separatePairs := strings.Split(authData, "\n")
usernamePasswordPair := separatePairs[0]

splitUsernamePassword := strings.Split(usernamePasswordPair, ":")
memcachedUserName, memcachedPassword := strings.TrimSpace(splitUsernamePassword[0]), strings.TrimSpace(splitUsernamePassword[1])

err = mcClient.SetAuth(&memcache.Item{
Key: kubedb.MemcachedHealthKey, Flags: 0, Expiration: 0, User: memcachedUserName, Pass: memcachedPassword,
})
if err != nil {
klog.Errorf("Authentication Error: %v", err.Error())
} else {
klog.V(5).Infof("Authentication Done Successfully !!...")
}
return nil
}

func (o *KubeDBClientBuilder) GetSecret() (*core.Secret, error) {
var authSecret core.Secret
err := o.kc.Get(context.TODO(), types.NamespacedName{
Name: o.db.GetMemcachedAuthSecretName(),
Namespace: o.db.Namespace,
}, &authSecret)
if err != nil {
return nil, err
}
return &authSecret, nil
}
9 changes: 9 additions & 0 deletions vendor/github.com/kubedb/gomemcache/AUTHORS

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

Loading
Loading