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

spinapp: Relax executor validation #308

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/controller/spinapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"hash/adler32"
"maps"
"time"

"github.com/pelletier/go-toml/v2"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -110,7 +111,9 @@ func (r *SpinAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
log.Error(err, "unable to fetch executor")
r.Recorder.Event(&spinApp, "Warning", "MissingExecutor",
fmt.Sprintf("Could not find SpinAppExecutor %s/%s", req.NamespacedName.Namespace, spinApp.Spec.Executor))
return ctrl.Result{}, err
return ctrl.Result{
RequeueAfter: 15 * time.Second,
}, err
}

// Update the status of the SpinApp
Expand Down
31 changes: 0 additions & 31 deletions internal/webhook/spinapp_validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,11 @@
return nil, nil
}

func (v *SpinAppValidator) validateSpinApp(ctx context.Context, spinApp *spinv1alpha1.SpinApp) error {

Check failure on line 55 in internal/webhook/spinapp_validating.go

View workflow job for this annotation

GitHub Actions / lint go

`(*SpinAppValidator).validateSpinApp` - `ctx` is unused (unparam)
var allErrs field.ErrorList
executor, err := validateExecutor(spinApp.Spec, v.fetchExecutor(ctx, spinApp.Namespace))
if err != nil {
allErrs = append(allErrs, err)
}
if err := validateReplicas(spinApp.Spec); err != nil {
allErrs = append(allErrs, err)
}
if err := validateAnnotations(spinApp.Spec, executor); err != nil {
allErrs = append(allErrs, err)
}
if len(allErrs) == 0 {
return nil
}
Expand All @@ -76,7 +69,7 @@
// fetchExecutor returns a function that fetches a named executor in the provided namespace.
//
// We assume that the executor must exist in the same namespace as the SpinApp.
func (v *SpinAppValidator) fetchExecutor(ctx context.Context, spinAppNs string) func(name string) (*spinv1alpha1.SpinAppExecutor, error) {

Check failure on line 72 in internal/webhook/spinapp_validating.go

View workflow job for this annotation

GitHub Actions / lint go

func `(*SpinAppValidator).fetchExecutor` is unused (unused)
return func(name string) (*spinv1alpha1.SpinAppExecutor, error) {
var executor spinv1alpha1.SpinAppExecutor
if err := v.Client.Get(ctx, client.ObjectKey{Name: name, Namespace: spinAppNs}, &executor); err != nil {
Expand All @@ -87,7 +80,7 @@
}
}

func validateExecutor(spec spinv1alpha1.SpinAppSpec, fetchExecutor func(name string) (*spinv1alpha1.SpinAppExecutor, error)) (*spinv1alpha1.SpinAppExecutor, *field.Error) {

Check failure on line 83 in internal/webhook/spinapp_validating.go

View workflow job for this annotation

GitHub Actions / lint go

func `validateExecutor` is unused (unused)
if spec.Executor == "" {
return nil, field.Invalid(
field.NewPath("spec").Child("executor"),
Expand All @@ -113,27 +106,3 @@

return nil
}

func validateAnnotations(spec spinv1alpha1.SpinAppSpec, executor *spinv1alpha1.SpinAppExecutor) *field.Error {
// We can't do any validation if the executor isn't available, but validation
// will fail because of earlier errors.
if executor == nil {
return nil
}

if executor.Spec.CreateDeployment {
return nil
}
// TODO: Make these validations opt in for executors? - Some runtimes may want these regardless.
if len(spec.DeploymentAnnotations) != 0 {
return field.Invalid(
field.NewPath("spec").Child("deploymentAnnotations"),
spec.DeploymentAnnotations,
"deploymentAnnotations can't be set when the executor does not use operator deployments")
}
if len(spec.PodAnnotations) != 0 {
return field.Invalid(field.NewPath("spec").Child("podAnnotations"), spec.PodAnnotations, "podAnnotations can't be set when the executor does not use operator deployments")
}

return nil
}
59 changes: 0 additions & 59 deletions internal/webhook/spinapp_validating_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
package webhook

import (
"errors"
"testing"

spinv1alpha1 "github.com/spinkube/spin-operator/api/v1alpha1"
"github.com/spinkube/spin-operator/internal/constants"
"github.com/stretchr/testify/require"
)

func TestValidateExecutor(t *testing.T) {
t.Parallel()

_, fldErr := validateExecutor(spinv1alpha1.SpinAppSpec{}, func(string) (*spinv1alpha1.SpinAppExecutor, error) { return nil, nil })
require.EqualError(t, fldErr, "spec.executor: Invalid value: \"\": executor must be set, likely no default executor was set because you have no executors installed")

_, fldErr = validateExecutor(
spinv1alpha1.SpinAppSpec{Executor: constants.CyclotronExecutor},
func(string) (*spinv1alpha1.SpinAppExecutor, error) { return nil, errors.New("executor not found?") })
require.EqualError(t, fldErr, "spec.executor: Invalid value: \"cyclotron\": executor does not exist in namespace")

_, fldErr = validateExecutor(spinv1alpha1.SpinAppSpec{Executor: constants.ContainerDShimSpinExecutor}, func(string) (*spinv1alpha1.SpinAppExecutor, error) { return nil, nil })
require.Nil(t, fldErr)
}

func TestValidateReplicas(t *testing.T) {
t.Parallel()

Expand All @@ -33,45 +16,3 @@ func TestValidateReplicas(t *testing.T) {
fldErr = validateReplicas(spinv1alpha1.SpinAppSpec{Replicas: 1})
require.Nil(t, fldErr)
}

func TestValidateAnnotations(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we're getting rid of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They only make sense in the context of an executor right now - which when it's not necessarily there already, would cause inconsistent validation

t.Parallel()

deploymentlessExecutor := &spinv1alpha1.SpinAppExecutor{
Spec: spinv1alpha1.SpinAppExecutorSpec{
CreateDeployment: false,
},
}
deploymentfullExecutor := &spinv1alpha1.SpinAppExecutor{
Spec: spinv1alpha1.SpinAppExecutorSpec{
CreateDeployment: true,
},
}

fldErr := validateAnnotations(spinv1alpha1.SpinAppSpec{
Executor: "an-executor",
DeploymentAnnotations: map[string]string{"key": "asdf"},
}, deploymentlessExecutor)
require.EqualError(t, fldErr,
`spec.deploymentAnnotations: Invalid value: map[string]string{"key":"asdf"}: `+
`deploymentAnnotations can't be set when the executor does not use operator deployments`)

fldErr = validateAnnotations(spinv1alpha1.SpinAppSpec{
Executor: "an-executor",
PodAnnotations: map[string]string{"key": "asdf"},
}, deploymentlessExecutor)
require.EqualError(t, fldErr,
`spec.podAnnotations: Invalid value: map[string]string{"key":"asdf"}: `+
`podAnnotations can't be set when the executor does not use operator deployments`)

fldErr = validateAnnotations(spinv1alpha1.SpinAppSpec{
Executor: "an-executor",
DeploymentAnnotations: map[string]string{"key": "asdf"},
}, deploymentfullExecutor)
require.Nil(t, fldErr)

fldErr = validateAnnotations(spinv1alpha1.SpinAppSpec{
Executor: "an-executor",
}, deploymentlessExecutor)
require.Nil(t, fldErr)
}
Loading