-
Notifications
You must be signed in to change notification settings - Fork 0
Add Memcached Client #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Memcached Client #170
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
989ca81
Add Memcached Client
Evanraisul c52de54
Add Memcached Authentication
Evanraisul 509ff8b
Fix AuthSecret
Evanraisul 5e2bffb
Fix Memcached AuthSecret
Evanraisul 6717a99
Add client
Evanraisul 8370770
remove extra print
Evanraisul 91c7d41
update deps
Evanraisul 1812251
update deps-2
Evanraisul bbc06d9
fix changes
Evanraisul 5f99042
update changes
Evanraisul 45207f3
fix getSecret
Evanraisul cc23dee
fix tlsSecret
Evanraisul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
| package memcached | ||
|
|
||
| import ( | ||
| "github.com/kubedb/gomemcache/memcache" | ||
| ) | ||
|
|
||
| type Client struct { | ||
| *memcache.Client | ||
| } |
This file contains hidden or 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,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() | ||
Evanraisul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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.") | ||
Evanraisul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| caCert := secret.Data["ca.crt"] | ||
| clientCert := secret.Data["tls.crt"] | ||
| clientKey := secret.Data["tls.key"] | ||
Evanraisul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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() | ||
Evanraisul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| klog.Error(err, "Failed to get auth-secret") | ||
Evanraisul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return errors.New("secret is not found") | ||
| } | ||
|
|
||
| authData := string(secret.Data[kubedb.AuthDataKey]) | ||
| separatePairs := strings.Split(authData, "\n") | ||
| usernamePasswordPair := separatePairs[0] | ||
Evanraisul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| splitUsernamePassword := strings.Split(usernamePasswordPair, ":") | ||
| memcachedUserName, memcachedPassword := strings.TrimSpace(splitUsernamePassword[0]), strings.TrimSpace(splitUsernamePassword[1]) | ||
Evanraisul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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 | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.