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

[release-4.7] Bug 1934798: reuse mgr client in webhooks #821

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/machineset/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func main() {
log.Fatal(err)
}

machineValidator, err := v1beta1.NewMachineValidator()
machineValidator, err := v1beta1.NewMachineValidator(mgr.GetClient())
if err != nil {
log.Fatal(err)
}
Expand All @@ -135,7 +135,7 @@ func main() {
log.Fatal(err)
}

machineSetValidator, err := v1beta1.NewMachineSetValidator()
machineSetValidator, err := v1beta1.NewMachineSetValidator(mgr.GetClient())
if err != nil {
log.Fatal(err)
}
Expand Down
13 changes: 2 additions & 11 deletions pkg/apis/machine/v1beta1/machine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,18 @@ type machineDefaulterHandler struct {
}

// NewValidator returns a new machineValidatorHandler.
func NewMachineValidator() (*machineValidatorHandler, error) {
func NewMachineValidator(client client.Client) (*machineValidatorHandler, error) {
infra, err := getInfra()
if err != nil {
return nil, err
}

cfg, err := ctrl.GetConfig()
if err != nil {
return nil, err
}
c, err := client.New(cfg, client.Options{})
if err != nil {
return nil, fmt.Errorf("failed to build kubernetes client: %v", err)
}

dns, err := getDNS()
if err != nil {
return nil, err
}

return createMachineValidator(infra, c, dns), nil
return createMachineValidator(infra, client, dns), nil
}

func createMachineValidator(infra *osconfigv1.Infrastructure, client client.Client, dns *osconfigv1.DNS) *machineValidatorHandler {
Expand Down
15 changes: 2 additions & 13 deletions pkg/apis/machine/v1beta1/machineset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package v1beta1
import (
"context"
"encoding/json"
"fmt"
"net/http"

osconfigv1 "github.com/openshift/api/config/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
Expand All @@ -30,27 +28,18 @@ type machineSetDefaulterHandler struct {
}

// NewMachineSetValidator returns a new machineSetValidatorHandler.
func NewMachineSetValidator() (*machineSetValidatorHandler, error) {
func NewMachineSetValidator(client client.Client) (*machineSetValidatorHandler, error) {
infra, err := getInfra()
if err != nil {
return nil, err
}

cfg, err := ctrl.GetConfig()
if err != nil {
return nil, err
}
c, err := client.New(cfg, client.Options{})
if err != nil {
return nil, fmt.Errorf("failed to build kubernetes client: %v", err)
}

dns, err := getDNS()
if err != nil {
return nil, err
}

return createMachineSetValidator(infra, c, dns), nil
return createMachineSetValidator(infra, client, dns), nil
}

func createMachineSetValidator(infra *osconfigv1.Infrastructure, client client.Client, dns *osconfigv1.DNS) *machineSetValidatorHandler {
Expand Down