Skip to content

Commit

Permalink
Small fixes based on Stefan's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed May 10, 2023
1 parent f6436cf commit c1d7401
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 35 deletions.
19 changes: 3 additions & 16 deletions api/v1beta1/machine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (m *Machine) SetupWebhookWithManager(mgr ctrl.Manager) error {
}

// +kubebuilder:webhook:verbs=create;update;delete,path=/validate-cluster-x-k8s-io-v1beta1-machine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machines,versions=v1beta1,name=validation.machine.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update;delete,path=/mutate-cluster-x-k8s-io-v1beta1-machine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machines,versions=v1beta1,name=default.machine.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-cluster-x-k8s-io-v1beta1-machine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machines,versions=v1beta1,name=default.machine.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.CustomValidator = &machineValidator{}
var _ webhook.Defaulter = &Machine{}
Expand Down Expand Up @@ -79,27 +79,14 @@ func (m *Machine) Default() {

// MachineValidator creates a new CustomValidator for Machines.
func MachineValidator(scheme *runtime.Scheme) webhook.CustomValidator {
// Note: The error return parameter is always nil and will be dropped with the next CR release.
decoder, _ := admission.NewDecoder(scheme)
return &machineValidator{
decoder: decoder,
}
return &machineValidator{}
}

// machineValidator implements a defaulting webhook for Machine.
type machineValidator struct {
decoder *admission.Decoder
}
type machineValidator struct{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (*machineValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error {
log := ctrl.LoggerFrom(ctx)

log.Info("Service account env is", "env", os.Getenv("POD_SERVICE_ACCOUNT"))
log.Info("Pod name env is", "env", os.Getenv("POD_NAME"))
log.Info("Pod namespace env is", "env", os.Getenv("POD_NAMESPACE"))
fmt.Printf("Service account env is %s\n", os.Getenv("POD_SERVICE_ACCOUNT"))

m, ok := obj.(*Machine)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a Machine but got a %T", obj))
Expand Down
4 changes: 0 additions & 4 deletions exp/api/v1beta1/machinepool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import (
const (
// MachinePoolFinalizer is used to ensure deletion of dependencies (nodes, infra).
MachinePoolFinalizer = "machinepool.cluster.x-k8s.io"

// MachinePoolNameLabel is the label indicating the name of the MachinePool a Machine is controlled by.
// Note: The value of this label may be a hash if the MachinePool name is longer than 63 characters.
MachinePoolNameLabel = "cluster.x-k8s.io/pool-name"
)

// ANCHOR: MachinePoolSpec
Expand Down
13 changes: 0 additions & 13 deletions exp/internal/controllers/machinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,6 @@ func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
return errors.Wrap(err, "failed setting up with a controller manager")
}

err = c.Watch(
&source.Kind{Type: &clusterv1.Cluster{}},
handler.EnqueueRequestsFromMapFunc(clusterToMachinePools),
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
predicates.All(ctrl.LoggerFrom(ctx),
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
),
)
if err != nil {
return errors.Wrap(err, "failed adding Watch for Cluster to controller manager")
}

r.controller = c
r.recorder = mgr.GetEventRecorderFor("machinepool-controller")
return nil
Expand Down
6 changes: 4 additions & 2 deletions exp/internal/controllers/machinepool_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ func (r *MachinePoolReconciler) reconcileMachinePoolMachines(ctx context.Context

// If one missing but not both, return error
if noKind != noSelector {
log.Info("Only one of infraMachineKind or infraMachineSelector found, this is unexpected")
return errors.Errorf("only one of infraMachineKind or infraMachineSelector found, both must be present")
}

Expand Down Expand Up @@ -433,8 +432,11 @@ func (r *MachinePoolReconciler) deleteDanglingMachines(ctx context.Context, mp *
// Until the InfraMachinePool populates its selector kind and selector labels, the MachinePool controller can't tell that it supports MachinePool Machines
// and will create fallback machines to begin with. When the selector fields gets populated, we want to clean up those fallback machines.
log.V(2).Info("Machine has an empty infraRef, this a fallback machine, will delete", "machine", machine.Name, "namespace", machine.Namespace)
if err := r.Client.Delete(ctx, machine); err != nil {
return err
}

return r.Client.Delete(ctx, machine)
continue
}

_, err := external.Get(ctx, r.Client, &infraRef, mp.Namespace)
Expand Down

0 comments on commit c1d7401

Please sign in to comment.