Skip to content

Commit

Permalink
reduce traffic edge-cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
珩轩 committed Jan 22, 2024
1 parent a0539a7 commit 0823a22
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
79 changes: 79 additions & 0 deletions cmd/agent/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import (
"fmt"
"net"
"os"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

Check failure on line 8 in cmd/agent/app/options/options.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"strconv"

"github.com/openyurtio/raven/pkg/utils"
"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/endpoints/discovery"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -190,6 +195,13 @@ func newMgr(cfg *restclient.Config, metricsBindAddress, healthyProbeAddress stri
Scheme: scheme,
MetricsBindAddress: metricsBindAddress,
HealthProbeBindAddress: healthyProbeAddress,
MapperProvider: func(c *restclient.Config) (meta.RESTMapper, error) {
opt := func() (meta.RESTMapper, error) {
return restmapper.NewDiscoveryRESTMapper(
[]*restmapper.APIGroupResources{getGatewayAPIGroupResource(), getLegacyAPIGroupResource()}), nil
}
return apiutil.NewDynamicRESTMapper(c, apiutil.WithCustomMapper(opt))
},
}

mgr, err := ctrl.NewManager(cfg, opt)
Expand All @@ -210,6 +222,73 @@ func newMgr(cfg *restclient.Config, metricsBindAddress, healthyProbeAddress stri
return mgr, nil
}

func getLegacyAPIGroupResource() *restmapper.APIGroupResources {
return &restmapper.APIGroupResources{
Group: metav1.APIGroup{
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: "v1", Version: "v1"}},
PreferredVersion: metav1.GroupVersionForDiscovery{GroupVersion: "v1", Version: "v1"},
},
VersionedResources: map[string][]metav1.APIResource{
"v1": {
{
Name: "nodes",
Namespaced: false,
Kind: "Node",
Verbs: metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
ShortNames: []string{"no"},
StorageVersionHash: discovery.StorageVersionHash("", "v1", "Node"),
},
{
Name: "pods",
Namespaced: true,
Kind: "Pod",
Verbs: metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
ShortNames: []string{"po"},
StorageVersionHash: discovery.StorageVersionHash("", "v1", "Pod"),
},
{
Name: "services",
Namespaced: true,
Kind: "Service",
Verbs: metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
ShortNames: []string{"svc"},
StorageVersionHash: discovery.StorageVersionHash("", "v1", "Service"),
},
},
},
}
}

func getGatewayAPIGroupResource() *restmapper.APIGroupResources {
return &restmapper.APIGroupResources{
Group: metav1.APIGroup{
Name: v1beta1.GroupVersion.Group,
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: v1beta1.GroupVersion.String(), Version: v1beta1.GroupVersion.Version}},
PreferredVersion: metav1.GroupVersionForDiscovery{GroupVersion: v1beta1.GroupVersion.String(), Version: v1beta1.GroupVersion.Version},
},
VersionedResources: map[string][]metav1.APIResource{
v1beta1.GroupVersion.Version: {
{
Name: "gateways",
Namespaced: false,
SingularName: "gateway",
Kind: "Gateway",
Verbs: metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
ShortNames: []string{"gw"},
Categories: []string{"all"},
StorageVersionHash: discovery.StorageVersionHash(v1beta1.GroupVersion.Group, v1beta1.GroupVersion.Version, "Gateway"),
},
{
Name: "gateways/status",
Namespaced: false,
Kind: "Gateway",
Verbs: metav1.Verbs{"get", "patch", "update"},
},
},
},
}
}

func resolveAddress(srcAddr, defaultHost, defaultPort string) string {
if srcAddr == "" {
return net.JoinHostPort(defaultHost, defaultPort)
Expand Down
3 changes: 2 additions & 1 deletion pkg/proxyengine/proxyserver/manageheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ func (h *headerManger) Handler(handler http.Handler) http.Handler {
return
}
oldHost := r.Host
klog.Info(utils.FormatProxyServer("request with host %s and url %s is processed by header manager", oldHost, r.URL.String()))
var host, ip, port string
var err error
if isAPIServerRequest(r) {
klog.Info(utils.FormatProxyServer("request from apiserver with host %s and url %s is processed by header manager", oldHost, r.URL.String()))
host, ip, port, err = h.getAPIServerRequestDestAddress(r)
if err != nil {
logAndHTTPError(w, http.StatusBadRequest, "request host %s and url %s is invalid, %s", r.Host, r.URL.String(), err.Error())
return
}
} else {
klog.Info(utils.FormatProxyServer("normal request with host %s and url %s is processed by header manager", oldHost, r.URL.String()))
host, ip, port, err = h.getNormalRequestDestAddress(r)
if err != nil {
logAndHTTPError(w, http.StatusBadRequest, "request host %s and url %s is invalid, %s", r.Host, r.URL.String(), err.Error())
Expand Down

0 comments on commit 0823a22

Please sign in to comment.