Skip to content

Commit

Permalink
Add webhooks for gRPC APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
sgayangi committed Aug 23, 2024
1 parent 3e62c26 commit 4cd52d1
Show file tree
Hide file tree
Showing 29 changed files with 1,568 additions and 20 deletions.
16 changes: 16 additions & 0 deletions common-go-libs/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: wso2.com
layout:
- go.kubebuilder.io/v3
Expand Down Expand Up @@ -160,4 +164,16 @@ resources:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: wso2.com
group: dp
kind: API
path: github.com/wso2/apk/common-go-libs/apis/dp/v1beta1
version: v1beta1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
version: "3"
28 changes: 14 additions & 14 deletions common-go-libs/apis/dp/v1alpha1/api_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
package v1alpha1

import (
"github.com/wso2/apk/common-go-libs/apis/dp/v1alpha2"
"github.com/wso2/apk/common-go-libs/apis/dp/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// ConvertTo converts this API CR to the Hub version (v1alpha2).
// src is v1alpha1.API and dst is v1alpha2.API.
// ConvertTo converts this API CR to the Hub version (v1beta1).
// src is v1alpha1.API and dst is v1beta1.API.
func (src *API) ConvertTo(dstRaw conversion.Hub) error {

dst := dstRaw.(*v1alpha2.API)
dst := dstRaw.(*v1beta1.API)
dst.ObjectMeta = src.ObjectMeta

// Spec
Expand All @@ -41,40 +41,40 @@ func (src *API) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.SystemAPI = src.Spec.SystemAPI

if src.Spec.Production != nil {
dst.Spec.Production = []v1alpha2.EnvConfig{}
dst.Spec.Production = []v1beta1.EnvConfig{}
for _, productionRef := range src.Spec.Production {
dst.Spec.Production = append(dst.Spec.Production, v1alpha2.EnvConfig{
dst.Spec.Production = append(dst.Spec.Production, v1beta1.EnvConfig{
RouteRefs: productionRef.HTTPRouteRefs,
})
}
}
if src.Spec.Sandbox != nil {
dst.Spec.Sandbox = []v1alpha2.EnvConfig{}
dst.Spec.Sandbox = []v1beta1.EnvConfig{}
for _, sandboxRef := range src.Spec.Sandbox {
dst.Spec.Sandbox = append(dst.Spec.Sandbox, v1alpha2.EnvConfig{
dst.Spec.Sandbox = append(dst.Spec.Sandbox, v1beta1.EnvConfig{
RouteRefs: sandboxRef.HTTPRouteRefs,
})
}
}

// Convert []Property to []v1alpha2.Property
var properties []v1alpha2.Property
var properties []v1beta1.Property
for _, p := range src.Spec.APIProperties {
properties = append(properties, v1alpha2.Property(p))
properties = append(properties, v1beta1.Property(p))
}
dst.Spec.APIProperties = properties

// Status
dst.Status.DeploymentStatus = v1alpha2.DeploymentStatus(src.Status.DeploymentStatus)
dst.Status.DeploymentStatus = v1beta1.DeploymentStatus(src.Status.DeploymentStatus)

return nil
}

// ConvertFrom converts from the Hub version (v1alpha2) to this version.
// src is v1alpha1.API and dst is v1alpha2.API.
// ConvertFrom converts from the Hub version (v1beta1) to this version.
// src is v1alpha1.API and dst is v1beta1.API.
func (src *API) ConvertFrom(srcRaw conversion.Hub) error {

dst := srcRaw.(*v1alpha2.API)
dst := srcRaw.(*v1beta1.API)
src.ObjectMeta = dst.ObjectMeta

// Spec
Expand Down
103 changes: 101 additions & 2 deletions common-go-libs/apis/dp/v1alpha2/api_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,104 @@

package v1alpha2

// Hub marks this type as a conversion hub.
func (*API) Hub() {}
import (
"github.com/wso2/apk/common-go-libs/apis/dp/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// ConvertTo converts this API CR to the Hub version (v1beta1).
// src is v1alpha2.API and dst is v1beta1.API.
func (src *API) ConvertTo(dstRaw conversion.Hub) error {

dst := dstRaw.(*v1beta1.API)
dst.ObjectMeta = src.ObjectMeta

// Spec
dst.Spec.APIName = src.Spec.APIName
dst.Spec.APIVersion = src.Spec.APIVersion
dst.Spec.IsDefaultVersion = src.Spec.IsDefaultVersion
dst.Spec.DefinitionFileRef = src.Spec.DefinitionFileRef
dst.Spec.DefinitionPath = src.Spec.DefinitionPath
dst.Spec.APIType = src.Spec.APIType
dst.Spec.BasePath = src.Spec.BasePath
dst.Spec.Organization = src.Spec.Organization
dst.Spec.SystemAPI = src.Spec.SystemAPI

if src.Spec.Production != nil {
dst.Spec.Production = []v1beta1.EnvConfig{}
for _, productionRef := range src.Spec.Production {
dst.Spec.Production = append(dst.Spec.Production, v1beta1.EnvConfig{
RouteRefs: productionRef.RouteRefs,
})
}
}
if src.Spec.Sandbox != nil {
dst.Spec.Sandbox = []v1beta1.EnvConfig{}
for _, sandboxRef := range src.Spec.Sandbox {
dst.Spec.Sandbox = append(dst.Spec.Sandbox, v1beta1.EnvConfig{
RouteRefs: sandboxRef.RouteRefs,
})
}
}

// Convert []Property to []v1alpha2.Property
var properties []v1beta1.Property
for _, p := range src.Spec.APIProperties {
properties = append(properties, v1beta1.Property(p))
}
dst.Spec.APIProperties = properties

// Status
dst.Status.DeploymentStatus = v1beta1.DeploymentStatus(src.Status.DeploymentStatus)

return nil
}

// ConvertFrom converts from the Hub version (v1beta1) to this version.
// src is v1alpha2.API and dst is v1beta1.API.
func (src *API) ConvertFrom(srcRaw conversion.Hub) error {

dst := srcRaw.(*v1beta1.API)
src.ObjectMeta = dst.ObjectMeta

// Spec
src.Spec.APIName = dst.Spec.APIName
src.Spec.APIVersion = dst.Spec.APIVersion
src.Spec.IsDefaultVersion = dst.Spec.IsDefaultVersion
src.Spec.DefinitionFileRef = dst.Spec.DefinitionFileRef
src.Spec.DefinitionPath = dst.Spec.DefinitionPath
src.Spec.APIType = dst.Spec.APIType
src.Spec.BasePath = dst.Spec.BasePath
src.Spec.Organization = dst.Spec.Organization
src.Spec.SystemAPI = dst.Spec.SystemAPI

if dst.Spec.Production != nil {
src.Spec.Production = []EnvConfig{}
for _, productionRef := range dst.Spec.Production {
src.Spec.Production = append(src.Spec.Production, EnvConfig{
RouteRefs: productionRef.RouteRefs,
})
}
}

if dst.Spec.Sandbox != nil {
src.Spec.Sandbox = []EnvConfig{}
for _, sandboxRef := range dst.Spec.Sandbox {
src.Spec.Sandbox = append(src.Spec.Sandbox, EnvConfig{
RouteRefs: sandboxRef.RouteRefs,
})
}
}

// Convert []Property to []v1alpha1.Property
var properties []Property
for _, p := range dst.Spec.APIProperties {
properties = append(properties, Property(p))
}
src.Spec.APIProperties = properties

// Status
src.Status.DeploymentStatus = DeploymentStatus(dst.Status.DeploymentStatus)

return nil
}
1 change: 0 additions & 1 deletion common-go-libs/apis/dp/v1alpha2/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ type DeploymentStatus struct {
// +genclient
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:storageversion
//+kubebuilder:printcolumn:name="API Name",type="string",JSONPath=".spec.apiName"
//+kubebuilder:printcolumn:name="Version",type="string",JSONPath=".spec.apiVersion"
//+kubebuilder:printcolumn:name="BasePath",type="string",JSONPath=".spec.basePath"
Expand Down
20 changes: 20 additions & 0 deletions common-go-libs/apis/dp/v1alpha2/api_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"fmt"
"io"
"regexp"
"strings"

gqlparser "github.com/vektah/gqlparser"
Expand Down Expand Up @@ -103,6 +104,8 @@ func (r *API) validateAPI() error {

if r.Spec.BasePath == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec").Child("basePath"), "API basePath is required"))
} else if errMsg := validateAPIBasePathRegex(r.Spec.BasePath, r.Spec.APIType); errMsg != "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("basePath"), r.Spec.BasePath, errMsg))
} else if errMsg := validateAPIBasePathFormat(r.Spec.BasePath, r.Spec.APIVersion); errMsg != "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("basePath"), r.Spec.BasePath, errMsg))
} else if err := r.validateAPIBasePathExistsAndDefaultVersion(); err != nil {
Expand Down Expand Up @@ -159,6 +162,23 @@ func (r *API) validateAPI() error {
return nil
}

func validateAPIBasePathRegex(basePath, apiType string) string {
var pattern string
if apiType == "GRPC" {
pattern = `^[/][a-zA-Z][a-zA-Z0-9_.]*$`
} else {
pattern = `^[/][a-zA-Z0-9~/_.-]*$`
}
re, err := regexp.Compile(pattern)
if err != nil {
return "Failed to compile basePath regex pattern"
}
if !re.MatchString(basePath) {
return "API basePath is not in a valid format for the specified API type"
}
return ""
}

func isEmptyStringsInArray(strings []string) bool {
for _, str := range strings {
if str == "" {
Expand Down
21 changes: 21 additions & 0 deletions common-go-libs/apis/dp/v1beta1/api_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package v1beta1

// Hub marks this type as a conversion hub.
func (*API) Hub() {}
Loading

0 comments on commit 4cd52d1

Please sign in to comment.