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

Refactor HTTP and Patch Operations For Schema,Table and Tenant Controller #33

Merged
merged 2 commits into from
May 1, 2023
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
21 changes: 6 additions & 15 deletions api/v1beta1/pinotschema_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,14 @@ type PinotSchemaSpec struct {
PinotSchemaJson string `json:"schema.json"`
}

type PinotSchemaConditionType string

const (
PinotSchemaCreateSuccess PinotSchemaConditionType = "PinotSchemaCreateSuccess"
PinotSchemaUpdateSuccess PinotSchemaConditionType = "PinotSchemaUpdateSuccess"
PinotSchemaCreateFail PinotSchemaConditionType = "PinotSchemaCreateFail"
PinotSchemaUpdateFail PinotSchemaConditionType = "PinotSchemaUpdateFail"
)

// PinotSchemaStatus defines the observed state of PinotSchema
type PinotSchemaStatus struct {
Type PinotSchemaConditionType `json:"type,omitempty"`
Status v1.ConditionStatus `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
LastUpdateTime string `json:"lastUpdateTime,omitempty"`
CurrentSchemasJson string `json:"currentSchemas.json"`
Type string `json:"type,omitempty"`
Status v1.ConditionStatus `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
LastUpdateTime metav1.Time `json:"lastUpdateTime,omitempty"`
CurrentSchemasJson string `json:"currentSchemas.json"`
}

// +kubebuilder:object:root=true
Expand Down
23 changes: 8 additions & 15 deletions api/v1beta1/pinottable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,18 @@ type PinotTableSpec struct {
PinotTableType PinotTableType `json:"pinotTableType"`
// +required
PinotTablesJson string `json:"tables.json"`
// +optional
SegmentReload bool `json:"segmentReload"`
}

type PinotTableConditionType string

const (
PinotTableCreateSuccess PinotSchemaConditionType = "PinotTableCreateSuccess"
PinotTableUpdateSuccess PinotSchemaConditionType = "PinotTableUpdateSuccess"
PinotTableCreateFail PinotSchemaConditionType = "PinotTableCreateFail"
PinotTableUpdateFail PinotSchemaConditionType = "PinotTableUpdateFail"
)

// PinotTableStatus defines the observed state of PinotTable
type PinotTableStatus struct {
Type PinotTableConditionType `json:"type,omitempty"`
Status v1.ConditionStatus `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
LastUpdateTime string `json:"lastUpdateTime,omitempty"`
CurrentTableJson string `json:"currentTable.json"`
Type string `json:"type,omitempty"`
Status v1.ConditionStatus `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
LastUpdateTime metav1.Time `json:"lastUpdateTime,omitempty"`
CurrentTableJson string `json:"currentTable.json"`
}

// +kubebuilder:object:root=true
Expand Down
21 changes: 6 additions & 15 deletions api/v1beta1/pinottenant_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,14 @@ type PinotTenantSpec struct {
PinotTenantsJson string `json:"tenants.json"`
}

type PinotTenantConditionType string

const (
PinotTenantCreateSuccess PinotTenantConditionType = "PinotTenantCreateSuccess"
PinotTenantUpdateSuccess PinotTenantConditionType = "PinotTenantUpdateSuccess"
PinotTenantCreateFail PinotTenantConditionType = "PinotTenantCreateFail"
PinotTenantUpdateFail PinotTenantConditionType = "PinotTenantUpdateFail"
)

// PinotTenantStatus defines the observed state of PinotTenant
type PinotTenantStatus struct {
Type PinotTenantConditionType `json:"type,omitempty"`
Status v1.ConditionStatus `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
LastUpdateTime string `json:"lastUpdateTime,omitempty"`
CurrentTenantsJson string `json:"currentTenants.json"`
Type string `json:"type,omitempty"`
Status v1.ConditionStatus `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
LastUpdateTime metav1.Time `json:"lastUpdateTime,omitempty"`
CurrentTenantsJson string `json:"currentTenants.json"`
}

//+kubebuilder:object:root=true
Expand Down
9 changes: 6 additions & 3 deletions api/v1beta1/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions config/crd/bases/datainfra.io_pinotschemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ spec:
currentSchemas.json:
type: string
lastUpdateTime:
format: date-time
type: string
message:
type: string
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/datainfra.io_pinottables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ spec:
type: string
pinotTableType:
type: string
segmentReload:
type: boolean
tables.json:
type: string
required:
Expand All @@ -65,6 +67,7 @@ spec:
currentTable.json:
type: string
lastUpdateTime:
format: date-time
type: string
message:
type: string
Expand Down
1 change: 1 addition & 0 deletions config/crd/bases/datainfra.io_pinottenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ spec:
currentTenants.json:
type: string
lastUpdateTime:
format: date-time
type: string
message:
type: string
Expand Down
68 changes: 13 additions & 55 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ package http

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)

