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 10, 2024
1 parent f036712 commit 0742988
Show file tree
Hide file tree
Showing 42 changed files with 1,396 additions and 182 deletions.
16 changes: 14 additions & 2 deletions adapter/internal/oasparser/model/adapter_internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,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 @@ -426,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 @@ -460,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 @@ -925,6 +936,7 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
}.String()].Spec
adapterInternalAPI.backendJWTTokenInfo = parseBackendJWTTokenToInternal(backendJWTPolicy)
}

return nil
}

Expand All @@ -943,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
12 changes: 6 additions & 6 deletions adapter/internal/oasparser/model/http_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,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 @@ -69,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 @@ -111,7 +111,7 @@ func parseRateLimitPolicyToInternal(ratelimitPolicy *dpv1alpha3.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 @@ -204,8 +204,8 @@ func concatRateLimitPolicies(schemeUp *dpv1alpha3.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
115 changes: 57 additions & 58 deletions adapter/internal/oasparser/model/http_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"testing"

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

Expand Down Expand Up @@ -199,157 +198,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
40 changes: 40 additions & 0 deletions adapter/internal/operator/config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ webhooks:
resources:
- apipolicies
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-dp-wso2-com-v1alpha3-apipolicy
failurePolicy: Fail
name: mapipolicy.kb.io
rules:
- apiGroups:
- dp.wso2.com
apiVersions:
- v1alpha3
operations:
- CREATE
- UPDATE
resources:
- apipolicies
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down Expand Up @@ -230,6 +250,26 @@ webhooks:
resources:
- apipolicies
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-dp-wso2-com-v1alpha3-apipolicy
failurePolicy: Fail
name: vapipolicy.kb.io
rules:
- apiGroups:
- dp.wso2.com
apiVersions:
- v1alpha3
operations:
- CREATE
- UPDATE
resources:
- apipolicies
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
Loading

0 comments on commit 0742988

Please sign in to comment.