本文整理汇总了Golang中k8s/io/kubernetes/pkg/labels.SelectorFromSet函数的典型用法代码示例。如果您正苦于以下问题:Golang SelectorFromSet函数的具体用法?Golang SelectorFromSet怎么用?Golang SelectorFromSet使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SelectorFromSet函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getTargetedRCs
func (d *DeploymentController) getTargetedRCs(deployment *experimental.Deployment) ([]api.ReplicationController, error) {
namespace := deployment.ObjectMeta.Namespace
// 1. Find all pods whose labels match deployment.Spec.Selector
podList, err := d.client.Pods(namespace).List(labels.SelectorFromSet(deployment.Spec.Selector), fields.Everything())
if err != nil {
return nil, fmt.Errorf("error listing pods: %v", err)
}
// 2. Find the corresponding RCs for pods in podList.
// TODO: Right now we list all RCs and then filter. We should add an API for this.
targetedRCs := map[string]api.ReplicationController{}
rcList, err := d.client.ReplicationControllers(namespace).List(labels.Everything())
if err != nil {
return nil, fmt.Errorf("error listing replication controllers: %v", err)
}
for _, pod := range podList.Items {
podLabelsSelector := labels.Set(pod.ObjectMeta.Labels)
for _, rc := range rcList.Items {
rcLabelsSelector := labels.SelectorFromSet(rc.Spec.Selector)
if rcLabelsSelector.Matches(podLabelsSelector) {
targetedRCs[rc.ObjectMeta.Name] = rc
continue
}
}
}
requiredRCs := []api.ReplicationController{}
for _, value := range targetedRCs {
requiredRCs = append(requiredRCs, value)
}
return requiredRCs, nil
}
示例2: GetOldRCs
// Returns the old RCs targetted by the given Deployment.
func GetOldRCs(deployment extensions.Deployment, c client.Interface) ([]*api.ReplicationController, error) {
namespace := deployment.ObjectMeta.Namespace
// 1. Find all pods whose labels match deployment.Spec.Selector
podList, err := c.Pods(namespace).List(labels.SelectorFromSet(deployment.Spec.Selector), fields.Everything(), unversioned.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing pods: %v", err)
}
// 2. Find the corresponding RCs for pods in podList.
// TODO: Right now we list all RCs and then filter. We should add an API for this.
oldRCs := map[string]api.ReplicationController{}
rcList, err := c.ReplicationControllers(namespace).List(labels.Everything(), fields.Everything(), unversioned.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing replication controllers: %v", err)
}
newRCTemplate := GetNewRCTemplate(deployment)
for _, pod := range podList.Items {
podLabelsSelector := labels.Set(pod.ObjectMeta.Labels)
for _, rc := range rcList.Items {
rcLabelsSelector := labels.SelectorFromSet(rc.Spec.Selector)
if rcLabelsSelector.Matches(podLabelsSelector) {
// Filter out RC that has the same pod template spec as the deployment - that is the new RC.
if api.Semantic.DeepEqual(rc.Spec.Template, &newRCTemplate) {
continue
}
oldRCs[rc.ObjectMeta.Name] = rc
}
}
}
requiredRCs := []*api.ReplicationController{}
for _, value := range oldRCs {
requiredRCs = append(requiredRCs, &value)
}
return requiredRCs, nil
}
示例3: getOverlappingControllers
// getOverlappingControllers finds rcs that this controller overlaps, as well as rcs overlapping this controller.
func getOverlappingControllers(rcClient coreclient.ReplicationControllerInterface, rc *api.ReplicationController) ([]api.ReplicationController, error) {
rcs, err := rcClient.List(api.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error getting replication controllers: %v", err)
}
var matchingRCs []api.ReplicationController
rcLabels := labels.Set(rc.Spec.Selector)
for _, controller := range rcs.Items {
newRCLabels := labels.Set(controller.Spec.Selector)
if labels.SelectorFromSet(newRCLabels).Matches(rcLabels) || labels.SelectorFromSet(rcLabels).Matches(newRCLabels) {
matchingRCs = append(matchingRCs, controller)
}
}
return matchingRCs, nil
}
示例4: GetReplicationControllerPodsEvents
// Gets events associated to pods in replication controller.
func GetReplicationControllerPodsEvents(client *client.Client, namespace, replicationControllerName string) ([]api.Event,
error) {
replicationController, err := client.ReplicationControllers(namespace).Get(replicationControllerName)
if err != nil {
return nil, err
}
pods, err := client.Pods(namespace).List(api.ListOptions{
LabelSelector: labels.SelectorFromSet(replicationController.Spec.Selector),
FieldSelector: fields.Everything(),
})
if err != nil {
return nil, err
}
events, err := GetPodsEvents(client, pods)
if err != nil {
return nil, err
}
return events, nil
}
示例5: NewNodeProcurement
// NewNodeProcurement returns a Procurement that checks whether the given pod task and offer
// have valid node informations available and wehther the pod spec node selector matches
// the pod labels.
// If the check is successful the slave ID and assigned slave is set in the given Spec.
func NewNodeProcurement() Procurement {
return ProcurementFunc(func(t *T, n *api.Node, ps *ProcureState) error {
// if the user has specified a target host, make sure this offer is for that host
if t.Pod.Spec.NodeName != "" && ps.offer.GetHostname() != t.Pod.Spec.NodeName {
return fmt.Errorf(
"NodeName %q does not match offer hostname %q",
t.Pod.Spec.NodeName, ps.offer.GetHostname(),
)
}
// check the NodeSelector
if len(t.Pod.Spec.NodeSelector) > 0 {
// *api.Node is optional for procurement
if n == nil || n.Labels == nil {
return fmt.Errorf(
"NodeSelector %v does not match empty labels of pod %s/%s",
t.Pod.Spec.NodeSelector, t.Pod.Namespace, t.Pod.Name,
)
}
selector := labels.SelectorFromSet(t.Pod.Spec.NodeSelector)
if !selector.Matches(labels.Set(n.Labels)) {
return fmt.Errorf(
"NodeSelector %v does not match labels %v of pod %s/%s",
t.Pod.Spec.NodeSelector, t.Pod.Labels, t.Pod.Namespace, t.Pod.Name,
)
}
}
ps.spec.SlaveID = ps.offer.GetSlaveId().GetValue()
ps.spec.AssignedSlave = ps.offer.GetHostname()
return nil
})
}
示例6: TestFiltering
func TestFiltering(t *testing.T) {
server, etcdStorage := newEtcdTestStorage(t, testapi.Default.Codec(), etcdtest.PathPrefix())
defer server.Terminate(t)
cacher := newTestCacher(etcdStorage)
defer cacher.Stop()
// Ensure that the cacher is initialized, before creating any pods,
// so that we are sure that all events will be present in cacher.
syncWatcher, err := cacher.Watch(context.TODO(), "pods/ns/foo", "0", storage.Everything)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
syncWatcher.Stop()
podFoo := makeTestPod("foo")
podFoo.Labels = map[string]string{"filter": "foo"}
podFooFiltered := makeTestPod("foo")
podFooPrime := makeTestPod("foo")
podFooPrime.Labels = map[string]string{"filter": "foo"}
podFooPrime.Spec.NodeName = "fakeNode"
podFooNS2 := makeTestPod("foo")
podFooNS2.Namespace += "2"
podFooNS2.Labels = map[string]string{"filter": "foo"}
// Create in another namespace first to make sure events from other namespaces don't get delivered
updatePod(t, etcdStorage, podFooNS2, nil)
fooCreated := updatePod(t, etcdStorage, podFoo, nil)
fooFiltered := updatePod(t, etcdStorage, podFooFiltered, fooCreated)
fooUnfiltered := updatePod(t, etcdStorage, podFoo, fooFiltered)
_ = updatePod(t, etcdStorage, podFooPrime, fooUnfiltered)
deleted := api.Pod{}
if err := etcdStorage.Delete(context.TODO(), etcdtest.AddPrefix("pods/ns/foo"), &deleted, nil); err != nil {
t.Errorf("Unexpected error: %v", err)
}
// Set up Watch for object "podFoo" with label filter set.
selector := labels.SelectorFromSet(labels.Set{"filter": "foo"})
filterFunc := func(obj runtime.Object) bool {
metadata, err := meta.Accessor(obj)
if err != nil {
t.Errorf("Unexpected error: %v", err)
return false
}
return selector.Matches(labels.Set(metadata.GetLabels()))
}
filter := storage.NewSimpleFilter(filterFunc, storage.NoTriggerFunc)
watcher, err := cacher.Watch(context.TODO(), "pods/ns/foo", fooCreated.ResourceVersion, filter)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer watcher.Stop()
verifyWatchEvent(t, watcher, watch.Deleted, podFooFiltered)
verifyWatchEvent(t, watcher, watch.Added, podFoo)
verifyWatchEvent(t, watcher, watch.Modified, podFooPrime)
verifyWatchEvent(t, watcher, watch.Deleted, podFooPrime)
}
示例7: reportLogsFromFluentdPod
func reportLogsFromFluentdPod(f *framework.Framework) error {
synthLoggerPod, err := f.PodClient().Get(synthLoggerPodName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Failed to get synth logger pod due to %v", err)
}
synthLoggerNodeName := synthLoggerPod.Spec.NodeName
if synthLoggerNodeName == "" {
return errors.New("Synthlogger pod is not assigned to the node")
}
label := labels.SelectorFromSet(labels.Set(map[string]string{"k8s-app": "fluentd-logging"}))
options := v1.ListOptions{LabelSelector: label.String()}
fluentdPods, err := f.ClientSet.Core().Pods(api.NamespaceSystem).List(options)
for _, fluentdPod := range fluentdPods.Items {
if fluentdPod.Spec.NodeName == synthLoggerNodeName {
containerName := fluentdPod.Spec.Containers[0].Name
logs, err := framework.GetPodLogs(f.ClientSet, api.NamespaceSystem, fluentdPod.Name, containerName)
if err != nil {
return fmt.Errorf("Failed to get logs from fluentd pod %s due to %v", fluentdPod.Name, err)
}
framework.Logf("Logs from fluentd pod %s:\n%s", fluentdPod.Name, logs)
return nil
}
}
return fmt.Errorf("Failed to find fluentd pod running on node %s", synthLoggerNodeName)
}
示例8: GetPodsEvents
// GetPodsEvents gets pods events associated to resource targeted by given resource selector.
func GetPodsEvents(client client.Interface, namespace string, resourceSelector map[string]string) (
[]api.Event, error) {
channels := &common.ResourceChannels{
PodList: common.GetPodListChannelWithOptions(
client,
common.NewSameNamespaceQuery(namespace),
api.ListOptions{
LabelSelector: labels.SelectorFromSet(resourceSelector),
FieldSelector: fields.Everything(),
},
1),
EventList: common.GetEventListChannel(client, common.NewSameNamespaceQuery(namespace), 1),
}
podList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}
eventList := <-channels.EventList.List
if err := <-channels.EventList.Error; err != nil {
return nil, err
}
events := filterEventsByPodsUID(eventList.Items, podList.Items)
return events, nil
}
示例9: GetServicePods
// GetServicePods gets list of pods targeted by given label selector in given namespace.
func GetServicePods(client k8sClient.Interface, heapsterClient client.HeapsterClient, namespace,
name string, dsQuery *dataselect.DataSelectQuery) (*pod.PodList, error) {
service, err := client.Core().Services(namespace).Get(name)
if err != nil {
return nil, err
}
labelSelector := labels.SelectorFromSet(service.Spec.Selector)
channels := &common.ResourceChannels{
PodList: common.GetPodListChannelWithOptions(client,
common.NewSameNamespaceQuery(namespace),
api.ListOptions{
LabelSelector: labelSelector,
FieldSelector: fields.Everything(),
},
1),
}
apiPodList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}
podList := pod.CreatePodList(apiPodList.Items, dsQuery, heapsterClient)
return &podList, nil
}
示例10: GetService
func (c *kregistry) GetService(name string) (*registry.Service, error) {
c.mtx.RLock()
svc, ok := c.services[name]
c.mtx.RUnlock()
if ok {
return svc, nil
}
selector := labels.SelectorFromSet(labels.Set{"name": name})
services, err := c.client.Services(c.namespace).List(selector, fields.Everything())
if err != nil {
return nil, err
}
if len(services.Items) == 0 {
return nil, fmt.Errorf("Service not found")
}
ks := ®istry.Service{
Name: name,
}
for _, item := range services.Items {
ks.Nodes = append(ks.Nodes, ®istry.Node{
Address: item.Spec.ClusterIP,
Port: item.Spec.Ports[0].Port,
})
}
return ks, nil
}
示例11: GetCustomMetric
// GetCustomMetric returns the average value of the given custom metric from the
// pods picked using the namespace and selector passed as arguments.
func (h *HeapsterMetricsClient) GetCustomMetric(customMetricName string, namespace string, selector map[string]string) (*float64, time.Time, error) {
metricSpec := getHeapsterCustomMetricDefinition(customMetricName)
labelSelector := labels.SelectorFromSet(labels.Set(selector))
podList, err := h.client.Core().Pods(namespace).List(api.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, time.Time{}, fmt.Errorf("failed to get pod list: %v", err)
}
podNames := []string{}
for _, pod := range podList.Items {
if pod.Status.Phase == api.PodPending {
// Skip pending pods.
continue
}
podNames = append(podNames, pod.Name)
}
if len(podNames) == 0 && len(podList.Items) > 0 {
return nil, time.Time{}, fmt.Errorf("no running pods")
}
value, timestamp, err := h.getForPods(metricSpec, namespace, podNames)
if err != nil {
return nil, time.Time{}, err
}
return &value.floatValue, timestamp, nil
}
示例12: GetAllPodServices
// get all pod services
func (cluster *CouchdbCluster) GetAllPodServices() (*[]api.Service, error) {
// service special label
serviceLabels := make(map[string]string)
for k, v := range cluster.Labels {
serviceLabels[k] = v
}
// add special pod-service label
serviceLabels[LABEL_POD_SERVICE] = "true"
// list options
listOptions := api.ListOptions{LabelSelector: labels.SelectorFromSet(labels.Set(serviceLabels))}
// get a new kube client
c, err := KubeClient(KUBE_API)
// check for errors
if err != nil {
ErrorLog("spawner_rc: get all pod Service: Cannot connect to Kubernetes api ")
ErrorLog(err)
return nil, err
} else {
// get list service
svcList, err := c.Services(cluster.Namespace).List(listOptions)
if err != nil {
ErrorLog("spawner_rc: get all pod Service: get svc list fail ")
ErrorLog(err)
return nil, err
}
// fine, no errors
return &svcList.Items, nil
}
}
示例13: podsCreated
func podsCreated(c *client.Client, ns, name string, replicas int) (*api.PodList, error) {
timeout := 2 * time.Minute
// List the pods, making sure we observe all the replicas.
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
for start := time.Now(); time.Since(start) < timeout; time.Sleep(5 * time.Second) {
pods, err := c.Pods(ns).List(label, fields.Everything())
if err != nil {
return nil, err
}
created := []api.Pod{}
for _, pod := range pods.Items {
if pod.DeletionTimestamp != nil {
continue
}
created = append(created, pod)
}
Logf("Pod name %s: Found %d pods out of %d", name, len(created), replicas)
if len(created) == replicas {
pods.Items = created
return pods, nil
}
}
return nil, fmt.Errorf("Pod name %s: Gave up waiting %v for %d pods to come up", name, timeout, replicas)
}
示例14: TestToLabelSelector
func TestToLabelSelector(t *testing.T) {
selector, _ := unversioned.LabelSelectorAsSelector(
&unversioned.LabelSelector{MatchLabels: map[string]string{"app": "test"}})
cases := []struct {
selector map[string]string
expected labels.Selector
}{
{
map[string]string{},
labels.SelectorFromSet(nil),
},
{
map[string]string{"app": "test"},
selector,
},
}
for _, c := range cases {
actual, _ := toLabelSelector(c.selector)
if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("toLabelSelector(%#v) == \n%#v\nexpected \n%#v\n",
c.selector, actual, c.expected)
}
}
}
开发者ID:FujitsuEnablingSoftwareTechnologyGmbH,项目名称:dashboard,代码行数:26,代码来源:replicationcontrollercommon_test.go
示例15: StartPods
// Simplified version of RunRC, that does not create RC, but creates plain Pods.
// Optionally waits for pods to start running (if waitForRunning == true).
// The number of replicas must be non-zero.
func StartPods(c clientset.Interface, replicas int, namespace string, podNamePrefix string,
pod api.Pod, waitForRunning bool, logFunc func(fmt string, args ...interface{})) error {
// no pod to start
if replicas < 1 {
panic("StartPods: number of replicas must be non-zero")
}
startPodsID := string(uuid.NewUUID()) // So that we can label and find them
for i := 0; i < replicas; i++ {
podName := fmt.Sprintf("%v-%v", podNamePrefix, i)
pod.ObjectMeta.Name = podName
pod.ObjectMeta.Labels["name"] = podName
pod.ObjectMeta.Labels["startPodsID"] = startPodsID
pod.Spec.Containers[0].Name = podName
_, err := c.Core().Pods(namespace).Create(&pod)
if err != nil {
return err
}
}
logFunc("Waiting for running...")
if waitForRunning {
label := labels.SelectorFromSet(labels.Set(map[string]string{"startPodsID": startPodsID}))
err := WaitForPodsWithLabelRunning(c, namespace, label)
if err != nil {
return fmt.Errorf("Error waiting for %d pods to be running - probably a timeout: %v", replicas, err)
}
}
return nil
}