-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Kafka Rest Proxy Signed-off-by: obaydullahmhs <[email protected]>
- Loading branch information
1 parent
495ccff
commit 49bebb7
Showing
33 changed files
with
6,508 additions
and
874 deletions.
There are no files selected for viewing
This file contains 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 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 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,98 @@ | ||
/* | ||
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 restproxy | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/pkg/errors" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type Client struct { | ||
*resty.Client | ||
Config *Config | ||
} | ||
|
||
type Response struct { | ||
Code int | ||
Header http.Header | ||
Body io.ReadCloser | ||
} | ||
|
||
type ResponseBody struct { | ||
Brokers []int `json:"brokers"` | ||
} | ||
|
||
type Config struct { | ||
host string | ||
api string | ||
connectionScheme string | ||
transport *http.Transport | ||
} | ||
|
||
func (cc *Client) GetKafkaBrokerList() (*Response, error) { | ||
req := cc.Client.R().SetDoNotParseResponse(true) | ||
res, err := req.Get(cc.Config.api) | ||
if err != nil { | ||
klog.Error(err, "Failed to send http request") | ||
return nil, err | ||
} | ||
response := &Response{ | ||
Code: res.StatusCode(), | ||
Header: res.Header(), | ||
Body: res.RawBody(), | ||
} | ||
|
||
if response.Code != http.StatusOK { | ||
return response, fmt.Errorf("failed to get broker list from rest proxy") | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// IsBrokerAvailableForRequest parse health response in json from server and | ||
// return overall status of the server | ||
func (cc *Client) IsBrokerAvailableForRequest(response *Response) (bool, error) { | ||
defer func(Body io.ReadCloser) { | ||
err := Body.Close() | ||
if err != nil { | ||
err1 := errors.Wrap(err, "failed to parse response body") | ||
if err1 != nil { | ||
return | ||
} | ||
return | ||
} | ||
}(response.Body) | ||
|
||
var responseBody ResponseBody | ||
body, _ := io.ReadAll(response.Body) | ||
err := json.Unmarshal(body, &responseBody) | ||
if err != nil { | ||
return false, errors.Wrap(err, "Failed to parse response body") | ||
} | ||
|
||
if len(responseBody.Brokers) == 0 { | ||
return false, fmt.Errorf("no brokers found to serve request with rest proxy") | ||
} | ||
|
||
return true, nil | ||
} |
This file contains 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,96 @@ | ||
/* | ||
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 restproxy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
kapi "kubedb.dev/apimachinery/apis/kafka/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type KubeDBClientBuilder struct { | ||
kc client.Client | ||
restproxy *kapi.RestProxy | ||
url string | ||
path string | ||
podName string | ||
ctx context.Context | ||
} | ||
|
||
func NewKubeDBClientBuilder(kc client.Client, restproxy *kapi.RestProxy) *KubeDBClientBuilder { | ||
return &KubeDBClientBuilder{ | ||
kc: kc, | ||
restproxy: restproxy, | ||
url: GetConnectionURL(restproxy), | ||
} | ||
} | ||
|
||
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) WithPath(path string) *KubeDBClientBuilder { | ||
o.path = path | ||
return o | ||
} | ||
|
||
func (o *KubeDBClientBuilder) WithContext(ctx context.Context) *KubeDBClientBuilder { | ||
o.ctx = ctx | ||
return o | ||
} | ||
|
||
func (o *KubeDBClientBuilder) GetRestProxyClient() (*Client, error) { | ||
config := Config{ | ||
host: o.url, | ||
api: o.path, | ||
transport: &http.Transport{ | ||
IdleConnTimeout: time.Second * 3, | ||
DialContext: (&net.Dialer{ | ||
Timeout: time.Second * 30, | ||
}).DialContext, | ||
}, | ||
connectionScheme: o.restproxy.GetConnectionScheme(), | ||
} | ||
|
||
newClient := resty.New() | ||
newClient.SetTransport(config.transport).SetScheme(config.connectionScheme).SetBaseURL(config.host) | ||
newClient.SetHeader("Accept", "application/vnd.kafka.json.v2+json") | ||
newClient.SetTimeout(time.Second * 30) | ||
newClient.SetDisableWarn(true) | ||
|
||
return &Client{ | ||
Client: newClient, | ||
Config: &config, | ||
}, nil | ||
} | ||
|
||
func GetConnectionURL(restproxy *kapi.RestProxy) string { | ||
return fmt.Sprintf("%s://%s.%s.svc:%d", restproxy.GetConnectionScheme(), restproxy.ServiceName(), restproxy.Namespace, kapi.RestProxyRESTPort) | ||
|
||
} |
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.