Skip to content

Commit 9efe1bd

Browse files
authored
Remove unused code in webhooks.go (#566)
* Remove unused code in webhooks * Fix issue where watch goroutine was failing after test completion
1 parent 0ba8bb6 commit 9efe1bd

3 files changed

Lines changed: 14 additions & 56 deletions

File tree

pkg/apiserver/webhooks.go

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ import (
5050
"sigs.k8s.io/controller-runtime/pkg/client"
5151
)
5252

53-
type WebhookType string
54-
55-
const (
56-
WebhookTypeService WebhookType = "service"
57-
WebhookTypeUrl WebhookType = "url"
58-
repositoryValidationEndpoint = "/validate-repository"
59-
)
53+
const repositoryValidationEndpoint = "/validate-repository"
6054

6155
var (
6256
cert tls.Certificate
@@ -67,10 +61,6 @@ var tracer = otel.Tracer("repository-webhook")
6761

6862
// WebhookConfig defines the configuration for the Repository validation webhook
6963
type WebhookConfig struct {
70-
Type WebhookType
71-
ServiceName string // only used if Type == WebhookTypeService
72-
ServiceNamespace string // only used if Type == WebhookTypeService
73-
Host string // only used if Type == WebhookTypeUrl
7464
Port int32
7565
RepositoryPath string
7666
RepoServiceName string
@@ -84,21 +74,6 @@ type WebhookConfig struct {
8474
// newWebhookConfig creates a new WebhookConfig object filled with values read from environment variables
8575
func newWebhookConfig(ctx context.Context) *WebhookConfig {
8676
var cfg WebhookConfig
87-
// NOTE: CERT_NAMESPACE is supported for backward compatibility.
88-
// TODO: We may consider using only WEBHOOK_SERVICE_NAMESPACE instead.
89-
if hasEnv("CERT_NAMESPACE") ||
90-
hasEnv("WEBHOOK_SERVICE_NAME") ||
91-
hasEnv("WEBHOOK_SERVICE_NAMESPACE") ||
92-
!hasEnv("WEBHOOK_HOST") {
93-
94-
cfg.Type = WebhookTypeService
95-
cfg.ServiceName, cfg.ServiceNamespace = webhookServiceName(ctx)
96-
cfg.Host = fmt.Sprintf("%s.%s.svc", cfg.ServiceName, cfg.ServiceNamespace)
97-
} else {
98-
cfg.Type = WebhookTypeUrl
99-
cfg.Host = getEnv("WEBHOOK_HOST", "localhost")
100-
}
101-
// Always use the WebhookTypeService for repository webhook validation
10277
cfg.RepositoryPath = repositoryValidationEndpoint
10378
cfg.RepoServiceName, cfg.RepoServiceNamespace = webhookServiceName(ctx)
10479
cfg.RepoHost = fmt.Sprintf("%s.%s.svc", cfg.RepoServiceName, cfg.RepoServiceNamespace)
@@ -183,17 +158,9 @@ func setupWebhooks(ctx context.Context, clientReader client.Reader) error {
183158
}
184159

185160
func createCerts(cfg *WebhookConfig) ([]byte, error) {
186-
klog.Infof("creating self-signing TLS cert and key for %q in directory %s", cfg.Host, cfg.CertStorageDir)
187-
commonName := cfg.Host
161+
klog.Infof("creating self-signing TLS cert and key for %q in directory %s", cfg.RepoHost, cfg.CertStorageDir)
162+
commonName := cfg.RepoHost
188163
dnsNames := []string{commonName}
189-
if cfg.Type == WebhookTypeService {
190-
dnsNames = append(dnsNames, cfg.ServiceName)
191-
dnsNames = append(dnsNames, fmt.Sprintf("%s.%s", cfg.ServiceName, cfg.ServiceNamespace))
192-
dnsNames = append(dnsNames, fmt.Sprintf("%s.%s.svc", cfg.ServiceName, cfg.ServiceNamespace))
193-
dnsNames = append(dnsNames, fmt.Sprintf("%s.%s.svc.cluster.local", cfg.ServiceName, cfg.ServiceNamespace))
194-
}
195-
196-
// DNS names for CA config - repository-validating-webhook
197164
dnsNames = append(dnsNames, cfg.RepoServiceName)
198165
dnsNames = append(dnsNames, fmt.Sprintf("%s.%s", cfg.RepoServiceName, cfg.RepoServiceNamespace))
199166
dnsNames = append(dnsNames, fmt.Sprintf("%s.%s.svc", cfg.RepoServiceName, cfg.RepoServiceNamespace))
@@ -295,7 +262,7 @@ func WriteFile(filepath string, c []byte) error {
295262

296263
func createValidatingWebhook(ctx context.Context, cfg *WebhookConfig, caCert []byte) error {
297264

298-
klog.Infof("Creating validating webhook for %s:%d", cfg.Host, cfg.Port)
265+
klog.Infof("Creating validating webhook for %s:%d", cfg.RepoHost, cfg.Port)
299266

300267
kubeConfig := ctrl.GetConfigOrDie()
301268
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
@@ -502,11 +469,6 @@ func writeErr(errMsg string, w *http.ResponseWriter) {
502469
}
503470
}
504471

505-
func hasEnv(key string) bool {
506-
_, found := os.LookupEnv(key)
507-
return found
508-
}
509-
510472
func getEnv(key string, defaultValue string) string {
511473
value, found := os.LookupEnv(key)
512474
if !found {

pkg/apiserver/webhooks_test.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ import (
3838

3939
func TestCreateCerts(t *testing.T) {
4040
webhookCfg := WebhookConfig{
41-
Type: WebhookTypeUrl,
42-
Host: "localhost",
41+
RepoHost: "localhost",
4342
CertStorageDir: t.TempDir(),
4443
}
4544
defer func() {
@@ -73,8 +72,7 @@ func TestLoadCertificate(t *testing.T) {
7372
//what do i need to test.
7473
// first create dummy certs for testing
7574
webhookCfg := WebhookConfig{
76-
Type: WebhookTypeUrl,
77-
Host: "localhost",
75+
RepoHost: "localhost",
7876
CertStorageDir: t.TempDir(),
7977
}
8078
defer func() {
@@ -149,8 +147,7 @@ func TestWatchCertificatesInvalidDirectory(t *testing.T) {
149147
}
150148
// Set up the temp directory with dummy certificate files
151149
webhookCfg := WebhookConfig{
152-
Type: WebhookTypeUrl,
153-
Host: "localhost",
150+
RepoHost: "localhost",
154151
CertStorageDir: t.TempDir(),
155152
}
156153
defer func() {
@@ -189,8 +186,7 @@ func TestWatchCertificatesSuccessfulReload(t *testing.T) {
189186
}
190187
// Set up the temp directory with dummy certificate files
191188
webhookCfg := WebhookConfig{
192-
Type: WebhookTypeUrl,
193-
Host: "localhost",
189+
RepoHost: "localhost",
194190
CertStorageDir: t.TempDir(),
195191
}
196192
defer func() {
@@ -238,8 +234,7 @@ func TestWatchCertificatesInvalidCertReload(t *testing.T) {
238234
}
239235
// Set up the temp directory with dummy certificate files
240236
webhookCfg := WebhookConfig{
241-
Type: WebhookTypeUrl,
242-
Host: "localhost",
237+
RepoHost: "localhost",
243238
CertStorageDir: t.TempDir(),
244239
}
245240
defer func() {
@@ -286,8 +281,7 @@ func TestWatchCertificatesGracefulTermination(t *testing.T) {
286281
}
287282
// Set up the temp directory with dummy certificate files
288283
webhookCfg := WebhookConfig{
289-
Type: WebhookTypeUrl,
290-
Host: "localhost",
284+
RepoHost: "localhost",
291285
CertStorageDir: t.TempDir(),
292286
}
293287
defer func() {

pkg/registry/porch/packagerevision_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,12 @@ func TestWatch(t *testing.T) {
443443
_, mockEngine := setup(t)
444444
mockWatcherManager := mockengine.NewMockWatcherManager(t)
445445
mockEngine.On("ObjectCache").Return(mockWatcherManager).Maybe()
446-
447446
mockWatcherManager.On("WatchPackageRevisions", mock.Anything, mock.Anything, mock.Anything).Return(fmt.Errorf("error starting watch")).Maybe()
448447

449-
_, err := packagerevisions.Watch(context.TODO(), &internalversion.ListOptions{})
448+
ctx, cancel := context.WithCancel(context.Background())
449+
defer cancel()
450+
451+
_, err := packagerevisions.Watch(ctx, &internalversion.ListOptions{})
450452
assert.NoError(t, err)
451453

452454
//=========================================================================================

0 commit comments

Comments
 (0)