140
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/webhook.go
generated
vendored
140
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/webhook.go
generated
vendored
@@ -23,7 +23,6 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
@@ -33,21 +32,21 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/client-go/rest"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration"
|
||||
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/addr"
|
||||
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
|
||||
"sigs.k8s.io/controller-runtime/pkg/internal/testing/certs"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// WebhookInstallOptions are the options for installing mutating or validating webhooks
|
||||
// WebhookInstallOptions are the options for installing mutating or validating webhooks.
|
||||
type WebhookInstallOptions struct {
|
||||
// Paths is a list of paths to the directories containing the mutating or validating webhooks yaml or json configs.
|
||||
DirectoryPaths []string
|
||||
// Paths is a list of paths to the directories or files containing the mutating or validating webhooks yaml or json configs.
|
||||
Paths []string
|
||||
|
||||
// MutatingWebhooks is a list of MutatingWebhookConfigurations to install
|
||||
MutatingWebhooks []runtime.Object
|
||||
MutatingWebhooks []client.Object
|
||||
|
||||
// ValidatingWebhooks is a list of ValidatingWebhookConfigurations to install
|
||||
ValidatingWebhooks []runtime.Object
|
||||
ValidatingWebhooks []client.Object
|
||||
|
||||
// IgnoreErrorIfPathMissing will ignore an error if a DirectoryPath does not exist when set to true
|
||||
IgnoreErrorIfPathMissing bool
|
||||
@@ -67,6 +66,9 @@ type WebhookInstallOptions struct {
|
||||
// CAData is the CA that can be used to trust the serving certificates in LocalServingCertDir.
|
||||
LocalServingCAData []byte
|
||||
|
||||
// LocalServingHostExternalName is the hostname to use to reach the webhook server.
|
||||
LocalServingHostExternalName string
|
||||
|
||||
// MaxTime is the max time to wait
|
||||
MaxTime time.Duration
|
||||
|
||||
@@ -76,8 +78,11 @@ type WebhookInstallOptions struct {
|
||||
|
||||
// ModifyWebhookDefinitions modifies webhook definitions by:
|
||||
// - applying CABundle based on the provided tinyca
|
||||
// - if webhook client config uses service spec, it's removed and replaced with direct url
|
||||
func (o *WebhookInstallOptions) ModifyWebhookDefinitions(caData []byte) error {
|
||||
// - if webhook client config uses service spec, it's removed and replaced with direct url.
|
||||
func (o *WebhookInstallOptions) ModifyWebhookDefinitions() error {
|
||||
caData := o.LocalServingCAData
|
||||
|
||||
// generate host port.
|
||||
hostPort, err := o.generateHostPort()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -137,13 +142,19 @@ func modifyWebhook(webhook map[string]interface{}, caData []byte, hostPort strin
|
||||
}
|
||||
|
||||
func (o *WebhookInstallOptions) generateHostPort() (string, error) {
|
||||
port, host, err := addr.Suggest()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to grab random port for serving webhooks on: %v", err)
|
||||
if o.LocalServingPort == 0 {
|
||||
port, host, err := addr.Suggest(o.LocalServingHost)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to grab random port for serving webhooks on: %v", err)
|
||||
}
|
||||
o.LocalServingPort = port
|
||||
o.LocalServingHost = host
|
||||
}
|
||||
o.LocalServingPort = port
|
||||
o.LocalServingHost = host
|
||||
return net.JoinHostPort(host, fmt.Sprintf("%d", port)), nil
|
||||
host := o.LocalServingHostExternalName
|
||||
if host == "" {
|
||||
host = o.LocalServingHost
|
||||
}
|
||||
return net.JoinHostPort(host, fmt.Sprintf("%d", o.LocalServingPort)), nil
|
||||
}
|
||||
|
||||
// PrepWithoutInstalling does the setup parts of Install (populating host-port,
|
||||
@@ -152,40 +163,33 @@ func (o *WebhookInstallOptions) generateHostPort() (string, error) {
|
||||
// controller-runtime, where we need a random host-port & caData for webhook
|
||||
// tests, but may be useful in similar scenarios.
|
||||
func (o *WebhookInstallOptions) PrepWithoutInstalling() error {
|
||||
hookCA, err := o.setupCA()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := parseWebhookDirs(o); err != nil {
|
||||
if err := o.setupCA(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = o.ModifyWebhookDefinitions(hookCA)
|
||||
if err != nil {
|
||||
if err := parseWebhook(o); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return o.ModifyWebhookDefinitions()
|
||||
}
|
||||
|
||||
// Install installs specified webhooks to the API server
|
||||
// Install installs specified webhooks to the API server.
|
||||
func (o *WebhookInstallOptions) Install(config *rest.Config) error {
|
||||
if err := o.PrepWithoutInstalling(); err != nil {
|
||||
return err
|
||||
if len(o.LocalServingCAData) == 0 {
|
||||
if err := o.PrepWithoutInstalling(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := createWebhooks(config, o.MutatingWebhooks, o.ValidatingWebhooks); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WaitForWebhooks(config, o.MutatingWebhooks, o.ValidatingWebhooks, *o); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return WaitForWebhooks(config, o.MutatingWebhooks, o.ValidatingWebhooks, *o)
|
||||
}
|
||||
|
||||
// Cleanup cleans up cert directories
|
||||
// Cleanup cleans up cert directories.
|
||||
func (o *WebhookInstallOptions) Cleanup() error {
|
||||
if o.LocalServingCertDir != "" {
|
||||
return os.RemoveAll(o.LocalServingCertDir)
|
||||
@@ -193,12 +197,11 @@ func (o *WebhookInstallOptions) Cleanup() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WaitForWebhooks waits for the Webhooks to be available through API server
|
||||
// WaitForWebhooks waits for the Webhooks to be available through API server.
|
||||
func WaitForWebhooks(config *rest.Config,
|
||||
mutatingWebhooks []runtime.Object,
|
||||
validatingWebhooks []runtime.Object,
|
||||
mutatingWebhooks []client.Object,
|
||||
validatingWebhooks []client.Object,
|
||||
options WebhookInstallOptions) error {
|
||||
|
||||
waitingFor := map[schema.GroupVersionKind]*sets.String{}
|
||||
|
||||
for _, hook := range runtimeListToUnstructured(append(validatingWebhooks, mutatingWebhooks...)) {
|
||||
@@ -213,7 +216,7 @@ func WaitForWebhooks(config *rest.Config,
|
||||
return wait.PollImmediate(options.PollInterval, options.MaxTime, p.poll)
|
||||
}
|
||||
|
||||
// poller checks if all the resources have been found in discovery, and returns false if not
|
||||
// poller checks if all the resources have been found in discovery, and returns false if not.
|
||||
type webhookPoller struct {
|
||||
// config is used to get discovery
|
||||
config *rest.Config
|
||||
@@ -222,7 +225,7 @@ type webhookPoller struct {
|
||||
waitingFor map[schema.GroupVersionKind]*sets.String
|
||||
}
|
||||
|
||||
// poll checks if all the resources have been found in discovery, and returns false if not
|
||||
// poll checks if all the resources have been found in discovery, and returns false if not.
|
||||
func (p *webhookPoller) poll() (done bool, err error) {
|
||||
// Create a new clientset to avoid any client caching of discovery
|
||||
c, err := client.New(p.config, client.Options{})
|
||||
@@ -248,7 +251,7 @@ func (p *webhookPoller) poll() (done bool, err error) {
|
||||
names.Delete(name)
|
||||
}
|
||||
|
||||
if errors.IsNotFound(err) {
|
||||
if apierrors.IsNotFound(err) {
|
||||
allFound = false
|
||||
}
|
||||
if err != nil {
|
||||
@@ -259,41 +262,42 @@ func (p *webhookPoller) poll() (done bool, err error) {
|
||||
return allFound, nil
|
||||
}
|
||||
|
||||
// setupCA creates CA for testing and writes them to disk
|
||||
func (o *WebhookInstallOptions) setupCA() ([]byte, error) {
|
||||
hookCA, err := integration.NewTinyCA()
|
||||
// setupCA creates CA for testing and writes them to disk.
|
||||
func (o *WebhookInstallOptions) setupCA() error {
|
||||
hookCA, err := certs.NewTinyCA()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to set up webhook CA: %v", err)
|
||||
return fmt.Errorf("unable to set up webhook CA: %v", err)
|
||||
}
|
||||
|
||||
hookCert, err := hookCA.NewServingCert()
|
||||
names := []string{"localhost", o.LocalServingHost, o.LocalServingHostExternalName}
|
||||
hookCert, err := hookCA.NewServingCert(names...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to set up webhook serving certs: %v", err)
|
||||
return fmt.Errorf("unable to set up webhook serving certs: %v", err)
|
||||
}
|
||||
|
||||
localServingCertsDir, err := ioutil.TempDir("", "envtest-serving-certs-")
|
||||
o.LocalServingCertDir = localServingCertsDir
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to create directory for webhook serving certs: %v", err)
|
||||
return fmt.Errorf("unable to create directory for webhook serving certs: %v", err)
|
||||
}
|
||||
|
||||
certData, keyData, err := hookCert.AsBytes()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to marshal webhook serving certs: %v", err)
|
||||
return fmt.Errorf("unable to marshal webhook serving certs: %v", err)
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil {
|
||||
return nil, fmt.Errorf("unable to write webhook serving cert to disk: %v", err)
|
||||
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil { //nolint:gosec
|
||||
return fmt.Errorf("unable to write webhook serving cert to disk: %v", err)
|
||||
}
|
||||
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil {
|
||||
return nil, fmt.Errorf("unable to write webhook serving key to disk: %v", err)
|
||||
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil { //nolint:gosec
|
||||
return fmt.Errorf("unable to write webhook serving key to disk: %v", err)
|
||||
}
|
||||
|
||||
o.LocalServingCAData = certData
|
||||
return certData, nil
|
||||
return err
|
||||
}
|
||||
|
||||
func createWebhooks(config *rest.Config, mutHooks []runtime.Object, valHooks []runtime.Object) error {
|
||||
func createWebhooks(config *rest.Config, mutHooks []client.Object, valHooks []client.Object) error {
|
||||
cs, err := client.New(config, client.Options{})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -315,7 +319,7 @@ func createWebhooks(config *rest.Config, mutHooks []runtime.Object, valHooks []r
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensureCreated creates or update object if already exists in the cluster
|
||||
// ensureCreated creates or update object if already exists in the cluster.
|
||||
func ensureCreated(cs client.Client, obj *unstructured.Unstructured) error {
|
||||
existing := obj.DeepCopy()
|
||||
err := cs.Get(context.Background(), client.ObjectKey{Name: obj.GetName()}, existing)
|
||||
@@ -336,10 +340,10 @@ func ensureCreated(cs client.Client, obj *unstructured.Unstructured) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseWebhookDirs reads the directories of Webhooks in options.DirectoryPaths and adds the Webhook structs to options
|
||||
func parseWebhookDirs(options *WebhookInstallOptions) error {
|
||||
if len(options.DirectoryPaths) > 0 {
|
||||
for _, path := range options.DirectoryPaths {
|
||||
// parseWebhook reads the directories or files of Webhooks in options.Paths and adds the Webhook structs to options.
|
||||
func parseWebhook(options *WebhookInstallOptions) error {
|
||||
if len(options.Paths) > 0 {
|
||||
for _, path := range options.Paths {
|
||||
_, err := os.Stat(path)
|
||||
if options.IgnoreErrorIfPathMissing && os.IsNotExist(err) {
|
||||
continue // skip this path
|
||||
@@ -359,21 +363,27 @@ func parseWebhookDirs(options *WebhookInstallOptions) error {
|
||||
}
|
||||
|
||||
// readWebhooks reads the Webhooks from files and Unmarshals them into structs
|
||||
// returns slice of mutating and validating webhook configurations
|
||||
func readWebhooks(path string) ([]runtime.Object, []runtime.Object, error) {
|
||||
// returns slice of mutating and validating webhook configurations.
|
||||
func readWebhooks(path string) ([]client.Object, []client.Object, error) {
|
||||
// Get the webhook files
|
||||
var files []os.FileInfo
|
||||
var err error
|
||||
log.V(1).Info("reading Webhooks from path", "path", path)
|
||||
if files, err = ioutil.ReadDir(path); err != nil {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
path, files = filepath.Dir(path), []os.FileInfo{info}
|
||||
} else if files, err = ioutil.ReadDir(path); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// file extensions that may contain Webhooks
|
||||
resourceExtensions := sets.NewString(".json", ".yaml", ".yml")
|
||||
|
||||
var mutHooks []runtime.Object
|
||||
var valHooks []runtime.Object
|
||||
var mutHooks []client.Object
|
||||
var valHooks []client.Object
|
||||
for _, file := range files {
|
||||
// Only parse allowlisted file types
|
||||
if !resourceExtensions.Has(filepath.Ext(file.Name())) {
|
||||
@@ -425,7 +435,7 @@ func readWebhooks(path string) ([]runtime.Object, []runtime.Object, error) {
|
||||
return mutHooks, valHooks, nil
|
||||
}
|
||||
|
||||
func runtimeListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
|
||||
func runtimeListToUnstructured(l []client.Object) []*unstructured.Unstructured {
|
||||
res := []*unstructured.Unstructured{}
|
||||
for _, obj := range l {
|
||||
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj.DeepCopyObject())
|
||||
|
||||
Reference in New Issue
Block a user