Skip to content

Commit

Permalink
added namespace endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Jasmin Gacic <jasmin.gacic@gmail.com>
  • Loading branch information
jasmingacic committed May 12, 2022
1 parent cdef4ba commit fd94b4f
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 117 deletions.
3 changes: 3 additions & 0 deletions server/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ go/api_apis_service.go
go/api_create_new_static_route.go
go/api_create_new_static_route_service.go
go/api_fleets.go
go/api_namespaces.go
go/api_namespaces_service.go
go/api_services.go
go/api_services_service.go
go/api_static_routes.go
Expand All @@ -19,6 +21,7 @@ go/model_api_item_fleet.go
go/model_api_item_service.go
go/model_envoy_fleet_item.go
go/model_inline_object.go
go/model_namespace_item.go
go/model_service_item.go
go/model_service_port_item.go
go/model_static_route_item.go
Expand Down
13 changes: 7 additions & 6 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ module github.com/GIT_USER_ID/GIT_REPO_ID

go 1.17

require github.com/gorilla/mux v1.8.0

require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/kubeshop/kusk-gateway v1.0.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/api v0.24.0
k8s.io/apimachinery v0.24.0
k8s.io/client-go v0.24.0
sigs.k8s.io/controller-runtime v0.11.2
sigs.k8s.io/controller-runtime v0.12.0
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
Expand All @@ -44,10 +45,10 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.28.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
Expand Down
55 changes: 38 additions & 17 deletions server/go.sum

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions server/go/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ type FleetsApiRouter interface {
GetEnvoyFleets(http.ResponseWriter, *http.Request)
}

// NamespacesApiRouter defines the required methods for binding the api requests to a responses for the NamespacesApi
// The NamespacesApiRouter implementation should parse necessary information from the http request,
// pass the data to a NamespacesApiServicer to perform the required actions, then write the service results to the http response.
type NamespacesApiRouter interface {
GetNamespaces(http.ResponseWriter, *http.Request)
}

// ServicesApiRouter defines the required methods for binding the api requests to a responses for the ServicesApi
// The ServicesApiRouter implementation should parse necessary information from the http request,
// pass the data to a ServicesApiServicer to perform the required actions, then write the service results to the http response.
Expand Down Expand Up @@ -88,6 +95,14 @@ type FleetsApiServicer interface {
GetEnvoyFleets(context.Context, string) (ImplResponse, error)
}

// NamespacesApiServicer defines the api actions for the NamespacesApi service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type NamespacesApiServicer interface {
GetNamespaces(context.Context) (ImplResponse, error)
}

// ServicesApiServicer defines the api actions for the ServicesApi service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can ignored with the .openapi-generator-ignore file
Expand Down
70 changes: 70 additions & 0 deletions server/go/api_namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Kusk Gateway API
*
* This is the Kusk Gateway Management API
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package openapi

import (
"net/http"
"strings"
)

// NamespacesApiController binds http requests to an api service and writes the service results to the http response
type NamespacesApiController struct {
service NamespacesApiServicer
errorHandler ErrorHandler
}

// NamespacesApiOption for how the controller is set up.
type NamespacesApiOption func(*NamespacesApiController)

// WithNamespacesApiErrorHandler inject ErrorHandler into controller
func WithNamespacesApiErrorHandler(h ErrorHandler) NamespacesApiOption {
return func(c *NamespacesApiController) {
c.errorHandler = h
}
}

// NewNamespacesApiController creates a default api controller
func NewNamespacesApiController(s NamespacesApiServicer, opts ...NamespacesApiOption) Router {
controller := &NamespacesApiController{
service: s,
errorHandler: DefaultErrorHandler,
}

for _, opt := range opts {
opt(controller)
}

return controller
}

// Routes returns all of the api route for the NamespacesApiController
func (c *NamespacesApiController) Routes() Routes {
return Routes{
{
"GetNamespaces",
strings.ToUpper("Get"),
"/namespaces",
c.GetNamespaces,
},
}
}

// GetNamespaces - Get a list of namespaces
func (c *NamespacesApiController) GetNamespaces(w http.ResponseWriter, r *http.Request) {
result, err := c.service.GetNamespaces(r.Context())
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)

}
42 changes: 42 additions & 0 deletions server/go/api_namespaces_service_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Kusk Gateway API
*
* This is the Kusk Gateway Management API
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package openapi

import (
"context"
"net/http"

kusk "github.com/GIT_USER_ID/GIT_REPO_ID/kusk"
)

// NamespacesApiService is a service that implements the logic for the NamespacesApiServicer
// This service should implement the business logic for every endpoint for the NamespacesApi API.
// Include any external packages or services that will be required by this service.
type NamespacesApiService struct {
kuskClient kusk.Client
}

// NewNamespacesApiService creates a default api service
func NewNamespacesApiService(kuskClient kusk.Client) NamespacesApiServicer {
return &NamespacesApiService{kuskClient: kuskClient}
}

