GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: persist effective deployer in func.yaml; block deployer switch e… · knative/func@02b00b7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • cmd
    • client_test.go
    • delete.go
    • delete_test.go
    • deploy.go
    • deploy_test.go
  • docs/reference
    • func_delete.md
    • func_deploy.md
  • pkg
    • config
      • config.go
      • config_test.go
    • deployers
      • deployers.go
      • deployers_test.go
    • deployer/testing
      • integration_test_helper.go
    • describer/testing
      • integration_test_helper.go
    • functions
      • client.go
      • client_int_test.go
      • client_test.go
      • function.go
    • k8s
      • deployer.go
    • keda
      • deployer.go
    • knative
      • deployer.go
    • lister/testing
      • integration_test_helper.go
    • mock
      • deployer.go
    • pipelines/tekton
      • pipelines_int_test.go
    • remover/testing
      • integration_test_helper.go
  • schema
    • func_yaml-schema.json
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎cmd/client_test.go‎

Copy file name to clipboardExpand all lines: cmd/client_test.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Test_NewTestClient(t *testing.T) {
2727

2828
// Trigger an invocation of the mocks by running the associated client
2929
// 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)
3131
if err != nil {
3232
t.Fatal(err)
3333
}
Collapse file

‎cmd/delete.go‎

Copy file name to clipboardExpand all lines: cmd/delete.go
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ This command undeploys a function from the cluster. By default the function from
2121
the project in the current directory is undeployed. Alternatively either the name
2222
of the function can be given as argument or the project path provided with --path.
2323
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.
2526
`,
2627
Example: `
2728
# Undeploy the function defined in the local directory
@@ -82,13 +83,19 @@ func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err
8283
defer done()
8384

8485
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
8688
} else { // Otherwise; delete the function at path (cwd by default)
8789
f, err := fn.NewFunction(cfg.Path)
8890
if err != nil {
8991
return err
9092
}
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()
9299
}
93100
}
94101

Collapse file

‎cmd/delete_test.go‎

Copy file name to clipboardExpand all lines: cmd/delete_test.go
+179Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"strings"
88
"testing"
99

10+
"knative.dev/func/pkg/deployers"
1011
fn "knative.dev/func/pkg/functions"
12+
"knative.dev/func/pkg/keda"
1113
"knative.dev/func/pkg/mock"
1214
. "knative.dev/func/pkg/testing"
1315
)
@@ -340,3 +342,180 @@ func TestDelete_NoFunctionAtPath(t *testing.T) {
340342
t.Fatalf("unexpected error message: %v", err)
341343
}
342344
}
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

‎cmd/deploy.go‎

Copy file name to clipboardExpand all lines: cmd/deploy.go
+13-11Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"knative.dev/func/cmd/common"
1717
"knative.dev/func/pkg/builders"
1818
"knative.dev/func/pkg/config"
19+
"knative.dev/func/pkg/deployers"
1920
fn "knative.dev/func/pkg/functions"
2021
"knative.dev/func/pkg/k8s"
2122
"knative.dev/func/pkg/keda"
@@ -159,6 +160,8 @@ EXAMPLES
159160
// contextually relevant function; but sets are flattened via cfg.Apply(f)
160161
cmd.Flags().StringP("builder", "b", cfg.Builder,
161162
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))
162165
cmd.Flags().StringP("registry", "r", cfg.Registry,
163166
"Container registry + registry namespace. (ex 'ghcr.io/myuser'). The full image name is automatically determined using this along with function name. ($FUNC_REGISTRY)")
164167
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
197200
"Service account to be used in the deployed function ($FUNC_SERVICE_ACCOUNT)")
198201
cmd.Flags().String("image-pull-secret", f.Deploy.ImagePullSecret,
199202
"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))
202203
// Static Flags:
203204
// Options which have static defaults only (not globally configurable nor
204205
// persisted with the function)
@@ -284,6 +285,12 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) {
284285
// Warn if registry changed but registryInsecure is still true
285286
warnRegistryInsecureChange(cmd.OutOrStderr(), cfg.Registry, f)
286287

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+
287294
if f, err = cfg.Configure(f); err != nil { // Updates f with deploy cfg
288295
return
289296
}
@@ -617,7 +624,7 @@ func (c deployConfig) Configure(f fn.Function) (fn.Function, error) {
617624
f.Build.RemoteStorageClass = c.RemoteStorageClass
618625
f.Deploy.ServiceAccountName = c.ServiceAccountName
619626
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
621628
f.Deploy.ManagementDisabled = c.ManagementDisabled
622629
f.Local.Remote = c.Remote
623630

@@ -804,21 +811,16 @@ func (c deployConfig) clientOptions() ([]fn.Option, error) {
804811
// This is needed for remote builds (deploy --remote)
805812
o = append(o, fn.WithPipelinesProvider(newTektonPipelinesProvider(creds, c.Verbose, t)))
806813

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 {
814816
case knative.KnativeDeployerName:
815817
o = append(o, fn.WithDeployer(newKnativeDeployer(c.Verbose)))
816818
case k8s.KubernetesDeployerName:
817819
o = append(o, fn.WithDeployer(newK8sDeployer(c.Verbose)))
818820
case keda.KedaDeployerName:
819821
o = append(o, fn.WithDeployer(newKedaDeployer(c.Verbose)))
820822
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)
822824
}
823825

824826
return o, nil

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.