mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-11-03 23:40:03 +00:00 
			
		
		
		
	version field selector field names in the client
This commit is contained in:
		@@ -248,36 +248,85 @@ func (r *Request) RequestURI(uri string) *Request {
 | 
				
			|||||||
	return r
 | 
						return r
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					const (
 | 
				
			||||||
// ParseSelectorParam parses the given string as a resource selector.
 | 
						// A constant that clients can use to refer in a field selector to the object name field.
 | 
				
			||||||
// This is a convenience function so you don't have to first check that it's a
 | 
						// Will be automatically emitted as the correct name for the API version.
 | 
				
			||||||
// validly formatted selector.
 | 
						ObjectNameField = "metadata.name"
 | 
				
			||||||
func (r *Request) ParseSelectorParam(paramName, item string) *Request {
 | 
						PodHost         = "spec.host"
 | 
				
			||||||
	if r.err != nil {
 | 
					)
 | 
				
			||||||
		return r
 | 
					
 | 
				
			||||||
 | 
					type clientFieldNameToAPIVersionFieldName map[string]string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c clientFieldNameToAPIVersionFieldName) filterField(field, value string) (newField, newValue string, err error) {
 | 
				
			||||||
 | 
						newFieldName, ok := c[field]
 | 
				
			||||||
 | 
						if !ok {
 | 
				
			||||||
 | 
							return "", "", fmt.Errorf("%v - %v - no field mapping defined", field, value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var selector string
 | 
						return newFieldName, value, nil
 | 
				
			||||||
	var err error
 | 
					}
 | 
				
			||||||
	switch paramName {
 | 
					
 | 
				
			||||||
	case "labels":
 | 
					type resourceTypeToFieldMapping map[string]clientFieldNameToAPIVersionFieldName
 | 
				
			||||||
		var lsel labels.Selector
 | 
					
 | 
				
			||||||
		if lsel, err = labels.Parse(item); err == nil {
 | 
					func (r resourceTypeToFieldMapping) filterField(resourceType, field, value string) (newField, newValue string, err error) {
 | 
				
			||||||
			selector = lsel.String()
 | 
						fMapping, ok := r[resourceType]
 | 
				
			||||||
		}
 | 
						if !ok {
 | 
				
			||||||
	case "fields":
 | 
							return "", "", fmt.Errorf("%v - %v - %v - no field mapping defined", resourceType, field, value)
 | 
				
			||||||
		var fsel fields.Selector
 | 
					 | 
				
			||||||
		if fsel, err = fields.ParseSelector(item); err == nil {
 | 
					 | 
				
			||||||
			selector = fsel.String()
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	default:
 | 
					 | 
				
			||||||
		err = fmt.Errorf("unknown parameter name '%s'", paramName)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						return fMapping.filterField(field, value)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type versionToResourceToFieldMapping map[string]resourceTypeToFieldMapping
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (v versionToResourceToFieldMapping) filterField(apiVersion, resourceType, field, value string) (newField, newValue string, err error) {
 | 
				
			||||||
 | 
						rMapping, ok := v[apiVersion]
 | 
				
			||||||
 | 
						if !ok {
 | 
				
			||||||
 | 
							glog.Warningf("field selector: %v - %v - %v - %v: need to check if this is versioned correctly.", apiVersion, resourceType, field, value)
 | 
				
			||||||
 | 
							return field, value, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						newField, newValue, err = rMapping.filterField(resourceType, field, value)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		r.err = err
 | 
							// This is only a warning until we find and fix all of the client's usages.
 | 
				
			||||||
		return r
 | 
							glog.Warningf("field selector: %v - %v - %v - %v: need to check if this is versioned correctly.", apiVersion, resourceType, field, value)
 | 
				
			||||||
 | 
							return field, value, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return r.setParam(paramName, selector)
 | 
						return newField, newValue, nil
 | 
				
			||||||
}*/
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var fieldMappings = versionToResourceToFieldMapping{
 | 
				
			||||||
 | 
						"v1beta1": resourceTypeToFieldMapping{
 | 
				
			||||||
 | 
							"nodes": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								ObjectNameField: "name",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"minions": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								ObjectNameField: "name",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"pods": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								PodHost: "DesiredState.Host",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
						"v1beta2": resourceTypeToFieldMapping{
 | 
				
			||||||
 | 
							"nodes": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								ObjectNameField: "name",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"minions": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								ObjectNameField: "name",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"pods": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								PodHost: "DesiredState.Host",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
						"v1beta3": resourceTypeToFieldMapping{
 | 
				
			||||||
 | 
							"nodes": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								ObjectNameField: "metadata.name",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"minions": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								ObjectNameField: "metadata.name",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"pods": clientFieldNameToAPIVersionFieldName{
 | 
				
			||||||
 | 
								PodHost: "spec.host",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FieldsSelectorParam adds the given selector as a query parameter with the name paramName.
 | 
					// FieldsSelectorParam adds the given selector as a query parameter with the name paramName.
 | 
				
			||||||
func (r *Request) FieldsSelectorParam(s fields.Selector) *Request {
 | 
					func (r *Request) FieldsSelectorParam(s fields.Selector) *Request {
 | 
				
			||||||
@@ -287,7 +336,14 @@ func (r *Request) FieldsSelectorParam(s fields.Selector) *Request {
 | 
				
			|||||||
	if s.Empty() {
 | 
						if s.Empty() {
 | 
				
			||||||
		return r
 | 
							return r
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return r.setParam(api.FieldSelectorQueryParam(r.apiVersion), s.String())
 | 
						s2, err := s.Transform(func(field, value string) (newField, newValue string, err error) {
 | 
				
			||||||
 | 
							return fieldMappings.filterField(r.apiVersion, r.resource, field, value)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							r.err = err
 | 
				
			||||||
 | 
							return r
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return r.setParam(api.FieldSelectorQueryParam(r.apiVersion), s2.String())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// LabelsSelectorParam adds the given selector as a query parameter
 | 
					// LabelsSelectorParam adds the given selector as a query parameter
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,8 +28,8 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewSourceApiserver creates a config source that watches and pulls from the apiserver.
 | 
					// NewSourceApiserver creates a config source that watches and pulls from the apiserver.
 | 
				
			||||||
func NewSourceApiserver(client *client.Client, hostname string, updates chan<- interface{}) {
 | 
					func NewSourceApiserver(c *client.Client, hostname string, updates chan<- interface{}) {
 | 
				
			||||||
	lw := cache.NewListWatchFromClient(client, "pods", api.NamespaceAll, fields.OneTermEqualSelector(getHostFieldLabel(client.APIVersion()), hostname))
 | 
						lw := cache.NewListWatchFromClient(c, "pods", api.NamespaceAll, fields.OneTermEqualSelector(client.PodHost, hostname))
 | 
				
			||||||
	newSourceApiserverFromLW(lw, updates)
 | 
						newSourceApiserverFromLW(lw, updates)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -168,7 +168,7 @@ func NewMainKubelet(
 | 
				
			|||||||
	if kubeClient != nil {
 | 
						if kubeClient != nil {
 | 
				
			||||||
		// TODO: cache.NewListWatchFromClient is limited as it takes a client implementation rather
 | 
							// TODO: cache.NewListWatchFromClient is limited as it takes a client implementation rather
 | 
				
			||||||
		// than an interface. There is no way to construct a list+watcher using resource name.
 | 
							// than an interface. There is no way to construct a list+watcher using resource name.
 | 
				
			||||||
		fieldSelector := fields.Set{"metadata.name": hostname}.AsSelector()
 | 
							fieldSelector := fields.Set{client.ObjectNameField: hostname}.AsSelector()
 | 
				
			||||||
		listWatch := &cache.ListWatch{
 | 
							listWatch := &cache.ListWatch{
 | 
				
			||||||
			ListFunc: func() (runtime.Object, error) {
 | 
								ListFunc: func() (runtime.Object, error) {
 | 
				
			||||||
				// TODO: Use List() with fieldSelector when it is supported.
 | 
									// TODO: Use List() with fieldSelector when it is supported.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -208,7 +208,7 @@ func (f *ConfigFactory) CreateFromKeys(predicateKeys, priorityKeys util.StringSe
 | 
				
			|||||||
// Returns a cache.ListWatch that finds all pods that need to be
 | 
					// Returns a cache.ListWatch that finds all pods that need to be
 | 
				
			||||||
// scheduled.
 | 
					// scheduled.
 | 
				
			||||||
func (factory *ConfigFactory) createUnassignedPodLW() *cache.ListWatch {
 | 
					func (factory *ConfigFactory) createUnassignedPodLW() *cache.ListWatch {
 | 
				
			||||||
	return cache.NewListWatchFromClient(factory.Client, "pods", api.NamespaceAll, fields.Set{getHostFieldLabel(factory.Client.APIVersion()): ""}.AsSelector())
 | 
						return cache.NewListWatchFromClient(factory.Client, "pods", api.NamespaceAll, fields.Set{client.PodHost: ""}.AsSelector())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseSelectorOrDie(s string) fields.Selector {
 | 
					func parseSelectorOrDie(s string) fields.Selector {
 | 
				
			||||||
@@ -224,7 +224,7 @@ func parseSelectorOrDie(s string) fields.Selector {
 | 
				
			|||||||
// TODO: return a ListerWatcher interface instead?
 | 
					// TODO: return a ListerWatcher interface instead?
 | 
				
			||||||
func (factory *ConfigFactory) createAssignedPodLW() *cache.ListWatch {
 | 
					func (factory *ConfigFactory) createAssignedPodLW() *cache.ListWatch {
 | 
				
			||||||
	return cache.NewListWatchFromClient(factory.Client, "pods", api.NamespaceAll,
 | 
						return cache.NewListWatchFromClient(factory.Client, "pods", api.NamespaceAll,
 | 
				
			||||||
		parseSelectorOrDie(getHostFieldLabel(factory.Client.APIVersion())+"!="))
 | 
							parseSelectorOrDie(client.PodHost+"!="))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// createMinionLW returns a cache.ListWatch that gets all changes to minions.
 | 
					// createMinionLW returns a cache.ListWatch that gets all changes to minions.
 | 
				
			||||||
@@ -295,15 +295,6 @@ func (factory *ConfigFactory) makeDefaultErrorFunc(backoff *podBackoff, podQueue
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getHostFieldLabel(apiVersion string) string {
 | 
					 | 
				
			||||||
	switch apiVersion {
 | 
					 | 
				
			||||||
	case "v1beta1", "v1beta2":
 | 
					 | 
				
			||||||
		return "DesiredState.Host"
 | 
					 | 
				
			||||||
	default:
 | 
					 | 
				
			||||||
		return "spec.host"
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// nodeEnumerator allows a cache.Poller to enumerate items in an api.NodeList
 | 
					// nodeEnumerator allows a cache.Poller to enumerate items in an api.NodeList
 | 
				
			||||||
type nodeEnumerator struct {
 | 
					type nodeEnumerator struct {
 | 
				
			||||||
	*api.NodeList
 | 
						*api.NodeList
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user