GeezerVison is showing github.com — fix: persist effective deployer in func.yaml; block deployer switch e… · knative/func@02b00b7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/client_test.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,7 +27,7 @@ func Test_NewTestClient(t *testing.T) { | 27 | 27 |
| 28 | 28 | // Trigger an invocation of the mocks by running the associated client | 29 | 29 | // methods which depend on them | 30 | | - err := client.Remove(t.Context(), "myfunc", "myns", fn.Function{}, true) | | 30 | + _, err := client.Remove(t.Context(), "myfunc", "myns", fn.Function{}, true) | 31 | 31 | if err != nil { | 32 | 32 | t.Fatal(err) | 33 | 33 | } | |
Collapse file Copy file name to clipboardExpand all lines: cmd/delete.go +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,8 @@ This command undeploys a function from the cluster. By default the function from | 21 | 21 | the project in the current directory is undeployed. Alternatively either the name | 22 | 22 | of the function can be given as argument or the project path provided with --path. | 23 | 23 |
| 24 | | -No local files are deleted. | | 24 | +No local files are deleted. When undeploying by project (not by name), on success | | 25 | +the local func.yaml is updated to reflect that the function is no longer deployed. | 25 | 26 | `, | 26 | 27 | Example: ` | 27 | 28 | # Undeploy the function defined in the local directory | @@ -82,13 +83,19 @@ func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err | 82 | 83 | defer done() | 83 | 84 |
| 84 | 85 | if cfg.Name != "" { // Delete by name if provided | 85 | | - return client.Remove(cmd.Context(), cfg.Name, cfg.Namespace, fn.Function{}, cfg.All) | | 86 | + _, err = client.Remove(cmd.Context(), cfg.Name, cfg.Namespace, fn.Function{}, cfg.All) | | 87 | + return err | 86 | 88 | } else { // Otherwise; delete the function at path (cwd by default) | 87 | 89 | f, err := fn.NewFunction(cfg.Path) | 88 | 90 | if err != nil { | 89 | 91 | return err | 90 | 92 | } | 91 | | - return client.Remove(cmd.Context(), "", "", f, cfg.All) | | 93 | + // updates f.Deploy (clears Deploy.Namespace on success) | | 94 | + f, err = client.Remove(cmd.Context(), "", "", f, cfg.All) | | 95 | + if err != nil { | | 96 | + return err | | 97 | + } | | 98 | + return f.Write() | 92 | 99 | } | 93 | 100 | } | 94 | 101 |
| |
Collapse file Copy file name to clipboardExpand all lines: cmd/delete_test.go +179Lines changed: 179 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | "strings" | 8 | 8 | "testing" | 9 | 9 |
| | 10 | + "knative.dev/func/pkg/deployers" | 10 | 11 | fn "knative.dev/func/pkg/functions" | | 12 | + "knative.dev/func/pkg/keda" | 11 | 13 | "knative.dev/func/pkg/mock" | 12 | 14 | . "knative.dev/func/pkg/testing" | 13 | 15 | ) | @@ -340,3 +342,180 @@ func TestDelete_NoFunctionAtPath(t *testing.T) { | 340 | 342 | t.Fatalf("unexpected error message: %v", err) | 341 | 343 | } | 342 | 344 | } | | 345 | + | | 346 | +// TestDelete_ByProjectClearsDeployedMarker ensures that a successful | | 347 | +// path-based delete clears the currently-deployed STATE (Deploy.Namespace and | | 348 | +// Deploy.Deployer), which is stale once undeployed, while leaving the user's | | 349 | +// INTENT (the top-level Deployer/Namespace) untouched for the next deploy. | | 350 | +func TestDelete_ByProjectClearsDeployedMarker(t *testing.T) { | | 351 | + root := FromTempDirectory(t) | | 352 | + f := fn.Function{ | | 353 | + Root: root, | | 354 | + Runtime: "go", | | 355 | + Registry: TestRegistry, | | 356 | + Deployer: keda.KedaDeployerName, // intent - how to deploy | | 357 | + Deploy: fn.DeploySpec{Namespace: "myns", Deployer: keda.KedaDeployerName}, | | 358 | + } | | 359 | + f, err := fn.New().Init(f) | | 360 | + if err != nil { | | 361 | + t.Fatal(err) | | 362 | + } | | 363 | + if err = f.Write(); err != nil { | | 364 | + t.Fatal(err) | | 365 | + } | | 366 | + | | 367 | + remover := mock.NewRemover() | | 368 | + remover.RemoveFn = func(_, _ string) error { return nil } | | 369 | + | | 370 | + cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover))) | | 371 | + cmd.SetArgs([]string{}) | | 372 | + if err := cmd.Execute(); err != nil { | | 373 | + t.Fatal(err) | | 374 | + } | | 375 | + if !remover.RemoveInvoked { | | 376 | + t.Fatal("fn.Remover not invoked") | | 377 | + } | | 378 | + | | 379 | + loaded, err := fn.NewFunction(root) | | 380 | + if err != nil { | | 381 | + t.Fatal(err) | | 382 | + } | | 383 | + if loaded.Deploy.Namespace != "" { | | 384 | + t.Fatalf("expected Deploy.Namespace cleared after a successful undeploy, got %q", loaded.Deploy.Namespace) | | 385 | + } | | 386 | + if loaded.Deploy.Deployer != "" { | | 387 | + t.Fatalf("expected Deploy.Deployer cleared after a successful undeploy, got %q", loaded.Deploy.Deployer) | | 388 | + } | | 389 | + if loaded.Deployer != keda.KedaDeployerName { | | 390 | + t.Fatalf("expected the intended Deployer preserved as a remembered choice, got %q", loaded.Deployer) | | 391 | + } | | 392 | +} | | 393 | + | | 394 | +// TestDelete_ByNameLeavesLocalFunctionUntouched ensures delete-by-name does NOT | | 395 | +// modify the local function's deployed state, even when a local function | | 396 | +// exists in the working directory. This is the distinction we make, its the | | 397 | +// caller's responsibility (of client Remover) to deal with this. | | 398 | +func TestDelete_ByNameLeavesLocalFunctionUntouched(t *testing.T) { | | 399 | + root := FromTempDirectory(t) | | 400 | + f := fn.Function{ | | 401 | + Root: root, | | 402 | + Runtime: "go", | | 403 | + Registry: TestRegistry, | | 404 | + Name: "localfn", | | 405 | + Deploy: fn.DeploySpec{Namespace: "myns", Deployer: keda.KedaDeployerName}, | | 406 | + } | | 407 | + f, err := fn.New().Init(f) | | 408 | + if err != nil { | | 409 | + t.Fatal(err) | | 410 | + } | | 411 | + if err = f.Write(); err != nil { | | 412 | + t.Fatal(err) | | 413 | + } | | 414 | + | | 415 | + remover := mock.NewRemover() | | 416 | + remover.RemoveFn = func(n, _ string) error { | | 417 | + if n != "otherfn" { | | 418 | + t.Fatalf("expected delete-by-name target %q, got %q", "otherfn", n) | | 419 | + } | | 420 | + return nil | | 421 | + } | | 422 | + | | 423 | + cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover))) | | 424 | + cmd.SetArgs([]string{"otherfn"}) // explicit name, distinct from the local function | | 425 | + if err := cmd.Execute(); err != nil { | | 426 | + t.Fatal(err) | | 427 | + } | | 428 | + if !remover.RemoveInvoked { | | 429 | + t.Fatal("fn.Remover not invoked") | | 430 | + } | | 431 | + | | 432 | + loaded, err := fn.NewFunction(root) | | 433 | + if err != nil { | | 434 | + t.Fatal(err) | | 435 | + } | | 436 | + if loaded.Deploy.Namespace != "myns" { | | 437 | + t.Fatalf("expected delete-by-name to leave the local function untouched, Deploy.Namespace changed to %q", loaded.Deploy.Namespace) | | 438 | + } | | 439 | +} | | 440 | + | | 441 | +// TestDelete_ByProjectPreservesDeployerForRedeploy ensures that redeploying | | 442 | +// after removal keeps the INTENT deployer intact and functional. | | 443 | +func TestDelete_ByProjectPreservesDeployerForRedeploy(t *testing.T) { | | 444 | + root := FromTempDirectory(t) | | 445 | + if _, err := fn.New().Init(fn.Function{Runtime: "go", Root: root, Registry: TestRegistry}); err != nil { | | 446 | + t.Fatal(err) | | 447 | + } | | 448 | + | | 449 | + // Deploy with keda | | 450 | + deployCmd := NewDeployCmd(NewTestClient(fn.WithDeployer(mock.NewDeployer()))) | | 451 | + deployCmd.SetArgs([]string{"--deployer", keda.KedaDeployerName, "--namespace", "myns"}) | | 452 | + if err := deployCmd.Execute(); err != nil { | | 453 | + t.Fatal(err) | | 454 | + } | | 455 | + | | 456 | + // Undeploy by project | | 457 | + remover := mock.NewRemover() | | 458 | + remover.RemoveFn = func(_, _ string) error { return nil } | | 459 | + deleteCmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover))) | | 460 | + deleteCmd.SetArgs([]string{}) | | 461 | + if err := deleteCmd.Execute(); err != nil { | | 462 | + t.Fatal(err) | | 463 | + } | | 464 | + | | 465 | + // Flag-less redeploy: must reuse the persisted keda deployer. | | 466 | + deployer := mock.NewDeployer() | | 467 | + redeployCmd := NewDeployCmd(NewTestClient(fn.WithDeployer(deployer))) | | 468 | + redeployCmd.SetArgs([]string{}) | | 469 | + if err := redeployCmd.Execute(); err != nil { | | 470 | + t.Fatalf("expected a flag-less redeploy after delete to succeed, got: %v", err) | | 471 | + } | | 472 | + if !deployer.DeployInvoked { | | 473 | + t.Fatal("expected the deployer to be invoked on the redeploy") | | 474 | + } | | 475 | + | | 476 | + loaded, err := fn.NewFunction(root) | | 477 | + if err != nil { | | 478 | + t.Fatal(err) | | 479 | + } | | 480 | + if loaded.Deploy.Deployer != keda.KedaDeployerName { | | 481 | + t.Fatalf("expected the flag-less redeploy to reuse the persisted %q deployer, got %q", keda.KedaDeployerName, loaded.Deploy.Deployer) | | 482 | + } | | 483 | +} | | 484 | + | | 485 | +// TestDelete_ByProjectThenRedeployWithDifferentDeployerNotBlocked ensures that | | 486 | +// undeploy removes "deployed status" effectively unblocking the subsequent | | 487 | +// re-deployment with different deployer | | 488 | +func TestDelete_ByProjectThenRedeployWithDifferentDeployerNotBlocked(t *testing.T) { | | 489 | + root := FromTempDirectory(t) | | 490 | + f := fn.Function{ | | 491 | + Root: root, | | 492 | + Runtime: "go", | | 493 | + Registry: TestRegistry, | | 494 | + Deploy: fn.DeploySpec{Namespace: "myns", Deployer: keda.KedaDeployerName}, | | 495 | + } | | 496 | + f, err := fn.New().Init(f) | | 497 | + if err != nil { | | 498 | + t.Fatal(err) | | 499 | + } | | 500 | + if err := f.Write(); err != nil { | | 501 | + t.Fatal(err) | | 502 | + } | | 503 | + | | 504 | + remover := mock.NewRemover() | | 505 | + remover.RemoveFn = func(_, _ string) error { return nil } | | 506 | + deleteCmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover))) | | 507 | + deleteCmd.SetArgs([]string{}) | | 508 | + if err := deleteCmd.Execute(); err != nil { | | 509 | + t.Fatal(err) | | 510 | + } | | 511 | + | | 512 | + deployer := mock.NewDeployer() | | 513 | + deployCmd := NewDeployCmd(NewTestClient(fn.WithDeployer(deployer))) | | 514 | + deployCmd.SetArgs([]string{"--deployer", deployers.Kubernetes}) | | 515 | + if err := deployCmd.Execute(); err != nil { | | 516 | + t.Fatalf("expected the redeploy with a different deployer to succeed after undeploy, got: %v", err) | | 517 | + } | | 518 | + if !deployer.DeployInvoked { | | 519 | + t.Fatal("expected the deployer to be invoked - the guard must not fire on an undeployed function") | | 520 | + } | | 521 | +} |
Collapse file Copy file name to clipboardExpand all lines: cmd/deploy.go +13-11Lines changed: 13 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import ( | 16 | 16 | "knative.dev/func/cmd/common" | 17 | 17 | "knative.dev/func/pkg/builders" | 18 | 18 | "knative.dev/func/pkg/config" | | 19 | + "knative.dev/func/pkg/deployers" | 19 | 20 | fn "knative.dev/func/pkg/functions" | 20 | 21 | "knative.dev/func/pkg/k8s" | 21 | 22 | "knative.dev/func/pkg/keda" | @@ -159,6 +160,8 @@ EXAMPLES | 159 | 160 | // contextually relevant function; but sets are flattened via cfg.Apply(f) | 160 | 161 | cmd.Flags().StringP("builder", "b", cfg.Builder, | 161 | 162 | fmt.Sprintf("Builder to use when creating the function's container. Currently supported builders are %s.", KnownBuilders())) | | 163 | + cmd.Flags().String("deployer", cfg.Deployer, | | 164 | + fmt.Sprintf("Type of deployment to use: '%s' for Knative Service, '%s' for Kubernetes Deployment, or '%s' for Deployment with a KEDA HTTP scaler ($FUNC_DEPLOYER)", deployers.Knative, deployers.Kubernetes, deployers.Keda)) | 162 | 165 | cmd.Flags().StringP("registry", "r", cfg.Registry, | 163 | 166 | "Container registry + registry namespace. (ex 'ghcr.io/myuser'). The full image name is automatically determined using this along with function name. ($FUNC_REGISTRY)") | 164 | 167 | cmd.Flags().Bool("registry-insecure", cfg.RegistryInsecure, "Skip TLS certificate verification when communicating in HTTPS with the registry. The value is persisted over consecutive runs ($FUNC_REGISTRY_INSECURE)") | @@ -197,8 +200,6 @@ EXAMPLES | 197 | 200 | "Service account to be used in the deployed function ($FUNC_SERVICE_ACCOUNT)") | 198 | 201 | cmd.Flags().String("image-pull-secret", f.Deploy.ImagePullSecret, | 199 | 202 | "Image pull secret to use when the function's image is in a private registry ($FUNC_IMAGE_PULL_SECRET)") | 200 | | - cmd.Flags().String("deployer", f.Deploy.Deployer, | 201 | | - fmt.Sprintf("Type of deployment to use: '%s' for Knative Service (default), '%s' for Kubernetes Deployment or '%s' for Deployment with a Keda HTTP scaler ($FUNC_DEPLOY_TYPE)", knative.KnativeDeployerName, k8s.KubernetesDeployerName, keda.KedaDeployerName)) | 202 | 203 | // Static Flags: | 203 | 204 | // Options which have static defaults only (not globally configurable nor | 204 | 205 | // persisted with the function) | @@ -284,6 +285,12 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) { | 284 | 285 | // Warn if registry changed but registryInsecure is still true | 285 | 286 | warnRegistryInsecureChange(cmd.OutOrStderr(), cfg.Registry, f) | 286 | 287 |
| | 288 | + // Back-compat: a function deployed before the deployer was recorded has a | | 289 | + // namespace but no deployer, which historically could only mean knative. | | 290 | + if f.Deploy.Namespace != "" && f.Deploy.Deployer == "" { | | 291 | + f.Deploy.Deployer = deployers.Knative | | 292 | + } | | 293 | + | 287 | 294 | if f, err = cfg.Configure(f); err != nil { // Updates f with deploy cfg | 288 | 295 | return | 289 | 296 | } | @@ -617,7 +624,7 @@ func (c deployConfig) Configure(f fn.Function) (fn.Function, error) { | 617 | 624 | f.Build.RemoteStorageClass = c.RemoteStorageClass | 618 | 625 | f.Deploy.ServiceAccountName = c.ServiceAccountName | 619 | 626 | f.Deploy.ImagePullSecret = c.ImagePullSecret | 620 | | - f.Deploy.Deployer = c.Deployer | | 627 | + f.Deployer = c.Deployer // intent; Deploy.Deployer is set from the deploy result | 621 | 628 | f.Deploy.ManagementDisabled = c.ManagementDisabled | 622 | 629 | f.Local.Remote = c.Remote | 623 | 630 |
| @@ -804,21 +811,16 @@ func (c deployConfig) clientOptions() ([]fn.Option, error) { | 804 | 811 | // This is needed for remote builds (deploy --remote) | 805 | 812 | o = append(o, fn.WithPipelinesProvider(newTektonPipelinesProvider(creds, c.Verbose, t))) | 806 | 813 |
| 807 | | - // Add the appropriate deployer based on deploy type | 808 | | - deployer := c.Deployer | 809 | | - if deployer == "" { | 810 | | - deployer = knative.KnativeDeployerName // default to knative for backwards compatibility | 811 | | - } | 812 | | - | 813 | | - switch deployer { | | 814 | + // Add the appropriate deployer based on deploy type. | | 815 | + switch c.Deployer { | 814 | 816 | case knative.KnativeDeployerName: | 815 | 817 | o = append(o, fn.WithDeployer(newKnativeDeployer(c.Verbose))) | 816 | 818 | case k8s.KubernetesDeployerName: | 817 | 819 | o = append(o, fn.WithDeployer(newK8sDeployer(c.Verbose))) | 818 | 820 | case keda.KedaDeployerName: | 819 | 821 | o = append(o, fn.WithDeployer(newKedaDeployer(c.Verbose))) | 820 | 822 | default: | 821 | | - return o, fmt.Errorf("unsupported deploy type: %s (supported: %s, %s, %s)", deployer, knative.KnativeDeployerName, k8s.KubernetesDeployerName, keda.KedaDeployerName) | | 823 | + return o, fmt.Errorf("unsupported deploy type: %s (supported: %s, %s, %s)", c.Deployer, knative.KnativeDeployerName, k8s.KubernetesDeployerName, keda.KedaDeployerName) | 822 | 824 | } | 823 | 825 |
| 824 | 826 | return o, nil | |
|