Skip to content

Commit

Permalink
Added webhook package (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenLevin authored Feb 5, 2024
1 parent b247415 commit 2fe92b8
Show file tree
Hide file tree
Showing 2 changed files with 309 additions and 0 deletions.
154 changes: 154 additions & 0 deletions pkg/webhook/mutatingwebhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package webhook

import (
"fmt"

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

admregv1 "k8s.io/api/admissionregistration/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
goclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// MutatingConfigurationBuilder provides struct for MutatingWebhookConfiguration object which contains connection
// to cluster and MutatingWebhookConfiguration definition.
type MutatingConfigurationBuilder struct {
// MutatingWebhookConfiguration definition. Used to create MutatingWebhookConfiguration object.
Definition *admregv1.MutatingWebhookConfiguration
// Created MutatingWebhookConfiguration object.
Object *admregv1.MutatingWebhookConfiguration
// Used in functions that define or mutate MutatingWebhookConfiguration definitions.
// errorMsg is processed before MutatingWebhookConfiguration object is created.
errorMsg string
// apiClient opens api connection to the cluster.
apiClient *clients.Settings
}

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

builder := MutatingConfigurationBuilder{
apiClient: apiClient,
Definition: &admregv1.MutatingWebhookConfiguration{
ObjectMeta: metaV1.ObjectMeta{
Name: name,
},
},
}

if name == "" {
glog.V(100).Infof("The name of the MutatingWebhookConfiguration is empty")

builder.errorMsg = "MutatingWebhookConfiguration 'name' cannot be empty"
}

if !builder.Exists() {
return nil, fmt.Errorf("MutatingWebhook object %s doesn't exist", name)
}

builder.Definition = builder.Object

return &builder, nil
}

// Exists checks whether the given MutatingWebhookConfiguration object exists in the cluster.
func (builder *MutatingConfigurationBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}

var err error
builder.Object, err = builder.Get()

return err == nil || !k8serrors.IsNotFound(err)
}

// Get returns MutatingWebhookConfiguration object.
func (builder *MutatingConfigurationBuilder) Get() (*admregv1.MutatingWebhookConfiguration, error) {
if valid, err := builder.validate(); !valid {
return &admregv1.MutatingWebhookConfiguration{}, err
}

mutatingWebhookConfiguration := &admregv1.MutatingWebhookConfiguration{}
err := builder.apiClient.Get(context.TODO(), goclient.ObjectKey{
Name: builder.Definition.Name,
}, mutatingWebhookConfiguration)

if err != nil {
glog.V(100).Infof("Failed to get MutatingConfigurationBuilder %s: %v", builder.Definition.Name, err)

return &admregv1.MutatingWebhookConfiguration{}, err
}

return mutatingWebhookConfiguration, err
}

// Delete removes a MutatingWebhookConfiguration from a cluster.
func (builder *MutatingConfigurationBuilder) Delete() (*MutatingConfigurationBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}

glog.V(100).Infof("Deleting the MutatingWebhookConfiguration %s", builder.Definition.Name)

if !builder.Exists() {
return builder, fmt.Errorf("MutatingWebhookConfiguration cannot be deleted because it does not exist")
}

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

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

builder.Object = nil

return builder, nil
}

// Update renovates the existing MutatingWebhookConfiguration object
// with the MutatingWebhookConfiguration definition in builder.
func (builder *MutatingConfigurationBuilder) Update() (*MutatingConfigurationBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}

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

err := builder.apiClient.Update(context.TODO(), builder.Definition)
if err == nil {
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 *MutatingConfigurationBuilder) validate() (bool, error) {
resourceCRD := "MutatingWebhookConfiguration"

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.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)

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

if builder.errorMsg != "" {
glog.V(100).Infof("The %s builder has error message: %s", resourceCRD, builder.errorMsg)

return false, fmt.Errorf(builder.errorMsg)
}

return true, nil
}
155 changes: 155 additions & 0 deletions pkg/webhook/validatingwebhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package webhook

import (
"fmt"

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

admregv1 "k8s.io/api/admissionregistration/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
goclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// ValidatingConfigurationBuilder provides struct for ValidatingWebhookConfiguration object
// which contains connection to cluster and ValidatingWebhookConfiguration definition.
type ValidatingConfigurationBuilder struct {
// ValidatingWebhookConfiguration definition. Used to create ValidatingWebhookConfiguration object.
Definition *admregv1.ValidatingWebhookConfiguration
// Created ValidatingWebhookConfiguration object.
Object *admregv1.ValidatingWebhookConfiguration
// Used in functions that define or mutate ValidatingWebhookConfiguration definitions.
// errorMsg is processed before ValidatingWebhookConfiguration object is created.
errorMsg string
// apiClient opens api connection to the cluster.
apiClient *clients.Settings
}

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

builder := ValidatingConfigurationBuilder{
apiClient: apiClient,
Definition: &admregv1.ValidatingWebhookConfiguration{
ObjectMeta: metaV1.ObjectMeta{
Name: name,
},
},
}

if name == "" {
glog.V(100).Infof("The name of the ValidatingWebhookConfiguration is empty")

builder.errorMsg = "ValidatingWebhookConfiguration 'name' cannot be empty"
}

if !builder.Exists() {
return nil, fmt.Errorf("ValidatingWebhookConfiguration object %s doesn't exist", name)
}

builder.Definition = builder.Object

return &builder, nil
}

// Exists checks whether the given ValidatingWebhookConfiguration object exists in the cluster.
func (builder *ValidatingConfigurationBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}

var err error
builder.Object, err = builder.Get()

return err == nil || !k8serrors.IsNotFound(err)
}

// Get returns ValidatingWebhookConfiguration object.
func (builder *ValidatingConfigurationBuilder) Get() (*admregv1.ValidatingWebhookConfiguration, error) {
if valid, err := builder.validate(); !valid {
return &admregv1.ValidatingWebhookConfiguration{}, err
}

validatingWebhookConfiguration := &admregv1.ValidatingWebhookConfiguration{}
err := builder.apiClient.Get(context.TODO(), goclient.ObjectKey{
Name: builder.Definition.Name,
}, validatingWebhookConfiguration)

if err != nil {
glog.V(100).Infof("Failed to get ValidatingWebhookConfiguration %s: %v", builder.Definition.Name, err)

return &admregv1.ValidatingWebhookConfiguration{}, err
}

return validatingWebhookConfiguration, err
}

// Delete removes a ValidatingWebhookConfiguration from a cluster.
func (builder *ValidatingConfigurationBuilder) Delete() (*ValidatingConfigurationBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}

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

if !builder.Exists() {
return builder, fmt.Errorf("ValidatingWebhookConfiguration cannot be deleted because it does not exist")
}

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

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

builder.Object = nil

return builder, nil
}

// Update renovates the existing ValidatingWebhookConfiguration object
// with the ValidatingWebhookConfiguration definition in builder.
func (builder *ValidatingConfigurationBuilder) Update() (*ValidatingConfigurationBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}

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

err := builder.apiClient.Update(context.TODO(), builder.Definition)
if err == nil {
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"

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.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)

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

if builder.errorMsg != "" {
glog.V(100).Infof("The %s builder has error message: %s", resourceCRD, builder.errorMsg)

return false, fmt.Errorf(builder.errorMsg)
}

return true, nil
}

0 comments on commit 2fe92b8

Please sign in to comment.