Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
89fbb8a
Added new option for webhook client connection type.
Oct 10, 2022
a0c80b4
Refactored validation of webhook client connection type string and us…
Oct 11, 2022
6f8a0b8
Set default value to be AKS Production friendly.
Oct 11, 2022
c53fa3f
Exposed webhook path constants.
Oct 11, 2022
b313c8b
Added func to create client configs dynamically.
Oct 11, 2022
04b7cdd
WIP
Oct 11, 2022
c7736ba
Fixed lookup. Fixed interface arguments.
Oct 11, 2022
463afba
Removed dead code comment. Cleaned up cert details.
Oct 11, 2022
3de6f36
Updated deployment values to match AKS production expectations. Updat…
Oct 11, 2022
7ee54c7
Updated deployment values to match AKS production expectations. Updat…
Oct 11, 2022
5a049d3
Temp - Removed WIP webhook e2e test.
Oct 11, 2022
8064042
Set webhook to enabled by default
Oct 11, 2022
1b25682
Fixed variable names, and usage
Oct 11, 2022
6fd521b
Merge branch 'Azure:main' into feat-webhook-connectiontype
Ealianis Oct 12, 2022
fb4db6d
WIP - E2E Test for pod validation webhook.
Oct 12, 2022
411f305
linted code
Oct 13, 2022
b429d2e
Updated webhook test to be more thorough.
Oct 14, 2022
c6a8202
Merge branch 'Azure:main' into feat-webhook-connectiontype
Ealianis Oct 14, 2022
73ff928
Removed verification of downstream implementation.
Oct 14, 2022
2099392
Added error type & message check. Removed false positive checks.
Oct 17, 2022
e50b251
Merge branch 'Azure:main' into feat-webhook-connectiontype
Ealianis Oct 18, 2022
51aa998
Updated It block text.
Oct 18, 2022
43e9555
Merge branch 'Azure:main' into feat-webhook-connectiontype
Ealianis Oct 18, 2022
6c01af4
Updated webhook to only listen for create requests. Updated tests.
Oct 19, 2022
466df97
Fixed comments.
Oct 19, 2022
287cf37
Upgraded golang.org/x/text 0.3.7 -> 0.4.0
Oct 24, 2022
206726a
Simplified pod e2e tests.
Oct 24, 2022
fa3bf41
Removed unused constants.
Oct 24, 2022
f1af660
Put pod constructor inline.
Oct 24, 2022
0720c38
Change chart to default to service.
Oct 24, 2022
799f6c9
WIP = CRP e2e tests
Oct 25, 2022
cb4377c
CRP Webhook tests.
Oct 25, 2022
134f0de
Merge branch 'Azure:main' into feat-webhook-connectiontype
Ealianis Oct 25, 2022
811074c
Reverted change to webhook events.
Oct 26, 2022
65f2f95
Updated webhook tests for CRP.
Oct 26, 2022
13f5d49
Removed constants that existed in test utils.
Oct 26, 2022
0254bb3
Small fixes.
Oct 27, 2022
50ab130
Small fixes.
Oct 27, 2022
6e650ba
Merge branch 'Azure:main' into feat-webhook-connectiontype
Ealianis Oct 27, 2022
0277a4f
Small fixes.
Oct 27, 2022
255154d
PR comment fixes.
Oct 27, 2022
6c52a11
Refactor to of webhook.go.
Oct 28, 2022
4d7521d
Merge branch 'Azure:main' into feat-webhook-connectiontype
Ealianis Oct 28, 2022
9af6ecf
Typo.
Oct 28, 2022
d8ab0bf
Merge remote-tracking branch 'origin/feat-webhook-connectiontype' int…
Oct 28, 2022
f1339ca
Reduced webhook operation calls. Removed duplicate code and missed st…
Oct 28, 2022
8bdaa22
Restructured test cases. Added update to each create denial test.
Oct 28, 2022
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
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ install-hub-agent-helm:
--set image.repository=$(REGISTRY)/$(HUB_AGENT_IMAGE_NAME) \
--set image.tag=$(HUB_AGENT_IMAGE_VERSION) \
--set logVerbosity=5 \
--set namespace=fleet-system
--set namespace=fleet-system \
--set enableWebhook=true \
--set webhookClientConnectionType=service