// PinotHTTP interface
type PinotHTTP interface {
Do() *Response
Do() (*Response, error)
}

// HTTP client
Expand Down Expand Up @@ -61,34 +60,22 @@ type BasicAuth struct {
Password string
}

// Pinot API error Response
// ex: {"code":404,"error":"Schema not found"}
type PinotErrorResponse struct {
Code int `json:"code"`
Error string `json:"error"`
}

// Pinot API success Response
// ex: {"unrecognizedProperties":{},"status":"airlineStats successfully added"}
type PinotSuccessResponse struct {
UnrecognizedProperties interface{} `json:"unrecognizedProperties"`
Status string `json:"status"`
}

// Response passed to controller
type Response struct {
Err error
StatusCode int
PinotErrorResponse
PinotSuccessResponse
ResponseBody string
StatusCode int
}

// Initiate HTTP call to pinot
func (c *Client) Do() *Response {
// GET /schemas returns 404 when schema not found with code and error as resp.
// GET /tenants returns 404 when tenant not found with code and error as resp
// GET /tables returns 200 when table not found with an empty response.

// Do method to be used schema and tenant controller.
func (c *Client) Do() (*Response, error) {

req, err := http.NewRequest(c.Method, c.URL, bytes.NewBuffer(c.Body))
if err != nil {
return &Response{Err: err}
return nil, err
}

if c.Auth.BasicAuth != (BasicAuth{}) {
Expand All @@ -98,44 +85,15 @@ func (c *Client) Do() *Response {
req.Header.Add("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return &Response{Err: err}
return nil, err
}

defer resp.Body.Close()

responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &Response{Err: err}
return nil, err
}

// GET /schemas returns 404 when schema not found with code and error as resp.
// GET /tenants returns 404 when tenant not found with code and error as resp
// GET /tables returns 200 when table not found with an empty response.
if string(responseBody) != "{}" {
if resp.StatusCode == 200 {
var pinotSuccess PinotSuccessResponse
if err := json.Unmarshal(responseBody, &pinotSuccess); err != nil {
return &Response{Err: err}
}
return &Response{StatusCode: resp.StatusCode, PinotSuccessResponse: pinotSuccess}
} else {
var pinotErr PinotErrorResponse
if err := json.Unmarshal(responseBody, &pinotErr); err != nil {
return &Response{StatusCode: resp.StatusCode, Err: err}
}
return &Response{StatusCode: resp.StatusCode, PinotErrorResponse: pinotErr}
}
} else {
if resp.StatusCode == 200 {
// resp is empty with 200 status code
// for tables API force 404
return &Response{StatusCode: 404}
} else {
var pinotErr PinotErrorResponse
if err := json.Unmarshal(responseBody, &pinotErr); err != nil {
return &Response{StatusCode: resp.StatusCode, Err: err}
}
return &Response{StatusCode: resp.StatusCode, PinotErrorResponse: pinotErr}
}
}
return &Response{ResponseBody: string(responseBody), StatusCode: resp.StatusCode}, nil
}
6 changes: 1 addition & 5 deletions internal/schema_controller/pinotschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

"github.com/datainfrahq/pinot-control-plane-k8s/api/v1beta1"
Expand Down Expand Up @@ -61,8 +60,6 @@ func NewPinotSchemaReconciler(mgr ctrl.Manager) *PinotSchemaReconciler {
// +kubebuilder:rbac:groups="",resources=secret,verbs=get
func (r *PinotSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {

logr := log.FromContext(ctx)

pinotSchemaCR := &v1beta1.PinotSchema{}
err := r.Get(context.TODO(), req.NamespacedName, pinotSchemaCR)
if err != nil {
Expand All @@ -73,7 +70,6 @@ func (r *PinotSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

if err := r.do(ctx, pinotSchemaCR); err != nil {
logr.Error(err, err.Error())
return ctrl.Result{}, err
} else {
return ctrl.Result{RequeueAfter: r.ReconcileWait}, nil
Expand All @@ -86,8 +82,8 @@ func (r *PinotSchemaReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&datainfraiov1beta1.PinotSchema{}).
WithEventFilter(predicate.Or(
GenericPredicates{},
predicate.ResourceVersionChangedPredicate{},
predicate.GenerationChangedPredicate{},
predicate.LabelChangedPredicate{},
)).
Complete(r)
}
Expand Down
Loading