// GetNamespaces - Get a list of namespaces
func (s *NamespacesApiService) GetNamespaces(ctx context.Context) (ImplResponse, error) {
namespaces, err := s.kuskClient.ListNamespaces()
if err != nil {
return Response(http.StatusInternalServerError, err), err
}
toReturn := []NamespaceItem{}
for _, ns := range namespaces.Items {
toReturn = append(toReturn, NamespaceItem{ns.Name})
}
return Response(http.StatusOK, toReturn), nil
}
31 changes: 31 additions & 0 deletions server/go/model_namespace_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Kusk Gateway API
*
* This is the Kusk Gateway Management API
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package openapi

type NamespaceItem struct {
Name string `json:"name,omitempty"`
}

// AssertNamespaceItemRequired checks if the required fields are not zero-ed
func AssertNamespaceItemRequired(obj NamespaceItem) error {
return nil
}

// AssertRecurseNamespaceItemRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of NamespaceItem (e.g. [][]NamespaceItem), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseNamespaceItemRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aNamespaceItem, ok := obj.(NamespaceItem)
if !ok {
return ErrTypeAssertionError
}
return AssertNamespaceItemRequired(aNamespaceItem)
})
}
94 changes: 0 additions & 94 deletions server/go/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ package openapi

import (
"encoding/json"
"errors"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
Expand All @@ -39,8 +36,6 @@ type Router interface {
Routes() Routes
}

const errMsgRequiredMissing = "required parameter is missing"

// NewRouter creates a new router for any number of api routers
func NewRouter(routers ...Router) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
Expand Down Expand Up @@ -129,92 +124,3 @@ func readFileHeaderToTempFile(fileHeader *multipart.FileHeader) (*os.File, error

return file, nil
}

// parseInt64Parameter parses a string parameter to an int64.
func parseInt64Parameter(param string, required bool) (int64, error) {
if param == "" {
if required {
return 0, errors.New(errMsgRequiredMissing)
}

return 0, nil
}

return strconv.ParseInt(param, 10, 64)
}

// parseInt32Parameter parses a string parameter to an int32.
func parseInt32Parameter(param string, required bool) (int32, error) {
if param == "" {
if required {
return 0, errors.New(errMsgRequiredMissing)
}

return 0, nil
}

val, err := strconv.ParseInt(param, 10, 32)
if err != nil {
return -1, err
}

return int32(val), nil
}

// parseBoolParameter parses a string parameter to a bool
func parseBoolParameter(param string) (bool, error) {
val, err := strconv.ParseBool(param)
if err != nil {
return false, err
}

return bool(val), nil
}

// parseInt64ArrayParameter parses a string parameter containing array of values to []int64.
func parseInt64ArrayParameter(param, delim string, required bool) ([]int64, error) {
if param == "" {
if required {
return nil, errors.New(errMsgRequiredMissing)
}

return nil, nil
}

str := strings.Split(param, delim)
ints := make([]int64, len(str))

for i, s := range str {
if v, err := strconv.ParseInt(s, 10, 64); err != nil {
return nil, err
} else {
ints[i] = v
}
}

return ints, nil
}

// parseInt32ArrayParameter parses a string parameter containing array of values to []int32.
func parseInt32ArrayParameter(param, delim string, required bool) ([]int32, error) {
if param == "" {
if required {
return nil, errors.New(errMsgRequiredMissing)
}

return nil, nil
}

str := strings.Split(param, delim)
ints := make([]int32, len(str))

for i, s := range str {
if v, err := strconv.ParseInt(s, 10, 32); err != nil {
return nil, err
} else {
ints[i] = int32(v)
}
}

return ints, nil
}
9 changes: 9 additions & 0 deletions server/kusk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Client interface {
GetStaticRoutes(namespace string) (*kuskv1.StaticRouteList, error)

GetSvc(namespace, name string) (*corev1.Service, error)
ListNamespaces() (*corev1.NamespaceList, error)
}

type kuskClient struct {
Expand Down Expand Up @@ -136,3 +137,11 @@ func (k *kuskClient) GetStaticRoutes(namespace string) (*kuskv1.StaticRouteList,

return list, nil
}

func (k *kuskClient) ListNamespaces() (*corev1.NamespaceList, error) {
list := &corev1.NamespaceList{}
if err := k.client.List(context.TODO(), list, &client.ListOptions{}); err != nil {
return nil, err
}
return list, nil
}
4 changes: 4 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ func main() {

ProbeController := openapi.NewProbeController()

NamespacesApiService := openapi.NewNamespacesApiService(kuskClient)
NamespaceApiController := openapi.NewNamespacesApiController(NamespacesApiService)

router := openapi.NewRouter(
ApisApiController,
FleetsApiController,
ServicesApiController,
StaticRouteApiController,
ProbeController,
NamespaceApiController,
)

log.Printf("Server started :8080")
Expand Down

0 comments on commit fd94b4f

Please sign in to comment.