Skip to content

Commit

Permalink
chore(kuma-cp): allow to pass more apiWebServiceCustomize fns (#8439)
Browse files Browse the repository at this point in the history
Signed-off-by: Bart Smykla <[email protected]>
  • Loading branch information
bartsmykla authored Nov 22, 2023
1 parent fd7f34d commit a029ccb
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 10 deletions.
9 changes: 3 additions & 6 deletions pkg/core/runtime/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type Builder struct {
*runtimeInfo
pgxConfigCustomizationFn config.PgxConfigCustomization
tenants multitenant.Tenants
apiWebServiceCustomize func(*restful.WebService) error
apiWebServiceCustomize []func(*restful.WebService) error
}

func BuilderFor(appCtx context.Context, cfg kuma_cp.Config) (*Builder, error) {
Expand Down Expand Up @@ -290,7 +290,7 @@ func (b *Builder) WithPgxConfigCustomizationFn(pgxConfigCustomizationFn config.P
}

func (b *Builder) WithAPIWebServiceCustomize(customize func(*restful.WebService) error) *Builder {
b.apiWebServiceCustomize = customize
b.apiWebServiceCustomize = append(b.apiWebServiceCustomize, customize)
return b
}

Expand Down Expand Up @@ -542,9 +542,6 @@ func (b *Builder) Tenants() multitenant.Tenants {
return b.tenants
}

func (b *Builder) APIWebServiceCustomize() func(*restful.WebService) error {
if b.apiWebServiceCustomize == nil {
return func(*restful.WebService) error { return nil }
}
func (b *Builder) APIWebServiceCustomize() []func(*restful.WebService) error {
return b.apiWebServiceCustomize
}
14 changes: 10 additions & 4 deletions pkg/core/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/emicklei/go-restful/v3"
"github.com/hashicorp/go-multierror"

"github.com/kumahq/kuma/pkg/api-server/authn"
api_server "github.com/kumahq/kuma/pkg/api-server/customization"
Expand Down Expand Up @@ -177,7 +178,7 @@ type runtimeContext struct {
interCpPool *client.Pool
pgxConfigCustomizationFn config.PgxConfigCustomization
tenants multitenant.Tenants
apiWebServiceCustomize func(*restful.WebService) error
apiWebServiceCustomize []func(*restful.WebService) error
}

func (rc *runtimeContext) Metrics() metrics.Metrics {
Expand Down Expand Up @@ -309,8 +310,13 @@ func (rc *runtimeContext) Tenants() multitenant.Tenants {
}

func (rc *runtimeContext) APIWebServiceCustomize() func(*restful.WebService) error {
if rc.apiWebServiceCustomize == nil {
return func(*restful.WebService) error { return nil }
return func(ws *restful.WebService) error {
err := &multierror.Error{}

for _, apiWebServiceCustomize := range rc.apiWebServiceCustomize {
err = multierror.Append(err, apiWebServiceCustomize(ws))
}

return err.ErrorOrNil()
}
return rc.apiWebServiceCustomize
}
11 changes: 11 additions & 0 deletions pkg/core/runtime/runtime_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package runtime_test

import (
"testing"

"github.com/kumahq/kuma/pkg/test"
)

func TestRuntime(t *testing.T) {
test.RunSpecs(t, "Runtime Suite")
}
73 changes: 73 additions & 0 deletions pkg/core/runtime/runtime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package runtime_test

import (
"context"
"net/http"

"github.com/emicklei/go-restful/v3"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

kuma_cp "github.com/kumahq/kuma/pkg/config/app/kuma-cp"
test_api_server "github.com/kumahq/kuma/pkg/test/api_server"
test_runtime "github.com/kumahq/kuma/pkg/test/runtime"
)

func return200ForPath(path string) func(ws *restful.WebService) error {
return func(ws *restful.WebService) error {
ws.Route(
ws.GET(path).To(func(req *restful.Request, res *restful.Response) {
res.WriteHeader(http.StatusOK)
}),
)

return nil
}
}

var _ = Describe("APIWebServiceCustomize", func() {
var stop chan struct{}

AfterEach(func() {
if stop != nil {
close(stop)
}
})

It("should allow for multiple customization functions", func() {
// given
cfg := kuma_cp.DefaultConfig()
cfg.ApiServer.HTTPS.Enabled = false
cfg.ApiServer.HTTP.Interface = "127.0.0.1"
builder, err := test_runtime.BuilderFor(context.Background(), cfg)
Expect(err).ToNot(HaveOccurred())

// when
rt, err := builder.
WithAPIWebServiceCustomize(return200ForPath("/foo")).
WithAPIWebServiceCustomize(return200ForPath("/bar")).
Build()
Expect(err).ToNot(HaveOccurred())

apiServer, err := test_api_server.NewApiServer(cfg, rt)
Expect(err).To(Succeed())

stop = make(chan struct{})
go func() {
defer GinkgoRecover()

Expect(apiServer.Start(stop)).To(Succeed())
}()

// then
Eventually(func(g Gomega) {
fooRes, err := http.Get("http://" + apiServer.Address() + "/foo")
g.Expect(err).To(Succeed())
g.Expect(fooRes.StatusCode).To(Equal(http.StatusOK))

barRes, err := http.Get("http://" + apiServer.Address() + "/bar")
g.Expect(err).To(Succeed())
g.Expect(barRes.StatusCode).To(Equal(http.StatusOK))
}).Should(Succeed())
})
})
46 changes: 46 additions & 0 deletions pkg/test/api_server/api_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package api_server

import (
"net"

"github.com/kumahq/kuma/pkg/api-server"
"github.com/kumahq/kuma/pkg/config/app/kuma-cp"
"github.com/kumahq/kuma/pkg/core/resources/model"
"github.com/kumahq/kuma/pkg/core/resources/registry"
"github.com/kumahq/kuma/pkg/core/runtime"
"github.com/kumahq/kuma/pkg/dns/vips"
"github.com/kumahq/kuma/pkg/xds/context"
"github.com/kumahq/kuma/pkg/xds/server"
)

func NewApiServer(cfg kuma_cp.Config, runtime runtime.Runtime) (*api_server.ApiServer, error) {
return api_server.NewApiServer(
runtime.ResourceManager(),
context.NewMeshContextBuilder(
runtime.ResourceManager(),
server.MeshResourceTypes(),
net.LookupIP,
cfg.Multizone.Zone.Name,
vips.NewPersistence(
runtime.ResourceManager(),
runtime.ConfigManager(),
cfg.Experimental.UseTagFirstVirtualOutboundModel,
),
cfg.DNSServer.Domain,
cfg.DNSServer.ServiceVipPort,
context.AnyToAnyReachableServicesGraphBuilder,
),
runtime.APIInstaller(),
registry.Global().ObjectDescriptors(model.HasWsEnabled()),
&cfg,
runtime.Metrics(),
runtime.GetInstanceId,
runtime.GetClusterId,
runtime.APIServerAuthenticator(),
runtime.Access(),
runtime.EnvoyAdminClient(),
runtime.TokenIssuers(),
runtime.APIWebServiceCustomize(),
runtime.GlobalInsightService(),
)
}
2 changes: 2 additions & 0 deletions pkg/test/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
secret_store "github.com/kumahq/kuma/pkg/core/secrets/store"
"github.com/kumahq/kuma/pkg/dns/vips"
"github.com/kumahq/kuma/pkg/dp-server/server"
"github.com/kumahq/kuma/pkg/envoy/admin/access"
"github.com/kumahq/kuma/pkg/events"
"github.com/kumahq/kuma/pkg/intercp"
kds_context "github.com/kumahq/kuma/pkg/kds/context"
Expand Down Expand Up @@ -131,6 +132,7 @@ func BuilderFor(appCtx context.Context, cfg kuma_cp.Config) (*core_runtime.Build
builder.WithAccess(core_runtime.Access{
ResourceAccess: resources_access.NewAdminResourceAccess(builder.Config().Access.Static.AdminResources),
DataplaneTokenAccess: tokens_access.NewStaticGenerateDataplaneTokenAccess(builder.Config().Access.Static.GenerateDPToken),
EnvoyAdminAccess: access.NoopEnvoyAdminAccess{},
})
builder.WithTokenIssuers(tokens_builtin.TokenIssuers{
DataplaneToken: tokens_builtin.NewDataplaneTokenIssuer(builder.ResourceManager()),
Expand Down

0 comments on commit a029ccb

Please sign in to comment.