mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Merge pull request #95179 from stevenshuang/master
Replace AreLabelsInWhiteList with IsSubset
This commit is contained in:
		@@ -148,7 +148,7 @@ func (p *Plugin) Validate(ctx context.Context, a admission.Attributes, o admissi
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if !labels.AreLabelsInWhiteList(pod.Spec.NodeSelector, whitelist) {
 | 
						if !isSubset(pod.Spec.NodeSelector, whitelist) {
 | 
				
			||||||
		return errors.NewForbidden(resource, pod.Name, fmt.Errorf("pod node label selector labels conflict with its namespace whitelist"))
 | 
							return errors.NewForbidden(resource, pod.Name, fmt.Errorf("pod node label selector labels conflict with its namespace whitelist"))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -259,3 +259,20 @@ func (p *Plugin) getNodeSelectorMap(namespace *corev1.Namespace) (labels.Set, er
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return selector, nil
 | 
						return selector, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func isSubset(subSet, superSet labels.Set) bool {
 | 
				
			||||||
 | 
						if len(superSet) == 0 {
 | 
				
			||||||
 | 
							return true
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for k, v := range subSet {
 | 
				
			||||||
 | 
							value, ok := superSet[k]
 | 
				
			||||||
 | 
							if !ok {
 | 
				
			||||||
 | 
								return false
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if value != v {
 | 
				
			||||||
 | 
								return false
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -141,25 +141,6 @@ func Equals(labels1, labels2 Set) bool {
 | 
				
			|||||||
	return true
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AreLabelsInWhiteList verifies if the provided label list
 | 
					 | 
				
			||||||
// is in the provided whitelist and returns true, otherwise false.
 | 
					 | 
				
			||||||
func AreLabelsInWhiteList(labels, whitelist Set) bool {
 | 
					 | 
				
			||||||
	if len(whitelist) == 0 {
 | 
					 | 
				
			||||||
		return true
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for k, v := range labels {
 | 
					 | 
				
			||||||
		value, ok := whitelist[k]
 | 
					 | 
				
			||||||
		if !ok {
 | 
					 | 
				
			||||||
			return false
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if value != v {
 | 
					 | 
				
			||||||
			return false
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return true
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ConvertSelectorToLabelsMap converts selector string to labels map
 | 
					// ConvertSelectorToLabelsMap converts selector string to labels map
 | 
				
			||||||
// and validates keys and values
 | 
					// and validates keys and values
 | 
				
			||||||
func ConvertSelectorToLabelsMap(selector string) (Set, error) {
 | 
					func ConvertSelectorToLabelsMap(selector string) (Set, error) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user