Skip to content

Commit

Permalink
Fix revive and test failes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharsanan1 committed Sep 9, 2024
1 parent 402fa7a commit 56334b1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 117 deletions.
17 changes: 11 additions & 6 deletions adapter/internal/oasparser/envoyconf/routes_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import (
v35 "github.com/envoyproxy/go-control-plane/envoy/type/metadata/v3"
)

const(
authzNamespace = "envoy.filters.http.ext_authz"
)

func generateRouteConfig(routeName string, match *routev3.RouteMatch, action *routev3.Route_Route, redirectAction *routev3.Route_Redirect,
metadata *corev3.Metadata, decorator *routev3.Decorator, typedPerFilterConfig map[string]*anypb.Any,
requestHeadersToAdd []*corev3.HeaderValueOption, requestHeadersToRemove []string,
Expand Down Expand Up @@ -172,9 +176,13 @@ func mapStatusCodeToEnum(statusCode int) int {
}
}
const (
// DescriptorKeyForAIRequestTokenCount is the descriptor key for AI request token count ratelimit
DescriptorKeyForAIRequestTokenCount = "airequesttokencount"
// DescriptorKeyForAIResponseTokenCount is the descriptor key for AI response token count ratelimit
DescriptorKeyForAIResponseTokenCount = "airesponsetokencount"
// DescriptorKeyForAITotalTokenCount is the descriptor key for AI total token count ratelimit
DescriptorKeyForAITotalTokenCount = "aitotaltokencount"
// DescriptorKeyForAIRequestCount is the descriptor key for AI request count ratelimit
DescriptorKeyForAIRequestCount = "airequestcount"
)

Expand Down Expand Up @@ -230,9 +238,6 @@ func generateBackendBasedAIRatelimit(descValue string) []*routev3.RateLimit {
return []*routev3.RateLimit{&rateLimitForRequestTokenCount, &rateLimitForResponseTokenCount, &rateLimitForRequestCount, &rateLimitForTotalTokenCount}
}

const(
authz_namespace = "envoy.filters.http.ext_authz"
)

