resize.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package container
  14. import (
  15. "k8s.io/apimachinery/pkg/util/runtime"
  16. "k8s.io/client-go/tools/remotecommand"
  17. )
  18. // handleResizing spawns a goroutine that processes the resize channel, calling resizeFunc for each
  19. // remotecommand.TerminalSize received from the channel. The resize channel must be closed elsewhere to stop the
  20. // goroutine.
  21. func HandleResizing(resize <-chan remotecommand.TerminalSize, resizeFunc func(size remotecommand.TerminalSize)) {
  22. if resize == nil {
  23. return
  24. }
  25. go func() {
  26. defer runtime.HandleCrash()
  27. for size := range resize {
  28. if size.Height < 1 || size.Width < 1 {
  29. continue
  30. }
  31. resizeFunc(size)
  32. }
  33. }()
  34. }