Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow porch namespace in cert/webhook to be configured #26

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The kpt and Nephio Authors
// Copyright 2022,2024 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/nephio-project/porch/api/porch/install"
Expand Down Expand Up @@ -281,9 +282,14 @@ func (c completedConfig) New() (*PorchServer, error) {

func (s *PorchServer) Run(ctx context.Context) error {
porch.RunBackground(ctx, s.coreClient, s.cache)
webhookNs, found := os.LookupEnv("CERT_NAMESPACE")
if !found || strings.TrimSpace(webhookNs) == "" {
webhookNs = "porch-system"
}

certStorageDir, found := os.LookupEnv("CERT_STORAGE_DIR")
if found && certStorageDir != "" {
if err := setupWebhooks(ctx, certStorageDir); err != nil {
if found && strings.TrimSpace(certStorageDir) != "" {
if err := setupWebhooks(ctx, webhookNs, certStorageDir); err != nil {
klog.Errorf("%v\n", err)
return err
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/apiserver/webhooks.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The kpt and Nephio Authors
// Copyright 2022,2024 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,12 +51,12 @@ const (
serverEndpoint = "/validate-deletion"
)

func setupWebhooks(ctx context.Context, certStorageDir string) error {
caBytes, err := createCerts(certStorageDir)
func setupWebhooks(ctx context.Context, webhookNs string, certStorageDir string) error {
caBytes, err := createCerts(webhookNs, certStorageDir)
if err != nil {
return err
}
if err := createValidatingWebhook(ctx, caBytes); err != nil {
if err := createValidatingWebhook(ctx, webhookNs, caBytes); err != nil {
return err
}
if err := runWebhookServer(certStorageDir); err != nil {
Expand All @@ -65,11 +65,11 @@ func setupWebhooks(ctx context.Context, certStorageDir string) error {
return nil
}

func createCerts(certStorageDir string) ([]byte, error) {
klog.Infoln("creating self-signing TLS cert and key ")
func createCerts(webhookNs string, certStorageDir string) ([]byte, error) {
klog.Infoln("creating self-signing TLS cert and key with namespace " + webhookNs + " in directory " + certStorageDir)
dnsNames := []string{"api",
liamfallon marked this conversation as resolved.
Show resolved Hide resolved
"api.porch-system", "api.porch-system.svc"}
commonName := "api.porch-system.svc"
"api." + webhookNs, "api." + webhookNs + ".svc"}
commonName := "api." + webhookNs + ".svc"

var caPEM, serverCertPEM, serverPrivateKeyPEM *bytes.Buffer
// CA config
Expand Down Expand Up @@ -165,8 +165,8 @@ func WriteFile(filepath string, c []byte) error {
return nil
}

func createValidatingWebhook(ctx context.Context, caCert []byte) error {
klog.Infoln("Creating validating webhook")
func createValidatingWebhook(ctx context.Context, webhookNs string, caCert []byte) error {
klog.Infoln("Creating validating webhook with namespace " + webhookNs)

cfg := ctrl.GetConfigOrDie()
kubeClient, err := kubernetes.NewForConfig(cfg)
Expand All @@ -175,7 +175,7 @@ func createValidatingWebhook(ctx context.Context, caCert []byte) error {
}

var (
webhookNamespace = "porch-system"
webhookNamespace = webhookNs
validationCfgName = "packagerev-deletion-validating-webhook"
webhookService = "api"
path = serverEndpoint
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit odd also. Hardcoded svc name

Copy link
Member Author

@liamfallon liamfallon Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, That and a number of other things need to be cleaned up. The aim with this PR is to allow the namespace to be changed, whihc is a blocker. I have raised another improvement issue which addresses these other hardcoded values. See nephio-project/nephio#554
The proposal is that rather than having a load of configuration in Porch, we allow users to specify their own webhook where they can specify what they want. The self-signed one here will be retained more for development and integration test. For in-service deployments we will recommend users create and configure their own webhooks.

Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/webhooks_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The kpt and Nephio Authors
// Copyright 2022,2024 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,7 @@ func TestCreateCerts(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

caCert, err := createCerts(dir)
caCert, err := createCerts("", dir)
require.NoError(t, err)

caStr := strings.TrimSpace(string(caCert))
Expand Down
Loading