// 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 }
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) }
func (j *ServiceTestJig) waitForPodsCreated(namespace string, replicas int) ([]string, error) { timeout := 2 * time.Minute // List the pods, making sure we observe all the replicas. label := labels.SelectorFromSet(labels.Set(j.Labels)) Logf("Waiting up to %v for %d pods to be created", timeout, replicas) for start := time.Now(); time.Since(start) < timeout; time.Sleep(2 * time.Second) { options := v1.ListOptions{LabelSelector: label.String()} pods, err := j.Client.Core().Pods(namespace).List(options) if err != nil { return nil, err } found := []string{} for _, pod := range pods.Items { if pod.DeletionTimestamp != nil { continue } found = append(found, pod.Name) } if len(found) == replicas { Logf("Found all %d pods", replicas) return found, nil } Logf("Found %d/%d pods - will retry", len(found), replicas) } return nil, fmt.Errorf("Timeout waiting for %d pods to be created", replicas) }
func (t *Tester) testListMatchLabels(obj runtime.Object, assignFn AssignFunc) { ctx := t.TestContext() testLabels := map[string]string{"key": "value"} foo3 := copyOrDie(obj) t.setObjectMeta(foo3, "foo3") foo4 := copyOrDie(obj) foo4Meta := t.getObjectMetaOrFail(foo4) foo4Meta.Name = "foo4" foo4Meta.Namespace = genericapirequest.NamespaceValue(ctx) foo4Meta.Labels = testLabels objs := ([]runtime.Object{foo3, foo4}) assignFn(objs) filtered := []runtime.Object{objs[1]} selector := labels.SelectorFromSet(labels.Set(testLabels)) options := &api.ListOptions{LabelSelector: selector} listObj, err := t.storage.(rest.Lister).List(ctx, options) if err != nil { t.Errorf("unexpected error: %v", err) } items, err := listToItems(listObj) if err != nil { t.Errorf("unexpected error: %v", err) } if len(items) != len(filtered) { t.Errorf("unexpected number of items: %v", len(items)) } if !api.Semantic.DeepEqual(filtered, items) { t.Errorf("expected: %#v, got: %#v", filtered, items) } }
// 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 v1.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 }
// ClusterLevelLoggingWithKibana is an end to end test that checks to see if Kibana is alive. func ClusterLevelLoggingWithKibana(f *framework.Framework) { // graceTime is how long to keep retrying requests for status information. const graceTime = 20 * time.Minute // Check for the existence of the Kibana service. By("Checking the Kibana service exists.") s := f.ClientSet.Core().Services(api.NamespaceSystem) // Make a few attempts to connect. This makes the test robust against // being run as the first e2e test just after the e2e cluster has been created. var err error for start := time.Now(); time.Since(start) < graceTime; time.Sleep(5 * time.Second) { if _, err = s.Get("kibana-logging", metav1.GetOptions{}); err == nil { break } framework.Logf("Attempt to check for the existence of the Kibana service failed after %v", time.Since(start)) } Expect(err).NotTo(HaveOccurred()) // Wait for the Kibana pod(s) to enter the running state. By("Checking to make sure the Kibana pods are running") label := labels.SelectorFromSet(labels.Set(map[string]string{kibanaKey: kibanaValue})) options := v1.ListOptions{LabelSelector: label.String()} pods, err := f.ClientSet.Core().Pods(api.NamespaceSystem).List(options) Expect(err).NotTo(HaveOccurred()) for _, pod := range pods.Items { err = framework.WaitForPodRunningInNamespace(f.ClientSet, &pod) Expect(err).NotTo(HaveOccurred()) } By("Checking to make sure we get a response from the Kibana UI.") err = nil for start := time.Now(); time.Since(start) < graceTime; time.Sleep(5 * time.Second) { proxyRequest, errProxy := framework.GetServicesProxyRequest(f.ClientSet, f.ClientSet.Core().RESTClient().Get()) if errProxy != nil { framework.Logf("After %v failed to get services proxy request: %v", time.Since(start), errProxy) err = errProxy continue } ctx, cancel := context.WithTimeout(context.Background(), framework.SingleCallTimeout) defer cancel() // Query against the root URL for Kibana. _, err = proxyRequest.Namespace(api.NamespaceSystem). Context(ctx). Name("kibana-logging"). DoRaw() if err != nil { if ctx.Err() != nil { framework.Failf("After %v proxy call to kibana-logging failed: %v", time.Since(start), err) break } framework.Logf("After %v proxy call to kibana-logging failed: %v", time.Since(start), err) continue } break } Expect(err).NotTo(HaveOccurred()) }
func (f *ring1Factory) LogsForObject(object, options runtime.Object) (*restclient.Request, error) { clientset, err := f.clientAccessFactory.ClientSetForVersion(nil) if err != nil { return nil, err } switch t := object.(type) { case *api.Pod: opts, ok := options.(*api.PodLogOptions) if !ok { return nil, errors.New("provided options object is not a PodLogOptions") } return clientset.Core().Pods(t.Namespace).GetLogs(t.Name, opts), nil case *api.ReplicationController: opts, ok := options.(*api.PodLogOptions) if !ok { return nil, errors.New("provided options object is not a PodLogOptions") } selector := labels.SelectorFromSet(t.Spec.Selector) sortBy := func(pods []*v1.Pod) sort.Interface { return controller.ByLogging(pods) } pod, numPods, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 20*time.Second, sortBy) if err != nil { return nil, err } if numPods > 1 { fmt.Fprintf(os.Stderr, "Found %v pods, using pod/%v\n", numPods, pod.Name) } return clientset.Core().Pods(pod.Namespace).GetLogs(pod.Name, opts), nil case *extensions.ReplicaSet: opts, ok := options.(*api.PodLogOptions) if !ok { return nil, errors.New("provided options object is not a PodLogOptions") } selector, err := metav1.LabelSelectorAsSelector(t.Spec.Selector) if err != nil { return nil, fmt.Errorf("invalid label selector: %v", err) } sortBy := func(pods []*v1.Pod) sort.Interface { return controller.ByLogging(pods) } pod, numPods, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 20*time.Second, sortBy) if err != nil { return nil, err } if numPods > 1 { fmt.Fprintf(os.Stderr, "Found %v pods, using pod/%v\n", numPods, pod.Name) } return clientset.Core().Pods(pod.Namespace).GetLogs(pod.Name, opts), nil default: gvks, _, err := api.Scheme.ObjectKinds(object) if err != nil { return nil, err } return nil, fmt.Errorf("cannot get the logs from %v", gvks[0]) } }
// CalculateAntiAffinityPriority spreads pods by minimizing the number of pods belonging to the same service // on machines with the same value for a particular label. // The label to be considered is provided to the struct (ServiceAntiAffinity). func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *v1.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodes []*v1.Node) (schedulerapi.HostPriorityList, error) { var nsServicePods []*v1.Pod if services, err := s.serviceLister.GetPodServices(pod); err == nil && len(services) > 0 { // just use the first service and get the other pods within the service // TODO: a separate predicate can be created that tries to handle all services for the pod selector := labels.SelectorFromSet(services[0].Spec.Selector) pods, err := s.podLister.List(selector) if err != nil { return nil, err } // consider only the pods that belong to the same namespace for _, nsPod := range pods { if nsPod.Namespace == pod.Namespace { nsServicePods = append(nsServicePods, nsPod) } } } // separate out the nodes that have the label from the ones that don't otherNodes := []string{} labeledNodes := map[string]string{} for _, node := range nodes { if labels.Set(node.Labels).Has(s.label) { label := labels.Set(node.Labels).Get(s.label) labeledNodes[node.Name] = label } else { otherNodes = append(otherNodes, node.Name) } } podCounts := map[string]int{} for _, pod := range nsServicePods { label, exists := labeledNodes[pod.Spec.NodeName] if !exists { continue } podCounts[label]++ } numServicePods := len(nsServicePods) result := []schedulerapi.HostPriority{} //score int - scale of 0-maxPriority // 0 being the lowest priority and maxPriority being the highest for node := range labeledNodes { // initializing to the default/max node score of maxPriority fScore := float32(maxPriority) if numServicePods > 0 { fScore = maxPriority * (float32(numServicePods-podCounts[labeledNodes[node]]) / float32(numServicePods)) } result = append(result, schedulerapi.HostPriority{Host: node, Score: int(fScore)}) } // add the open nodes with a score of 0 for _, node := range otherNodes { result = append(result, schedulerapi.HostPriority{Host: node, Score: 0}) } return result, nil }
// CreateServiceForSimpleAppWithPods is a convenience wrapper to create a service and its matching pods all at once. func (f *Framework) CreateServiceForSimpleAppWithPods(contPort int, svcPort int, appName string, podSpec func(n v1.Node) v1.PodSpec, count int, block bool) (error, *v1.Service) { var err error = nil theService := f.CreateServiceForSimpleApp(contPort, svcPort, appName) f.CreatePodsPerNodeForSimpleApp(appName, podSpec, count) if block { err = testutils.WaitForPodsWithLabelRunning(f.ClientSet, f.Namespace.Name, labels.SelectorFromSet(labels.Set(theService.Spec.Selector))) } return err, theService }
// newInformerWatchPod creates an informer to check whether all pods are running. func newInformerWatchPod(f *framework.Framework, mutex *sync.Mutex, watchTimes map[string]metav1.Time, podType string) cache.Controller { ns := f.Namespace.Name checkPodRunning := func(p *v1.Pod) { mutex.Lock() defer mutex.Unlock() defer GinkgoRecover() if p.Status.Phase == v1.PodRunning { if _, found := watchTimes[p.Name]; !found { watchTimes[p.Name] = metav1.Now() } } } _, controller := cache.NewInformer( &cache.ListWatch{ ListFunc: func(options v1.ListOptions) (runtime.Object, error) { options.LabelSelector = labels.SelectorFromSet(labels.Set{"type": podType}).String() obj, err := f.ClientSet.Core().Pods(ns).List(options) return runtime.Object(obj), err }, WatchFunc: func(options v1.ListOptions) (watch.Interface, error) { options.LabelSelector = labels.SelectorFromSet(labels.Set{"type": podType}).String() return f.ClientSet.Core().Pods(ns).Watch(options) }, }, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { p, ok := obj.(*v1.Pod) Expect(ok).To(Equal(true)) go checkPodRunning(p) }, UpdateFunc: func(oldObj, newObj interface{}) { p, ok := newObj.(*v1.Pod) Expect(ok).To(Equal(true)) go checkPodRunning(p) }, }, ) return controller }
func (t *dnsConfigMapTest) init() { By("Finding a DNS pod") label := labels.SelectorFromSet(labels.Set(map[string]string{"k8s-app": "kube-dns"})) options := v1.ListOptions{LabelSelector: label.String()} pods, err := t.f.ClientSet.Core().Pods("kube-system").List(options) Expect(err).NotTo(HaveOccurred()) Expect(len(pods.Items)).Should(BeNumerically(">=", 1)) t.dnsPod = &pods.Items[0] framework.Logf("Using DNS pod: %v", t.dnsPod.Name) }
// Check that the pods comprising a service get spread evenly across available zones func SpreadServiceOrFail(f *framework.Framework, replicaCount int, image string) { // First create the service serviceName := "test-service" serviceSpec := &v1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: serviceName, Namespace: f.Namespace.Name, }, Spec: v1.ServiceSpec{ Selector: map[string]string{ "service": serviceName, }, Ports: []v1.ServicePort{{ Port: 80, TargetPort: intstr.FromInt(80), }}, }, } _, err := f.ClientSet.Core().Services(f.Namespace.Name).Create(serviceSpec) Expect(err).NotTo(HaveOccurred()) // Now create some pods behind the service podSpec := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: serviceName, Labels: map[string]string{"service": serviceName}, }, Spec: v1.PodSpec{ Containers: []v1.Container{ { Name: "test", Image: framework.GetPauseImageName(f.ClientSet), }, }, }, } // Caution: StartPods requires at least one pod to replicate. // Based on the callers, replicas is always positive number: zoneCount >= 0 implies (2*zoneCount)+1 > 0. // Thus, no need to test for it. Once the precondition changes to zero number of replicas, // test for replicaCount > 0. Otherwise, StartPods panics. framework.ExpectNoError(testutils.StartPods(f.ClientSet, replicaCount, f.Namespace.Name, serviceName, *podSpec, false, framework.Logf)) // Wait for all of them to be scheduled selector := labels.SelectorFromSet(labels.Set(map[string]string{"service": serviceName})) pods, err := framework.WaitForPodsWithLabelScheduled(f.ClientSet, f.Namespace.Name, selector) Expect(err).NotTo(HaveOccurred()) // Now make sure they're spread across zones zoneNames, err := getZoneNames(f.ClientSet) Expect(err).NotTo(HaveOccurred()) Expect(checkZoneSpreading(f.ClientSet, pods, zoneNames)).To(Equal(true)) }
// Returns selectors of services, RCs and RSs matching the given pod. func getSelectors(pod *v1.Pod, sl algorithm.ServiceLister, cl algorithm.ControllerLister, rsl algorithm.ReplicaSetLister) []labels.Selector { selectors := make([]labels.Selector, 0, 3) if services, err := sl.GetPodServices(pod); err == nil { for _, service := range services { selectors = append(selectors, labels.SelectorFromSet(service.Spec.Selector)) } } if rcs, err := cl.GetPodControllers(pod); err == nil { for _, rc := range rcs { selectors = append(selectors, labels.SelectorFromSet(rc.Spec.Selector)) } } if rss, err := rsl.GetPodReplicaSets(pod); err == nil { for _, rs := range rss { if selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector); err == nil { selectors = append(selectors, selector) } } } return selectors }
// Check that the pods comprising a replication controller get spread evenly across available zones func SpreadRCOrFail(f *framework.Framework, replicaCount int32, image string) { name := "ubelite-spread-rc-" + string(uuid.NewUUID()) By(fmt.Sprintf("Creating replication controller %s", name)) controller, err := f.ClientSet.Core().ReplicationControllers(f.Namespace.Name).Create(&v1.ReplicationController{ ObjectMeta: metav1.ObjectMeta{ Namespace: f.Namespace.Name, Name: name, }, Spec: v1.ReplicationControllerSpec{ Replicas: &replicaCount, Selector: map[string]string{ "name": name, }, Template: &v1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{"name": name}, }, Spec: v1.PodSpec{ Containers: []v1.Container{ { Name: name, Image: image, Ports: []v1.ContainerPort{{ContainerPort: 9376}}, }, }, }, }, }, }) Expect(err).NotTo(HaveOccurred()) // Cleanup the replication controller when we are done. defer func() { // Resize the replication controller to zero to get rid of pods. if err := framework.DeleteRCAndPods(f.ClientSet, f.InternalClientset, f.Namespace.Name, controller.Name); err != nil { framework.Logf("Failed to cleanup replication controller %v: %v.", controller.Name, err) } }() // List the pods, making sure we observe all the replicas. selector := labels.SelectorFromSet(labels.Set(map[string]string{"name": name})) pods, err := framework.PodsCreated(f.ClientSet, f.Namespace.Name, name, replicaCount) Expect(err).NotTo(HaveOccurred()) // Wait for all of them to be scheduled By(fmt.Sprintf("Waiting for %d replicas of %s to be scheduled. Selector: %v", replicaCount, name, selector)) pods, err = framework.WaitForPodsWithLabelScheduled(f.ClientSet, f.Namespace.Name, selector) Expect(err).NotTo(HaveOccurred()) // Now make sure they're spread across zones zoneNames, err := getZoneNames(f.ClientSet) Expect(err).NotTo(HaveOccurred()) Expect(checkZoneSpreading(f.ClientSet, pods, zoneNames)).To(Equal(true)) }
func (h *haproxyControllerTester) start(namespace string) (err error) { // Create a replication controller with the given configuration. rc := rcFromManifest(h.cfg) rc.Namespace = namespace rc.Spec.Template.Labels["name"] = rc.Name // Add the --namespace arg. // TODO: Remove this when we have proper namespace support. for i, c := range rc.Spec.Template.Spec.Containers { rc.Spec.Template.Spec.Containers[i].Args = append( c.Args, fmt.Sprintf("--namespace=%v", namespace)) framework.Logf("Container args %+v", rc.Spec.Template.Spec.Containers[i].Args) } rc, err = h.client.Core().ReplicationControllers(rc.Namespace).Create(rc) if err != nil { return } if err = framework.WaitForControlledPodsRunning(h.client, namespace, rc.Name, api.Kind("ReplicationController")); err != nil { return } h.rcName = rc.Name h.rcNamespace = rc.Namespace // Find the pods of the rc we just created. labelSelector := labels.SelectorFromSet( labels.Set(map[string]string{"name": h.rcName})) options := v1.ListOptions{LabelSelector: labelSelector.String()} pods, err := h.client.Core().Pods(h.rcNamespace).List(options) if err != nil { return err } // Find the external addresses of the nodes the pods are running on. for _, p := range pods.Items { wait.Poll(pollInterval, framework.ServiceRespondingTimeout, func() (bool, error) { address, err := framework.GetHostExternalAddress(h.client, &p) if err != nil { framework.Logf("%v", err) return false, nil } h.address = append(h.address, address) return true, nil }) } if len(h.address) == 0 { return fmt.Errorf("No external ips found for loadbalancer %v", h.getName()) } return nil }
func getDNSReplicas(c clientset.Interface) (int, error) { label := labels.SelectorFromSet(labels.Set(map[string]string{ClusterAddonLabelKey: DNSLabelName})) listOpts := v1.ListOptions{LabelSelector: label.String()} deployments, err := c.Extensions().Deployments(api.NamespaceSystem).List(listOpts) if err != nil { return 0, err } if len(deployments.Items) != 1 { return 0, fmt.Errorf("expected 1 DNS deployment, got %v", len(deployments.Items)) } deployment := deployments.Items[0] return int(*(deployment.Spec.Replicas)), nil }
// filterLabels returns a list of pods which have labels. func filterLabels(selectors map[string]string, cli clientset.Interface, ns string) (*v1.PodList, error) { var err error var selector labels.Selector var pl *v1.PodList // List pods based on selectors. This might be a tiny optimization rather then filtering // everything manually. if len(selectors) > 0 { selector = labels.SelectorFromSet(labels.Set(selectors)) options := v1.ListOptions{LabelSelector: selector.String()} pl, err = cli.Core().Pods(ns).List(options) } else { pl, err = cli.Core().Pods(ns).List(v1.ListOptions{}) } return pl, err }
// Wait for all pods to become Running. Only use when pods will run for a long time, or it will be racy. func waitForAllPodsRunning(c clientset.Interface, ns, jobName string, parallelism int32) error { label := labels.SelectorFromSet(labels.Set(map[string]string{jobSelectorKey: jobName})) return wait.Poll(framework.Poll, jobTimeout, func() (bool, error) { options := v1.ListOptions{LabelSelector: label.String()} pods, err := c.Core().Pods(ns).List(options) if err != nil { return false, err } count := int32(0) for _, p := range pods.Items { if p.Status.Phase == v1.PodRunning { count++ } } return count == parallelism, nil }) }
// scaleFromRC returns a scale subresource for a replication controller. func scaleFromRC(rc *api.ReplicationController) *autoscaling.Scale { return &autoscaling.Scale{ ObjectMeta: metav1.ObjectMeta{ Name: rc.Name, Namespace: rc.Namespace, UID: rc.UID, ResourceVersion: rc.ResourceVersion, CreationTimestamp: rc.CreationTimestamp, }, Spec: autoscaling.ScaleSpec{ Replicas: rc.Spec.Replicas, }, Status: autoscaling.ScaleStatus{ Replicas: rc.Status.Replicas, Selector: labels.SelectorFromSet(rc.Spec.Selector).String(), }, } }
func deleteDNSAutoscalerPod(c clientset.Interface) error { label := labels.SelectorFromSet(labels.Set(map[string]string{ClusterAddonLabelKey: DNSAutoscalerLabelName})) listOpts := v1.ListOptions{LabelSelector: label.String()} pods, err := c.Core().Pods(api.NamespaceSystem).List(listOpts) if err != nil { return err } if len(pods.Items) != 1 { return fmt.Errorf("expected 1 autoscaler pod, got %v", len(pods.Items)) } podName := pods.Items[0].Name if err := c.Core().Pods(api.NamespaceSystem).Delete(podName, nil); err != nil { return err } framework.Logf("DNS autoscaling pod %v deleted.", podName) return nil }
// Scales RC to a random size within [0.5*size, 1.5*size] and lists all the pods afterwards. // Scaling happens always based on original size, not the current size. func scaleResource(wg *sync.WaitGroup, config testutils.RunObjectConfig, scalingTime time.Duration) { defer GinkgoRecover() defer wg.Done() sleepUpTo(scalingTime) newSize := uint(rand.Intn(config.GetReplicas()) + config.GetReplicas()/2) framework.ExpectNoError(framework.ScaleResource( config.GetClient(), config.GetInternalClient(), config.GetNamespace(), config.GetName(), newSize, true, config.GetKind()), fmt.Sprintf("scaling rc %s for the first time", config.GetName())) selector := labels.SelectorFromSet(labels.Set(map[string]string{"name": config.GetName()})) options := v1.ListOptions{ LabelSelector: selector.String(), ResourceVersion: "0", } _, err := config.GetClient().Core().Pods(config.GetNamespace()).List(options) framework.ExpectNoError(err, fmt.Sprintf("listing pods from rc %v", config.GetName())) }
// The pod can only schedule onto nodes that satisfy requirements in both NodeAffinity and nodeSelector. func podMatchesNodeLabels(pod *v1.Pod, node *v1.Node) bool { // Check if node.Labels match pod.Spec.NodeSelector. if len(pod.Spec.NodeSelector) > 0 { selector := labels.SelectorFromSet(pod.Spec.NodeSelector) if !selector.Matches(labels.Set(node.Labels)) { return false } } // 1. nil NodeSelector matches all nodes (i.e. does not filter out any nodes) // 2. nil []NodeSelectorTerm (equivalent to non-nil empty NodeSelector) matches no nodes // 3. zero-length non-nil []NodeSelectorTerm matches no nodes also, just for simplicity // 4. nil []NodeSelectorRequirement (equivalent to non-nil empty NodeSelectorTerm) matches no nodes // 5. zero-length non-nil []NodeSelectorRequirement matches no nodes also, just for simplicity // 6. non-nil empty NodeSelectorRequirement is not allowed nodeAffinityMatches := true affinity := pod.Spec.Affinity if affinity != nil && affinity.NodeAffinity != nil { nodeAffinity := affinity.NodeAffinity // if no required NodeAffinity requirements, will do no-op, means select all nodes. // TODO: Replace next line with subsequent commented-out line when implement RequiredDuringSchedulingRequiredDuringExecution. if nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil { // if nodeAffinity.RequiredDuringSchedulingRequiredDuringExecution == nil && nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil { return true } // Match node selector for requiredDuringSchedulingRequiredDuringExecution. // TODO: Uncomment this block when implement RequiredDuringSchedulingRequiredDuringExecution. // if nodeAffinity.RequiredDuringSchedulingRequiredDuringExecution != nil { // nodeSelectorTerms := nodeAffinity.RequiredDuringSchedulingRequiredDuringExecution.NodeSelectorTerms // glog.V(10).Infof("Match for RequiredDuringSchedulingRequiredDuringExecution node selector terms %+v", nodeSelectorTerms) // nodeAffinityMatches = nodeMatchesNodeSelectorTerms(node, nodeSelectorTerms) // } // Match node selector for requiredDuringSchedulingIgnoredDuringExecution. if nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil { nodeSelectorTerms := nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms glog.V(10).Infof("Match for RequiredDuringSchedulingIgnoredDuringExecution node selector terms %+v", nodeSelectorTerms) nodeAffinityMatches = nodeAffinityMatches && nodeMatchesNodeSelectorTerms(node, nodeSelectorTerms) } } return nodeAffinityMatches }
func logPodStartupStatus(c clientset.Interface, expectedPods int, observedLabels map[string]string, period time.Duration, stopCh chan struct{}) { label := labels.SelectorFromSet(labels.Set(observedLabels)) podStore := testutils.NewPodStore(c, v1.NamespaceAll, label, fields.Everything()) defer podStore.Stop() ticker := time.NewTicker(period) defer ticker.Stop() for { select { case <-ticker.C: pods := podStore.List() startupStatus := testutils.ComputeRCStartupStatus(pods, expectedPods) framework.Logf(startupStatus.String("Density")) case <-stopCh: pods := podStore.List() startupStatus := testutils.ComputeRCStartupStatus(pods, expectedPods) framework.Logf(startupStatus.String("Density")) return } } }
func (f *ring1Factory) AttachablePodForObject(object runtime.Object) (*api.Pod, error) { clientset, err := f.clientAccessFactory.ClientSetForVersion(nil) if err != nil { return nil, err } switch t := object.(type) { case *api.ReplicationController: selector := labels.SelectorFromSet(t.Spec.Selector) sortBy := func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) return pod, err case *extensions.Deployment: selector, err := metav1.LabelSelectorAsSelector(t.Spec.Selector) if err != nil { return nil, fmt.Errorf("invalid label selector: %v", err) } sortBy := func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) return pod, err case *batch.Job: selector, err := metav1.LabelSelectorAsSelector(t.Spec.Selector) if err != nil { return nil, fmt.Errorf("invalid label selector: %v", err) } sortBy := func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) return pod, err case *api.Pod: return t, nil default: gvks, _, err := api.Scheme.ObjectKinds(object) if err != nil { return nil, err } return nil, fmt.Errorf("cannot attach to %v: not implemented", gvks[0]) } }
func TestScaleGet(t *testing.T) { storage, server := newStorage(t) defer server.Terminate(t) defer storage.Controller.Store.DestroyFunc() ctx := genericapirequest.WithNamespace(genericapirequest.NewContext(), namespace) rc, err := createController(storage.Controller, *validController, t) if err != nil { t.Fatalf("error setting new replication controller %v: %v", *validController, err) } want := &autoscaling.Scale{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, UID: rc.UID, ResourceVersion: rc.ResourceVersion, CreationTimestamp: rc.CreationTimestamp, }, Spec: autoscaling.ScaleSpec{ Replicas: validController.Spec.Replicas, }, Status: autoscaling.ScaleStatus{ Replicas: validController.Status.Replicas, Selector: labels.SelectorFromSet(validController.Spec.Template.Labels).String(), }, } obj, err := storage.Scale.Get(ctx, name, &metav1.GetOptions{}) if err != nil { t.Fatalf("error fetching scale for %s: %v", name, err) } got := obj.(*autoscaling.Scale) if !api.Semantic.DeepEqual(want, got) { t.Errorf("unexpected scale: %s", diff.ObjectDiff(want, got)) } }
func (cont *NginxIngressController) init() { mkpath := func(file string) string { return filepath.Join(framework.TestContext.RepoRoot, ingressManifestPath, "nginx", file) } framework.Logf("initializing nginx ingress controller") framework.RunKubectlOrDie("create", "-f", mkpath("rc.yaml"), fmt.Sprintf("--namespace=%v", cont.ns)) rc, err := cont.c.Core().ReplicationControllers(cont.ns).Get("nginx-ingress-controller", metav1.GetOptions{}) framework.ExpectNoError(err) cont.rc = rc framework.Logf("waiting for pods with label %v", rc.Spec.Selector) sel := labels.SelectorFromSet(labels.Set(rc.Spec.Selector)) framework.ExpectNoError(testutils.WaitForPodsWithLabelRunning(cont.c, cont.ns, sel)) pods, err := cont.c.Core().Pods(cont.ns).List(v1.ListOptions{LabelSelector: sel.String()}) framework.ExpectNoError(err) if len(pods.Items) == 0 { framework.Failf("Failed to find nginx ingress controller pods with selector %v", sel) } cont.pod = &pods.Items[0] cont.externalIP, err = framework.GetHostExternalAddress(cont.c, cont.pod) framework.ExpectNoError(err) framework.Logf("ingress controller running in pod %v on ip %v", cont.pod.Name, cont.externalIP) }
previousParams = pcm.Data }) // This test is separated because it is slow and need to run serially. // Will take around 5 minutes to run on a 4 nodes cluster. It("[Serial] [Slow] kube-dns-autoscaler should scale kube-dns pods when cluster size changed", func() { By("Replace the dns autoscaling parameters with testing parameters") err := updateDNSScalingConfigMap(c, packDNSScalingConfigMap(packLinearParams(&DNSParams_1))) Expect(err).NotTo(HaveOccurred()) defer func() { By("Restoring intial dns autoscaling parameters") Expect(updateDNSScalingConfigMap(c, packDNSScalingConfigMap(previousParams))).NotTo(HaveOccurred()) By("Wait for number of running and ready kube-dns pods recover") label := labels.SelectorFromSet(labels.Set(map[string]string{ClusterAddonLabelKey: DNSLabelName})) _, err := framework.WaitForPodsWithLabelRunningReady(c, api.NamespaceSystem, label, originDNSReplicasCount, DNSdefaultTimeout) Expect(err).NotTo(HaveOccurred()) }() By("Wait for kube-dns scaled to expected number") getExpectReplicasLinear := getExpectReplicasFuncLinear(c, &DNSParams_1) Expect(waitForDNSReplicasSatisfied(c, getExpectReplicasLinear, DNSdefaultTimeout)).NotTo(HaveOccurred()) originalSizes := make(map[string]int) sum := 0 for _, mig := range strings.Split(framework.TestContext.CloudConfig.NodeInstanceGroup, ",") { size, err := GroupSize(mig) Expect(err).NotTo(HaveOccurred()) By(fmt.Sprintf("Initial size of %s: %d", mig, size)) originalSizes[mig] = size sum += size
}, } By("submitting the pod to kubernetes") defer func() { By("deleting the pod") podClient.Delete(pod.Name, nil) }() if _, err := podClient.Create(pod); err != nil { framework.Failf("Failed to create pod: %v", err) } framework.ExpectNoError(f.WaitForPodRunning(pod.Name)) By("verifying the pod is in kubernetes") selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})) options := v1.ListOptions{LabelSelector: selector.String()} pods, err := podClient.List(options) Expect(len(pods.Items)).To(Equal(1)) By("retrieving the pod") podWithUid, err := podClient.Get(pod.Name, metav1.GetOptions{}) if err != nil { framework.Failf("Failed to get pod: %v", err) } fmt.Printf("%+v\n", podWithUid) var events *v1.EventList // Check for scheduler event about the pod. By("checking for scheduler event about the pod") framework.ExpectNoError(wait.Poll(time.Second*2, time.Second*60, func() (bool, error) { selector := fields.Set{
var _ = framework.KubeDescribe("Mesos", func() { f := framework.NewDefaultFramework("pods") var c clientset.Interface var ns string BeforeEach(func() { framework.SkipUnlessProviderIs("mesos/docker") c = f.ClientSet ns = f.Namespace.Name }) It("applies slave attributes as labels", func() { nodeClient := f.ClientSet.Core().Nodes() rackA := labels.SelectorFromSet(map[string]string{"k8s.mesosphere.io/attribute-rack": "1"}) options := v1.ListOptions{LabelSelector: rackA.String()} nodes, err := nodeClient.List(options) if err != nil { framework.Failf("Failed to query for node: %v", err) } Expect(len(nodes.Items)).To(Equal(1)) var addr string for _, a := range nodes.Items[0].Status.Addresses { if a.Type == v1.NodeInternalIP { addr = a.Address } } Expect(len(addr)).NotTo(Equal("")) })
framework.KubeDescribe("[ReplicationController]", func() { It("should recreate pods scheduled on the unreachable node "+ "AND allow scheduling of pods on a node after it rejoins the cluster", func() { // Create a replication controller for a service that serves its hostname. // The source for the Docker container kubernetes/serve_hostname is in contrib/for-demos/serve_hostname name := "my-hostname-net" newSVCByName(c, ns, name) replicas := int32(framework.TestContext.CloudConfig.NumNodes) newRCByName(c, ns, name, replicas, nil) err := framework.VerifyPods(c, ns, name, true, replicas) Expect(err).NotTo(HaveOccurred(), "Each pod should start running and responding") By("choose a node with at least one pod - we will block some network traffic on this node") label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name})) options := v1.ListOptions{LabelSelector: label.String()} pods, err := c.Core().Pods(ns).List(options) // list pods after all have been scheduled Expect(err).NotTo(HaveOccurred()) nodeName := pods.Items[0].Spec.NodeName node, err := c.Core().Nodes().Get(nodeName, metav1.GetOptions{}) Expect(err).NotTo(HaveOccurred()) // This creates a temporary network partition, verifies that 'podNameToDisappear', // that belongs to replication controller 'rcName', really disappeared (because its // grace period is set to 0). // Finally, it checks that the replication controller recreates the // pods on another node and that now the number of replicas is equal 'replicas'. By(fmt.Sprintf("blocking network traffic from node %s", node.Name)) testUnderTemporaryNetworkFailure(c, ns, node, func() {