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

Improve DP to CP communication #2212

Merged
merged 5 commits into from
Apr 1, 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
57 changes: 36 additions & 21 deletions adapter/internal/controlplane/eventPublisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,35 @@ type APICPEvent struct {

// API holds the data that needs to be sent to agent
type API struct {
APIUUID string `json:"apiUUID"`
APIName string `json:"apiName"`
APIVersion string `json:"apiVersion"`
IsDefaultVersion bool `json:"isDefaultVersion"`
Definition string `json:"definition"`
APIType string `json:"apiType"`
BasePath string `json:"basePath"`
Organization string `json:"organization"`
SystemAPI bool `json:"systemAPI"`
APIProperties []Property `json:"apiProperties,omitempty"`
Environment string `json:"environment,omitempty"`
RevisionID string `json:"revisionID"`
APIUUID string `json:"apiUUID"`
APIName string `json:"apiName"`
APIVersion string `json:"apiVersion"`
IsDefaultVersion bool `json:"isDefaultVersion"`
Definition string `json:"definition"`
APIType string `json:"apiType"`
BasePath string `json:"basePath"`
Organization string `json:"organization"`
SystemAPI bool `json:"systemAPI"`
APIProperties map[string]string `json:"apiProperties,omitempty"`
Environment string `json:"environment,omitempty"`
RevisionID string `json:"revisionID"`
SandEndpoint string `json:"sandEndpoint"`
ProdEndpoint string `json:"prodEndpoint"`
EndpointProtocol string `json:"endpointProtocol"`
CORSPolicy *CORSPolicy `json:"cORSPolicy,omitempty"`
Vhost string `json:"vhost"`
SecurityScheme []string `json:"securityScheme"`
AuthHeader string `json:"authHeader"`
}

// Property holds key value pair of APIProperties
type Property struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
// CORSPolicy hold cors configs
type CORSPolicy struct {
AccessControlAllowCredentials bool `json:"accessControlAllowCredentials,omitempty"`
AccessControlAllowHeaders []string `json:"accessControlAllowHeaders,omitempty"`
AccessControlAllowOrigins []string `json:"accessControlAllowOrigins,omitempty"`
AccessControlExposeHeaders []string `json:"accessControlExposeHeaders,omitempty"`
AccessControlMaxAge *int `json:"accessControlMaxAge,omitempty"`
AccessControlAllowMethods []string `json:"accessControlAllowMethods,omitempty"`
}

// init reads the configuration and starts the worker to send data.
Expand Down Expand Up @@ -155,7 +166,7 @@ func sendData() {
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusServiceUnavailable {
body, _ := ioutil.ReadAll(resp.Body)
loggers.LoggerAPK.Errorf("Error: Unexpected status code: %d, received message: %s, retrying after %d seconds", resp.StatusCode, string(body), retryInterval)
// Sleep for some time before retrying
Expand All @@ -174,15 +185,18 @@ func sendData() {
}
// Assuming the response contains an ID field, you can extract it like this:
id, ok := responseMap["id"].(string)
if !ok {
loggers.LoggerAPK.Errorf("Id field not present in response body. encoded body: %+v", responseMap)
break
revisionID, revisionOk := responseMap["revisionID"].(string)
if !ok || !revisionOk {
loggers.LoggerAPK.Errorf("Id or/both revision Id field not present in response body. encoded body: %+v", responseMap)
id = ""
revisionID = ""
// break
}
loggers.LoggerAPK.Infof("Adding label update to API %s/%s, Lebels: apiUUID: %s", event.CRNamespace, event.CRName, id)
labelsQueue <- APICRLabelsUpdate{
Namespace: event.CRNamespace,
Name: event.CRName,
Labels: map[string]string{"apiUUID": id},
Labels: map[string]string{"apiUUID": id, "revisionID": revisionID},
}
break
}
Expand All @@ -191,6 +205,7 @@ func sendData() {

// AddToEventQueue adds the api event to queue
func AddToEventQueue(data APICPEvent) {
loggers.LoggerAPK.Debugf("Event added to CP Event queue : %+v", data)
eventQueue <- data
}

Expand Down
Loading
Loading