Skip to content

NETOBSERV-1905 Query UDNs without flows #688

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions pkg/handler/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package handler

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

"github.com/netobserv/network-observability-console-plugin/pkg/kubernetes/auth"
"github.com/netobserv/network-observability-console-plugin/pkg/kubernetes/resources"
"github.com/netobserv/network-observability-console-plugin/pkg/utils"

kerr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func (h *Handlers) GetUDNIdss(ctx context.Context) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
token, err := auth.GetUserToken(r.Header)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
}

cudns, err := resources.List(ctx, token, schema.GroupVersionResource{
Group: "k8s.ovn.org",
Version: "v1",
Resource: "clusteruserdefinednetworks",
})
if err != nil {
var k8sErr *kerr.StatusError
if errors.As(err, &k8sErr) {
writeError(w, int(k8sErr.ErrStatus.Code), err.Error())
} else {
writeError(w, http.StatusInternalServerError, err.Error())
}
}

udns, err := resources.List(ctx, token, schema.GroupVersionResource{
Group: "k8s.ovn.org",
Version: "v1",
Resource: "userdefinednetworks",
})
if err != nil {
var k8sErr *kerr.StatusError
if errors.As(err, &k8sErr) {
writeError(w, int(k8sErr.ErrStatus.Code), err.Error())
} else {
writeError(w, http.StatusInternalServerError, err.Error())
}
}

values := []string{}
for _, cudn := range cudns {
md := cudn.Object["metadata"].(map[string]interface{})
values = append(values, fmt.Sprintf("%s", md["name"]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
values = append(values, fmt.Sprintf("%s", md["name"]))
values = append(values, md["name"])

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I commited this but I will revert it as the metadata can contains other things than strings such as boolean

}
for _, udn := range udns {
md := udn.Object["metadata"].(map[string]interface{})
values = append(values, fmt.Sprintf("%s.%s", md["namespace"], md["name"]))
}
writeJSON(w, http.StatusOK, utils.NonEmpty(utils.Dedup(values)))
}
}
6 changes: 3 additions & 3 deletions pkg/kubernetes/auth/check_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (b *DenyAllChecker) CheckAdmin(_ context.Context, _ http.Header) error {
return errors.New("deny all auth mode selected")
}

func getUserToken(header http.Header) (string, error) {
func GetUserToken(header http.Header) (string, error) {
authValue := header.Get(AuthHeader)
if authValue != "" {
parts := strings.Split(authValue, "Bearer ")
Expand Down Expand Up @@ -130,7 +130,7 @@ type BearerTokenChecker struct {

func (c *BearerTokenChecker) CheckAuth(ctx context.Context, header http.Header) error {
hlog.Debug("Checking authenticated user")
token, err := getUserToken(header)
token, err := GetUserToken(header)
if err != nil {
return err
}
Expand All @@ -145,7 +145,7 @@ func (c *BearerTokenChecker) CheckAuth(ctx context.Context, header http.Header)

func (c *BearerTokenChecker) CheckAdmin(ctx context.Context, header http.Header) error {
hlog.Debug("Checking admin user")
token, err := getUserToken(header)
token, err := GetUserToken(header)
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/kubernetes/resources/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package resources

import (
"context"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relying on dynamic client here allows us to read the CRDs without importing the whole API.

"k8s.io/client-go/rest"
)

func List(ctx context.Context, token string, gvr schema.GroupVersionResource) ([]unstructured.Unstructured, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
config.BearerToken = token
config.BearerTokenFile = ""

dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}

// Retrieve the custom resource
list, err := dynamicClient.Resource(gvr).List(ctx, v1.ListOptions{})
if err != nil {
return nil, err
}
return list.Items, err
}
3 changes: 3 additions & 0 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func setupRoutes(ctx context.Context, cfg *config.Config, authChecker auth.Check
api.HandleFunc("/resources/namespaces", h.GetNamespaces(ctx))
api.HandleFunc("/resources/names", h.GetNames(ctx))

// K8S endpoints
api.HandleFunc("/k8s/resources/udnIds", h.GetUDNIdss(ctx))

// Frontend files
api.HandleFunc("/frontend-config", h.GetFrontendConfig())
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./web/dist/")))
Expand Down
63 changes: 63 additions & 0 deletions vendor/k8s.io/client-go/dynamic/interface.go

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

108 changes: 108 additions & 0 deletions vendor/k8s.io/client-go/dynamic/scheme.go

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

Loading