Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom policies added to the CRD #126

Merged
merged 5 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apis/apps/v1alpha1/apicast_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"fmt"

appscommon "github.com/3scale/apicast-operator/apis/apps"

v1 "k8s.io/api/core/v1"
Expand All @@ -28,6 +30,21 @@ import (
// 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.

// CustomPolicySpec contains or has reference to an APIcast custom policy
type CustomPolicySpec struct {
eguzki marked this conversation as resolved.
Show resolved Hide resolved
// Name specifies the name of the custom policy
Name string `json:"name"`
// Version specifies the name of the custom policy
Version string `json:"version"`

// SecretRef specifies the secret holding the custom policy metadata and lua code
SecretRef *v1.LocalObjectReference `json:"secretRef"`
}

func (c *CustomPolicySpec) VersionName() string {
return fmt.Sprintf("%s%s", c.Name, c.Version)
}

// APIcastSpec defines the desired state of APIcast.
type APIcastSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Expand Down Expand Up @@ -145,6 +162,10 @@ type APIcastSpec struct {
// Timezone specifies the local timezone of the APIcast deployment pods. A timezone value available in the TZ database must be set.
// +optional
Timezone *string `json:"timezone,omitempty"` // TZ

// CustomPolicies specifies an array of defined custome policies to be loaded
// +optional
CustomPolicies []CustomPolicySpec `json:"customPolicies,omitempty"`
}

type DeploymentEnvironmentType string
Expand Down Expand Up @@ -250,6 +271,29 @@ func (a *APIcast) Validate() field.ErrorList {
errors = append(errors, field.Invalid(httpsPortFldPath, a.Spec.HTTPSPort, "HTTPS port conflicts with HTTP port"))
}

customPoliciesFldPath := specFldPath.Child("customPolicies")
// check custom policy secret is set
for idx, customPolicySpec := range a.Spec.CustomPolicies {
if customPolicySpec.SecretRef == nil {
customPoliciesIdxFldPath := customPoliciesFldPath.Index(idx)
errors = append(errors, field.Invalid(customPoliciesIdxFldPath, customPolicySpec, "custom policy secret is mandatory"))
} else if customPolicySpec.SecretRef.Name == "" {
customPoliciesIdxFldPath := customPoliciesFldPath.Index(idx)
errors = append(errors, field.Invalid(customPoliciesIdxFldPath, customPolicySpec, "custom policy secret name is empty"))
}
}

// check duplicated custom policy version name
duplicateMap := make(map[string]int)
for idx, customPolicySpec := range a.Spec.CustomPolicies {
if _, ok := duplicateMap[customPolicySpec.VersionName()]; ok {
customPoliciesIdxFldPath := customPoliciesFldPath.Index(idx)
errors = append(errors, field.Invalid(customPoliciesIdxFldPath, customPolicySpec, "custom policy secret name version tuple is duplicated"))
break
}
duplicateMap[customPolicySpec.VersionName()] = 0
}

return errors
}

Expand Down
27 changes: 27 additions & 0 deletions apis/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions bundle/manifests/apps.3scale.net_apicasts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ spec:
- boot
- lazy
type: string
customPolicies:
description: CustomPolicies specifies an array of defined custome policies to be loaded
items:
description: CustomPolicySpec contains or has reference to an APIcast custom policy
properties:
name:
description: Name specifies the name of the custom policy
type: string
secretRef:
description: SecretRef specifies the secret holding the custom policy metadata and lua code
properties:
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
type: object
version:
description: Version specifies the name of the custom policy
type: string
required:
- name
- secretRef
- version
type: object
type: array
deploymentEnvironment:
description: DeploymentEnvironment is the environment for which the configuration will be downloaded from 3scale (Staging or Production), when using APIcast. The value will also be used in the header X-3scale-User-Agent in the authorize/report requests made to 3scale Service Management API. It is used by 3scale for statistics.
type: string
Expand Down
28 changes: 28 additions & 0 deletions config/crd/bases/apps.3scale.net_apicasts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ spec:
- boot
- lazy
type: string
customPolicies:
description: CustomPolicies specifies an array of defined custome
policies to be loaded
items:
description: CustomPolicySpec contains or has reference to an APIcast
custom policy
properties:
name:
description: Name specifies the name of the custom policy
type: string
secretRef:
description: SecretRef specifies the secret holding the custom
policy metadata and lua code
properties:
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
type: object
version:
description: Version specifies the name of the custom policy
type: string
required:
- name
- secretRef
- version
type: object
type: array
deploymentEnvironment:
description: DeploymentEnvironment is the environment for which the
configuration will be downloaded from 3scale (Staging or Production),
Expand Down
1 change: 0 additions & 1 deletion controllers/apps/apicast_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func (r *APIcastReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return ctrl.Result{Requeue: true}, nil
}

log.Error(err, "Main reconciler")
return result, err
}
if result.Requeue {
Expand Down
Loading