GeezerVison is showing github.com — fix: persist effective deployer in func.yaml; block deployer switch e… · knative/func@88f644d
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/delete.go +10-2Lines changed: 10 additions & 2 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 | @@ -88,7 +89,14 @@ func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err | 88 | 89 | if err != nil { | 89 | 90 | return err | 90 | 91 | } | 91 | | - return client.Remove(cmd.Context(), "", "", f, cfg.All) | | 92 | + if err = client.Remove(cmd.Context(), "", "", f, cfg.All); err != nil { | | 93 | + return err | | 94 | + } | | 95 | + // Successful undeploy: clear live-deployment state so func.yaml | | 96 | + // reflects reality. Deploy-section config (image, options, etc.) is kept. | | 97 | + f.Deploy.Namespace = "" | | 98 | + f.Deploy.Deployer = "" | | 99 | + return f.Write() | 92 | 100 | } | 93 | 101 | } | 94 | 102 |
| |
Collapse file Copy file name to clipboardExpand all lines: cmd/delete_test.go +134Lines changed: 134 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | "testing" | 9 | 9 |
| 10 | 10 | fn "knative.dev/func/pkg/functions" | | 11 | + "knative.dev/func/pkg/k8s" | | 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,135 @@ 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 both Deploy.Namespace and Deploy.Deployer - | | 348 | +// live-deployment state that's stale once undeployed. The next flagless | | 349 | +// deploy correctly falls back to the global config's deployer default. | | 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 | + Deploy: fn.DeploySpec{Namespace: "myns", Deployer: keda.KedaDeployerName}, | | 357 | + } | | 358 | + f, err := fn.New().Init(f) | | 359 | + if err != nil { | | 360 | + t.Fatal(err) | | 361 | + } | | 362 | + if err = f.Write(); err != nil { | | 363 | + t.Fatal(err) | | 364 | + } | | 365 | + | | 366 | + remover := mock.NewRemover() | | 367 | + remover.RemoveFn = func(_, _ string) error { return nil } | | 368 | + | | 369 | + cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover))) | | 370 | + cmd.SetArgs([]string{}) | | 371 | + if err := cmd.Execute(); err != nil { | | 372 | + t.Fatal(err) | | 373 | + } | | 374 | + if !remover.RemoveInvoked { | | 375 | + t.Fatal("fn.Remover not invoked") | | 376 | + } | | 377 | + | | 378 | + loaded, err := fn.NewFunction(root) | | 379 | + if err != nil { | | 380 | + t.Fatal(err) | | 381 | + } | | 382 | + if loaded.Deploy.Namespace != "" { | | 383 | + t.Fatalf("expected Deploy.Namespace cleared after a successful undeploy, got %q", loaded.Deploy.Namespace) | | 384 | + } | | 385 | + if loaded.Deploy.Deployer != "" { | | 386 | + t.Fatalf("expected Deploy.Deployer cleared after a successful undeploy, got %q", loaded.Deploy.Deployer) | | 387 | + } | | 388 | +} | | 389 | + | | 390 | +// TestDelete_ByProjectThenRedeployWithDifferentDeployerNotBlocked: | | 391 | +// a keda function, undeployed by project, must not have its subsequent | | 392 | +// redeploy with a different deployer blocked by the deployer-switch | | 393 | +// guard - the guard only fires on an already-deployed function, and | | 394 | +// undeploy should've cleared up that function & func.yaml. | | 395 | +func TestDelete_ByProjectThenRedeployWithDifferentDeployerNotBlocked(t *testing.T) { | | 396 | + root := FromTempDirectory(t) | | 397 | + f := fn.Function{ | | 398 | + Root: root, | | 399 | + Runtime: "go", | | 400 | + Registry: TestRegistry, | | 401 | + Deploy: fn.DeploySpec{Namespace: "myns", Deployer: keda.KedaDeployerName}, | | 402 | + } | | 403 | + f, err := fn.New().Init(f) | | 404 | + if err != nil { | | 405 | + t.Fatal(err) | | 406 | + } | | 407 | + if err = f.Write(); err != nil { | | 408 | + t.Fatal(err) | | 409 | + } | | 410 | + | | 411 | + remover := mock.NewRemover() | | 412 | + remover.RemoveFn = func(_, _ string) error { return nil } | | 413 | + | | 414 | + deleteCmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover))) | | 415 | + deleteCmd.SetArgs([]string{}) | | 416 | + if err := deleteCmd.Execute(); err != nil { | | 417 | + t.Fatal(err) | | 418 | + } | | 419 | + | | 420 | + deployer := mock.NewDeployer() | | 421 | + deployCmd := NewDeployCmd(NewTestClient(fn.WithDeployer(deployer))) | | 422 | + deployCmd.SetArgs([]string{"--deployer", k8s.KubernetesDeployerName}) | | 423 | + if err := deployCmd.Execute(); err != nil { | | 424 | + t.Fatalf("expected the redeploy with a different deployer to succeed after undeploy, got: %v", err) | | 425 | + } | | 426 | + if !deployer.DeployInvoked { | | 427 | + t.Fatal("expected the deployer to be invoked - the guard must not fire on an undeployed function") | | 428 | + } | | 429 | +} | | 430 | + | | 431 | +// TestDelete_ByNameDoesNotTouchLocalFuncYaml ensures delete-by-name never | | 432 | +// reads or writes the func.yaml in the current directory, even when one | | 433 | +// exists there - the deployed-marker clearing added for path-based delete | | 434 | +// is scoped to that branch only. | | 435 | +func TestDelete_ByNameDoesNotTouchLocalFuncYaml(t *testing.T) { | | 436 | + root := FromTempDirectory(t) | | 437 | + f := fn.Function{ | | 438 | + Root: root, | | 439 | + Runtime: "go", | | 440 | + Registry: TestRegistry, | | 441 | + Name: "localfn", | | 442 | + Deploy: fn.DeploySpec{Namespace: "myns", Deployer: keda.KedaDeployerName}, | | 443 | + } | | 444 | + f, err := fn.New().Init(f) | | 445 | + if err != nil { | | 446 | + t.Fatal(err) | | 447 | + } | | 448 | + if err = f.Write(); err != nil { | | 449 | + t.Fatal(err) | | 450 | + } | | 451 | + | | 452 | + remover := mock.NewRemover() | | 453 | + remover.RemoveFn = func(n, _ string) error { | | 454 | + if n != "otherfn" { | | 455 | + t.Fatalf("expected delete-by-name target %q, got %q", "otherfn", n) | | 456 | + } | | 457 | + return nil | | 458 | + } | | 459 | + | | 460 | + cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover))) | | 461 | + cmd.SetArgs([]string{"otherfn"}) // explicit name, distinct from the local function | | 462 | + if err := cmd.Execute(); err != nil { | | 463 | + t.Fatal(err) | | 464 | + } | | 465 | + if !remover.RemoveInvoked { | | 466 | + t.Fatal("fn.Remover not invoked") | | 467 | + } | | 468 | + | | 469 | + loaded, err := fn.NewFunction(root) | | 470 | + if err != nil { | | 471 | + t.Fatal(err) | | 472 | + } | | 473 | + if loaded.Deploy.Namespace != "myns" { | | 474 | + t.Fatalf("expected the local func.yaml untouched by delete-by-name, Deploy.Namespace changed to %q", loaded.Deploy.Namespace) | | 475 | + } | | 476 | +} |
Collapse file Copy file name to clipboardExpand all lines: cmd/deploy.go +25-10Lines changed: 25 additions & 10 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_DEPLOY_TYPE)", 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,25 @@ 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 | + // Block switching deployer type on an already-deployed function. | | 289 | + // Keda embeds Raw deployer. raw -> keda is the only safe transition. | | 290 | + // | | 291 | + // gauron99: once we add gateway API and its adoption becomes more integrated | | 292 | + // with core k8s, I think it would be beneficial to have as many safe-switch | | 293 | + // directions as possible including the exposure tech. This is starting point. | | 294 | + if f.Deploy.Namespace != "" { // already deployed | | 295 | + from, to := f.Deploy.Deployer, cfg.Deployer | | 296 | + if from == "" { | | 297 | + // backwards compatible function without deployer persisted. | | 298 | + // Pin to Knative deployer, not Default deliberately. | | 299 | + from = deployers.Knative | | 300 | + } | | 301 | + safeSwitch := from == k8s.KubernetesDeployerName && to == keda.KedaDeployerName | | 302 | + if from != to && !safeSwitch { | | 303 | + return fmt.Errorf("function %q was deployed with the %q deployer; redeploying with %q would orphan the old deployer's resources on the cluster - run func delete first to remove them, then redeploy", f.Name, from, to) | | 304 | + } | | 305 | + } | | 306 | + | 287 | 307 | if f, err = cfg.Configure(f); err != nil { // Updates f with deploy cfg | 288 | 308 | return | 289 | 309 | } | @@ -804,21 +824,16 @@ func (c deployConfig) clientOptions() ([]fn.Option, error) { | 804 | 824 | // This is needed for remote builds (deploy --remote) | 805 | 825 | o = append(o, fn.WithPipelinesProvider(newTektonPipelinesProvider(creds, c.Verbose, t))) | 806 | 826 |
| 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 { | | 827 | + // Add the appropriate deployer based on deploy type. | | 828 | + switch c.Deployer { | 814 | 829 | case knative.KnativeDeployerName: | 815 | 830 | o = append(o, fn.WithDeployer(newKnativeDeployer(c.Verbose))) | 816 | 831 | case k8s.KubernetesDeployerName: | 817 | 832 | o = append(o, fn.WithDeployer(newK8sDeployer(c.Verbose))) | 818 | 833 | case keda.KedaDeployerName: | 819 | 834 | o = append(o, fn.WithDeployer(newKedaDeployer(c.Verbose))) | 820 | 835 | default: | 821 | | - return o, fmt.Errorf("unsupported deploy type: %s (supported: %s, %s, %s)", deployer, knative.KnativeDeployerName, k8s.KubernetesDeployerName, keda.KedaDeployerName) | | 836 | + return o, fmt.Errorf("unsupported deploy type: %s (supported: %s, %s, %s)", c.Deployer, knative.KnativeDeployerName, k8s.KubernetesDeployerName, keda.KedaDeployerName) | 822 | 837 | } | 823 | 838 |
| 824 | 839 | return o, nil | |
|