nfs.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright 2022 The Rook Authors. All rights reserved.
  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 v1
  14. import (
  15. "reflect"
  16. "github.com/pkg/errors"
  17. v1 "k8s.io/api/core/v1"
  18. )
  19. // KerberosEnabled returns true if Kerberos is enabled from the spec.
  20. func (n *NFSSecuritySpec) KerberosEnabled() bool {
  21. if n == nil {
  22. return false
  23. }
  24. if n.Kerberos != nil {
  25. return true
  26. }
  27. return false
  28. }
  29. // GetPrincipalName gets the principal name for the Kerberos spec or the default value if it is unset.
  30. func (k *KerberosSpec) GetPrincipalName() string {
  31. if k.PrincipalName == "" {
  32. return "nfs"
  33. }
  34. return k.PrincipalName
  35. }
  36. func (n *CephNFS) IsHostNetwork(c *ClusterSpec) bool {
  37. if n.Spec.Server.HostNetwork != nil {
  38. return *n.Spec.Server.HostNetwork
  39. }
  40. return c.Network.IsHost()
  41. }
  42. func (sec *NFSSecuritySpec) Validate() error {
  43. if sec == nil {
  44. return nil
  45. }
  46. if sec.SSSD != nil {
  47. sidecar := sec.SSSD.Sidecar
  48. if sidecar == nil {
  49. return errors.New("System Security Services Daemon (SSSD) is enabled, but no runtime option is specified; supported: [runInSidecar]")
  50. }
  51. if sidecar.Image == "" {
  52. return errors.New("System Security Services Daemon (SSSD) sidecar is enabled, but no image is specified")
  53. }
  54. if volSourceExistsAndIsEmpty(sidecar.SSSDConfigFile.VolumeSource.ToKubernetesVolumeSource()) {
  55. return errors.New("System Security Services Daemon (SSSD) sidecar is enabled with config from a VolumeSource, but no source is specified")
  56. }
  57. subDirs := map[string]bool{}
  58. for _, additionalFile := range sidecar.AdditionalFiles {
  59. subDir := additionalFile.SubPath
  60. if subDir == "" {
  61. return errors.New("System Security Services Daemon (SSSD) sidecar is enabled with additional file having no subPath specified")
  62. }
  63. if volSourceExistsAndIsEmpty(additionalFile.VolumeSource.ToKubernetesVolumeSource()) {
  64. return errors.Errorf("System Security Services Daemon (SSSD) sidecar is enabled with additional file (subPath %q), but no source is specified", subDir)
  65. }
  66. if _, ok := subDirs[subDir]; ok {
  67. return errors.Errorf("System Security Services Daemon (SSSD) sidecar is enabled with additional file containing duplicate subPath %q", subDir)
  68. }
  69. subDirs[subDir] = true
  70. }
  71. }
  72. krb := sec.Kerberos
  73. if krb != nil {
  74. if volSourceExistsAndIsEmpty(krb.ConfigFiles.VolumeSource.ToKubernetesVolumeSource()) {
  75. return errors.New("Kerberos is enabled with config from a VolumeSource, but no source is specified")
  76. }
  77. if volSourceExistsAndIsEmpty(krb.KeytabFile.VolumeSource.ToKubernetesVolumeSource()) {
  78. return errors.New("Kerberos is enabled with keytab from a VolumeSource, but no source is specified")
  79. }
  80. }
  81. return nil
  82. }
  83. func volSourceExistsAndIsEmpty(v *v1.VolumeSource) bool {
  84. return v != nil && reflect.DeepEqual(*v, v1.VolumeSource{})
  85. }