Skip to content

Commit

Permalink
Add Kafka Rest Proxy
Browse files Browse the repository at this point in the history
Signed-off-by: obaydullahmhs <[email protected]>
  • Loading branch information
obaydullahmhs committed Jul 30, 2024
1 parent 495ccff commit abeb011
Show file tree
Hide file tree
Showing 14 changed files with 4,475 additions and 1 deletion.
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,13 @@ kmodules.xyz/monitoring-agent-api v0.29.0 h1:gpFl6OZrlMLb/ySMHdREI9EwGtnJ91oZBn9
kmodules.xyz/monitoring-agent-api v0.29.0/go.mod h1:iNbvaMTgVFOI5q2LJtGK91j4Dmjv4ZRiRdasGmWLKQI=
kmodules.xyz/offshoot-api v0.30.0 h1:dq9F93pu4Q8rL9oTcCk+vGGy8vpS7RNt0GSwx7Bvhec=
kmodules.xyz/offshoot-api v0.30.0/go.mod h1:o9VoA3ImZMDBp3lpLb8+kc2d/KBxioRwCpaKDfLIyDw=
<<<<<<< HEAD
kubedb.dev/apimachinery v0.47.0-rc.1.0.20240717082707-f8438b7e77c7 h1:HqdKBHKbi5fhHRHstoDggFXpX6v7r+2P8/XevDdHjeg=
kubedb.dev/apimachinery v0.47.0-rc.1.0.20240717082707-f8438b7e77c7/go.mod h1:Gs/kwdVYmGjJmYmvCUNDmNbbprXqi/gbSj/JrsoM9sE=
=======
kubedb.dev/apimachinery v0.47.0-rc.1.0.20240718040433-0ab49fcf1584 h1:DXD8CxBJh25AwVYIB+Q2nWKSYqFMzIV2cuqAo5I+AXg=
kubedb.dev/apimachinery v0.47.0-rc.1.0.20240718040433-0ab49fcf1584/go.mod h1:Gs/kwdVYmGjJmYmvCUNDmNbbprXqi/gbSj/JrsoM9sE=
>>>>>>> 298cc3fb (Add Kafka Rest Proxy)
kubeops.dev/petset v0.0.6 h1:0IbvxD9fadZfH+3iMZWzN6ZHsO0vX458JlioamwyPKQ=
kubeops.dev/petset v0.0.6/go.mod h1:A15vh0r979NsvL65DTIZKWsa/NoX9VapHBAEw1ZsdYI=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
Expand Down
99 changes: 99 additions & 0 deletions kafka/restproxy/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
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 {
Status string `json:"status"`
Brokers []int `json:"checks"`
}

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
}
96 changes: 96 additions & 0 deletions kafka/restproxy/kubedb_client_builder.go
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)

}
7 changes: 6 additions & 1 deletion kafka/schemaregistry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package schemaregistry

import (
"encoding/json"
"fmt"
"io"
"net/http"

Expand Down Expand Up @@ -92,5 +93,9 @@ func (cc *Client) IsSchemaRegistryHealthy(response *Response) (bool, error) {
return false, errors.Wrap(err, "Failed to parse response body")
}

return responseBody.Status == "UP", nil
if responseBody.Status != "UP" {
return false, fmt.Errorf("schema registry is not healthy")
}

return true, nil
}
10 changes: 10 additions & 0 deletions vendor/kubedb.dev/apimachinery/apis/kafka/v1alpha1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,13 @@ const (
SchemaRegistryOperatorVolumeConfig = "registry-operator-config"
SchemaRegistryOperatorConfigPath = "/deployments/config"
)

// RestProxy constants

const (
RestProxyPrimaryPortName = "primary"
RestProxyPortName = "restproxy"
RestProxyRESTPort = 8082
RestProxyContainerName = "rest-proxy"
RestProxyConfigFileName = "karapace.config.json"
)
Loading

0 comments on commit abeb011

Please sign in to comment.