func generateSubscriptionBasedAIRatelimit(descValue string) []*routev3.RateLimit {
rateLimitForRequestTokenCount := routev3.RateLimit{
Expand All @@ -242,7 +247,7 @@ func generateSubscriptionBasedAIRatelimit(descValue string) []*routev3.RateLimit
Metadata: &routev3.RateLimit_Action_MetaData{
DescriptorKey: DescriptorKeyForAIRequestTokenCount,
MetadataKey: &v35.MetadataKey{
Key: authz_namespace,
Key: authzNamespace,
Path: []*v35.MetadataKey_PathSegment{
&v35.MetadataKey_PathSegment{
Segment: &v35.MetadataKey_PathSegment_Key{
Expand All @@ -263,7 +268,7 @@ func generateSubscriptionBasedAIRatelimit(descValue string) []*routev3.RateLimit
Metadata: &routev3.RateLimit_Action_MetaData{
DescriptorKey: DescriptorKeyForAIResponseTokenCount,
MetadataKey: &v35.MetadataKey{
Key: authz_namespace,
Key: authzNamespace,
Path: []*v35.MetadataKey_PathSegment{
&v35.MetadataKey_PathSegment{
Segment: &v35.MetadataKey_PathSegment_Key{
Expand All @@ -284,7 +289,7 @@ func generateSubscriptionBasedAIRatelimit(descValue string) []*routev3.RateLimit
Metadata: &routev3.RateLimit_Action_MetaData{
DescriptorKey: DescriptorKeyForAIRequestCount,
MetadataKey: &v35.MetadataKey{
Key: authz_namespace,
Key: authzNamespace,
Path: []*v35.MetadataKey_PathSegment{
&v35.MetadataKey_PathSegment{
Segment: &v35.MetadataKey_PathSegment_Key{
Expand Down
8 changes: 4 additions & 4 deletions adapter/internal/oasparser/model/adapter_internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
ratelimitPolicy = *outputRatelimitPolicy
}

for ruleId, rule := range httpRoute.Spec.Rules {
for ruleID, rule := range httpRoute.Spec.Rules {
var endPoints []Endpoint
var policies = OperationPolicies{}
var circuitBreaker *dpv1alpha1.CircuitBreaker
Expand All @@ -493,12 +493,12 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap

enableBackendBasedAIRatelimit := false
descriptorValue := ""
if aiRatelimitPolicy, exists := ruleIdxToAiRatelimitPolicyMapping[ruleId]; exists {
loggers.LoggerAPI.Infof("Found AI ratelimit mapping for ruleId: %d, related api: %s", ruleId, adapterInternalAPI.UUID)
if aiRatelimitPolicy, exists := ruleIdxToAiRatelimitPolicyMapping[ruleID]; exists {
loggers.LoggerAPI.Infof("Found AI ratelimit mapping for ruleId: %d, related api: %s", ruleID, adapterInternalAPI.UUID)
enableBackendBasedAIRatelimit = true
descriptorValue = prepareAIRatelimitIdentifier(adapterInternalAPI.OrganizationID, utils.NamespacedName(aiRatelimitPolicy), &aiRatelimitPolicy.Spec)
} else {
loggers.LoggerAPI.Infof("Could not find AIratelimit for ruleId: %d, len of map: %d, related api: %s", ruleId, len(ruleIdxToAiRatelimitPolicyMapping), adapterInternalAPI.UUID)
loggers.LoggerAPI.Infof("Could not find AIratelimit for ruleId: %d, len of map: %d, related api: %s", ruleID, len(ruleIdxToAiRatelimitPolicyMapping), adapterInternalAPI.UUID)
}

backendBasePath := ""
Expand Down
36 changes: 17 additions & 19 deletions adapter/internal/operator/controllers/dp/api_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const (
aiRatelimitPolicyToBackendIndex = "aiRatelimitPolicyToBackendIndex"
aiRatelimitPolicyToSubscriptionIndex = "aiRatelimitPolicyToSubscriptionIndex"
subscriptionToAPIIndex = "subscriptionToAPIIndex"
ApiToSubscriptionIndex = "ApiToSubscriptionIndex"
apiToSubscriptionIndex = "apiToSubscriptionIndex"
)

var (
Expand Down Expand Up @@ -838,22 +838,21 @@ func (apiReconciler *APIReconciler) resolveAiSubscriptionRatelimitPolicies(ctx c
apiState.IsAiSubscriptionRatelimitEnabled = false
subscriptionList := &cpv1alpha2.SubscriptionList{}
if err := apiReconciler.client.List(ctx, subscriptionList, &k8client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(subscriptionToAPIIndex, utils.GetSubscriptionToAPIIndexId(apiState.APIDefinition.Spec.APIName, apiState.APIDefinition.Spec.APIVersion)),
FieldSelector: fields.OneTermEqualSelector(subscriptionToAPIIndex, utils.GetSubscriptionToAPIIndexID(apiState.APIDefinition.Spec.APIName, apiState.APIDefinition.Spec.APIVersion)),
}); err != nil {
loggers.LoggerAPKOperator.Infof("No associated subscription found for API: %s", utils.NamespacedName(apiState.APIDefinition))
return
} else {
for _, subscription := range subscriptionList.Items {
aiRatelimitPolicyList := &dpv1alpha3.AIRateLimitPolicyList{}
if err := apiReconciler.client.List(ctx, aiRatelimitPolicyList, &k8client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(aiRatelimitPolicyToSubscriptionIndex, utils.NamespacedName(&subscription).String()),
}); err != nil {
loggers.LoggerAPKOperator.Infof("No associated aiRatelimitPolicy found for Subscription: %s", utils.NamespacedName(&subscription))
return
}
if len(aiRatelimitPolicyList.Items) > 0 {
apiState.IsAiSubscriptionRatelimitEnabled = true
}
}
for _, subscription := range subscriptionList.Items {
aiRatelimitPolicyList := &dpv1alpha3.AIRateLimitPolicyList{}
if err := apiReconciler.client.List(ctx, aiRatelimitPolicyList, &k8client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(aiRatelimitPolicyToSubscriptionIndex, utils.NamespacedName(&subscription).String()),
}); err != nil {
loggers.LoggerAPKOperator.Infof("No associated aiRatelimitPolicy found for Subscription: %s", utils.NamespacedName(&subscription))
return
}
if len(aiRatelimitPolicyList.Items) > 0 {
apiState.IsAiSubscriptionRatelimitEnabled = true
}
}
}
Expand Down Expand Up @@ -924,7 +923,7 @@ func (apiReconciler *APIReconciler) getResolvedBackendsMapping(ctx context.Conte
return nil, fmt.Errorf("unable to find backend %s", backendNamespacedName.String())
}
}

}

for _, filter := range rule.Filters {
Expand Down Expand Up @@ -1456,9 +1455,8 @@ func (apiReconciler *APIReconciler) getAPIsForAIRatelimitPolicy(ctx context.Cont
if err := apiReconciler.client.Get(ctx, namespacedName, backend); err != nil {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2621, logging.MINOR, "Unable to find associated Backend for AIratelimitPolicy targetref: %s", namespacedName.String()))
return []reconcile.Request{}
} else {
return apiReconciler.getAPIsForBackend(ctx, backend)
}
return apiReconciler.getAPIsForBackend(ctx, backend)
}
return []reconcile.Request{}
}
Expand All @@ -1473,7 +1471,7 @@ func (apiReconciler *APIReconciler) getAPIsForSubscription(ctx context.Context,
}
apiList := &dpv1alpha2.APIList{}
if err := apiReconciler.client.List(ctx, apiList, &k8client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(ApiToSubscriptionIndex, utils.GetSubscriptionToAPIIndexId(subscription.Spec.API.Name, subscription.Spec.API.Version)),
FieldSelector: fields.OneTermEqualSelector(apiToSubscriptionIndex, utils.GetSubscriptionToAPIIndexID(subscription.Spec.API.Name, subscription.Spec.API.Version)),
}); err != nil {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2649, logging.CRITICAL, "Unable to find associated APIs for subscription: %s, error: %v", utils.NamespacedName(subscription).String(), err.Error()))
return []reconcile.Request{}
Expand Down Expand Up @@ -2058,7 +2056,7 @@ func addIndexes(ctx context.Context, mgr manager.Manager) error {
}

// API to Subscription indexer
if err := mgr.GetFieldIndexer().IndexField(ctx, &dpv1alpha2.API{}, ApiToSubscriptionIndex,
if err := mgr.GetFieldIndexer().IndexField(ctx, &dpv1alpha2.API{}, apiToSubscriptionIndex,
func(rawObj k8client.Object) []string {
api := rawObj.(*dpv1alpha2.API)
var apis []string
Expand Down
6 changes: 3 additions & 3 deletions adapter/internal/operator/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func ContainsString(list []string, target string) bool {
return false
}

// GetSubscriptionToAPIIndexId returns the id which can be used to list subscriptions related to a api.
func GetSubscriptionToAPIIndexId(name string, version string) string {
// GetSubscriptionToAPIIndexID returns the id which can be used to list subscriptions related to a api.
func GetSubscriptionToAPIIndexID(name string, version string) string {
return fmt.Sprintf("%s_%s", name, version)
}
}
4 changes: 2 additions & 2 deletions common-controller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ tasks.named('go_revive_run').configure {
}

tasks.named('go_build').configure {
// dependsOn go_revive_run
// dependsOn go_vet
dependsOn go_revive_run
dependsOn go_vet
println("Running go build")
finalizedBy docker_build
}
Expand Down
4 changes: 2 additions & 2 deletions common-go-libs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ tasks.named('go_revive_run').configure {
}

tasks.named('go_build').configure {
// dependsOn go_revive_run
// dependsOn go_vet
dependsOn go_revive_run
dependsOn go_vet
println("Running go build")
}

Expand Down
81 changes: 0 additions & 81 deletions common-go-libs/controllers/dp/suite_test.go

This file was deleted.

0 comments on commit 56334b1

Please sign in to comment.