From 58e75678e44d7f36ac68fb01c8795bd0888388b8 Mon Sep 17 00:00:00 2001 From: Pubudu Gunatilaka Date: Mon, 29 Apr 2019 12:55:50 +0530 Subject: [PATCH 01/13] Create README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 20bbbe7..2fa103a 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# k8s-apim-operator \ No newline at end of file +# k8s-apim-operator From 87c50b5ba1f052ff4d19c9feac383e7f8e1db779 Mon Sep 17 00:00:00 2001 From: pubudu538 Date: Sat, 4 May 2019 22:01:32 +0530 Subject: [PATCH 02/13] Adding API kind --- .../deploy/crds/wso2_v1alpha1_api_crd.yaml | 168 ------------------ .../wso2/v1alpha1/zz_generated.openapi.go | 161 +---------------- 2 files changed, 2 insertions(+), 327 deletions(-) diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_api_crd.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_api_crd.yaml index 743c76a..ab6f72a 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_api_crd.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_api_crd.yaml @@ -10,8 +10,6 @@ spec: plural: apis singular: api scope: Namespaced - subresources: - status: {} validation: openAPIV3Schema: properties: @@ -28,172 +26,6 @@ spec: metadata: type: object spec: - properties: - advancedThrottlingPolicy: - type: string - apiProperties: - items: - properties: - name: - type: string - value: - type: string - required: - - name - - value - type: object - type: array - authorizationHeader: - type: string - businessInformation: - properties: - businessOwner: - type: string - businessOwnerEmail: - type: string - technicalOwner: - type: string - technicalOwnerEmail: - type: string - required: - - businessOwner - - businessOwnerEmail - - technicalOwner - - technicalOwnerEmail - type: object - context: - type: string - description: - type: string - endpoints: - items: - properties: - dockerImage: - type: string - endpointName: - type: string - hostname: - type: string - port: - format: int32 - type: integer - protocol: - type: string - type: - type: string - required: - - type - - protocol - - hostname - - port - - dockerImage - - endpointName - type: object - type: array - labels: - items: - type: string - type: array - mode: - type: string - name: - description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster - Important: Run "operator-sdk generate k8s" to regenerate code after - modifying this file Add custom validation using kubebuilder tags: - https://book.kubebuilder.io/beyond_basics/generating_crd.html' - type: string - replicaCount: - format: int32 - type: integer - requestInterceptor: - type: string - responseInterceptor: - type: string - security: - items: - type: string - type: array - subscriptionTiers: - items: - type: string - type: array - tags: - items: - type: string - type: array - urlPatterns: - items: - properties: - advancedThrottlingPolicy: - type: string - endpoints: - items: - properties: - dockerImage: - type: string - endpointName: - type: string - hostname: - type: string - port: - format: int32 - type: integer - protocol: - type: string - type: - type: string - required: - - type - - protocol - - hostname - - port - - dockerImage - - endpointName - type: object - type: array - method: - type: string - path: - type: string - requestInterceptor: - type: string - responseInterceptor: - type: string - scopes: - items: - type: string - type: array - required: - - path - - method - - scopes - - requestInterceptor - - responseInterceptor - - endpoints - - advancedThrottlingPolicy - type: object - type: array - version: - type: string - required: - - name - - context - - version - - description - - tags - - endpoints - - requestInterceptor - - responseInterceptor - - authorizationHeader - - labels - - urlPatterns - - security - - subscriptionTiers - - advancedThrottlingPolicy - - businessInformation - - apiProperties - - mode - - replicaCount type: object status: type: object diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go index 56dd904..bdf0f04 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go @@ -67,167 +67,10 @@ func schema_pkg_apis_wso2_v1alpha1_APISpec(ref common.ReferenceCallback) common. Schema: spec.Schema{ SchemaProps: spec.SchemaProps{ Description: "APISpec defines the desired state of API", - Properties: map[string]spec.Schema{ - "name": { - SchemaProps: spec.SchemaProps{ - Description: "INSERT ADDITIONAL SPEC FIELDS - desired state of cluster Important: Run \"operator-sdk generate k8s\" to regenerate code after modifying this file Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html", - Type: []string{"string"}, - Format: "", - }, - }, - "context": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - "version": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - "description": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - "tags": { - SchemaProps: spec.SchemaProps{ - Type: []string{"array"}, - Items: &spec.SchemaOrArray{ - Schema: &spec.Schema{ - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - }, - }, - }, - "endpoints": { - SchemaProps: spec.SchemaProps{ - Type: []string{"array"}, - Items: &spec.SchemaOrArray{ - Schema: &spec.Schema{ - SchemaProps: spec.SchemaProps{ - Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Endpoint"), - }, - }, - }, - }, - }, - "requestInterceptor": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - "responseInterceptor": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - "authorizationHeader": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - "labels": { - SchemaProps: spec.SchemaProps{ - Type: []string{"array"}, - Items: &spec.SchemaOrArray{ - Schema: &spec.Schema{ - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - }, - }, - }, - "urlPatterns": { - SchemaProps: spec.SchemaProps{ - Type: []string{"array"}, - Items: &spec.SchemaOrArray{ - Schema: &spec.Schema{ - SchemaProps: spec.SchemaProps{ - Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.URLPattern"), - }, - }, - }, - }, - }, - "security": { - SchemaProps: spec.SchemaProps{ - Type: []string{"array"}, - Items: &spec.SchemaOrArray{ - Schema: &spec.Schema{ - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - }, - }, - }, - "subscriptionTiers": { - SchemaProps: spec.SchemaProps{ - Type: []string{"array"}, - Items: &spec.SchemaOrArray{ - Schema: &spec.Schema{ - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - }, - }, - }, - "advancedThrottlingPolicy": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - "businessInformation": { - SchemaProps: spec.SchemaProps{ - Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.BusinessInformation"), - }, - }, - "apiProperties": { - SchemaProps: spec.SchemaProps{ - Type: []string{"array"}, - Items: &spec.SchemaOrArray{ - Schema: &spec.Schema{ - SchemaProps: spec.SchemaProps{ - Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.APIProperty"), - }, - }, - }, - }, - }, - "mode": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, - "replicaCount": { - SchemaProps: spec.SchemaProps{ - Type: []string{"integer"}, - Format: "int32", - }, - }, - }, - Required: []string{"name", "context", "version", "description", "tags", "endpoints", "requestInterceptor", "responseInterceptor", "authorizationHeader", "labels", "urlPatterns", "security", "subscriptionTiers", "advancedThrottlingPolicy", "businessInformation", "apiProperties", "mode", "replicaCount"}, + Properties: map[string]spec.Schema{}, }, }, - Dependencies: []string{ - "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.APIProperty", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.BusinessInformation", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Endpoint", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.URLPattern"}, + Dependencies: []string{}, } } From 22c8dbb121a86599297638f484153a114f5f6805 Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Mon, 6 May 2019 13:34:52 +0530 Subject: [PATCH 03/13] Add RateLimiting kind --- .../deploy/crds/wso2_v1alpha1_api_crd.yaml | 168 +++++++++++ .../crds/wso2_v1alpha1_ratelimiting_cr.yaml | 26 ++ .../crds/wso2_v1alpha1_ratelimiting_crd.yaml | 107 +++++++ apim-operator/deploy/operator.yaml | 2 +- apim-operator/deploy/role.yaml | 1 + .../apis/wso2/v1alpha1/ratelimiting_types.go | 89 ++++++ .../wso2/v1alpha1/zz_generated.deepcopy.go | 177 +++++++++++ .../wso2/v1alpha1/zz_generated.defaults.go | 16 + .../wso2/v1alpha1/zz_generated.openapi.go | 274 +++++++++++++++++- .../pkg/controller/add_ratelimiting.go | 10 + .../ratelimiting/ratelimiting_controller.go | 153 ++++++++++ 11 files changed, 1017 insertions(+), 6 deletions(-) create mode 100644 apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml create mode 100644 apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml create mode 100644 apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go create mode 100644 apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.defaults.go create mode 100644 apim-operator/pkg/controller/add_ratelimiting.go create mode 100644 apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_api_crd.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_api_crd.yaml index ab6f72a..743c76a 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_api_crd.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_api_crd.yaml @@ -10,6 +10,8 @@ spec: plural: apis singular: api scope: Namespaced + subresources: + status: {} validation: openAPIV3Schema: properties: @@ -26,6 +28,172 @@ spec: metadata: type: object spec: + properties: + advancedThrottlingPolicy: + type: string + apiProperties: + items: + properties: + name: + type: string + value: + type: string + required: + - name + - value + type: object + type: array + authorizationHeader: + type: string + businessInformation: + properties: + businessOwner: + type: string + businessOwnerEmail: + type: string + technicalOwner: + type: string + technicalOwnerEmail: + type: string + required: + - businessOwner + - businessOwnerEmail + - technicalOwner + - technicalOwnerEmail + type: object + context: + type: string + description: + type: string + endpoints: + items: + properties: + dockerImage: + type: string + endpointName: + type: string + hostname: + type: string + port: + format: int32 + type: integer + protocol: + type: string + type: + type: string + required: + - type + - protocol + - hostname + - port + - dockerImage + - endpointName + type: object + type: array + labels: + items: + type: string + type: array + mode: + type: string + name: + description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + Important: Run "operator-sdk generate k8s" to regenerate code after + modifying this file Add custom validation using kubebuilder tags: + https://book.kubebuilder.io/beyond_basics/generating_crd.html' + type: string + replicaCount: + format: int32 + type: integer + requestInterceptor: + type: string + responseInterceptor: + type: string + security: + items: + type: string + type: array + subscriptionTiers: + items: + type: string + type: array + tags: + items: + type: string + type: array + urlPatterns: + items: + properties: + advancedThrottlingPolicy: + type: string + endpoints: + items: + properties: + dockerImage: + type: string + endpointName: + type: string + hostname: + type: string + port: + format: int32 + type: integer + protocol: + type: string + type: + type: string + required: + - type + - protocol + - hostname + - port + - dockerImage + - endpointName + type: object + type: array + method: + type: string + path: + type: string + requestInterceptor: + type: string + responseInterceptor: + type: string + scopes: + items: + type: string + type: array + required: + - path + - method + - scopes + - requestInterceptor + - responseInterceptor + - endpoints + - advancedThrottlingPolicy + type: object + type: array + version: + type: string + required: + - name + - context + - version + - description + - tags + - endpoints + - requestInterceptor + - responseInterceptor + - authorizationHeader + - labels + - urlPatterns + - security + - subscriptionTiers + - advancedThrottlingPolicy + - businessInformation + - apiProperties + - mode + - replicaCount type: object status: type: object diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml new file mode 100644 index 0000000..f4b58de --- /dev/null +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml @@ -0,0 +1,26 @@ +apiVersion: wso2.com/v1alpha1 +kind: RateLimiting +metadata: + name: bronze-ratelimiting +spec: + # Add fields here + #size: 3 + type: advanced # application ,subscription + description: Allow 1000 requests per minute + timeUnit: min + unitTime: 1 + requestCount: + limit: 1000 + bandwidth: + dataAmount: "" + dataUnit: "" + conditions: # not required for application and subscription policies + headerCondition: + headerName: “host” + headerValue: “abc.com” + ipCondition: + type: ipRange + specificIp: "" + negation : no + startIp: 10.100.7.2 + endIp: 10.100.7.255 \ No newline at end of file diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml new file mode 100644 index 0000000..54f37af --- /dev/null +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml @@ -0,0 +1,107 @@ +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: ratelimitings.wso2.com +spec: + group: wso2.com + names: + kind: RateLimiting + listKind: RateLimitingList + plural: ratelimitings + singular: ratelimiting + scope: Namespaced + subresources: + status: {} + validation: + openAPIV3Schema: + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + properties: + bandwidth: + properties: + dataAmount: + type: string + dataUnit: + type: string + required: + - dataAmount + - dataUnit + type: object + conditions: + properties: + headerCondition: + properties: + headerName: + type: string + headerValue: + type: string + required: + - headerName + - headerValue + type: object + ipCondition: + properties: + endIp: + type: string + negation: + type: boolean + specificIp: + type: string + startIp: + type: string + type: + type: string + required: + - type + - specificIp + - negation + - startIp + - endIp + type: object + required: + - headerCondition + - ipCondition + type: object + description: + type: string + requestCount: + properties: + limit: + format: int64 + type: integer + required: + - limit + type: object + timeUnit: + type: string + type: + type: string + unitTime: + format: int64 + type: integer + required: + - type + - description + - timeUnit + - unitTime + - requestCount + - bandwidth + - conditions + type: object + version: v1alpha1 + versions: + - name: v1alpha1 + served: true + storage: true diff --git a/apim-operator/deploy/operator.yaml b/apim-operator/deploy/operator.yaml index 03e8f68..5a9dca5 100644 --- a/apim-operator/deploy/operator.yaml +++ b/apim-operator/deploy/operator.yaml @@ -16,7 +16,7 @@ spec: containers: - name: apim-operator # Replace this with the built image name - image: pubudu/apim-operator:1.0.0 + image: rameshakaru/api-operator:v0.0.1 command: - apim-operator imagePullPolicy: Always diff --git a/apim-operator/deploy/role.yaml b/apim-operator/deploy/role.yaml index 5498f06..8970e45 100644 --- a/apim-operator/deploy/role.yaml +++ b/apim-operator/deploy/role.yaml @@ -44,5 +44,6 @@ rules: - wso2.com resources: - '*' + - ratelimitings verbs: - '*' diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go b/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go new file mode 100644 index 0000000..df8ea97 --- /dev/null +++ b/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go @@ -0,0 +1,89 @@ +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. + +// RateLimitingSpec defines the desired state of RateLimiting +// +k8s:openapi-gen=true +type RateLimitingSpec struct { + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + // Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file + // Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html + + Type string `json:"type"` + Description string `json:"description"` + TimeUnit string `json:"timeUnit"` + UnitTime int `json:"unitTime"` + RequestCount RequestCount `json:"requestCount"` + Bandwidth Bandwidth `json:"bandwidth"` + Conditions Conditions `json:"conditions"` +} + +//RequestCount is exported type in Ratelimiting Spec +type RequestCount struct { + Limit int `json:"limit"` +} + +//Bandwidth is exported type in Ratelimiting Spec +type Bandwidth struct { + DataAmount string `json:"dataAmount"` + DataUnit string `json:"dataUnit"` +} + +//Conditions is exported type in Ratelimiting Spec +type Conditions struct { + HeaderCondition HeaderCondition `json:"headerCondition"` + IPCondition IPCondition `json:"ipCondition"` +} + +//HeaderCondition is exported type in Ratelimiting Spec +type HeaderCondition struct { + HeaderName string `json:"headerName"` + HeaderValue string `json:"headerValue"` +} + +//IPCondition is exported type in Ratelimiting Spec +type IPCondition struct { + Type string `json:"type"` + SpecificIP string `json:"specificIp"` + Negation bool `json:"negation"` + StartIP string `json:"startIp"` + EndIP string `json:"endIp"` +} + +// RateLimitingStatus defines the observed state of RateLimiting +// +k8s:openapi-gen=true +type RateLimitingStatus struct { + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster + // Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file + // Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// RateLimiting is the Schema for the ratelimitings API +// +k8s:openapi-gen=true +type RateLimiting struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec RateLimitingSpec `json:"spec,omitempty"` + //Status RateLimitingStatus `json:"status,omitempty"` +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// RateLimitingList contains a list of RateLimiting +type RateLimitingList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []RateLimiting `json:"items"` +} + +func init() { + SchemeBuilder.Register(&RateLimiting{}, &RateLimitingList{}) +} diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go index 4733362..495c4ff 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go @@ -155,6 +155,22 @@ func (in *APIStatus) DeepCopy() *APIStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Bandwidth) DeepCopyInto(out *Bandwidth) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Bandwidth. +func (in *Bandwidth) DeepCopy() *Bandwidth { + if in == nil { + return nil + } + out := new(Bandwidth) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *BusinessInformation) DeepCopyInto(out *BusinessInformation) { *out = *in @@ -171,6 +187,24 @@ func (in *BusinessInformation) DeepCopy() *BusinessInformation { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Conditions) DeepCopyInto(out *Conditions) { + *out = *in + out.HeaderCondition = in.HeaderCondition + out.IPCondition = in.IPCondition + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Conditions. +func (in *Conditions) DeepCopy() *Conditions { + if in == nil { + return nil + } + out := new(Conditions) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Endpoint) DeepCopyInto(out *Endpoint) { *out = *in @@ -187,6 +221,149 @@ func (in *Endpoint) DeepCopy() *Endpoint { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HeaderCondition) DeepCopyInto(out *HeaderCondition) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HeaderCondition. +func (in *HeaderCondition) DeepCopy() *HeaderCondition { + if in == nil { + return nil + } + out := new(HeaderCondition) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *IPCondition) DeepCopyInto(out *IPCondition) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IPCondition. +func (in *IPCondition) DeepCopy() *IPCondition { + if in == nil { + return nil + } + out := new(IPCondition) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RateLimiting) DeepCopyInto(out *RateLimiting) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Spec = in.Spec + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RateLimiting. +func (in *RateLimiting) DeepCopy() *RateLimiting { + if in == nil { + return nil + } + out := new(RateLimiting) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *RateLimiting) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RateLimitingList) DeepCopyInto(out *RateLimitingList) { + *out = *in + out.TypeMeta = in.TypeMeta + out.ListMeta = in.ListMeta + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]RateLimiting, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RateLimitingList. +func (in *RateLimitingList) DeepCopy() *RateLimitingList { + if in == nil { + return nil + } + out := new(RateLimitingList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *RateLimitingList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RateLimitingSpec) DeepCopyInto(out *RateLimitingSpec) { + *out = *in + out.RequestCount = in.RequestCount + out.Bandwidth = in.Bandwidth + out.Conditions = in.Conditions + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RateLimitingSpec. +func (in *RateLimitingSpec) DeepCopy() *RateLimitingSpec { + if in == nil { + return nil + } + out := new(RateLimitingSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RateLimitingStatus) DeepCopyInto(out *RateLimitingStatus) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RateLimitingStatus. +func (in *RateLimitingStatus) DeepCopy() *RateLimitingStatus { + if in == nil { + return nil + } + out := new(RateLimitingStatus) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RequestCount) DeepCopyInto(out *RequestCount) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RequestCount. +func (in *RequestCount) DeepCopy() *RequestCount { + if in == nil { + return nil + } + out := new(RequestCount) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *URLPattern) DeepCopyInto(out *URLPattern) { *out = *in diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.defaults.go b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.defaults.go new file mode 100644 index 0000000..7985166 --- /dev/null +++ b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.defaults.go @@ -0,0 +1,16 @@ +// +build !ignore_autogenerated + +// Code generated by defaulter-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// RegisterDefaults adds defaulters functions to the given scheme. +// Public to allow building arbitrary schemes. +// All generated defaulters are covering - they call all nested defaulters. +func RegisterDefaults(scheme *runtime.Scheme) error { + return nil +} diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go index bdf0f04..cc81574 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go @@ -13,9 +13,12 @@ import ( func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition { return map[string]common.OpenAPIDefinition{ - "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.API": schema_pkg_apis_wso2_v1alpha1_API(ref), - "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.APISpec": schema_pkg_apis_wso2_v1alpha1_APISpec(ref), - "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.APIStatus": schema_pkg_apis_wso2_v1alpha1_APIStatus(ref), + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.API": schema_pkg_apis_wso2_v1alpha1_API(ref), + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.APISpec": schema_pkg_apis_wso2_v1alpha1_APISpec(ref), + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.APIStatus": schema_pkg_apis_wso2_v1alpha1_APIStatus(ref), + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RateLimiting": schema_pkg_apis_wso2_v1alpha1_RateLimiting(ref), + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RateLimitingSpec": schema_pkg_apis_wso2_v1alpha1_RateLimitingSpec(ref), + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RateLimitingStatus": schema_pkg_apis_wso2_v1alpha1_RateLimitingStatus(ref), } } @@ -67,10 +70,167 @@ func schema_pkg_apis_wso2_v1alpha1_APISpec(ref common.ReferenceCallback) common. Schema: spec.Schema{ SchemaProps: spec.SchemaProps{ Description: "APISpec defines the desired state of API", - Properties: map[string]spec.Schema{}, + Properties: map[string]spec.Schema{ + "name": { + SchemaProps: spec.SchemaProps{ + Description: "INSERT ADDITIONAL SPEC FIELDS - desired state of cluster Important: Run \"operator-sdk generate k8s\" to regenerate code after modifying this file Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html", + Type: []string{"string"}, + Format: "", + }, + }, + "context": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "version": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "description": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "tags": { + SchemaProps: spec.SchemaProps{ + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + }, + }, + }, + "endpoints": { + SchemaProps: spec.SchemaProps{ + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Endpoint"), + }, + }, + }, + }, + }, + "requestInterceptor": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "responseInterceptor": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "authorizationHeader": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "labels": { + SchemaProps: spec.SchemaProps{ + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + }, + }, + }, + "urlPatterns": { + SchemaProps: spec.SchemaProps{ + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.URLPattern"), + }, + }, + }, + }, + }, + "security": { + SchemaProps: spec.SchemaProps{ + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + }, + }, + }, + "subscriptionTiers": { + SchemaProps: spec.SchemaProps{ + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + }, + }, + }, + "advancedThrottlingPolicy": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "businessInformation": { + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.BusinessInformation"), + }, + }, + "apiProperties": { + SchemaProps: spec.SchemaProps{ + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.APIProperty"), + }, + }, + }, + }, + }, + "mode": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "replicaCount": { + SchemaProps: spec.SchemaProps{ + Type: []string{"integer"}, + Format: "int32", + }, + }, + }, + Required: []string{"name", "context", "version", "description", "tags", "endpoints", "requestInterceptor", "responseInterceptor", "authorizationHeader", "labels", "urlPatterns", "security", "subscriptionTiers", "advancedThrottlingPolicy", "businessInformation", "apiProperties", "mode", "replicaCount"}, }, }, - Dependencies: []string{}, + Dependencies: []string{ + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.APIProperty", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.BusinessInformation", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Endpoint", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.URLPattern"}, } } @@ -85,3 +245,107 @@ func schema_pkg_apis_wso2_v1alpha1_APIStatus(ref common.ReferenceCallback) commo Dependencies: []string{}, } } + +func schema_pkg_apis_wso2_v1alpha1_RateLimiting(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Description: "RateLimiting is the Schema for the ratelimitings API", + Properties: map[string]spec.Schema{ + "kind": { + SchemaProps: spec.SchemaProps{ + Description: "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds", + Type: []string{"string"}, + Format: "", + }, + }, + "apiVersion": { + SchemaProps: spec.SchemaProps{ + Description: "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources", + Type: []string{"string"}, + Format: "", + }, + }, + "metadata": { + SchemaProps: spec.SchemaProps{ + Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"), + }, + }, + "spec": { + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RateLimitingSpec"), + }, + }, + }, + }, + }, + Dependencies: []string{ + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RateLimitingSpec", "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"}, + } +} + +func schema_pkg_apis_wso2_v1alpha1_RateLimitingSpec(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Description: "RateLimitingSpec defines the desired state of RateLimiting", + Properties: map[string]spec.Schema{ + "type": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "description": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "timeUnit": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "unitTime": { + SchemaProps: spec.SchemaProps{ + Type: []string{"integer"}, + Format: "int32", + }, + }, + "requestCount": { + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RequestCount"), + }, + }, + "bandwidth": { + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Bandwidth"), + }, + }, + "conditions": { + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Conditions"), + }, + }, + }, + Required: []string{"type", "description", "timeUnit", "unitTime", "requestCount", "bandwidth", "conditions"}, + }, + }, + Dependencies: []string{ + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Bandwidth", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Conditions", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RequestCount"}, + } +} + +func schema_pkg_apis_wso2_v1alpha1_RateLimitingStatus(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Description: "RateLimitingStatus defines the observed state of RateLimiting", + Properties: map[string]spec.Schema{}, + }, + }, + Dependencies: []string{}, + } +} diff --git a/apim-operator/pkg/controller/add_ratelimiting.go b/apim-operator/pkg/controller/add_ratelimiting.go new file mode 100644 index 0000000..82fa111 --- /dev/null +++ b/apim-operator/pkg/controller/add_ratelimiting.go @@ -0,0 +1,10 @@ +package controller + +import ( + "github.com/apim-crd/apim-operator/pkg/controller/ratelimiting" +) + +func init() { + // AddToManagerFuncs is a list of functions to create controllers and add them to a manager. + AddToManagerFuncs = append(AddToManagerFuncs, ratelimiting.Add) +} diff --git a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go new file mode 100644 index 0000000..4966781 --- /dev/null +++ b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go @@ -0,0 +1,153 @@ +package ratelimiting + +import ( + "context" + + wso2v1alpha1 "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/manager" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" + "sigs.k8s.io/controller-runtime/pkg/source" +) + +var log = logf.Log.WithName("controller_ratelimiting") + +/** +* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller +* business logic. Delete these comments after modifying this file.* + */ + +// Add creates a new RateLimiting Controller and adds it to the Manager. The Manager will set fields on the Controller +// and Start it when the Manager is Started. +func Add(mgr manager.Manager) error { + return add(mgr, newReconciler(mgr)) +} + +// newReconciler returns a new reconcile.Reconciler +func newReconciler(mgr manager.Manager) reconcile.Reconciler { + return &ReconcileRateLimiting{client: mgr.GetClient(), scheme: mgr.GetScheme()} +} + +// add adds a new Controller to mgr with r as the reconcile.Reconciler +func add(mgr manager.Manager, r reconcile.Reconciler) error { + // Create a new controller + c, err := controller.New("ratelimiting-controller", mgr, controller.Options{Reconciler: r}) + if err != nil { + return err + } + + // Watch for changes to primary resource RateLimiting + err = c.Watch(&source.Kind{Type: &wso2v1alpha1.RateLimiting{}}, &handler.EnqueueRequestForObject{}) + if err != nil { + return err + } + + // TODO(user): Modify this to be the types you create that are owned by the primary resource + // Watch for changes to secondary resource Pods and requeue the owner RateLimiting + err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForOwner{ + IsController: true, + OwnerType: &wso2v1alpha1.RateLimiting{}, + }) + if err != nil { + return err + } + + return nil +} + +var _ reconcile.Reconciler = &ReconcileRateLimiting{} + +// ReconcileRateLimiting reconciles a RateLimiting object +type ReconcileRateLimiting struct { + // This client, initialized using mgr.Client() above, is a split client + // that reads objects from the cache and writes to the apiserver + client client.Client + scheme *runtime.Scheme +} + +// Reconcile reads that state of the cluster for a RateLimiting object and makes changes based on the state read +// and what is in the RateLimiting.Spec +// TODO(user): Modify this Reconcile function to implement your Controller logic. This example creates +// a Pod as an example +// Note: +// The Controller will requeue the Request to be processed again if the returned error is non-nil or +// Result.Requeue is true, otherwise upon completion it will remove the work from the queue. +func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile.Result, error) { + reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) + reqLogger.Info("Reconciling RateLimiting") + + // Fetch the RateLimiting instance + instance := &wso2v1alpha1.RateLimiting{} + err := r.client.Get(context.TODO(), request.NamespacedName, instance) + if err != nil { + if errors.IsNotFound(err) { + // Request object not found, could have been deleted after reconcile request. + // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. + // Return and don't requeue + return reconcile.Result{}, nil + } + // Error reading the object - requeue the request. + return reconcile.Result{}, err + } + + // Define a new Pod object + pod := newPodForCR(instance) + + // Set RateLimiting instance as the owner and controller + if err := controllerutil.SetControllerReference(instance, pod, r.scheme); err != nil { + return reconcile.Result{}, err + } + + // Check if this Pod already exists + found := &corev1.Pod{} + err = r.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, found) + if err != nil && errors.IsNotFound(err) { + reqLogger.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name) + err = r.client.Create(context.TODO(), pod) + if err != nil { + return reconcile.Result{}, err + } + + // Pod created successfully - don't requeue + return reconcile.Result{}, nil + } else if err != nil { + return reconcile.Result{}, err + } + + // Pod already exists - don't requeue + reqLogger.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name) + return reconcile.Result{}, nil +} + +// newPodForCR returns a busybox pod with the same name/namespace as the cr +func newPodForCR(cr *wso2v1alpha1.RateLimiting) *corev1.Pod { + labels := map[string]string{ + "app": cr.Name, + } + return &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: cr.Name + "-pod", + Namespace: cr.Namespace, + Labels: labels, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "busybox", + Image: "busybox", + Command: []string{"sleep", "3600"}, + }, + }, + }, + } +} From 2194199ff504816f90d3380e5ad096317482275e Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Tue, 7 May 2019 14:49:15 +0530 Subject: [PATCH 04/13] Modifying ratelimiting kind --- apim-operator/build/Dockerfile | 1 + apim-operator/build/policy.mustache | 58 ++++++++++++ .../crds/wso2_v1alpha1_ratelimiting_cr.yaml | 4 +- .../ratelimiting/ratelimiting_controller.go | 88 +++++++++++++++++++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 apim-operator/build/policy.mustache diff --git a/apim-operator/build/Dockerfile b/apim-operator/build/Dockerfile index 803cc02..65cecb7 100644 --- a/apim-operator/build/Dockerfile +++ b/apim-operator/build/Dockerfile @@ -6,6 +6,7 @@ ENV OPERATOR=/usr/local/bin/apim-operator \ # install operator binary COPY build/_output/bin/apim-operator ${OPERATOR} +COPY build/policy.mustache /usr/local/bin COPY build/bin /usr/local/bin RUN /usr/local/bin/user_setup diff --git a/apim-operator/build/policy.mustache b/apim-operator/build/policy.mustache new file mode 100644 index 0000000..cb8047e --- /dev/null +++ b/apim-operator/build/policy.mustache @@ -0,0 +1,58 @@ +import ballerina/io; +import ballerina/runtime; +import ballerina/http; +import ballerina/log; +import wso2/gateway; + +stream s{{name}}intermediateStream = new; +stream s{{name}}resultStream = new; +stream s{{name}}eligibilityStream = new; +stream s{{name}}reqCopy= gateway:requestStream; +stream s{{name}}globalThrotCopy = gateway:globalThrottleStream; + +function {{funcName}}() { + +forever { + from s{{name}}reqCopy + select s{{name}}reqCopy.messageID as messageID, (s{{name}}reqCopy.{{tierType}} == "{{name}}") as + isEligible, s{{name}}reqCopy.{{policyKey}} as throttleKey, 0 as expiryTimestamp + => (gateway:EligibilityStreamDTO[] counts) { + foreach var c in counts{ + s{{name}}eligibilityStream.publish(c); + } + } + + from s{{name}}eligibilityStream + throttler:timeBatch({{unitTime}}) + where s{{name}}eligibilityStream.isEligible == true + select s{{name}}eligibilityStream.throttleKey as throttleKey, count() as eventCount, {{stopOnQuotaReach}} as + stopOnQuota, expiryTimeStamp + group by s{{name}}eligibilityStream.throttleKey + => (gateway:IntermediateStream[] counts) { + foreach var c in counts{ + s{{name}}intermediateStream.publish(c); + } + } + + from s{{name}}intermediateStream + select s{{name}}intermediateStream.throttleKey, s{{name}}intermediateStream.eventCount>= {{count}} as isThrottled, + s{{name}}intermediateStream.stopOnQuota, s{{name}}intermediateStream.expiryTimeStamp + group by s{{name}}eligibilityStream.throttleKey + => (gateway:GlobalThrottleStreamDTO[] counts) { + foreach var c in counts{ + s{{name}}resultStream.publish(c); + } + } + + from s{{name}}resultStream + throttler:emitOnStateChange(s{{name}}resultStream.throttleKey, s{{name}}resultStream.isThrottled) + select s{{name}}resultStream.throttleKey as throttleKey, s{{name}}resultStream.isThrottled, + s{{name}}resultStream.stopOnQuota, s{{name}}resultStream.expiryTimeStamp + => (gateway:GlobalThrottleStreamDTO[] counts) { + foreach var c in counts{ + s{{name}}globalThrotCopy.publish(c); + } + } +} +} + diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml index f4b58de..599b3e8 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml @@ -1,11 +1,11 @@ apiVersion: wso2.com/v1alpha1 kind: RateLimiting metadata: - name: bronze-ratelimiting + name: silver-ratelimiting spec: # Add fields here #size: 3 - type: advanced # application ,subscription + type: Subscription # application ,subscription description: Allow 1000 requests per minute timeUnit: min unitTime: 1 diff --git a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go index 4966781..be9e3e1 100644 --- a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go +++ b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go @@ -1,6 +1,7 @@ package ratelimiting import ( + //"k8s.io/client-go/kubernetes" "context" wso2v1alpha1 "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1" @@ -18,6 +19,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" "sigs.k8s.io/controller-runtime/pkg/source" + + "strconv" + "strings" + + "fmt" + + mustache "github.com/cbroglie/mustache" ) var log = logf.Log.WithName("controller_ratelimiting") @@ -100,6 +108,66 @@ func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile. return reconcile.Result{}, err } + // GENERATE POLICY CODE USING CRD INSTANCE + + nameArray := strings.Split(instance.ObjectMeta.Name, "-") + name := nameArray[0] + log.Info(name) + + funcName := "init" + instance.Spec.Type + name + "Policy" + log.Info(funcName) + + tierType := instance.Spec.Type + "Tier" + log.Info(tierType) + + policyKey := instance.Spec.Type + "Key" + log.Info(policyKey) + + unitTime := strconv.Itoa(instance.Spec.UnitTime) + log.Info(unitTime) + + count := strconv.Itoa(instance.Spec.RequestCount.Limit) + log.Info(count) + + filename := "/usr/local/bin/policy.mustache" + output, err := mustache.RenderFile(filename, map[string]string{"name": name, "funcName": funcName, "tierType": tierType, "policyKey": policyKey, "unitTime": unitTime, "stopOnQuotaReach": "true", "count": count}) + + log.Info(output) + fmt.Println(output) + + if err != nil { + log.Error(err, "error in rendering ") + } + + //CREATE CONFIG MAP + + confmap, confEr := createConfigMap(output, name, instance) + fmt.Println(confmap) + log.Error(confEr, "Error in config map structure creation") + + //confmapCreate, confEr :=kubernetes.Interface.CoreV1().ConfigMap(instance.Namespace).Create(confmap) + // confEr = r.client.Create(context.TODO(), confmap) + //log.Error(confEr, "Error in config map instance creation") + + // Check if this configmap already exists + foundmap := &corev1.ConfigMap{} + err = r.client.Get(context.TODO(), types.NamespacedName{Name: confmap.Name, Namespace: confmap.Namespace}, foundmap) + log.Error(err, "error 1") + if err != nil && errors.IsNotFound(err) { + reqLogger.Info("Creating a new Config map", "confmap.Namespace", confmap.Namespace, "confmap.Name", confmap.Name) + err = r.client.Create(context.TODO(), confmap) + if err != nil { + log.Error(err, "error 2") + //return reconcile.Result{}, err + } + + // confmap created successfully - don't requeue + //return reconcile.Result{}, nil + } else if err != nil { + //return reconcile.Result{}, err + log.Error(err, "error 3") + } + // Define a new Pod object pod := newPodForCR(instance) @@ -151,3 +219,23 @@ func newPodForCR(cr *wso2v1alpha1.RateLimiting) *corev1.Pod { }, } } + +// createConfigMap creates a config file with the generated code +func createConfigMap(output string, name string, cr *wso2v1alpha1.RateLimiting) (*corev1.ConfigMap, error) { + + //mapName := name + "ConfMap" + return &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: cr.Name + "-configmap", + Namespace: cr.Namespace, + // Namespace: gateway.Namespace, + // Labels: createGatewayLabels(gateway), + // OwnerReferences: []metav1.OwnerReference{ + // *controller.CreateGatewayOwnerRef(gateway), + // }, + }, + Data: map[string]string{ + "Code": output, + }, + }, nil +} From 3e0371cf41a82a89e903e509543985aff9655b8a Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Tue, 7 May 2019 15:25:54 +0530 Subject: [PATCH 05/13] Modifying the RateLimiting kind --- .../crds/wso2_v1alpha1_ratelimiting_cr.yaml | 2 +- .../ratelimiting/ratelimiting_controller.go | 78 +++---------------- 2 files changed, 11 insertions(+), 69 deletions(-) diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml index 599b3e8..41223db 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml @@ -1,7 +1,7 @@ apiVersion: wso2.com/v1alpha1 kind: RateLimiting metadata: - name: silver-ratelimiting + name: bronze-ratelimiting spec: # Add fields here #size: 3 diff --git a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go index be9e3e1..771ea66 100644 --- a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go +++ b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go @@ -1,7 +1,6 @@ package ratelimiting import ( - //"k8s.io/client-go/kubernetes" "context" wso2v1alpha1 "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1" @@ -13,7 +12,7 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -142,97 +141,40 @@ func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile. //CREATE CONFIG MAP confmap, confEr := createConfigMap(output, name, instance) - fmt.Println(confmap) - log.Error(confEr, "Error in config map structure creation") - - //confmapCreate, confEr :=kubernetes.Interface.CoreV1().ConfigMap(instance.Namespace).Create(confmap) - // confEr = r.client.Create(context.TODO(), confmap) - //log.Error(confEr, "Error in config map instance creation") + if confEr != nil { + log.Error(confEr, "Error in config map structure creation") + } // Check if this configmap already exists foundmap := &corev1.ConfigMap{} err = r.client.Get(context.TODO(), types.NamespacedName{Name: confmap.Name, Namespace: confmap.Namespace}, foundmap) - log.Error(err, "error 1") + if err != nil && errors.IsNotFound(err) { reqLogger.Info("Creating a new Config map", "confmap.Namespace", confmap.Namespace, "confmap.Name", confmap.Name) err = r.client.Create(context.TODO(), confmap) if err != nil { - log.Error(err, "error 2") - //return reconcile.Result{}, err - } - - // confmap created successfully - don't requeue - //return reconcile.Result{}, nil - } else if err != nil { - //return reconcile.Result{}, err - log.Error(err, "error 3") - } - - // Define a new Pod object - pod := newPodForCR(instance) - - // Set RateLimiting instance as the owner and controller - if err := controllerutil.SetControllerReference(instance, pod, r.scheme); err != nil { - return reconcile.Result{}, err - } - - // Check if this Pod already exists - found := &corev1.Pod{} - err = r.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, found) - if err != nil && errors.IsNotFound(err) { - reqLogger.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name) - err = r.client.Create(context.TODO(), pod) - if err != nil { + log.Error(err, "error ") return reconcile.Result{}, err } - // Pod created successfully - don't requeue + // confmap created successfully - don't requeue return reconcile.Result{}, nil } else if err != nil { + log.Error(err, "error ") return reconcile.Result{}, err } - - // Pod already exists - don't requeue - reqLogger.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name) + reqLogger.Info("Skip reconcile: map already exists", "confmap.Namespace", foundmap.Namespace, "confmap.Name", foundmap.Name) return reconcile.Result{}, nil -} -// newPodForCR returns a busybox pod with the same name/namespace as the cr -func newPodForCR(cr *wso2v1alpha1.RateLimiting) *corev1.Pod { - labels := map[string]string{ - "app": cr.Name, - } - return &corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: cr.Name + "-pod", - Namespace: cr.Namespace, - Labels: labels, - }, - Spec: corev1.PodSpec{ - Containers: []corev1.Container{ - { - Name: "busybox", - Image: "busybox", - Command: []string{"sleep", "3600"}, - }, - }, - }, - } } // createConfigMap creates a config file with the generated code func createConfigMap(output string, name string, cr *wso2v1alpha1.RateLimiting) (*corev1.ConfigMap, error) { - //mapName := name + "ConfMap" return &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: cr.Name + "-configmap", + Name: cr.Name + "-configmap", Namespace: cr.Namespace, - // Namespace: gateway.Namespace, - // Labels: createGatewayLabels(gateway), - // OwnerReferences: []metav1.OwnerReference{ - // *controller.CreateGatewayOwnerRef(gateway), - // }, }, Data: map[string]string{ "Code": output, From e7f760b71cc9751fa9bde484966e20353ca38f6a Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Tue, 7 May 2019 16:56:59 +0530 Subject: [PATCH 06/13] namespace changes --- apim-operator/cmd/manager/main.go | 14 +++++++------- .../deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml | 2 +- apim-operator/deploy/operator.yaml | 4 +--- apim-operator/deploy/role.yaml | 2 +- apim-operator/deploy/role_binding.yaml | 6 ++++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/apim-operator/cmd/manager/main.go b/apim-operator/cmd/manager/main.go index 4294e8c..75e7324 100644 --- a/apim-operator/cmd/manager/main.go +++ b/apim-operator/cmd/manager/main.go @@ -13,7 +13,7 @@ import ( "github.com/apim-crd/apim-operator/pkg/apis" "github.com/apim-crd/apim-operator/pkg/controller" - "github.com/operator-framework/operator-sdk/pkg/k8sutil" + //"github.com/operator-framework/operator-sdk/pkg/k8sutil" "github.com/operator-framework/operator-sdk/pkg/leader" "github.com/operator-framework/operator-sdk/pkg/log/zap" "github.com/operator-framework/operator-sdk/pkg/metrics" @@ -62,11 +62,11 @@ func main() { printVersion() - namespace, err := k8sutil.GetWatchNamespace() - if err != nil { - log.Error(err, "Failed to get watch namespace") - os.Exit(1) - } + // namespace, err := k8sutil.GetWatchNamespace() + // if err != nil { + // log.Error(err, "Failed to get watch namespace") + // os.Exit(1) + // } // Get a config to talk to the apiserver cfg, err := config.GetConfig() @@ -86,7 +86,7 @@ func main() { // Create a new Cmd to provide shared dependencies and start components mgr, err := manager.New(cfg, manager.Options{ - Namespace: namespace, + Namespace: "", MapperProvider: restmapper.NewDynamicRESTMapper, MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), }) diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml index 41223db..27c3af9 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml @@ -1,7 +1,7 @@ apiVersion: wso2.com/v1alpha1 kind: RateLimiting metadata: - name: bronze-ratelimiting + name: test-ratelimiting spec: # Add fields here #size: 3 diff --git a/apim-operator/deploy/operator.yaml b/apim-operator/deploy/operator.yaml index 5a9dca5..48f13d7 100644 --- a/apim-operator/deploy/operator.yaml +++ b/apim-operator/deploy/operator.yaml @@ -22,9 +22,7 @@ spec: imagePullPolicy: Always env: - name: WATCH_NAMESPACE - valueFrom: - fieldRef: - fieldPath: metadata.namespace + value: "" - name: POD_NAME valueFrom: fieldRef: diff --git a/apim-operator/deploy/role.yaml b/apim-operator/deploy/role.yaml index 8970e45..25dfa55 100644 --- a/apim-operator/deploy/role.yaml +++ b/apim-operator/deploy/role.yaml @@ -1,5 +1,5 @@ apiVersion: rbac.authorization.k8s.io/v1 -kind: Role +kind: ClusterRole metadata: creationTimestamp: null name: apim-operator diff --git a/apim-operator/deploy/role_binding.yaml b/apim-operator/deploy/role_binding.yaml index b878550..97624f3 100644 --- a/apim-operator/deploy/role_binding.yaml +++ b/apim-operator/deploy/role_binding.yaml @@ -1,11 +1,13 @@ -kind: RoleBinding +kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: apim-operator subjects: - kind: ServiceAccount name: apim-operator + # Replace this with the namespace the operator is deployed in. + namespace: wso2system roleRef: - kind: Role + kind: ClusterRole name: apim-operator apiGroup: rbac.authorization.k8s.io From 0c7947aea40a34071ba0449d70bd56835a1ca742 Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Wed, 8 May 2019 07:17:57 +0530 Subject: [PATCH 07/13] modifying ratelimiting fields --- .../crds/wso2_v1alpha1_ratelimiting_cr.yaml | 29 ++++---- .../crds/wso2_v1alpha1_ratelimiting_crd.yaml | 52 +------------- .../apis/wso2/v1alpha1/ratelimiting_types.go | 55 +++++++-------- .../wso2/v1alpha1/zz_generated.deepcopy.go | 68 ------------------- .../wso2/v1alpha1/zz_generated.openapi.go | 20 ++---- .../ratelimiting/ratelimiting_controller.go | 4 ++ 6 files changed, 53 insertions(+), 175 deletions(-) diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml index 27c3af9..7d17b54 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml @@ -1,26 +1,25 @@ apiVersion: wso2.com/v1alpha1 kind: RateLimiting metadata: - name: test-ratelimiting + name: testing2-ratelimiting spec: - # Add fields here - #size: 3 type: Subscription # application ,subscription - description: Allow 1000 requests per minute + #description: Allow 1000 requests per minute timeUnit: min unitTime: 1 requestCount: - limit: 1000 + limit: 2000 bandwidth: dataAmount: "" dataUnit: "" - conditions: # not required for application and subscription policies - headerCondition: - headerName: “host” - headerValue: “abc.com” - ipCondition: - type: ipRange - specificIp: "" - negation : no - startIp: 10.100.7.2 - endIp: 10.100.7.255 \ No newline at end of file + #stopOnQuotaReach: true + # conditions: # not required for application and subscription policies + # headerCondition: + # headerName: “host” + # headerValue: “abc.com” + # ipCondition: + # type: ipRange + # specificIp: "" + # negation : no + # startIp: 10.100.7.2 + # endIp: 10.100.7.255 \ No newline at end of file diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml index 54f37af..c1745f7 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml @@ -29,53 +29,6 @@ spec: type: object spec: properties: - bandwidth: - properties: - dataAmount: - type: string - dataUnit: - type: string - required: - - dataAmount - - dataUnit - type: object - conditions: - properties: - headerCondition: - properties: - headerName: - type: string - headerValue: - type: string - required: - - headerName - - headerValue - type: object - ipCondition: - properties: - endIp: - type: string - negation: - type: boolean - specificIp: - type: string - startIp: - type: string - type: - type: string - required: - - type - - specificIp - - negation - - startIp - - endIp - type: object - required: - - headerCondition - - ipCondition - type: object - description: - type: string requestCount: properties: limit: @@ -84,6 +37,8 @@ spec: required: - limit type: object + stopOnQuotaReach: + type: boolean timeUnit: type: string type: @@ -93,12 +48,9 @@ spec: type: integer required: - type - - description - timeUnit - unitTime - requestCount - - bandwidth - - conditions type: object version: v1alpha1 versions: diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go b/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go index df8ea97..fdb6762 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go @@ -14,13 +14,14 @@ type RateLimitingSpec struct { // Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file // Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html - Type string `json:"type"` - Description string `json:"description"` - TimeUnit string `json:"timeUnit"` - UnitTime int `json:"unitTime"` - RequestCount RequestCount `json:"requestCount"` - Bandwidth Bandwidth `json:"bandwidth"` - Conditions Conditions `json:"conditions"` + Type string `json:"type"` + TimeUnit string `json:"timeUnit"` + UnitTime int `json:"unitTime"` + RequestCount RequestCount `json:"requestCount"` + StopOnQuotaReach bool `json:"stopOnQuotaReach"` + //Description string `json:"description"` + //Bandwidth Bandwidth `json:"bandwidth"` + //Conditions Conditions `json:"conditions"` } //RequestCount is exported type in Ratelimiting Spec @@ -29,31 +30,31 @@ type RequestCount struct { } //Bandwidth is exported type in Ratelimiting Spec -type Bandwidth struct { - DataAmount string `json:"dataAmount"` - DataUnit string `json:"dataUnit"` -} +// type Bandwidth struct { +// DataAmount string `json:"dataAmount"` +// DataUnit string `json:"dataUnit"` +// } //Conditions is exported type in Ratelimiting Spec -type Conditions struct { - HeaderCondition HeaderCondition `json:"headerCondition"` - IPCondition IPCondition `json:"ipCondition"` -} +// type Conditions struct { +// HeaderCondition HeaderCondition `json:"headerCondition"` +// IPCondition IPCondition `json:"ipCondition"` +// } //HeaderCondition is exported type in Ratelimiting Spec -type HeaderCondition struct { - HeaderName string `json:"headerName"` - HeaderValue string `json:"headerValue"` -} +// type HeaderCondition struct { +// HeaderName string `json:"headerName"` +// HeaderValue string `json:"headerValue"` +// } //IPCondition is exported type in Ratelimiting Spec -type IPCondition struct { - Type string `json:"type"` - SpecificIP string `json:"specificIp"` - Negation bool `json:"negation"` - StartIP string `json:"startIp"` - EndIP string `json:"endIp"` -} +// type IPCondition struct { +// Type string `json:"type"` +// SpecificIP string `json:"specificIp"` +// Negation bool `json:"negation"` +// StartIP string `json:"startIp"` +// EndIP string `json:"endIp"` +// } // RateLimitingStatus defines the observed state of RateLimiting // +k8s:openapi-gen=true @@ -71,7 +72,7 @@ type RateLimiting struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` - Spec RateLimitingSpec `json:"spec,omitempty"` + Spec RateLimitingSpec `json:"spec,omitempty"` //Status RateLimitingStatus `json:"status,omitempty"` } diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go index 495c4ff..594bc36 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go @@ -155,22 +155,6 @@ func (in *APIStatus) DeepCopy() *APIStatus { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Bandwidth) DeepCopyInto(out *Bandwidth) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Bandwidth. -func (in *Bandwidth) DeepCopy() *Bandwidth { - if in == nil { - return nil - } - out := new(Bandwidth) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *BusinessInformation) DeepCopyInto(out *BusinessInformation) { *out = *in @@ -187,24 +171,6 @@ func (in *BusinessInformation) DeepCopy() *BusinessInformation { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Conditions) DeepCopyInto(out *Conditions) { - *out = *in - out.HeaderCondition = in.HeaderCondition - out.IPCondition = in.IPCondition - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Conditions. -func (in *Conditions) DeepCopy() *Conditions { - if in == nil { - return nil - } - out := new(Conditions) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Endpoint) DeepCopyInto(out *Endpoint) { *out = *in @@ -221,38 +187,6 @@ func (in *Endpoint) DeepCopy() *Endpoint { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *HeaderCondition) DeepCopyInto(out *HeaderCondition) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HeaderCondition. -func (in *HeaderCondition) DeepCopy() *HeaderCondition { - if in == nil { - return nil - } - out := new(HeaderCondition) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *IPCondition) DeepCopyInto(out *IPCondition) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IPCondition. -func (in *IPCondition) DeepCopy() *IPCondition { - if in == nil { - return nil - } - out := new(IPCondition) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *RateLimiting) DeepCopyInto(out *RateLimiting) { *out = *in @@ -317,8 +251,6 @@ func (in *RateLimitingList) DeepCopyObject() runtime.Object { func (in *RateLimitingSpec) DeepCopyInto(out *RateLimitingSpec) { *out = *in out.RequestCount = in.RequestCount - out.Bandwidth = in.Bandwidth - out.Conditions = in.Conditions return } diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go index cc81574..0e2ef4a 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go @@ -296,12 +296,6 @@ func schema_pkg_apis_wso2_v1alpha1_RateLimitingSpec(ref common.ReferenceCallback Format: "", }, }, - "description": { - SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", - }, - }, "timeUnit": { SchemaProps: spec.SchemaProps{ Type: []string{"string"}, @@ -319,22 +313,18 @@ func schema_pkg_apis_wso2_v1alpha1_RateLimitingSpec(ref common.ReferenceCallback Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RequestCount"), }, }, - "bandwidth": { + "stopOnQuotaReach": { SchemaProps: spec.SchemaProps{ - Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Bandwidth"), - }, - }, - "conditions": { - SchemaProps: spec.SchemaProps{ - Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Conditions"), + Type: []string{"boolean"}, + Format: "", }, }, }, - Required: []string{"type", "description", "timeUnit", "unitTime", "requestCount", "bandwidth", "conditions"}, + Required: []string{"type", "timeUnit", "unitTime", "requestCount"}, }, }, Dependencies: []string{ - "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Bandwidth", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Conditions", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RequestCount"}, + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RequestCount"}, } } diff --git a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go index 771ea66..5ce4dac 100644 --- a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go +++ b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go @@ -128,6 +128,10 @@ func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile. count := strconv.Itoa(instance.Spec.RequestCount.Limit) log.Info(count) + stopOnQuotaReach := strconv.FormatBool(instance.Spec.StopOnQuotaReach) + log.Info("QUOTAREACH") + log.Info(stopOnQuotaReach) + filename := "/usr/local/bin/policy.mustache" output, err := mustache.RenderFile(filename, map[string]string{"name": name, "funcName": funcName, "tierType": tierType, "policyKey": policyKey, "unitTime": unitTime, "stopOnQuotaReach": "true", "count": count}) From f754fc62a4cdb7ae9627b976556814deece1e3e5 Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Wed, 8 May 2019 07:21:49 +0530 Subject: [PATCH 08/13] change ns name --- apim-operator/deploy/role_binding.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apim-operator/deploy/role_binding.yaml b/apim-operator/deploy/role_binding.yaml index 97624f3..06a9c97 100644 --- a/apim-operator/deploy/role_binding.yaml +++ b/apim-operator/deploy/role_binding.yaml @@ -6,7 +6,7 @@ subjects: - kind: ServiceAccount name: apim-operator # Replace this with the namespace the operator is deployed in. - namespace: wso2system + namespace: wso2-system roleRef: kind: ClusterRole name: apim-operator From 8fd58db07d91e9a47fe0ffbd9b6156384e191797 Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Thu, 9 May 2019 10:39:45 +0530 Subject: [PATCH 09/13] Adding ns yaml and changes to ratelimiting kind template types --- .../crds/wso2_v1alpha1_ratelimiting_cr.yaml | 8 ++-- apim-operator/deploy/namespace.yaml | 6 +++ apim-operator/deploy/operator.yaml | 1 + apim-operator/deploy/role.yaml | 1 + apim-operator/deploy/role_binding.yaml | 1 + apim-operator/deploy/service_account.yaml | 1 + .../ratelimiting/ratelimiting_controller.go | 38 +++++++++++++++---- 7 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 apim-operator/deploy/namespace.yaml diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml index 7d17b54..ee4acac 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml @@ -1,18 +1,18 @@ apiVersion: wso2.com/v1alpha1 kind: RateLimiting metadata: - name: testing2-ratelimiting + name: testing1-ratelimiting spec: - type: Subscription # application ,subscription + type: subscription # application ,subscription #description: Allow 1000 requests per minute timeUnit: min unitTime: 1 requestCount: - limit: 2000 + limit: 1000 bandwidth: dataAmount: "" dataUnit: "" - #stopOnQuotaReach: true + stopOnQuotaReach: false # conditions: # not required for application and subscription policies # headerCondition: # headerName: “host” diff --git a/apim-operator/deploy/namespace.yaml b/apim-operator/deploy/namespace.yaml new file mode 100644 index 0000000..a2b10b4 --- /dev/null +++ b/apim-operator/deploy/namespace.yaml @@ -0,0 +1,6 @@ +kind: Namespace +apiVersion: v1 +metadata: + name: wso2-system + labels: + name: wso2-system \ No newline at end of file diff --git a/apim-operator/deploy/operator.yaml b/apim-operator/deploy/operator.yaml index 48f13d7..552a831 100644 --- a/apim-operator/deploy/operator.yaml +++ b/apim-operator/deploy/operator.yaml @@ -2,6 +2,7 @@ apiVersion: apps/v1 kind: Deployment metadata: name: apim-operator + namespace: wso2-system spec: replicas: 1 selector: diff --git a/apim-operator/deploy/role.yaml b/apim-operator/deploy/role.yaml index 25dfa55..ddfb806 100644 --- a/apim-operator/deploy/role.yaml +++ b/apim-operator/deploy/role.yaml @@ -3,6 +3,7 @@ kind: ClusterRole metadata: creationTimestamp: null name: apim-operator + namespace: wso2-system rules: - apiGroups: - "" diff --git a/apim-operator/deploy/role_binding.yaml b/apim-operator/deploy/role_binding.yaml index 06a9c97..bfd8c45 100644 --- a/apim-operator/deploy/role_binding.yaml +++ b/apim-operator/deploy/role_binding.yaml @@ -2,6 +2,7 @@ kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: apim-operator + namespace: wso2-system subjects: - kind: ServiceAccount name: apim-operator diff --git a/apim-operator/deploy/service_account.yaml b/apim-operator/deploy/service_account.yaml index a6dc42c..aee8d35 100644 --- a/apim-operator/deploy/service_account.yaml +++ b/apim-operator/deploy/service_account.yaml @@ -2,3 +2,4 @@ apiVersion: v1 kind: ServiceAccount metadata: name: apim-operator + namespace: wso2-system diff --git a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go index 5ce4dac..5bda00e 100644 --- a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go +++ b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go @@ -113,27 +113,51 @@ func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile. name := nameArray[0] log.Info(name) - funcName := "init" + instance.Spec.Type + name + "Policy" + policyType := instance.Spec.Type + if policyType == "subscription" { + policyType = "Subscription" + } else if policyType == "application" { + policyType = "Application" + } + + funcName := "init" + policyType + name + "Policy" log.Info(funcName) - tierType := instance.Spec.Type + "Tier" + var tierType string + var policyKey string + tierType = instance.Spec.Type + "Tier" + if policyType == "Application" { + tierType = "appTier" + policyKey = "appKey" + } else if policyType == "Subscription" { + tierType = "subscriptionTier" + policyKey = "subscriptionKey" + } log.Info(tierType) - - policyKey := instance.Spec.Type + "Key" log.Info(policyKey) - unitTime := strconv.Itoa(instance.Spec.UnitTime) + var unitTime string + if instance.Spec.TimeUnit == "sec" || instance.Spec.TimeUnit == "seconds" { + unitTime = strconv.Itoa(instance.Spec.UnitTime) + } else { + unitTime = strconv.Itoa(instance.Spec.UnitTime * 60000) + } log.Info(unitTime) count := strconv.Itoa(instance.Spec.RequestCount.Limit) log.Info(count) - stopOnQuotaReach := strconv.FormatBool(instance.Spec.StopOnQuotaReach) + var stopOnQuotaReach string + if policyType == "Subscription" { + stopOnQuotaReach = strconv.FormatBool(instance.Spec.StopOnQuotaReach) + } else { + stopOnQuotaReach = "true" + } log.Info("QUOTAREACH") log.Info(stopOnQuotaReach) filename := "/usr/local/bin/policy.mustache" - output, err := mustache.RenderFile(filename, map[string]string{"name": name, "funcName": funcName, "tierType": tierType, "policyKey": policyKey, "unitTime": unitTime, "stopOnQuotaReach": "true", "count": count}) + output, err := mustache.RenderFile(filename, map[string]string{"name": name, "funcName": funcName, "tierType": tierType, "policyKey": policyKey, "unitTime": unitTime, "stopOnQuotaReach": stopOnQuotaReach, "count": count}) log.Info(output) fmt.Println(output) From a543bfc060c162f307c662a6257bd4b6e6a65522 Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Thu, 9 May 2019 11:34:23 +0530 Subject: [PATCH 10/13] Remove watch ns --- apim-operator/cmd/manager/main.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/apim-operator/cmd/manager/main.go b/apim-operator/cmd/manager/main.go index 75e7324..cca85d4 100644 --- a/apim-operator/cmd/manager/main.go +++ b/apim-operator/cmd/manager/main.go @@ -13,7 +13,6 @@ import ( "github.com/apim-crd/apim-operator/pkg/apis" "github.com/apim-crd/apim-operator/pkg/controller" - //"github.com/operator-framework/operator-sdk/pkg/k8sutil" "github.com/operator-framework/operator-sdk/pkg/leader" "github.com/operator-framework/operator-sdk/pkg/log/zap" "github.com/operator-framework/operator-sdk/pkg/metrics" @@ -62,12 +61,6 @@ func main() { printVersion() - // namespace, err := k8sutil.GetWatchNamespace() - // if err != nil { - // log.Error(err, "Failed to get watch namespace") - // os.Exit(1) - // } - // Get a config to talk to the apiserver cfg, err := config.GetConfig() if err != nil { From 8f75833d216d8d7bfa58cb236cfdce5c144913e7 Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Thu, 9 May 2019 12:22:04 +0530 Subject: [PATCH 11/13] modifying crd fields --- .../crds/wso2_v1alpha1_ratelimiting_cr.yaml | 28 ++++---- .../crds/wso2_v1alpha1_ratelimiting_crd.yaml | 47 +++++++++++++ .../apis/wso2/v1alpha1/ratelimiting_types.go | 44 ++++++------ .../wso2/v1alpha1/zz_generated.deepcopy.go | 68 +++++++++++++++++++ .../wso2/v1alpha1/zz_generated.openapi.go | 18 ++++- 5 files changed, 168 insertions(+), 37 deletions(-) diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml index ee4acac..1ca1cca 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_cr.yaml @@ -4,22 +4,22 @@ metadata: name: testing1-ratelimiting spec: type: subscription # application ,subscription - #description: Allow 1000 requests per minute - timeUnit: min + description: Allow 1000 requests per minute # optional + timeUnit: min # min or sec unitTime: 1 requestCount: limit: 1000 - bandwidth: + bandwidth: # optional dataAmount: "" dataUnit: "" - stopOnQuotaReach: false - # conditions: # not required for application and subscription policies - # headerCondition: - # headerName: “host” - # headerValue: “abc.com” - # ipCondition: - # type: ipRange - # specificIp: "" - # negation : no - # startIp: 10.100.7.2 - # endIp: 10.100.7.255 \ No newline at end of file + stopOnQuotaReach: false # not required for application policies + conditions: # optional + headerCondition: + headerName: “host” + headerValue: “abc.com” + ipCondition: + type: ipRange + specificIp: "" + negation : no + startIp: 10.100.7.2 + endIp: 10.100.7.255 \ No newline at end of file diff --git a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml index c1745f7..28bc531 100644 --- a/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml +++ b/apim-operator/deploy/crds/wso2_v1alpha1_ratelimiting_crd.yaml @@ -29,6 +29,53 @@ spec: type: object spec: properties: + bandwidth: + properties: + dataAmount: + type: string + dataUnit: + type: string + required: + - dataAmount + - dataUnit + type: object + conditions: + properties: + headerCondition: + properties: + headerName: + type: string + headerValue: + type: string + required: + - headerName + - headerValue + type: object + ipCondition: + properties: + endIp: + type: string + negation: + type: boolean + specificIp: + type: string + startIp: + type: string + type: + type: string + required: + - type + - specificIp + - negation + - startIp + - endIp + type: object + required: + - headerCondition + - ipCondition + type: object + description: + type: string requestCount: properties: limit: diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go b/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go index fdb6762..a38a42f 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/ratelimiting_types.go @@ -19,9 +19,9 @@ type RateLimitingSpec struct { UnitTime int `json:"unitTime"` RequestCount RequestCount `json:"requestCount"` StopOnQuotaReach bool `json:"stopOnQuotaReach"` - //Description string `json:"description"` - //Bandwidth Bandwidth `json:"bandwidth"` - //Conditions Conditions `json:"conditions"` + Description string `json:"description"` + Bandwidth Bandwidth `json:"bandwidth"` + Conditions Conditions `json:"conditions"` } //RequestCount is exported type in Ratelimiting Spec @@ -30,31 +30,31 @@ type RequestCount struct { } //Bandwidth is exported type in Ratelimiting Spec -// type Bandwidth struct { -// DataAmount string `json:"dataAmount"` -// DataUnit string `json:"dataUnit"` -// } +type Bandwidth struct { + DataAmount string `json:"dataAmount"` + DataUnit string `json:"dataUnit"` +} //Conditions is exported type in Ratelimiting Spec -// type Conditions struct { -// HeaderCondition HeaderCondition `json:"headerCondition"` -// IPCondition IPCondition `json:"ipCondition"` -// } +type Conditions struct { + HeaderCondition HeaderCondition `json:"headerCondition"` + IPCondition IPCondition `json:"ipCondition"` +} //HeaderCondition is exported type in Ratelimiting Spec -// type HeaderCondition struct { -// HeaderName string `json:"headerName"` -// HeaderValue string `json:"headerValue"` -// } +type HeaderCondition struct { + HeaderName string `json:"headerName"` + HeaderValue string `json:"headerValue"` +} //IPCondition is exported type in Ratelimiting Spec -// type IPCondition struct { -// Type string `json:"type"` -// SpecificIP string `json:"specificIp"` -// Negation bool `json:"negation"` -// StartIP string `json:"startIp"` -// EndIP string `json:"endIp"` -// } +type IPCondition struct { + Type string `json:"type"` + SpecificIP string `json:"specificIp"` + Negation bool `json:"negation"` + StartIP string `json:"startIp"` + EndIP string `json:"endIp"` +} // RateLimitingStatus defines the observed state of RateLimiting // +k8s:openapi-gen=true diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go index 594bc36..495c4ff 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.deepcopy.go @@ -155,6 +155,22 @@ func (in *APIStatus) DeepCopy() *APIStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Bandwidth) DeepCopyInto(out *Bandwidth) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Bandwidth. +func (in *Bandwidth) DeepCopy() *Bandwidth { + if in == nil { + return nil + } + out := new(Bandwidth) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *BusinessInformation) DeepCopyInto(out *BusinessInformation) { *out = *in @@ -171,6 +187,24 @@ func (in *BusinessInformation) DeepCopy() *BusinessInformation { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Conditions) DeepCopyInto(out *Conditions) { + *out = *in + out.HeaderCondition = in.HeaderCondition + out.IPCondition = in.IPCondition + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Conditions. +func (in *Conditions) DeepCopy() *Conditions { + if in == nil { + return nil + } + out := new(Conditions) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Endpoint) DeepCopyInto(out *Endpoint) { *out = *in @@ -187,6 +221,38 @@ func (in *Endpoint) DeepCopy() *Endpoint { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HeaderCondition) DeepCopyInto(out *HeaderCondition) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HeaderCondition. +func (in *HeaderCondition) DeepCopy() *HeaderCondition { + if in == nil { + return nil + } + out := new(HeaderCondition) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *IPCondition) DeepCopyInto(out *IPCondition) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IPCondition. +func (in *IPCondition) DeepCopy() *IPCondition { + if in == nil { + return nil + } + out := new(IPCondition) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *RateLimiting) DeepCopyInto(out *RateLimiting) { *out = *in @@ -251,6 +317,8 @@ func (in *RateLimitingList) DeepCopyObject() runtime.Object { func (in *RateLimitingSpec) DeepCopyInto(out *RateLimitingSpec) { *out = *in out.RequestCount = in.RequestCount + out.Bandwidth = in.Bandwidth + out.Conditions = in.Conditions return } diff --git a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go index 0e2ef4a..d508cb1 100644 --- a/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go +++ b/apim-operator/pkg/apis/wso2/v1alpha1/zz_generated.openapi.go @@ -319,12 +319,28 @@ func schema_pkg_apis_wso2_v1alpha1_RateLimitingSpec(ref common.ReferenceCallback Format: "", }, }, + "description": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "bandwidth": { + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Bandwidth"), + }, + }, + "conditions": { + SchemaProps: spec.SchemaProps{ + Ref: ref("github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Conditions"), + }, + }, }, Required: []string{"type", "timeUnit", "unitTime", "requestCount"}, }, }, Dependencies: []string{ - "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RequestCount"}, + "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Bandwidth", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.Conditions", "github.com/apim-crd/apim-operator/pkg/apis/wso2/v1alpha1.RequestCount"}, } } From 5bf2c41dd92faf8ebe110ab367f7cf85aa9a4dbd Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Thu, 9 May 2019 14:30:43 +0530 Subject: [PATCH 12/13] Validation of types --- .../ratelimiting/ratelimiting_controller.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go index 5bda00e..6635522 100644 --- a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go +++ b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go @@ -12,7 +12,6 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" - "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -22,8 +21,6 @@ import ( "strconv" "strings" - "fmt" - mustache "github.com/cbroglie/mustache" ) @@ -114,10 +111,13 @@ func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile. log.Info(name) policyType := instance.Spec.Type - if policyType == "subscription" { + if policyType == "subscription" || policyType == "Subscription" { policyType = "Subscription" - } else if policyType == "application" { + } else if policyType == "application" || policyType == "Application" { policyType = "Application" + } else { + log.Info("INVALID policy type. Use application or subscription in crd object for type") + return reconcile.Result{}, nil } funcName := "init" + policyType + name + "Policy" @@ -125,7 +125,6 @@ func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile. var tierType string var policyKey string - tierType = instance.Spec.Type + "Tier" if policyType == "Application" { tierType = "appTier" policyKey = "appKey" @@ -153,14 +152,12 @@ func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile. } else { stopOnQuotaReach = "true" } - log.Info("QUOTAREACH") log.Info(stopOnQuotaReach) filename := "/usr/local/bin/policy.mustache" output, err := mustache.RenderFile(filename, map[string]string{"name": name, "funcName": funcName, "tierType": tierType, "policyKey": policyKey, "unitTime": unitTime, "stopOnQuotaReach": stopOnQuotaReach, "count": count}) log.Info(output) - fmt.Println(output) if err != nil { log.Error(err, "error in rendering ") From 93fa286c9a899b8968f9563aa7a2fa37474202a4 Mon Sep 17 00:00:00 2001 From: RameshaKaru Date: Thu, 9 May 2019 14:57:58 +0530 Subject: [PATCH 13/13] Update configmap creation --- .../controller/ratelimiting/ratelimiting_controller.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go index 6635522..7c7501d 100644 --- a/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go +++ b/apim-operator/pkg/controller/ratelimiting/ratelimiting_controller.go @@ -188,7 +188,13 @@ func (r *ReconcileRateLimiting) Reconcile(request reconcile.Request) (reconcile. log.Error(err, "error ") return reconcile.Result{}, err } - reqLogger.Info("Skip reconcile: map already exists", "confmap.Namespace", foundmap.Namespace, "confmap.Name", foundmap.Name) + reqLogger.Info("Map already exists", "confmap.Namespace", foundmap.Namespace, "confmap.Name", foundmap.Name) + reqLogger.Info("Updating Config map", "confmap.Namespace", confmap.Namespace, "confmap.Name", confmap.Name) + err = r.client.Update(context.TODO(), confmap) + if err != nil { + log.Error(err, "error ") + return reconcile.Result{}, err + } return reconcile.Result{}, nil }