config.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2016 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 imagepolicy contains an admission controller that configures a webhook to which policy
  14. // decisions are delegated.
  15. package imagepolicy
  16. import (
  17. "fmt"
  18. "time"
  19. "k8s.io/klog"
  20. )
  21. const (
  22. defaultRetryBackoff = time.Duration(500) * time.Millisecond
  23. minRetryBackoff = time.Duration(1)
  24. maxRetryBackoff = time.Duration(5) * time.Minute
  25. defaultAllowTTL = time.Duration(5) * time.Minute
  26. defaultDenyTTL = time.Duration(30) * time.Second
  27. minAllowTTL = time.Duration(1) * time.Second
  28. maxAllowTTL = time.Duration(30) * time.Minute
  29. minDenyTTL = time.Duration(1) * time.Second
  30. maxDenyTTL = time.Duration(30) * time.Minute
  31. useDefault = time.Duration(0) //sentinel for using default TTL
  32. disableTTL = time.Duration(-1) //sentinel for disabling a TTL
  33. )
  34. // imagePolicyWebhookConfig holds config data for imagePolicyWebhook
  35. type imagePolicyWebhookConfig struct {
  36. KubeConfigFile string `json:"kubeConfigFile"`
  37. AllowTTL time.Duration `json:"allowTTL"`
  38. DenyTTL time.Duration `json:"denyTTL"`
  39. RetryBackoff time.Duration `json:"retryBackoff"`
  40. DefaultAllow bool `json:"defaultAllow"`
  41. }
  42. // AdmissionConfig holds config data for admission controllers
  43. type AdmissionConfig struct {
  44. ImagePolicyWebhook imagePolicyWebhookConfig `json:"imagePolicy"`
  45. }
  46. func normalizeWebhookConfig(config *imagePolicyWebhookConfig) (err error) {
  47. config.RetryBackoff, err = normalizeConfigDuration("backoff", time.Millisecond, config.RetryBackoff, minRetryBackoff, maxRetryBackoff, defaultRetryBackoff)
  48. if err != nil {
  49. return err
  50. }
  51. config.AllowTTL, err = normalizeConfigDuration("allow cache", time.Second, config.AllowTTL, minAllowTTL, maxAllowTTL, defaultAllowTTL)
  52. if err != nil {
  53. return err
  54. }
  55. config.DenyTTL, err = normalizeConfigDuration("deny cache", time.Second, config.DenyTTL, minDenyTTL, maxDenyTTL, defaultDenyTTL)
  56. if err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func normalizeConfigDuration(name string, scale, value, min, max, defaultValue time.Duration) (time.Duration, error) {
  62. // disable with -1 sentinel
  63. if value == disableTTL {
  64. klog.V(2).Infof("image policy webhook %s disabled", name)
  65. return time.Duration(0), nil
  66. }
  67. // use default with 0 sentinel
  68. if value == useDefault {
  69. klog.V(2).Infof("image policy webhook %s using default value", name)
  70. return defaultValue, nil
  71. }
  72. // convert to s; unmarshalling gives ns
  73. value *= scale
  74. // check value is within range
  75. if value < min || value > max {
  76. return value, fmt.Errorf("valid value is between %v and %v, got %v", min, max, value)
  77. }
  78. return value, nil
  79. }