Skip to content

Commit

Permalink
adding API Provider CRD implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowleyRajapakse committed Sep 8, 2024
1 parent 77916c1 commit ad9ccb8
Show file tree
Hide file tree
Showing 30 changed files with 673 additions and 143 deletions.
17 changes: 15 additions & 2 deletions adapter/internal/oasparser/model/adapter_internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/wso2/apk/adapter/internal/operator/utils"
dpv1alpha1 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha1"
dpv1alpha2 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha2"
dpv1alpha3 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha3"
"golang.org/x/exp/maps"
"k8s.io/apimachinery/pkg/types"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
Expand Down Expand Up @@ -79,6 +80,7 @@ type AdapterInternalAPI struct {
environment string
Endpoints *EndpointCluster
EndpointSecurity []*EndpointSecurity
AIProvider dpv1alpha3.AIProvider
}

// BackendJWTTokenInfo represents the object structure holding the information related to the JWT Generator
Expand Down Expand Up @@ -425,6 +427,16 @@ func (adapterInternalAPI *AdapterInternalAPI) GetEnvironment() string {
return adapterInternalAPI.environment
}

// SetAIProvider sets the AIProvider of the API.
func (adapterInternalAPI *AdapterInternalAPI) SetAIProvider(aiProvider dpv1alpha3.AIProvider) {
adapterInternalAPI.AIProvider = aiProvider
}

// GetAIProvider returns the AIProvider of the API
func (adapterInternalAPI *AdapterInternalAPI) GetAIProvider() dpv1alpha3.AIProvider {
return adapterInternalAPI.AIProvider
}

// Validate method confirms that the adapterInternalAPI has all required fields in the required format.
// This needs to be checked prior to generate router/enforcer related resources.
func (adapterInternalAPI *AdapterInternalAPI) Validate() error {
Expand Down Expand Up @@ -459,7 +471,7 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
if outputAuthScheme != nil {
authScheme = *outputAuthScheme
}
var apiPolicy *dpv1alpha2.APIPolicy
var apiPolicy *dpv1alpha3.APIPolicy
if outputAPIPolicy != nil {
apiPolicy = *outputAPIPolicy
}
Expand Down Expand Up @@ -924,6 +936,7 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
}.String()].Spec
adapterInternalAPI.backendJWTTokenInfo = parseBackendJWTTokenToInternal(backendJWTPolicy)
}

return nil
}

