Skip to content

Commit

Permalink
Merge branch 'main' into cherry
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaliMatharaarachchi committed Sep 27, 2024
1 parent e54bd98 commit 197aed9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
22 changes: 15 additions & 7 deletions adapter/internal/controlplane/eventPublisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type API struct {
IsDefaultVersion bool `json:"isDefaultVersion"`
Definition string `json:"definition"`
APIType string `json:"apiType"`
APISubType string `json:"apiSubType"`
BasePath string `json:"basePath"`
Organization string `json:"organization"`
SystemAPI bool `json:"systemAPI"`
Expand All @@ -98,18 +99,25 @@ type API struct {
AuthHeader string `json:"authHeader"`
APIKeyHeader string `json:"apiKeyHeader"`
Operations []Operation `json:"operations"`
AIConfiguration AIConfiguration `json:"aiConfiguration"`
APIHash string `json:"-"`
SandAIRL *AIRL `json:"sandAIRL"`
ProdAIRL *AIRL `json:"prodAIRL"`
SandAIRL *AIRL `json:"sandAIRL"`
ProdAIRL *AIRL `json:"prodAIRL"`
}

// AIRL holds AI ratelimit related data
type AIRL struct {
PromptTokenCount *uint32 `json:"promptTokenCount"`
CompletionTokenCount *uint32 `json:"CompletionTokenCount"`
TotalTokenCount *uint32 `json:"totalTokenCount"`
TimeUnit string `json:"timeUnit"`
RequestCount *uint32 `json:"requestCount"`
PromptTokenCount *uint32 `json:"promptTokenCount"`
CompletionTokenCount *uint32 `json:"CompletionTokenCount"`
TotalTokenCount *uint32 `json:"totalTokenCount"`
TimeUnit string `json:"timeUnit"`
RequestCount *uint32 `json:"requestCount"`
}

// AIConfiguration holds the AI configuration
type AIConfiguration struct {
LLMProviderName string `json:"llmProviderName"`
LLMProviderAPIVersion string `json:"llmProviderAPIVersion"`
}

// Headers contains the request and response header modifier information
Expand Down
34 changes: 27 additions & 7 deletions adapter/internal/operator/controllers/dp/api_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ func (apiReconciler *APIReconciler) resolveAPIRefs(ctx context.Context, api dpv1
prodRouteRefs, namespace)
}
}
apiState.ProdAIRL = prodAirl
var sandAirl *dpv1alpha3.AIRateLimitPolicy
if len(sandRouteRefs) > 0 && apiState.APIDefinition.Spec.APIType == "REST" {
apiState.SandHTTPRoute = &synchronizer.HTTPRouteState{}
Expand All @@ -447,7 +448,7 @@ func (apiReconciler *APIReconciler) resolveAPIRefs(ctx context.Context, api dpv1
sandRouteRefs, namespace)
}
}

