Add webhook server to tests (#2873)
* Add webhook server to tests * fix config for webhoook service * Fix logger in webhook manager * Use interface for webhook manager * single reference for router url * Cleanup token code --------- Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
@@ -2,9 +2,13 @@ package framework
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@@ -29,10 +33,34 @@ type Framework struct {
|
||||
env *envtest.Environment
|
||||
config *rest.Config
|
||||
logger *zap.Logger
|
||||
ServiceInfo map[string]ServiceInfo
|
||||
serviceInfo map[string]ServiceInfo
|
||||
}
|
||||
|
||||
func NewWebhookOptions() (*envtest.WebhookInstallOptions, error) {
|
||||
webhookPort, err := utils.FindFreePort()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error finding unused port: %v", err)
|
||||
}
|
||||
_, filename, _, _ := runtime.Caller(0) //nolint
|
||||
root := filepath.Dir(filename)
|
||||
|
||||
options := &envtest.WebhookInstallOptions{
|
||||
LocalServingHost: "localhost",
|
||||
LocalServingPort: webhookPort,
|
||||
Paths: []string{filepath.Join(root, "webhook-manifest.yaml")},
|
||||
}
|
||||
err = options.PrepWithoutInstalling()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error preparing webhook install options: %v", err)
|
||||
}
|
||||
return options, nil
|
||||
}
|
||||
|
||||
func NewFramework() *Framework {
|
||||
webhookOptions, err := NewWebhookOptions()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &Framework{
|
||||
logger: loggerfactory.GetLogger(),
|
||||
env: &envtest.Environment{
|
||||
@@ -41,12 +69,17 @@ func NewFramework() *Framework {
|
||||
CRDInstallOptions: envtest.CRDInstallOptions{
|
||||
MaxTime: 60 * time.Second,
|
||||
},
|
||||
WebhookInstallOptions: *webhookOptions,
|
||||
BinaryAssetsDirectory: os.Getenv("KUBEBUILDER_ASSETS"),
|
||||
},
|
||||
ServiceInfo: make(map[string]ServiceInfo),
|
||||
serviceInfo: make(map[string]ServiceInfo),
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Framework) GetEnv() *envtest.Environment {
|
||||
return f.env
|
||||
}
|
||||
|
||||
func (f *Framework) Start(ctx context.Context) error {
|
||||
var err error
|
||||
f.config, err = f.env.Start()
|
||||
@@ -84,3 +117,41 @@ func (f *Framework) Stop() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Framework) AddServiceInfo(name string, info ServiceInfo) {
|
||||
f.serviceInfo[name] = info
|
||||
f.logger.Info("Added service", zap.String("name", name), zap.Any("info", info))
|
||||
}
|
||||
|
||||
func (f *Framework) GetServiceURL(name string) (string, error) {
|
||||
info, ok := f.serviceInfo[name]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("service %s not found", name)
|
||||
}
|
||||
if info.Port == 0 {
|
||||
return "", fmt.Errorf("service %s port not set", name)
|
||||
}
|
||||
return fmt.Sprintf("http://localhost:%d", info.Port), nil
|
||||
}
|
||||
|
||||
func (f *Framework) CheckService(name string) error {
|
||||
_, err := f.GetServiceURL(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config := &tls.Config{
|
||||
InsecureSkipVerify: true, //nolint:gosec // config is used to connect to our own webhook port.
|
||||
}
|
||||
|
||||
d := &net.Dialer{Timeout: 10 * time.Second}
|
||||
conn, err := tls.DialWithDialer(d, "tcp", net.JoinHostPort("localhost", strconv.Itoa(f.serviceInfo[name].Port)), config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("webhook server is not reachable: %w", err)
|
||||
}
|
||||
|
||||
if err := conn.Close(); err != nil {
|
||||
return fmt.Errorf("webhook server is not reachable: closing connection: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user