Expand All @@ -942,7 +955,7 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoGQLRouteCR(gqlRoute *dpv1al
if outputAuthScheme != nil {
authScheme = *outputAuthScheme
}
var apiPolicy *dpv1alpha2.APIPolicy
var apiPolicy *dpv1alpha3.APIPolicy
if outputAPIPolicy != nil {
apiPolicy = *outputAPIPolicy
}
Expand Down
13 changes: 7 additions & 6 deletions adapter/internal/oasparser/model/http_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/wso2/apk/adapter/internal/operator/utils"
dpv1alpha1 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha1"
dpv1alpha2 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha2"
dpv1alpha3 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha3"
"k8s.io/apimachinery/pkg/types"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)
Expand All @@ -32,8 +33,8 @@ import (
type ResourceParams struct {
AuthSchemes map[string]dpv1alpha2.Authentication
ResourceAuthSchemes map[string]dpv1alpha2.Authentication
APIPolicies map[string]dpv1alpha2.APIPolicy
ResourceAPIPolicies map[string]dpv1alpha2.APIPolicy
APIPolicies map[string]dpv1alpha3.APIPolicy
ResourceAPIPolicies map[string]dpv1alpha3.APIPolicy
InterceptorServiceMapping map[string]dpv1alpha1.InterceptorService
BackendJWTMapping map[string]dpv1alpha1.BackendJWT
BackendMapping map[string]*dpv1alpha2.ResolvedBackend
Expand Down Expand Up @@ -68,7 +69,7 @@ func parseBackendJWTTokenToInternal(backendJWTToken dpv1alpha1.BackendJWTSpec) *
return backendJWTTokenInternal
}

func getCorsConfigFromAPIPolicy(apiPolicy *dpv1alpha2.APIPolicy) *CorsConfig {
func getCorsConfigFromAPIPolicy(apiPolicy *dpv1alpha3.APIPolicy) *CorsConfig {
globalCorsConfig := config.ReadConfigs().Enforcer.Cors

var corsConfig = CorsConfig{
Expand Down Expand Up @@ -110,7 +111,7 @@ func parseRateLimitPolicyToInternal(ratelimitPolicy *dpv1alpha1.RateLimitPolicy)
}

// addOperationLevelInterceptors add the operation level interceptor policy to the policies
func addOperationLevelInterceptors(policies *OperationPolicies, apiPolicy *dpv1alpha2.APIPolicy,
func addOperationLevelInterceptors(policies *OperationPolicies, apiPolicy *dpv1alpha3.APIPolicy,
interceptorServicesMapping map[string]dpv1alpha1.InterceptorService,
backendMapping map[string]*dpv1alpha2.ResolvedBackend, namespace string) {
if apiPolicy != nil && apiPolicy.Spec.Override != nil {
Expand Down Expand Up @@ -203,8 +204,8 @@ func concatRateLimitPolicies(schemeUp *dpv1alpha1.RateLimitPolicy, schemeDown *d
return &finalRateLimit
}

func concatAPIPolicies(schemeUp *dpv1alpha2.APIPolicy, schemeDown *dpv1alpha2.APIPolicy) *dpv1alpha2.APIPolicy {
apiPolicy := dpv1alpha2.APIPolicy{}
func concatAPIPolicies(schemeUp *dpv1alpha3.APIPolicy, schemeDown *dpv1alpha3.APIPolicy) *dpv1alpha3.APIPolicy {
apiPolicy := dpv1alpha3.APIPolicy{}
if schemeUp != nil && schemeDown != nil {
apiPolicy.Spec.Override = utils.SelectPolicy(&schemeUp.Spec.Override, &schemeUp.Spec.Default, &schemeDown.Spec.Override, &schemeDown.Spec.Default)
} else if schemeUp != nil {
Expand Down
116 changes: 58 additions & 58 deletions adapter/internal/oasparser/model/http_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/stretchr/testify/assert"
dpv1alpha1 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha1"
dpv1alpha2 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha2"
dpv1alpha3 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha3"
)

func TestConcatRateLimitPolicies(t *testing.T) {
Expand Down Expand Up @@ -199,157 +199,157 @@ func TestConcatRateLimitPolicies(t *testing.T) {
func TestConcatAPIPolicies(t *testing.T) {

type testItem struct {
schemeUpSpec dpv1alpha2.APIPolicySpec
schemeDownSpec dpv1alpha2.APIPolicySpec
result dpv1alpha2.APIPolicySpec
schemeUpSpec dpv1alpha3.APIPolicySpec
schemeDownSpec dpv1alpha3.APIPolicySpec
result dpv1alpha3.APIPolicySpec
message string
}

schemeUp := &dpv1alpha2.APIPolicy{}
schemeDown := &dpv1alpha2.APIPolicy{}
resultScheme := &dpv1alpha2.APIPolicy{}
schemeUp := &dpv1alpha3.APIPolicy{}
schemeDown := &dpv1alpha3.APIPolicy{}
resultScheme := &dpv1alpha3.APIPolicy{}

dataItems := []testItem{
{
schemeUpSpec: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeUpSpec: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i1"},
},
},
},
schemeDownSpec: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeDownSpec: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i2"},
},
},
},
result: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
result: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i1"},
},
},
},
message: "only schemeUp override policies should be provided",
},
{
schemeUpSpec: dpv1alpha2.APIPolicySpec{
Default: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeUpSpec: dpv1alpha3.APIPolicySpec{
Default: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i1"},
},
},
},
schemeDownSpec: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeDownSpec: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i2"},
},
},
},
result: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
result: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i2"},
},
},
},
message: "only schemeDown override policies should be provided",
},
{
schemeUpSpec: dpv1alpha2.APIPolicySpec{
Default: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeUpSpec: dpv1alpha3.APIPolicySpec{
Default: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i1"},
},
},
},
schemeDownSpec: dpv1alpha2.APIPolicySpec{
Default: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeDownSpec: dpv1alpha3.APIPolicySpec{
Default: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i2"},
},
},
},
result: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
result: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i2"},
},
},
},
message: "only schemeDown default policies should be provided",
},
{
schemeUpSpec: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeUpSpec: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i1"},
},
},
},
result: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
result: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i1"},
},
},
},
message: "only schemeUp override policies is provided",
},
{
schemeUpSpec: dpv1alpha2.APIPolicySpec{
Default: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeUpSpec: dpv1alpha3.APIPolicySpec{
Default: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i1"},
},
},
},
result: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
result: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{Name: "i1"},
},
},
},
message: "only schemeUp default policies is provided",
},
{
schemeUpSpec: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeUpSpec: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{
Name: "up-request-interceptor",
},
},
},
},
schemeDownSpec: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
schemeDownSpec: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{
Name: "down-request-interceptor",
},
},
},
Default: &dpv1alpha2.PolicySpec{
ResponseInterceptors: []dpv1alpha2.InterceptorReference{
Default: &dpv1alpha3.PolicySpec{
ResponseInterceptors: []dpv1alpha3.InterceptorReference{
{
Name: "down-response-interceptor",
},
},
},
},
result: dpv1alpha2.APIPolicySpec{
Override: &dpv1alpha2.PolicySpec{
RequestInterceptors: []dpv1alpha2.InterceptorReference{
result: dpv1alpha3.APIPolicySpec{
Override: &dpv1alpha3.PolicySpec{
RequestInterceptors: []dpv1alpha3.InterceptorReference{
{
Name: "up-request-interceptor",
},
},
ResponseInterceptors: []dpv1alpha2.InterceptorReference{
ResponseInterceptors: []dpv1alpha3.InterceptorReference{
{
Name: "down-response-interceptor",
},
Expand Down
Loading

0 comments on commit ad9ccb8

Please sign in to comment.