apiState.SandAIRL = sandAirl
// handle gql apis
if len(prodRouteRefs) > 0 && apiState.APIDefinition.Spec.APIType == "GraphQL" {
if apiState.ProdGQLRoute, err = apiReconciler.resolveGQLRouteRefs(ctx, prodRouteRefs, namespace,
Expand Down Expand Up @@ -2879,8 +2880,8 @@ func (apiReconciler *APIReconciler) convertAPIStateToAPICp(ctx context.Context,
sandVhost := geSandVhost(&apiState)
securityScheme, authHeader, apiKeyHeader := prepareSecuritySchemeForCP(&apiState)
operations := prepareOperations(&apiState)
var sandAIRLToAgent controlplane.AIRL
var prodAIRLToAgent controlplane.AIRL
var sandAIRLToAgent *controlplane.AIRL
var prodAIRLToAgent *controlplane.AIRL
if prodAIRL != nil {
var promptTC, completionTC, totalTC, requestC *uint32
var timeUnit string
Expand All @@ -2894,7 +2895,7 @@ func (apiReconciler *APIReconciler) convertAPIStateToAPICp(ctx context.Context,
timeUnit = prodAIRL.Spec.Override.RequestCount.Unit
requestC = &prodAIRL.Spec.Override.RequestCount.RequestsPerUnit
}
prodAIRLToAgent = controlplane.AIRL{
prodAIRLToAgent = &controlplane.AIRL{
PromptTokenCount: promptTC,
CompletionTokenCount: completionTC,
TotalTokenCount: totalTC,
Expand All @@ -2915,19 +2916,32 @@ func (apiReconciler *APIReconciler) convertAPIStateToAPICp(ctx context.Context,
timeUnit = sandAIRL.Spec.Override.RequestCount.Unit
requestC = &sandAIRL.Spec.Override.RequestCount.RequestsPerUnit
}
sandAIRLToAgent = controlplane.AIRL{
sandAIRLToAgent = &controlplane.AIRL{
PromptTokenCount: promptTC,
CompletionTokenCount: completionTC,
TotalTokenCount: totalTC,
TimeUnit: timeUnit,
RequestCount: requestC,
}
}
subType := "DEFAULT"
aiConfiguration := controlplane.AIConfiguration{}
if apiState.AIProvider != nil {
loggers.LoggerAPKOperator.Debugf("AIProvider is found")
subType = "AIAPI"
aiConfiguration = controlplane.AIConfiguration{
LLMProviderName: apiState.AIProvider.Spec.ProviderName,
LLMProviderAPIVersion: apiState.AIProvider.Spec.ProviderAPIVersion,
}
}
loggers.LoggerAPKOperator.Debugf("Resolved aiConfiguration: %+v", aiConfiguration)

api := controlplane.API{
APIName: spec.APIName,
APIVersion: spec.APIVersion,
IsDefaultVersion: spec.IsDefaultVersion,
APIType: spec.APIType,
APISubType: subType,
BasePath: spec.BasePath,
Organization: spec.Organization,
Environment: spec.Environment,
Expand All @@ -2947,8 +2961,9 @@ func (apiReconciler *APIReconciler) convertAPIStateToAPICp(ctx context.Context,
Operations: operations,
APIHash: apiHash,
APIKeyHeader: apiKeyHeader,
SandAIRL: &sandAIRLToAgent,
ProdAIRL: &prodAIRLToAgent,
SandAIRL: sandAIRLToAgent,
ProdAIRL: prodAIRLToAgent,
AIConfiguration: aiConfiguration,
}
apiCPEvent.API = api
apiCPEvent.CRName = apiState.APIDefinition.ObjectMeta.Name
Expand Down Expand Up @@ -3029,6 +3044,9 @@ func (apiReconciler *APIReconciler) getAPIHash(apiState *synchronizer.APIState)
loggers.LoggerAPK.Infof("Error occured while extracting values using reflection. Error: %+v", r)
}
}()
if obj == nil {
return "nil"
}
var sb strings.Builder
objValue := reflect.ValueOf(obj)
if objValue.Kind() == reflect.Ptr {
Expand All @@ -3055,6 +3073,8 @@ func (apiReconciler *APIReconciler) getAPIHash(apiState *synchronizer.APIState)

uniqueIDs := make([]string, 0)
uniqueIDs = append(uniqueIDs, getUniqueID(apiState.APIDefinition, "ObjectMeta.Name", "ObjectMeta.Namespace", "ObjectMeta.Generation"))
uniqueIDs = append(uniqueIDs, getUniqueID(apiState.SandAIRL, "ObjectMeta.Name", "ObjectMeta.Namespace", "ObjectMeta.Generation"))
uniqueIDs = append(uniqueIDs, getUniqueID(apiState.ProdAIRL, "ObjectMeta.Name", "ObjectMeta.Namespace", "ObjectMeta.Generation"))
for _, auth := range apiState.Authentications {
uniqueIDs = append(uniqueIDs, getUniqueID(auth, "ObjectMeta.Name", "ObjectMeta.Namespace", "ObjectMeta.Generation"))
}
Expand Down
2 changes: 2 additions & 0 deletions adapter/internal/operator/synchronizer/api_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type APIState struct {
APIDefinitionFile []byte
SubscriptionValidation bool
MutualSSL *v1alpha2.MutualSSL
ProdAIRL *v1alpha3.AIRateLimitPolicy
SandAIRL *v1alpha3.AIRateLimitPolicy
}

// HTTPRouteState holds the state of the deployed httpRoutes. This state is compared with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ private CheckResponse buildResponse(CheckRequest request, ResponseObject respons

Struct.Builder structBuilder = Struct.newBuilder();
if (responseObject.getMetaDataMap() != null) {
responseObject.getMetaDataMap().forEach((key, value) ->
addMetadata(structBuilder, key, value));
responseObject.getMetaDataMap().forEach((key, value) -> {
if (value != null) {
addMetadata(structBuilder, key, value);
}
});
}

//Adds original request path header without params as a metadata for access logging.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -309,6 +310,9 @@ private static Usage extractUsageFromBody(String body, String completionTokenPat
JsonNode rootNode = mapper.readTree(body);
// Extract prompt token count
String[] keysForPromtTokens = promptTokenPath.split("\\.");
if (keysForPromtTokens.length > 0 && "$".equals(keysForPromtTokens[0])) {
keysForPromtTokens = Arrays.copyOfRange(keysForPromtTokens, 1, keysForPromtTokens.length);
}
JsonNode currentNodeForPromtToken = null;
if (rootNode.has(keysForPromtTokens[0])) {
currentNodeForPromtToken = rootNode.get(keysForPromtTokens[0]);
Expand Down
12 changes: 6 additions & 6 deletions helm-charts/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ wso2:
failureThreshold: 5
strategy: RollingUpdate
replicas: 1
imagePullPolicy: IfNotPresent
image: apk-adapter:1.2.0-SNAPSHOT
imagePullPolicy: Always
image: wso2/apk-adapter:latest
security:
sslHostname: "adapter"
logging:
Expand Down Expand Up @@ -154,8 +154,8 @@ wso2:
failureThreshold: 5
strategy: RollingUpdate
replicas: 1
imagePullPolicy: IfNotPresent
image: apk-common-controller:1.2.0-SNAPSHOT
imagePullPolicy: Always
image: wso2/apk-common-controller:latest
security:
sslHostname: "commoncontroller"
# configs:
Expand Down Expand Up @@ -242,8 +242,8 @@ wso2:
periodSeconds: 20
failureThreshold: 5
strategy: RollingUpdate
imagePullPolicy: IfNotPresent
image: apk-enforcer:1.2.0-SNAPSHOT
imagePullPolicy: Always
image: wso2/apk-enforcer:latest
security:
sslHostname: "enforcer"
# logging:
Expand Down

0 comments on commit 197aed9

Please sign in to comment.