Skip to content
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

Handle parsing mysql go driver url parsing #105

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ DOCKER_REPO_ROOT := /go/src/$(GO_PKG)/$(REPO)
.PHONY: clientset
clientset:
@docker run --rm \
-u $$(id -u):$$(id -g) \
-v /tmp:/.cache \
-v $$(pwd):$(DOCKER_REPO_ROOT) \
-w $(DOCKER_REPO_ROOT) \
Expand Down
16 changes: 11 additions & 5 deletions apis/appcatalog/v1alpha1/appbinding_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (a AppBinding) Host() (string, error) {
} else if c.URL != nil {
u, err := url.Parse(*c.URL)
if err != nil {
return "", err
return ParseMySQLHost(*c.URL)
}
return u.Host, nil
}
Expand All @@ -99,7 +99,7 @@ func (a AppBinding) Hostname() (string, error) {
} else if c.URL != nil {
u, err := url.Parse(*c.URL)
if err != nil {
return "", err
return ParseMySQLHostname(*c.URL)
}
return u.Hostname(), nil
}
Expand All @@ -111,12 +111,18 @@ func (a AppBinding) Port() (int32, error) {
if c.Service != nil { // preferred source for MYSQL app binding
return c.Service.Port, nil
} else if c.URL != nil {
var port string
u, err := url.Parse(*c.URL)
if err != nil {
return 0, err
port, err = ParseMySQLPort(*c.URL)
if err != nil {
return 0, nil
}
} else {
port = u.Port()
}
port, err := strconv.ParseInt(u.Port(), 10, 32)
return int32(port), err
result, err := strconv.ParseInt(port, 10, 32)
return int32(result), err
}
return 0, errors.New("connection url is missing")
}
Expand Down
93 changes: 93 additions & 0 deletions apis/appcatalog/v1alpha1/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
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 v1alpha1

import (
"net"
"net/url"
"strings"

"github.com/go-sql-driver/mysql"
)

/*
MySQL Go driver (https://github.com/go-sql-driver/mysql) uses a custom connection string (DSN) format.
[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
*/

func (a AppBinding) MySQLDSN() (string, error) {
dsn, err := a.URL()
if err != nil {
return "", err
}
return CanonicalMySQLDSN(dsn)
}

// CanonicalMySQLDSN will convert a regular URL into MySQL DSN format
func CanonicalMySQLDSN(dsn string) (string, error) {
_, err := mysql.ParseDSN(dsn)
if err == nil {
return dsn, nil
}

u, err := url.Parse(dsn)
if err != nil {
return "", err
}

rebuild := mysql.NewConfig()
rebuild.Net = u.Scheme
rebuild.Addr = u.Host
rebuild.DBName = strings.TrimPrefix(u.Path, "/")
if u.User != nil {
rebuild.User = u.User.Username()
if pass, found := u.User.Password(); found {
rebuild.Passwd = pass
}
}
rebuild.Params = map[string]string{}
for k, v := range u.Query() {
rebuild.Params[k] = v[0]
}
return rebuild.FormatDSN(), nil
}

func ParseMySQLHost(dsn string) (string, error) {
cfg, err := mysql.ParseDSN(dsn)
if err != nil {
return "", err
}
return cfg.Addr, err
}

func ParseMySQLHostname(dsn string) (string, error) {
cfg, err := mysql.ParseDSN(dsn)
if err != nil {
return "", err
}
host, _, err := net.SplitHostPort(cfg.Addr)
return host, err
}

func ParseMySQLPort(dsn string) (string, error) {
cfg, err := mysql.ParseDSN(dsn)
if err != nil {
return "", err
}
_, port, err := net.SplitHostPort(cfg.Addr)
return port, err
}
Loading
Loading