Skip to content

Commit

Permalink
Getting 42Crunch URL for the dashboard
Browse files Browse the repository at this point in the history
Signed-off-by: jasmingacic <jasmin.gacic@gmail.com>
  • Loading branch information
jasmingacic committed Dec 7, 2022
1 parent 90669df commit 5b5ccf0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
2 changes: 2 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ components:
$ref: "#/components/schemas/ApiItem_Service"
version:
type: string
crunch42URL:
type: string
ServiceItem:
type: object
required:
Expand Down
6 changes: 3 additions & 3 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/kubeshop/kusk-gateway v1.3.12
github.com/stretchr/testify v1.8.0
github.com/kubeshop/kusk-gateway v1.5.1-0.20221207083051-c3507a110867
github.com/stretchr/testify v1.8.1
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.25.2
k8s.io/apimachinery v0.25.2
Expand All @@ -26,7 +26,7 @@ require (
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/getkin/kin-openapi v0.106.0 // indirect
github.com/getkin/kin-openapi v0.110.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
Expand Down
14 changes: 8 additions & 6 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8S
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/getkin/kin-openapi v0.106.0 h1:hrqfqJPAvWvuO/V0lCr/xyQOq4Gy21mcr28JJOSRcEI=
github.com/getkin/kin-openapi v0.106.0/go.mod h1:9Dhr+FasATJZjS4iOLvB0hkaxgYdulrNYm2e9epLWOo=
github.com/getkin/kin-openapi v0.110.0 h1:1GnJALxsltcSzCMqgtqKlLhYQeULv3/jesmV2sC5qE0=
github.com/getkin/kin-openapi v0.110.0/go.mod h1:QtwUNt0PAAgIIBEvFWYfB7dfngxtAaqCX1zYHMZDeK8=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -214,13 +214,13 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kubeshop/kusk-gateway v1.3.12 h1:3EwcueWJkqZdO+7CIRitFBNsl/iT/XG9ii87sQjupAU=
github.com/kubeshop/kusk-gateway v1.3.12/go.mod h1:qQ9twNe4K9eAdTNV5gPzwqX7TVaxhesmzxCf1HGDGh0=
github.com/kubeshop/kusk-gateway v1.5.1-0.20221207083051-c3507a110867 h1:regFX89cDAeyKj7aNwTbIV3I4VM3eipwZYT2quoMvyA=
github.com/kubeshop/kusk-gateway v1.5.1-0.20221207083051-c3507a110867/go.mod h1:oVgZPQM4uM5ax5KxCiFZXMisi0QtHNXv/aPO35QYyKM=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
Expand Down Expand Up @@ -292,13 +292,15 @@ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
Expand Down
28 changes: 28 additions & 0 deletions server/go/api_apis_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ package openapi

import (
"context"
"fmt"
"net/http"
"strings"

kuskv1 "github.com/kubeshop/kusk-gateway/api/v1alpha1"
"github.com/kubeshop/kusk-gateway/pkg/analytics"
crunch "github.com/kubeshop/kusk-gateway/pkg/crunch42"
"github.com/kubeshop/kusk-gateway/pkg/spec"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -123,6 +125,32 @@ func (s *ApisApiService) convertAPICRDtoAPIModel(api *kuskv1.API) ApiItem {
}
}

if opts.Security != nil && opts.Security.Crunch42 != nil {
secret, _ := s.kuskClient.GetSecret(opts.Security.Crunch42.Token.Name, opts.Security.Crunch42.Token.Namespace)
crunchClient, _ := crunch.NewClient(string(secret.Data[crunch.Crunch42Token]), nil)

crunchCollections, _, _ := crunchClient.ListCollections() // if we can't get 42crunch ID there is no need to error out

var cid string
for _, col := range crunchCollections.List {
if col.Desc.Name == api.Name {
cid = col.Desc.ID
break
}
}
apis, _, _ := crunchClient.ListAPIs(cid)

var apiID string
for _, api := range apis.List {
if api.Name == api.Name {
apiID = api.ID
break
}
}

apiItem.Crunch42URL = fmt.Sprintf("https://platform.42crunch.com/apis/%s/api-summary", apiID)
}

apiItem.Fleet = ApiItemFleet{
Name: api.Spec.Fleet.Name,
Namespace: api.Spec.Fleet.Namespace,
Expand Down
2 changes: 2 additions & 0 deletions server/go/model_api_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type ApiItem struct {
Service ApiItemService `json:"service"`

Version string `json:"version"`

Crunch42URL string `json:"crunc42url"`
}

// AssertApiItemRequired checks if the required fields are not zero-ed
Expand Down
21 changes: 15 additions & 6 deletions server/kusk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
kuskv1 "github.com/kubeshop/kusk-gateway/api/v1alpha1"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/util/retry"
Expand Down Expand Up @@ -41,6 +42,7 @@ type Client interface {
GetSvc(namespace, name string) (*corev1.Service, error)
ListServices(namespace string) (*corev1.ServiceList, error)
ListNamespaces() (*corev1.NamespaceList, error)
GetSecret(name, namespace string) (*v1.Secret, error)

K8sClient() client.Client
}
Expand Down Expand Up @@ -149,7 +151,7 @@ func (k *kuskClient) GetApiByEnvoyFleet(namespace, fleetNamespace, fleetName str

func (k *kuskClient) CreateApi(namespace, name, openapispec string, fleetName string, fleetnamespace string) (*kuskv1.API, error) {
api := &kuskv1.API{
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Expand Down Expand Up @@ -197,7 +199,7 @@ func (k *kuskClient) UpdateApi(namespace, name, openapispec string, fleetName st

func (k *kuskClient) DeleteAPI(namespace, name string) error {
api := &kuskv1.API{
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Expand Down Expand Up @@ -260,7 +262,7 @@ func (k *kuskClient) GetStaticRoutes(namespace string) (*kuskv1.StaticRouteList,

func (k *kuskClient) CreateStaticRoute(namespace, name, fleetName, fleetNamespace, specs string) (*kuskv1.StaticRoute, error) {
staticRoute := &kuskv1.StaticRoute{
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Expand All @@ -279,7 +281,6 @@ func (k *kuskClient) CreateStaticRoute(namespace, name, fleetName, fleetNamespac
fmt.Println(err)
}

staticRoute.Spec.Paths = tmp.Spec.Paths
staticRoute.Spec.Hosts = tmp.Spec.Hosts

if err := k.client.Create(context.TODO(), staticRoute, &client.CreateOptions{}); err != nil {
Expand Down Expand Up @@ -310,7 +311,6 @@ func (k *kuskClient) UpdateStaticRoute(namespace, name, fleetName, fleetNamespac
Name: fleetName,
Namespace: fleetNamespace,
},
Paths: tmp.Spec.Paths,
Hosts: tmp.Spec.Hosts,
}

Expand All @@ -335,3 +335,12 @@ func (k *kuskClient) ListNamespaces() (*corev1.NamespaceList, error) {
func (k *kuskClient) K8sClient() client.Client {
return k.client
}

func (k *kuskClient) GetSecret(name, namespace string) (*v1.Secret, error) {
sec := &v1.Secret{}
if err := k.client.Get(context.TODO(), client.ObjectKey{Name: name, Namespace: namespace}, sec); err != nil {
return nil, err
}

return sec, nil
}

0 comments on commit 5b5ccf0

Please sign in to comment.