admission.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. Copyright 2017 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 noderestriction
  14. import (
  15. "fmt"
  16. "io"
  17. "strings"
  18. "k8s.io/api/core/v1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. "k8s.io/apimachinery/pkg/api/errors"
  21. "k8s.io/apimachinery/pkg/api/meta"
  22. "k8s.io/apimachinery/pkg/util/diff"
  23. "k8s.io/apimachinery/pkg/util/sets"
  24. "k8s.io/apiserver/pkg/admission"
  25. apiserveradmission "k8s.io/apiserver/pkg/admission/initializer"
  26. utilfeature "k8s.io/apiserver/pkg/util/feature"
  27. "k8s.io/client-go/informers"
  28. corev1lister "k8s.io/client-go/listers/core/v1"
  29. "k8s.io/component-base/featuregate"
  30. "k8s.io/klog"
  31. podutil "k8s.io/kubernetes/pkg/api/pod"
  32. authenticationapi "k8s.io/kubernetes/pkg/apis/authentication"
  33. coordapi "k8s.io/kubernetes/pkg/apis/coordination"
  34. api "k8s.io/kubernetes/pkg/apis/core"
  35. "k8s.io/kubernetes/pkg/apis/policy"
  36. storage "k8s.io/kubernetes/pkg/apis/storage"
  37. "k8s.io/kubernetes/pkg/auth/nodeidentifier"
  38. "k8s.io/kubernetes/pkg/features"
  39. kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
  40. )
  41. // PluginName is a string with the name of the plugin
  42. const PluginName = "NodeRestriction"
  43. // Register registers a plugin
  44. func Register(plugins *admission.Plugins) {
  45. plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
  46. return NewPlugin(nodeidentifier.NewDefaultNodeIdentifier()), nil
  47. })
  48. }
  49. // NewPlugin creates a new NodeRestriction admission plugin.
  50. // This plugin identifies requests from nodes
  51. func NewPlugin(nodeIdentifier nodeidentifier.NodeIdentifier) *Plugin {
  52. return &Plugin{
  53. Handler: admission.NewHandler(admission.Create, admission.Update, admission.Delete),
  54. nodeIdentifier: nodeIdentifier,
  55. features: utilfeature.DefaultFeatureGate,
  56. }
  57. }
  58. // Plugin holds state for and implements the admission plugin.
  59. type Plugin struct {
  60. *admission.Handler
  61. nodeIdentifier nodeidentifier.NodeIdentifier
  62. podsGetter corev1lister.PodLister
  63. // allows overriding for testing
  64. features featuregate.FeatureGate
  65. }
  66. var (
  67. _ = admission.Interface(&Plugin{})
  68. _ = apiserveradmission.WantsExternalKubeInformerFactory(&Plugin{})
  69. )
  70. // SetExternalKubeInformerFactory registers an informer factory into Plugin
  71. func (p *Plugin) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
  72. p.podsGetter = f.Core().V1().Pods().Lister()
  73. }
  74. // ValidateInitialization validates the Plugin was initialized properly
  75. func (p *Plugin) ValidateInitialization() error {
  76. if p.nodeIdentifier == nil {
  77. return fmt.Errorf("%s requires a node identifier", PluginName)
  78. }
  79. if p.podsGetter == nil {
  80. return fmt.Errorf("%s requires a pod getter", PluginName)
  81. }
  82. return nil
  83. }
  84. var (
  85. podResource = api.Resource("pods")
  86. nodeResource = api.Resource("nodes")
  87. pvcResource = api.Resource("persistentvolumeclaims")
  88. svcacctResource = api.Resource("serviceaccounts")
  89. leaseResource = coordapi.Resource("leases")
  90. csiNodeResource = storage.Resource("csinodes")
  91. )
  92. // Admit checks the admission policy and triggers corresponding actions
  93. func (p *Plugin) Admit(a admission.Attributes, o admission.ObjectInterfaces) error {
  94. nodeName, isNode := p.nodeIdentifier.NodeIdentity(a.GetUserInfo())
  95. // Our job is just to restrict nodes
  96. if !isNode {
  97. return nil
  98. }
  99. if len(nodeName) == 0 {
  100. // disallow requests we cannot match to a particular node
  101. return admission.NewForbidden(a, fmt.Errorf("could not determine node from user %q", a.GetUserInfo().GetName()))
  102. }
  103. switch a.GetResource().GroupResource() {
  104. case podResource:
  105. switch a.GetSubresource() {
  106. case "":
  107. return p.admitPod(nodeName, a)
  108. case "status":
  109. return p.admitPodStatus(nodeName, a)
  110. case "eviction":
  111. return p.admitPodEviction(nodeName, a)
  112. default:
  113. return admission.NewForbidden(a, fmt.Errorf("unexpected pod subresource %q, only 'status' and 'eviction' are allowed", a.GetSubresource()))
  114. }
  115. case nodeResource:
  116. return p.admitNode(nodeName, a)
  117. case pvcResource:
  118. switch a.GetSubresource() {
  119. case "status":
  120. return p.admitPVCStatus(nodeName, a)
  121. default:
  122. return admission.NewForbidden(a, fmt.Errorf("may only update PVC status"))
  123. }
  124. case svcacctResource:
  125. if p.features.Enabled(features.TokenRequest) {
  126. return p.admitServiceAccount(nodeName, a)
  127. }
  128. return nil
  129. case leaseResource:
  130. if p.features.Enabled(features.NodeLease) {
  131. return p.admitLease(nodeName, a)
  132. }
  133. return admission.NewForbidden(a, fmt.Errorf("disabled by feature gate %s", features.NodeLease))
  134. case csiNodeResource:
  135. if p.features.Enabled(features.KubeletPluginsWatcher) && p.features.Enabled(features.CSINodeInfo) {
  136. return p.admitCSINode(nodeName, a)
  137. }
  138. return admission.NewForbidden(a, fmt.Errorf("disabled by feature gates %s and %s", features.KubeletPluginsWatcher, features.CSINodeInfo))
  139. default:
  140. return nil
  141. }
  142. }
  143. // admitPod allows creating or deleting a pod if it is assigned to the
  144. // current node and fulfills related criteria.
  145. func (p *Plugin) admitPod(nodeName string, a admission.Attributes) error {
  146. switch a.GetOperation() {
  147. case admission.Create:
  148. // require a pod object
  149. pod, ok := a.GetObject().(*api.Pod)
  150. if !ok {
  151. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  152. }
  153. // only allow nodes to create mirror pods
  154. if _, isMirrorPod := pod.Annotations[api.MirrorPodAnnotationKey]; !isMirrorPod {
  155. return admission.NewForbidden(a, fmt.Errorf("pod does not have %q annotation, node %q can only create mirror pods", api.MirrorPodAnnotationKey, nodeName))
  156. }
  157. // only allow nodes to create a pod bound to itself
  158. if pod.Spec.NodeName != nodeName {
  159. return admission.NewForbidden(a, fmt.Errorf("node %q can only create pods with spec.nodeName set to itself", nodeName))
  160. }
  161. // don't allow a node to create a pod that references any other API objects
  162. if pod.Spec.ServiceAccountName != "" {
  163. return admission.NewForbidden(a, fmt.Errorf("node %q can not create pods that reference a service account", nodeName))
  164. }
  165. hasSecrets := false
  166. podutil.VisitPodSecretNames(pod, func(name string) (shouldContinue bool) { hasSecrets = true; return false })
  167. if hasSecrets {
  168. return admission.NewForbidden(a, fmt.Errorf("node %q can not create pods that reference secrets", nodeName))
  169. }
  170. hasConfigMaps := false
  171. podutil.VisitPodConfigmapNames(pod, func(name string) (shouldContinue bool) { hasConfigMaps = true; return false })
  172. if hasConfigMaps {
  173. return admission.NewForbidden(a, fmt.Errorf("node %q can not create pods that reference configmaps", nodeName))
  174. }
  175. for _, v := range pod.Spec.Volumes {
  176. if v.PersistentVolumeClaim != nil {
  177. return admission.NewForbidden(a, fmt.Errorf("node %q can not create pods that reference persistentvolumeclaims", nodeName))
  178. }
  179. }
  180. return nil
  181. case admission.Delete:
  182. // get the existing pod
  183. existingPod, err := p.podsGetter.Pods(a.GetNamespace()).Get(a.GetName())
  184. if errors.IsNotFound(err) {
  185. return err
  186. }
  187. if err != nil {
  188. return admission.NewForbidden(a, err)
  189. }
  190. // only allow a node to delete a pod bound to itself
  191. if existingPod.Spec.NodeName != nodeName {
  192. return admission.NewForbidden(a, fmt.Errorf("node %q can only delete pods with spec.nodeName set to itself", nodeName))
  193. }
  194. return nil
  195. default:
  196. return admission.NewForbidden(a, fmt.Errorf("unexpected operation %q, node %q can only create and delete mirror pods", a.GetOperation(), nodeName))
  197. }
  198. }
  199. // admitPodStatus allows to update the status of a pod if it is
  200. // assigned to the current node.
  201. func (p *Plugin) admitPodStatus(nodeName string, a admission.Attributes) error {
  202. switch a.GetOperation() {
  203. case admission.Update:
  204. // require an existing pod
  205. pod, ok := a.GetOldObject().(*api.Pod)
  206. if !ok {
  207. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetOldObject()))
  208. }
  209. // only allow a node to update status of a pod bound to itself
  210. if pod.Spec.NodeName != nodeName {
  211. return admission.NewForbidden(a, fmt.Errorf("node %q can only update pod status for pods with spec.nodeName set to itself", nodeName))
  212. }
  213. return nil
  214. default:
  215. return admission.NewForbidden(a, fmt.Errorf("unexpected operation %q", a.GetOperation()))
  216. }
  217. }
  218. // admitPodEviction allows to evict a pod if it is assigned to the current node.
  219. func (p *Plugin) admitPodEviction(nodeName string, a admission.Attributes) error {
  220. switch a.GetOperation() {
  221. case admission.Create:
  222. // require eviction to an existing pod object
  223. eviction, ok := a.GetObject().(*policy.Eviction)
  224. if !ok {
  225. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  226. }
  227. // use pod name from the admission attributes, if set, rather than from the submitted Eviction object
  228. podName := a.GetName()
  229. if len(podName) == 0 {
  230. if len(eviction.Name) == 0 {
  231. return admission.NewForbidden(a, fmt.Errorf("could not determine pod from request data"))
  232. }
  233. podName = eviction.Name
  234. }
  235. // get the existing pod
  236. existingPod, err := p.podsGetter.Pods(a.GetNamespace()).Get(podName)
  237. if errors.IsNotFound(err) {
  238. return err
  239. }
  240. if err != nil {
  241. return admission.NewForbidden(a, err)
  242. }
  243. // only allow a node to evict a pod bound to itself
  244. if existingPod.Spec.NodeName != nodeName {
  245. return admission.NewForbidden(a, fmt.Errorf("node %s can only evict pods with spec.nodeName set to itself", nodeName))
  246. }
  247. return nil
  248. default:
  249. return admission.NewForbidden(a, fmt.Errorf("unexpected operation %s", a.GetOperation()))
  250. }
  251. }
  252. func (p *Plugin) admitPVCStatus(nodeName string, a admission.Attributes) error {
  253. switch a.GetOperation() {
  254. case admission.Update:
  255. if !p.features.Enabled(features.ExpandPersistentVolumes) {
  256. return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to update persistentvolumeclaim metadata", nodeName))
  257. }
  258. oldPVC, ok := a.GetOldObject().(*api.PersistentVolumeClaim)
  259. if !ok {
  260. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetOldObject()))
  261. }
  262. newPVC, ok := a.GetObject().(*api.PersistentVolumeClaim)
  263. if !ok {
  264. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  265. }
  266. // make copies for comparison
  267. oldPVC = oldPVC.DeepCopy()
  268. newPVC = newPVC.DeepCopy()
  269. // zero out resourceVersion to avoid comparing differences,
  270. // since the new object could leave it empty to indicate an unconditional update
  271. oldPVC.ObjectMeta.ResourceVersion = ""
  272. newPVC.ObjectMeta.ResourceVersion = ""
  273. oldPVC.Status.Capacity = nil
  274. newPVC.Status.Capacity = nil
  275. oldPVC.Status.Conditions = nil
  276. newPVC.Status.Conditions = nil
  277. // TODO(apelisse): We don't have a good mechanism to
  278. // verify that only the things that should have changed
  279. // have changed. Ignore it for now.
  280. oldPVC.ObjectMeta.ManagedFields = nil
  281. newPVC.ObjectMeta.ManagedFields = nil
  282. // ensure no metadata changed. nodes should not be able to relabel, add finalizers/owners, etc
  283. if !apiequality.Semantic.DeepEqual(oldPVC, newPVC) {
  284. return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to update fields other than status.capacity and status.conditions: %v", nodeName, diff.ObjectReflectDiff(oldPVC, newPVC)))
  285. }
  286. return nil
  287. default:
  288. return admission.NewForbidden(a, fmt.Errorf("unexpected operation %q", a.GetOperation()))
  289. }
  290. }
  291. func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
  292. requestedName := a.GetName()
  293. if a.GetOperation() == admission.Create {
  294. node, ok := a.GetObject().(*api.Node)
  295. if !ok {
  296. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  297. }
  298. // Don't allow a node to create its Node API object with the config source set.
  299. // We scope node access to things listed in the Node.Spec, so allowing this would allow a view escalation.
  300. if node.Spec.ConfigSource != nil {
  301. return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to create pods with a non-nil configSource", nodeName))
  302. }
  303. // Don't allow a node to register with labels outside the allowed set.
  304. // This would allow a node to add or modify its labels in a way that would let it steer privileged workloads to itself.
  305. modifiedLabels := getModifiedLabels(node.Labels, nil)
  306. if forbiddenLabels := p.getForbiddenCreateLabels(modifiedLabels); len(forbiddenLabels) > 0 {
  307. return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to set the following labels: %s", nodeName, strings.Join(forbiddenLabels.List(), ", ")))
  308. }
  309. // check and warn if nodes set labels on create that would have been forbidden on update
  310. // TODO(liggitt): in 1.17, expand getForbiddenCreateLabels to match getForbiddenUpdateLabels and drop this
  311. if forbiddenUpdateLabels := p.getForbiddenUpdateLabels(modifiedLabels); len(forbiddenUpdateLabels) > 0 {
  312. klog.Warningf("node %q added disallowed labels on node creation: %s", nodeName, strings.Join(forbiddenUpdateLabels.List(), ", "))
  313. }
  314. // On create, get name from new object if unset in admission
  315. if len(requestedName) == 0 {
  316. requestedName = node.Name
  317. }
  318. }
  319. if requestedName != nodeName {
  320. return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to modify node %q", nodeName, requestedName))
  321. }
  322. if a.GetOperation() == admission.Update {
  323. node, ok := a.GetObject().(*api.Node)
  324. if !ok {
  325. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  326. }
  327. oldNode, ok := a.GetOldObject().(*api.Node)
  328. if !ok {
  329. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  330. }
  331. // Don't allow a node to update the config source on its Node API object.
  332. // We scope node access to things listed in the Node.Spec, so allowing this would allow a view escalation.
  333. // We only do the check if the new node's configSource is non-nil; old kubelets might drop the field during a status update.
  334. if node.Spec.ConfigSource != nil && !apiequality.Semantic.DeepEqual(node.Spec.ConfigSource, oldNode.Spec.ConfigSource) {
  335. return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to update configSource to a new non-nil configSource", nodeName))
  336. }
  337. // Don't allow a node to update its own taints. This would allow a node to remove or modify its
  338. // taints in a way that would let it steer disallowed workloads to itself.
  339. if !apiequality.Semantic.DeepEqual(node.Spec.Taints, oldNode.Spec.Taints) {
  340. return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to modify taints", nodeName))
  341. }
  342. // Don't allow a node to update labels outside the allowed set.
  343. // This would allow a node to add or modify its labels in a way that would let it steer privileged workloads to itself.
  344. modifiedLabels := getModifiedLabels(node.Labels, oldNode.Labels)
  345. if forbiddenUpdateLabels := p.getForbiddenUpdateLabels(modifiedLabels); len(forbiddenUpdateLabels) > 0 {
  346. return admission.NewForbidden(a, fmt.Errorf("is not allowed to modify labels: %s", strings.Join(forbiddenUpdateLabels.List(), ", ")))
  347. }
  348. }
  349. return nil
  350. }
  351. // getModifiedLabels returns the set of label keys that are different between the two maps
  352. func getModifiedLabels(a, b map[string]string) sets.String {
  353. modified := sets.NewString()
  354. for k, v1 := range a {
  355. if v2, ok := b[k]; !ok || v1 != v2 {
  356. modified.Insert(k)
  357. }
  358. }
  359. for k, v1 := range b {
  360. if v2, ok := a[k]; !ok || v1 != v2 {
  361. modified.Insert(k)
  362. }
  363. }
  364. return modified
  365. }
  366. func isKubernetesLabel(key string) bool {
  367. namespace := getLabelNamespace(key)
  368. if namespace == "kubernetes.io" || strings.HasSuffix(namespace, ".kubernetes.io") {
  369. return true
  370. }
  371. if namespace == "k8s.io" || strings.HasSuffix(namespace, ".k8s.io") {
  372. return true
  373. }
  374. return false
  375. }
  376. func getLabelNamespace(key string) string {
  377. if parts := strings.SplitN(key, "/", 2); len(parts) == 2 {
  378. return parts[0]
  379. }
  380. return ""
  381. }
  382. // getForbiddenCreateLabels returns the set of labels that may not be set by the node.
  383. // TODO(liggitt): in 1.17, expand to match getForbiddenUpdateLabels()
  384. func (p *Plugin) getForbiddenCreateLabels(modifiedLabels sets.String) sets.String {
  385. if len(modifiedLabels) == 0 {
  386. return nil
  387. }
  388. forbiddenLabels := sets.NewString()
  389. for label := range modifiedLabels {
  390. namespace := getLabelNamespace(label)
  391. // forbid kubelets from setting node-restriction labels
  392. if namespace == v1.LabelNamespaceNodeRestriction || strings.HasSuffix(namespace, "."+v1.LabelNamespaceNodeRestriction) {
  393. forbiddenLabels.Insert(label)
  394. }
  395. }
  396. return forbiddenLabels
  397. }
  398. // getForbiddenLabels returns the set of labels that may not be set by the node on update.
  399. func (p *Plugin) getForbiddenUpdateLabels(modifiedLabels sets.String) sets.String {
  400. if len(modifiedLabels) == 0 {
  401. return nil
  402. }
  403. forbiddenLabels := sets.NewString()
  404. for label := range modifiedLabels {
  405. namespace := getLabelNamespace(label)
  406. // forbid kubelets from setting node-restriction labels
  407. if namespace == v1.LabelNamespaceNodeRestriction || strings.HasSuffix(namespace, "."+v1.LabelNamespaceNodeRestriction) {
  408. forbiddenLabels.Insert(label)
  409. }
  410. // forbid kubelets from setting unknown kubernetes.io and k8s.io labels on update
  411. if isKubernetesLabel(label) && !kubeletapis.IsKubeletLabel(label) {
  412. // TODO: defer to label policy once available
  413. forbiddenLabels.Insert(label)
  414. }
  415. }
  416. return forbiddenLabels
  417. }
  418. func (p *Plugin) admitServiceAccount(nodeName string, a admission.Attributes) error {
  419. if a.GetOperation() != admission.Create {
  420. return nil
  421. }
  422. if a.GetSubresource() != "token" {
  423. return nil
  424. }
  425. tr, ok := a.GetObject().(*authenticationapi.TokenRequest)
  426. if !ok {
  427. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  428. }
  429. // TokenRequests from a node must have a pod binding. That pod must be
  430. // scheduled on the node.
  431. ref := tr.Spec.BoundObjectRef
  432. if ref == nil ||
  433. ref.APIVersion != "v1" ||
  434. ref.Kind != "Pod" ||
  435. ref.Name == "" {
  436. return admission.NewForbidden(a, fmt.Errorf("node requested token not bound to a pod"))
  437. }
  438. if ref.UID == "" {
  439. return admission.NewForbidden(a, fmt.Errorf("node requested token with a pod binding without a uid"))
  440. }
  441. pod, err := p.podsGetter.Pods(a.GetNamespace()).Get(ref.Name)
  442. if errors.IsNotFound(err) {
  443. return err
  444. }
  445. if err != nil {
  446. return admission.NewForbidden(a, err)
  447. }
  448. if ref.UID != pod.UID {
  449. return admission.NewForbidden(a, fmt.Errorf("the UID in the bound object reference (%s) does not match the UID in record (%s). The object might have been deleted and then recreated", ref.UID, pod.UID))
  450. }
  451. if pod.Spec.NodeName != nodeName {
  452. return admission.NewForbidden(a, fmt.Errorf("node requested token bound to a pod scheduled on a different node"))
  453. }
  454. return nil
  455. }
  456. func (p *Plugin) admitLease(nodeName string, a admission.Attributes) error {
  457. // the request must be against the system namespace reserved for node leases
  458. if a.GetNamespace() != api.NamespaceNodeLease {
  459. return admission.NewForbidden(a, fmt.Errorf("can only access leases in the %q system namespace", api.NamespaceNodeLease))
  460. }
  461. // the request must come from a node with the same name as the lease
  462. if a.GetOperation() == admission.Create {
  463. // a.GetName() won't return the name on create, so we drill down to the proposed object
  464. lease, ok := a.GetObject().(*coordapi.Lease)
  465. if !ok {
  466. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  467. }
  468. if lease.Name != nodeName {
  469. return admission.NewForbidden(a, fmt.Errorf("can only access node lease with the same name as the requesting node"))
  470. }
  471. } else {
  472. if a.GetName() != nodeName {
  473. return admission.NewForbidden(a, fmt.Errorf("can only access node lease with the same name as the requesting node"))
  474. }
  475. }
  476. return nil
  477. }
  478. func (p *Plugin) admitCSINode(nodeName string, a admission.Attributes) error {
  479. // the request must come from a node with the same name as the CSINode object
  480. if a.GetOperation() == admission.Create {
  481. // a.GetName() won't return the name on create, so we drill down to the proposed object
  482. accessor, err := meta.Accessor(a.GetObject())
  483. if err != nil {
  484. return admission.NewForbidden(a, fmt.Errorf("unable to access the object name"))
  485. }
  486. if accessor.GetName() != nodeName {
  487. return admission.NewForbidden(a, fmt.Errorf("can only access CSINode with the same name as the requesting node"))
  488. }
  489. } else {
  490. if a.GetName() != nodeName {
  491. return admission.NewForbidden(a, fmt.Errorf("can only access CSINode with the same name as the requesting node"))
  492. }
  493. }
  494. return nil
  495. }