Skip to content

Commit

Permalink
feat: allow image deletion for in-deleting pools
Browse files Browse the repository at this point in the history
if a pool has a deletionTimestamp set, it's fine to not
validate the pool-CR again to force the deletion of the CR

Signed-off-by: Mario Constanti <[email protected]>
  • Loading branch information
bavarianbidi committed Oct 25, 2023
1 parent 2be4b2f commit df70011
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
7 changes: 5 additions & 2 deletions api/v1alpha1/image_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ func (i *Image) attachedPools(ctx context.Context) ([]Pool, error) {
}

for _, pool := range pools.Items {
if pool.Spec.ImageName == i.Name {
result = append(result, pool)
// we do not care about pools that are already deleted
if pool.GetDeletionTimestamp() == nil {
if pool.Spec.ImageName == i.Name {
result = append(result, pool)
}
}
}

Expand Down
67 changes: 35 additions & 32 deletions api/v1alpha1/pool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,51 +77,54 @@ func (r *Pool) ValidateCreate() (admission.Warnings, error) {
if len(poolList.Items) > 0 {
existing := poolList.Items[0]
return nil, apierrors.NewBadRequest(
fmt.Sprintf("can not create pool, pool=%s with same image=%s , flavor=%s and provider=%s already exists for specified GitHubScope=%s", existing.Name, existing.Spec.ImageName, existing.Spec.Flavor, existing.Spec.ProviderName, existing.Spec.GitHubScopeRef.Name))
fmt.Sprintf("can not create pool, pool=%s with same image=%s, flavor=%s and provider=%s already exists for specified GitHubScope=%s", existing.Name, existing.Spec.ImageName, existing.Spec.Flavor, existing.Spec.ProviderName, existing.Spec.GitHubScopeRef.Name))
}

return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Pool) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
poollog.Info("validate update", "name", r)
poollog.Info("validate update", "name", r.Name, "namespace", r.Namespace)

oldCRD, ok := old.(*Pool)
if !ok {
return nil, apierrors.NewBadRequest("failed to convert runtime.Object to Pool CRD")
}

err := validateImageName(context.Background(), r)
if err != nil {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
r.Name,
field.ErrorList{err},
)
}

if err := r.validateExtraSpec(); err != nil {
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
r.Name,
field.ErrorList{err},
)
}

if err := r.validateProviderName(oldCRD); err != nil {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
r.Name,
field.ErrorList{err},
)
}

if err := r.validateGitHubScope(oldCRD); err != nil {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
r.Name,
field.ErrorList{err},
)
// if the object is being deleted, skip validation
if r.GetDeletionTimestamp() == nil {
err := validateImageName(context.Background(), r)
if err != nil {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
r.Name,
field.ErrorList{err},
)
}

if err := r.validateExtraSpec(); err != nil {
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
r.Name,
field.ErrorList{err},
)
}

if err := r.validateProviderName(oldCRD); err != nil {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
r.Name,
field.ErrorList{err},
)
}

if err := r.validateGitHubScope(oldCRD); err != nil {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
r.Name,
field.ErrorList{err},
)
}
}

return nil, nil
Expand Down

0 comments on commit df70011

Please sign in to comment.