Ejemplo n.º 1
0
func NewRootListAction(resource string, opts unversioned.ListOptions) ListActionImpl {
	action := ListActionImpl{}
	action.Verb = "list"
	action.Resource = resource
	labelSelector := opts.LabelSelector.Selector
	if labelSelector == nil {
		labelSelector = labels.Everything()
	}
	fieldSelector := opts.FieldSelector.Selector
	if fieldSelector == nil {
		fieldSelector = fields.Everything()
	}
	action.ListRestrictions = ListRestrictions{labelSelector, fieldSelector}

	return action
}
Ejemplo n.º 2
0
func NewRootWatchAction(resource string, opts unversioned.ListOptions) WatchActionImpl {
	action := WatchActionImpl{}
	action.Verb = "watch"
	action.Resource = resource
	labelSelector := opts.LabelSelector.Selector
	if labelSelector == nil {
		labelSelector = labels.Everything()
	}
	fieldSelector := opts.FieldSelector.Selector
	if fieldSelector == nil {
		fieldSelector = fields.Everything()
	}
	action.WatchRestrictions = WatchRestrictions{labelSelector, fieldSelector, opts.ResourceVersion}

	return action
}
Ejemplo n.º 3
0
func (c *FakePods) List(opts unversioned.ListOptions) (*api.PodList, error) {
	obj, err := c.Fake.Invokes(NewListAction("pods", c.Namespace, opts), &api.PodList{})
	if obj == nil {
		return nil, err
	}
	label := opts.LabelSelector.Selector
	if label == nil {
		label = labels.Everything()
	}
	list := &api.PodList{}
	for _, pod := range obj.(*api.PodList).Items {
		if label.Matches(labels.Set(pod.Labels)) {
			list.Items = append(list.Items, pod)
		}
	}
	return list, err
}
Ejemplo n.º 4
0
func (c *FakeDeployments) List(opts unversioned.ListOptions) (*extensions.DeploymentList, error) {
	obj, err := c.Fake.Invokes(NewListAction("deployments", c.Namespace, opts), &extensions.DeploymentList{})
	if obj == nil {
		return nil, err
	}
	label := opts.LabelSelector.Selector
	if label == nil {
		label = labels.Everything()
	}
	list := &extensions.DeploymentList{}
	for _, deployment := range obj.(*extensions.DeploymentList).Items {
		if label.Matches(labels.Set(deployment.Labels)) {
			list.Items = append(list.Items, deployment)
		}
	}
	return list, err
}
Ejemplo n.º 5
0
func (c *FakeHorizontalPodAutoscalers) List(opts unversioned.ListOptions) (*extensions.HorizontalPodAutoscalerList, error) {
	obj, err := c.Fake.Invokes(NewListAction("horizontalpodautoscalers", c.Namespace, opts), &extensions.HorizontalPodAutoscalerList{})
	if obj == nil {
		return nil, err
	}
	label := opts.LabelSelector.Selector
	if label == nil {
		label = labels.Everything()
	}
	list := &extensions.HorizontalPodAutoscalerList{}
	for _, a := range obj.(*extensions.HorizontalPodAutoscalerList).Items {
		if label.Matches(labels.Set(a.Labels)) {
			list.Items = append(list.Items, a)
		}
	}
	return list, err
}
Ejemplo n.º 6
0
// PodSelectorAsSelector converts the PodSelector api type into a struct that implements
// labels.Selector
func PodSelectorAsSelector(ps *PodSelector) (labels.Selector, error) {
	if ps == nil {
		return labels.Nothing(), nil
	}
	if len(ps.MatchLabels)+len(ps.MatchExpressions) == 0 {
		return labels.Everything(), nil
	}
	selector := labels.LabelSelector{}
	for k, v := range ps.MatchLabels {
		req, err := labels.NewRequirement(k, labels.InOperator, sets.NewString(v))
		if err != nil {
			return nil, err
		}
		selector = append(selector, *req)
	}
	for _, expr := range ps.MatchExpressions {
		var op labels.Operator
		switch expr.Operator {
		case PodSelectorOpIn:
			op = labels.InOperator
		case PodSelectorOpNotIn:
			op = labels.NotInOperator
		case PodSelectorOpExists:
			op = labels.ExistsOperator
		case PodSelectorOpDoesNotExist:
			op = labels.DoesNotExistOperator
		default:
			return nil, fmt.Errorf("%q is not a valid pod selector operator", expr.Operator)
		}
		req, err := labels.NewRequirement(expr.Key, op, sets.NewString(expr.Values...))
		if err != nil {
			return nil, err
		}
		selector = append(selector, *req)
	}
	sort.Sort(labels.ByKey(selector))
	return selector, nil
}
Ejemplo n.º 7
0
func init() {
	Scheme.AddDefaultingFuncs(
		func(obj *unversioned.ListOptions) {
			if obj.LabelSelector.Selector == nil {
				obj.LabelSelector = unversioned.LabelSelector{labels.Everything()}
			}
			if obj.FieldSelector.Selector == nil {
				obj.FieldSelector = unversioned.FieldSelector{fields.Everything()}
			}
		},
	)
	Scheme.AddConversionFuncs(
		func(in *unversioned.Time, out *unversioned.Time, s conversion.Scope) error {
			// Cannot deep copy these, because time.Time has unexported fields.
			*out = *in
			return nil
		},
		func(in *string, out *labels.Selector, s conversion.Scope) error {
			selector, err := labels.Parse(*in)
			if err != nil {
				return err
			}
			*out = selector
			return nil
		},
		func(in *string, out *fields.Selector, s conversion.Scope) error {
			selector, err := fields.ParseSelector(*in)
			if err != nil {
				return err
			}
			*out = selector
			return nil
		},
		func(in *labels.Selector, out *string, s conversion.Scope) error {
			if *in == nil {
				return nil
			}
			*out = (*in).String()
			return nil
		},
		func(in *fields.Selector, out *string, s conversion.Scope) error {
			if *in == nil {
				return nil
			}
			*out = (*in).String()
			return nil
		},
		func(in *string, out *unversioned.LabelSelector, s conversion.Scope) error {
			selector, err := labels.Parse(*in)
			if err != nil {
				return err
			}
			*out = unversioned.LabelSelector{selector}
			return nil
		},
		func(in *string, out *unversioned.FieldSelector, s conversion.Scope) error {
			selector, err := fields.ParseSelector(*in)
			if err != nil {
				return err
			}
			*out = unversioned.FieldSelector{selector}
			return nil
		},
		func(in *[]string, out *unversioned.LabelSelector, s conversion.Scope) error {
			selectorString := ""
			if len(*in) > 0 {
				selectorString = (*in)[0]
			}
			selector, err := labels.Parse(selectorString)
			if err != nil {
				return err
			}
			*out = unversioned.LabelSelector{selector}
			return nil
		},
		func(in *[]string, out *unversioned.FieldSelector, s conversion.Scope) error {
			selectorString := ""
			if len(*in) > 0 {
				selectorString = (*in)[0]
			}
			selector, err := fields.ParseSelector(selectorString)
			if err != nil {
				return err
			}
			*out = unversioned.FieldSelector{selector}
			return nil
		},
		func(in *unversioned.LabelSelector, out *string, s conversion.Scope) error {
			if in.Selector == nil {
				return nil
			}
			*out = in.Selector.String()
			return nil
		},
		func(in *unversioned.FieldSelector, out *string, s conversion.Scope) error {
			if in.Selector == nil {
				return nil
			}
			*out = in.Selector.String()
			return nil
		},
		func(in *unversioned.LabelSelector, out *unversioned.LabelSelector, s conversion.Scope) error {
			if in.Selector == nil {
				return nil
			}
			selector, err := labels.Parse(in.Selector.String())
			if err != nil {
				return err
			}
			out.Selector = selector
			return nil
		},
		func(in *unversioned.FieldSelector, out *unversioned.FieldSelector, s conversion.Scope) error {
			if in.Selector == nil {
				return nil
			}
			selector, err := fields.ParseSelector(in.Selector.String())
			if err != nil {
				return err
			}
			out.Selector = selector
			return nil
		},
		func(in *resource.Quantity, out *resource.Quantity, s conversion.Scope) error {
			// Cannot deep copy these, because inf.Dec has unexported fields.
			*out = *in.Copy()
			return nil
		},
	)
}