Skip to content

Commit

Permalink
feat(infrastructure): Rewrite URL to v1beta1
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Apr 2, 2024
1 parent a437680 commit 4543f9a
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion internal/infrastructure/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ package infrastructure
import (
"context"
"fmt"
"net/http"
"strings"

"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
clicfg "sigs.k8s.io/controller-runtime/pkg/client/config"

Expand Down Expand Up @@ -37,10 +40,21 @@ type Manager interface {
func NewManager(cfg *config.Server) (Manager, error) {
var mgr Manager
if cfg.EnvoyGateway.Provider.Type == v1alpha1.ProviderTypeKubernetes {
cli, err := client.New(clicfg.GetConfigOrDie(), client.Options{Scheme: envoygateway.GetScheme()})
clientConf := clicfg.GetConfigOrDie()

httpClient, err := newHTTPClient(clientConf)
if err != nil {
return nil, err
}

cli, err := client.New(clientConf, client.Options{
Scheme: envoygateway.GetScheme(),
HTTPClient: httpClient,
})
if err != nil {
return nil, err
}

mgr = kubernetes.NewInfra(cli, cfg)
} else {
// Kube is the only supported provider type for now.
Expand All @@ -49,3 +63,35 @@ func NewManager(cfg *config.Server) (Manager, error) {

return mgr, nil
}

func newHTTPClient(config *rest.Config) (*http.Client, error) {
transport, err := rest.TransportFor(config)
if err != nil {
return nil, fmt.Errorf("could not create HTTP transport: %w", err)
}

transport = rewriteAPIVersion{transport}

return &http.Client{
Transport: transport,
Timeout: config.Timeout,
}, nil
}

const (
gatewayAPIPath = "/apis/gateway.networking.k8s.io/"
gatewayAPIPathV1 = gatewayAPIPath + "v1/"
gatewayAPIPathV1Beta1 = gatewayAPIPath + "v1beta1/"
)

type rewriteAPIVersion struct {
http.RoundTripper
}

func (r rewriteAPIVersion) RoundTrip(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.Path, gatewayAPIPathV1) {
req.URL.Path = strings.Replace(req.URL.Path, gatewayAPIPathV1, gatewayAPIPathV1Beta1, 1)
}

return r.RoundTripper.RoundTrip(req)
}

0 comments on commit 4543f9a

Please sign in to comment.