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@91da09f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • cmd
    • 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
    • k8s
      • deployer.go
    • keda
      • deployer.go
    • knative
      • deployer.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎cmd/delete.go‎

Copy file name to clipboardExpand all lines: cmd/delete.go
+10-2Lines changed: 10 additions & 2 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
@@ -88,7 +89,14 @@ func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err
8889
if err != nil {
8990
return err
9091
}
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()
92100
}
93101
}
94102

Collapse file

‎cmd/delete_test.go‎

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

1010
fn "knative.dev/func/pkg/functions"
11+
"knative.dev/func/pkg/k8s"
12+
"knative.dev/func/pkg/keda"
1113
"knative.dev/func/pkg/mock"
1214
. "knative.dev/func/pkg/testing"
1315
)
@@ -340,3 +342,135 @@ 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 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

‎cmd/deploy.go‎

Copy file name to clipboardExpand all lines: cmd/deploy.go
+25-10Lines changed: 25 additions & 10 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_DEPLOY_TYPE)", 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,25 @@ 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+
// 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+
287307
if f, err = cfg.Configure(f); err != nil { // Updates f with deploy cfg
288308
return
289309
}
@@ -804,21 +824,16 @@ func (c deployConfig) clientOptions() ([]fn.Option, error) {
804824
// This is needed for remote builds (deploy --remote)
805825
o = append(o, fn.WithPipelinesProvider(newTektonPipelinesProvider(creds, c.Verbose, t)))
806826

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 {
814829
case knative.KnativeDeployerName:
815830
o = append(o, fn.WithDeployer(newKnativeDeployer(c.Verbose)))
816831
case k8s.KubernetesDeployerName:
817832
o = append(o, fn.WithDeployer(newK8sDeployer(c.Verbose)))
818833
case keda.KedaDeployerName:
819834
o = append(o, fn.WithDeployer(newKedaDeployer(c.Verbose)))
820835
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)
822837
}
823838

824839
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.