Skip to content

Commit

Permalink
webhook: add validating unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klaskosk committed Nov 24, 2024
1 parent 5adf3eb commit eb80b1a
Show file tree
Hide file tree
Showing 2 changed files with 323 additions and 16 deletions.
59 changes: 43 additions & 16 deletions pkg/webhook/validatingwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/msg"
"golang.org/x/net/context"

admregv1 "k8s.io/api/admissionregistration/v1"
Expand All @@ -24,16 +25,28 @@ type ValidatingConfigurationBuilder struct {
// errorMsg is processed before ValidatingWebhookConfiguration object is created.
errorMsg string
// apiClient opens api connection to the cluster.
apiClient *clients.Settings
apiClient goclient.Client
}

// PullValidatingConfiguration pulls existing ValidatingWebhookConfiguration from cluster.
func PullValidatingConfiguration(apiClient *clients.Settings, name string) (
*ValidatingConfigurationBuilder, error) {
func PullValidatingConfiguration(apiClient *clients.Settings, name string) (*ValidatingConfigurationBuilder, error) {
glog.V(100).Infof("Pulling existing ValidatingWebhookConfiguration name %s from cluster", name)

if apiClient == nil {
glog.V(100).Infof("The ValidatingWebhookConfiguration apiClient is nil")

return nil, fmt.Errorf("validatingWebhookConfiguration 'apiClient' cannot be nil")
}

err := apiClient.AttachScheme(admregv1.AddToScheme)
if err != nil {
glog.V(100).Infof("Failed to add admissionregistration v1 scheme to client schemes")

return nil, err
}

builder := ValidatingConfigurationBuilder{
apiClient: apiClient,
apiClient: apiClient.Client,
Definition: &admregv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -44,11 +57,11 @@ func PullValidatingConfiguration(apiClient *clients.Settings, name string) (
if name == "" {
glog.V(100).Infof("The name of the ValidatingWebhookConfiguration is empty")

builder.errorMsg = "ValidatingWebhookConfiguration 'name' cannot be empty"
return nil, fmt.Errorf("validatingWebhookConfiguration 'name' cannot be empty")
}

if !builder.Exists() {
return nil, fmt.Errorf("ValidatingWebhookConfiguration object %s does not exist", name)
return nil, fmt.Errorf("validatingWebhookConfiguration object %s does not exist", name)
}

builder.Definition = builder.Object
Expand All @@ -71,7 +84,7 @@ func (builder *ValidatingConfigurationBuilder) Exists() bool {
// Get returns ValidatingWebhookConfiguration object.
func (builder *ValidatingConfigurationBuilder) Get() (*admregv1.ValidatingWebhookConfiguration, error) {
if valid, err := builder.validate(); !valid {
return &admregv1.ValidatingWebhookConfiguration{}, err
return nil, err
}

validatingWebhookConfiguration := &admregv1.ValidatingWebhookConfiguration{}
Expand All @@ -82,7 +95,7 @@ func (builder *ValidatingConfigurationBuilder) Get() (*admregv1.ValidatingWebhoo
if err != nil {
glog.V(100).Infof("Failed to get ValidatingWebhookConfiguration %s: %v", builder.Definition.Name, err)

return &admregv1.ValidatingWebhookConfiguration{}, err
return nil, err
}

return validatingWebhookConfiguration, err
Expand All @@ -97,8 +110,7 @@ func (builder *ValidatingConfigurationBuilder) Delete() (*ValidatingConfiguratio
glog.V(100).Infof("Deleting the ValidatingWebhookConfiguration %s", builder.Definition.Name)

if !builder.Exists() {
glog.V(100).Infof("ValidatingWebhookConfiguration %s cannot be deleted"+
" because it does not exist",
glog.V(100).Infof("ValidatingWebhookConfiguration %s cannot be deleted because it does not exist",
builder.Definition.Name)

builder.Object = nil
Expand All @@ -107,9 +119,8 @@ func (builder *ValidatingConfigurationBuilder) Delete() (*ValidatingConfiguratio
}

err := builder.apiClient.Delete(context.TODO(), builder.Definition)

if err != nil {
return builder, fmt.Errorf("can not delete ValidatingWebhookConfiguration: %w", err)
return builder, err
}

builder.Object = nil
Expand All @@ -126,29 +137,45 @@ func (builder *ValidatingConfigurationBuilder) Update() (*ValidatingConfiguratio

glog.V(100).Infof("Updating ValidatingWebhookConfiguration %s", builder.Definition.Name)

if !builder.Exists() {
glog.V(100).Infof("Cannot update ValidatingWebhookConfiguration %s as it does not exist", builder.Definition.Name)

return builder, fmt.Errorf("cannot update non-existent validatingWebhookConfiguration")
}

builder.Definition.ResourceVersion = builder.Object.ResourceVersion
err := builder.apiClient.Update(context.TODO(), builder.Definition)
if err == nil {
builder.Object = builder.Definition

if err != nil {
return builder, err
}

builder.Object = builder.Definition

return builder, err
}

// validate will check that the builder and builder definition are properly initialized before
// accessing any member fields.
func (builder *ValidatingConfigurationBuilder) validate() (bool, error) {
resourceCRD := "ValidatingWebhookConfiguration"
resourceCRD := "validatingWebhookConfiguration"

if builder == nil {
glog.V(100).Infof("The %s builder is uninitialized", resourceCRD)

return false, fmt.Errorf("error: received nil %s builder", resourceCRD)
}

if builder.Definition == nil {
glog.V(100).Infof("The %s is undefined", resourceCRD)

return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD))
}

if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)

builder.errorMsg = fmt.Sprintf("%s builder cannot have nil apiClient", resourceCRD)
return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD)
}

if builder.errorMsg != "" {
Expand Down
Loading

0 comments on commit eb80b1a

Please sign in to comment.