.PHONY: e2e-hub-kubeconfig-secret
e2e-hub-kubeconfig-secret:
Expand Down
1 change: 1 addition & 0 deletions charts/hub-agent/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ spec:
args:
- --leader-elect=true
- --enable-webhook={{ .Values.enableWebhook }}
- --webhook-client-connection-type={{.Values.webhookClientConnectionType}}
- --v={{ .Values.logVerbosity }}
- -add_dir_header
ports:
Expand Down
5 changes: 1 addition & 4 deletions charts/hub-agent/templates/webhookservice.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# We use a headless service for webhook assuming the apiserver's dns can resolve it.
# The webhook will normally use a service reference with a cluster assigned IP.
apiVersion: v1
kind: Service
metadata:
Expand All @@ -7,9 +7,6 @@ metadata:
name: fleetwebhook
namespace: {{ .Values.namespace }}
spec:
clusterIP: None
clusterIPs:
- None
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
Expand Down
3 changes: 2 additions & 1 deletion charts/hub-agent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ image:

logVerbosity: 5

enableWebhook: false
enableWebhook: true
webhookClientConnectionType: service

namespace:
fleet-system
Expand Down
48 changes: 13 additions & 35 deletions cmd/hubagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/

package main

import (
"context"
"flag"
"os"

Expand All @@ -25,7 +25,7 @@ import (
"go.goms.io/fleet/pkg/controllers/membercluster"
fleetmetrics "go.goms.io/fleet/pkg/metrics"
"go.goms.io/fleet/pkg/webhook"
//+kubebuilder:scaffold:imports
// +kubebuilder:scaffold:imports
)

var (
Expand All @@ -49,7 +49,7 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(fleetv1alpha1.AddToScheme(scheme))
utilruntime.Must(workv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
// +kubebuilder:scaffold:scheme
klog.InitFlags(nil)

metrics.Registry.MustRegister(fleetmetrics.JoinResultMetrics, fleetmetrics.LeaveResultMetrics, fleetmetrics.PlacementApplyFailedCount, fleetmetrics.PlacementApplySucceedCount)
Expand Down Expand Up @@ -109,7 +109,7 @@ func main() {
}

if opts.EnableWebhook {
if err := SetupWebhook(mgr); err != nil {
if err := SetupWebhook(mgr, options.WebhookClientConnectionType(opts.WebhookClientConnectionType)); err != nil {
klog.ErrorS(err, "unable to set up webhook")
exitWithErrorFunc()
}
Expand All @@ -121,51 +121,29 @@ func main() {
exitWithErrorFunc()
}

//+kubebuilder:scaffold:builder
// +kubebuilder:scaffold:builder

if err := mgr.Start(ctx); err != nil {
klog.ErrorS(err, "problem starting manager")
exitWithErrorFunc()
}
}

// SetupWebhook generate the webhook cert and then setup the webhook configurator
func SetupWebhook(mgr manager.Manager) error {
// Generate self-signed key and crt files in FleetWebhookCertDir for the webhook server to start
caPEM, err := webhook.GenCertificate(FleetWebhookCertDir)
// SetupWebhook generates the webhook cert and then set up the webhook configurator.
func SetupWebhook(mgr manager.Manager, webhookClientConnectionType options.WebhookClientConnectionType) error {
// Generate self-signed key and crt files in FleetWebhookCertDir for the webhook server to start.
w, err := webhook.NewWebhookConfig(mgr, FleetWebhookPort, &webhookClientConnectionType, FleetWebhookCertDir)
if err != nil {
klog.ErrorS(err, "fail to generate certificates for webhook server")
klog.ErrorS(err, "fail to generate WebhookConfig")
return err
}

if err := mgr.Add(&webhookApiserverConfigurator{
mgr: mgr,
caPEM: caPEM,
port: FleetWebhookPort,
}); err != nil {
klog.ErrorS(err, "unable to add webhookApiserverConfigurator")
if err = mgr.Add(w); err != nil {
klog.ErrorS(err, "unable to add WebhookConfig")
return err
}
if err := webhook.AddToManager(mgr); err != nil {
if err = webhook.AddToManager(mgr); err != nil {
klog.ErrorS(err, "unable to register webhooks to the manager")
return err
}
return nil
}

type webhookApiserverConfigurator struct {
mgr manager.Manager
caPEM []byte
port int
}

var _ manager.Runnable = &webhookApiserverConfigurator{}

func (c *webhookApiserverConfigurator) Start(ctx context.Context) error {
klog.V(2).InfoS("setting up webhooks in apiserver from the leader")
if err := webhook.CreateFleetWebhookConfiguration(ctx, c.mgr.GetClient(), c.caPEM, c.port); err != nil {
klog.ErrorS(err, "unable to setup webhook configurations in apiserver")
return err
}
return nil
}
6 changes: 5 additions & 1 deletion cmd/hubagent/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/

package options

import (
Expand Down Expand Up @@ -34,6 +35,8 @@ type Options struct {
MetricsBindAddress string
// EnableWebhook indicates if we will run a webhook
EnableWebhook bool
// Sets the connection type for the webhook.
WebhookClientConnectionType string
// NetworkingAgentsEnabled indicates if we enable network agents
NetworkingAgentsEnabled bool
// ClusterUnhealthyThreshold is the duration of failure for the cluster to be considered unhealthy.
Expand Down Expand Up @@ -87,7 +90,8 @@ func (o *Options) AddFlags(flags *flag.FlagSet) {
flags.BoolVar(&o.LeaderElection.LeaderElect, "leader-elect", false, "Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.")
flags.DurationVar(&o.LeaderElection.LeaseDuration.Duration, "leader-lease-duration", 15*time.Second, "This is effectively the maximum duration that a leader can be stopped before someone else will replace it.")
flag.StringVar(&o.LeaderElection.ResourceNamespace, "leader-election-namespace", utils.FleetSystemNamespace, "The namespace in which the leader election resource will be created.")
flag.BoolVar(&o.EnableWebhook, "enable-webhook", false, "If set, the fleet webhook is enabled.")
flag.BoolVar(&o.EnableWebhook, "enable-webhook", true, "If set, the fleet webhook is enabled.")
flag.StringVar(&o.WebhookClientConnectionType, "webhook-client-connection-type", "url", "Sets the connection type used by the webhook client. Only URL or Service is valid.")
flag.BoolVar(&o.NetworkingAgentsEnabled, "networking-agents-enabled", false, "Whether the networking agents are enabled or not.")
flags.DurationVar(&o.ClusterUnhealthyThreshold.Duration, "cluster-unhealthy-threshold", 60*time.Second, "The duration for a member cluster to be in a degraded state before considered unhealthy.")
flags.DurationVar(&o.WorkPendingGracePeriod.Duration, "work-pending-grace-period", 15*time.Second,
Expand Down
5 changes: 5 additions & 0 deletions cmd/hubagent/options/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ func (o *Options) Validate() field.ErrorList {
errs = append(errs, field.Invalid(newPath.Child("WorkPendingGracePeriod"), o.WorkPendingGracePeriod, "must be greater than 0"))
}

connectionType := o.WebhookClientConnectionType
if _, err := parseWebhookClientConnectionString(connectionType); err != nil {
errs = append(errs, field.Invalid(newPath.Child("WebhookClientConnectionType"), o.EnableWebhook, err.Error()))
}

return errs
}
33 changes: 33 additions & 0 deletions cmd/hubagent/options/webhookconnectiontype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/

package options

import (
"errors"
"strings"
)

type WebhookClientConnectionType string

const (
URL WebhookClientConnectionType = "url"
Service WebhookClientConnectionType = "service"
)

var (
capabilitiesMap = map[string]WebhookClientConnectionType{
"service": Service,
"url": URL,
}
)

func parseWebhookClientConnectionString(str string) (WebhookClientConnectionType, error) {
t, ok := capabilitiesMap[strings.ToLower(str)]
if !ok {
return "", errors.New("must be \"service\" or \"url\"")
}
return t, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
fleetv1alpha1 "go.goms.io/fleet/apis/v1alpha1"
)

const (
// ValidationPath is the webhook service path which admission requests are routed to for validating ClusterResourcePlacement resources.
ValidationPath = "/validate-fleet-azure-com-v1alpha1-clusterresourceplacement"
)

func Add(mgr manager.Manager) error {
return (&fleetv1alpha1.ClusterResourcePlacement{}).SetupWebhookWithManager(mgr)
}
7 changes: 6 additions & 1 deletion pkg/webhook/pod/pod_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

const (
// ValidationPath is the webhook service path which admission requests are routed to for validating Pod resources.
ValidationPath = "/validate-v1-pod"
)

// Add registers the webhook for K8s bulit-in object types.
func Add(mgr manager.Manager) error {
hookServer := mgr.GetWebhookServer()
hookServer.Register("/validate-v1-pod", &webhook.Admission{Handler: &podValidator{Client: mgr.GetClient()}})
hookServer.Register(ValidationPath, &webhook.Admission{Handler: &podValidator{Client: mgr.GetClient()}})
return nil
}

Expand Down
Loading