mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-04 10:47:25 +00:00
test/utils
This commit is contained in:
@@ -18,33 +18,34 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"k8s.io/kubernetes/pkg/api"
|
|
||||||
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ContainerFailures struct {
|
type ContainerFailures struct {
|
||||||
status *api.ContainerStateTerminated
|
status *v1.ContainerStateTerminated
|
||||||
Restarts int
|
Restarts int
|
||||||
}
|
}
|
||||||
|
|
||||||
// PodRunningReady checks whether pod p's phase is running and it has a ready
|
// PodRunningReady checks whether pod p's phase is running and it has a ready
|
||||||
// condition of status true.
|
// condition of status true.
|
||||||
func PodRunningReady(p *api.Pod) (bool, error) {
|
func PodRunningReady(p *v1.Pod) (bool, error) {
|
||||||
// Check the phase is running.
|
// Check the phase is running.
|
||||||
if p.Status.Phase != api.PodRunning {
|
if p.Status.Phase != v1.PodRunning {
|
||||||
return false, fmt.Errorf("want pod '%s' on '%s' to be '%v' but was '%v'",
|
return false, fmt.Errorf("want pod '%s' on '%s' to be '%v' but was '%v'",
|
||||||
p.ObjectMeta.Name, p.Spec.NodeName, api.PodRunning, p.Status.Phase)
|
p.ObjectMeta.Name, p.Spec.NodeName, v1.PodRunning, p.Status.Phase)
|
||||||
}
|
}
|
||||||
// Check the ready condition is true.
|
// Check the ready condition is true.
|
||||||
if !PodReady(p) {
|
if !PodReady(p) {
|
||||||
return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
|
return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
|
||||||
p.ObjectMeta.Name, p.Spec.NodeName, api.PodReady, api.ConditionTrue, p.Status.Conditions)
|
p.ObjectMeta.Name, p.Spec.NodeName, v1.PodReady, v1.ConditionTrue, p.Status.Conditions)
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PodRunningReadyOrSucceeded(p *api.Pod) (bool, error) {
|
func PodRunningReadyOrSucceeded(p *v1.Pod) (bool, error) {
|
||||||
// Check if the phase is succeeded.
|
// Check if the phase is succeeded.
|
||||||
if p.Status.Phase == api.PodSucceeded {
|
if p.Status.Phase == v1.PodSucceeded {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
return PodRunningReady(p)
|
return PodRunningReady(p)
|
||||||
@@ -54,7 +55,7 @@ func PodRunningReadyOrSucceeded(p *api.Pod) (bool, error) {
|
|||||||
// information for containers that have failed or been restarted.
|
// information for containers that have failed or been restarted.
|
||||||
// A map is returned where the key is the containerID and the value is a
|
// A map is returned where the key is the containerID and the value is a
|
||||||
// struct containing the restart and failure information
|
// struct containing the restart and failure information
|
||||||
func FailedContainers(pod *api.Pod) map[string]ContainerFailures {
|
func FailedContainers(pod *v1.Pod) map[string]ContainerFailures {
|
||||||
var state ContainerFailures
|
var state ContainerFailures
|
||||||
states := make(map[string]ContainerFailures)
|
states := make(map[string]ContainerFailures)
|
||||||
|
|
||||||
@@ -85,7 +86,7 @@ func FailedContainers(pod *api.Pod) map[string]ContainerFailures {
|
|||||||
// TerminatedContainers inspects all containers in a pod and returns a map
|
// TerminatedContainers inspects all containers in a pod and returns a map
|
||||||
// of "container name: termination reason", for all currently terminated
|
// of "container name: termination reason", for all currently terminated
|
||||||
// containers.
|
// containers.
|
||||||
func TerminatedContainers(pod *api.Pod) map[string]string {
|
func TerminatedContainers(pod *v1.Pod) map[string]string {
|
||||||
states := make(map[string]string)
|
states := make(map[string]string)
|
||||||
statuses := pod.Status.ContainerStatuses
|
statuses := pod.Status.ContainerStatuses
|
||||||
if len(statuses) == 0 {
|
if len(statuses) == 0 {
|
||||||
@@ -100,20 +101,20 @@ func TerminatedContainers(pod *api.Pod) map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PodNotReady checks whether pod p's has a ready condition of status false.
|
// PodNotReady checks whether pod p's has a ready condition of status false.
|
||||||
func PodNotReady(p *api.Pod) (bool, error) {
|
func PodNotReady(p *v1.Pod) (bool, error) {
|
||||||
// Check the ready condition is false.
|
// Check the ready condition is false.
|
||||||
if PodReady(p) {
|
if PodReady(p) {
|
||||||
return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
|
return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
|
||||||
p.ObjectMeta.Name, p.Spec.NodeName, api.PodReady, api.ConditionFalse, p.Status.Conditions)
|
p.ObjectMeta.Name, p.Spec.NodeName, v1.PodReady, v1.ConditionFalse, p.Status.Conditions)
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// podReady returns whether pod has a condition of Ready with a status of true.
|
// podReady returns whether pod has a condition of Ready with a status of true.
|
||||||
// TODO: should be replaced with api.IsPodReady
|
// TODO: should be replaced with v1.IsPodReady
|
||||||
func PodReady(pod *api.Pod) bool {
|
func PodReady(pod *v1.Pod) bool {
|
||||||
for _, cond := range pod.Status.Conditions {
|
for _, cond := range pod.Status.Conditions {
|
||||||
if cond.Type == api.PodReady && cond.Status == api.ConditionTrue {
|
if cond.Type == v1.PodReady && cond.Status == v1.ConditionTrue {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -23,7 +23,8 @@ import (
|
|||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
apierrs "k8s.io/kubernetes/pkg/api/errors"
|
apierrs "k8s.io/kubernetes/pkg/api/errors"
|
||||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
@@ -57,7 +58,7 @@ func AddLabelsToNode(c clientset.Interface, nodeName string, labels map[string]s
|
|||||||
// RemoveLabelOffNode is for cleaning up labels temporarily added to node,
|
// RemoveLabelOffNode is for cleaning up labels temporarily added to node,
|
||||||
// won't fail if target label doesn't exist or has been removed.
|
// won't fail if target label doesn't exist or has been removed.
|
||||||
func RemoveLabelOffNode(c clientset.Interface, nodeName string, labelKeys []string) error {
|
func RemoveLabelOffNode(c clientset.Interface, nodeName string, labelKeys []string) error {
|
||||||
var node *api.Node
|
var node *v1.Node
|
||||||
var err error
|
var err error
|
||||||
for attempt := 0; attempt < retries; attempt++ {
|
for attempt := 0; attempt < retries; attempt++ {
|
||||||
node, err = c.Core().Nodes().Get(nodeName)
|
node, err = c.Core().Nodes().Get(nodeName)
|
||||||
|
@@ -17,16 +17,16 @@ limitations under the License.
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
"k8s.io/kubernetes/pkg/client/cache"
|
"k8s.io/kubernetes/pkg/client/cache"
|
||||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||||
"k8s.io/kubernetes/pkg/fields"
|
"k8s.io/kubernetes/pkg/fields"
|
||||||
"k8s.io/kubernetes/pkg/labels"
|
"k8s.io/kubernetes/pkg/labels"
|
||||||
"k8s.io/kubernetes/pkg/runtime"
|
"k8s.io/kubernetes/pkg/runtime"
|
||||||
"k8s.io/kubernetes/pkg/watch"
|
"k8s.io/kubernetes/pkg/watch"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Convenient wrapper around cache.Store that returns list of api.Pod instead of interface{}.
|
// Convenient wrapper around cache.Store that returns list of v1.Pod instead of interface{}.
|
||||||
type PodStore struct {
|
type PodStore struct {
|
||||||
cache.Store
|
cache.Store
|
||||||
stopCh chan struct{}
|
stopCh chan struct{}
|
||||||
@@ -35,30 +35,30 @@ type PodStore struct {
|
|||||||
|
|
||||||
func NewPodStore(c clientset.Interface, namespace string, label labels.Selector, field fields.Selector) *PodStore {
|
func NewPodStore(c clientset.Interface, namespace string, label labels.Selector, field fields.Selector) *PodStore {
|
||||||
lw := &cache.ListWatch{
|
lw := &cache.ListWatch{
|
||||||
ListFunc: func(options api.ListOptions) (runtime.Object, error) {
|
ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
|
||||||
options.LabelSelector = label
|
options.LabelSelector = label.String()
|
||||||
options.FieldSelector = field
|
options.FieldSelector = field.String()
|
||||||
obj, err := c.Core().Pods(namespace).List(options)
|
obj, err := c.Core().Pods(namespace).List(options)
|
||||||
return runtime.Object(obj), err
|
return runtime.Object(obj), err
|
||||||
},
|
},
|
||||||
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
|
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
|
||||||
options.LabelSelector = label
|
options.LabelSelector = label.String()
|
||||||
options.FieldSelector = field
|
options.FieldSelector = field.String()
|
||||||
return c.Core().Pods(namespace).Watch(options)
|
return c.Core().Pods(namespace).Watch(options)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
store := cache.NewStore(cache.MetaNamespaceKeyFunc)
|
store := cache.NewStore(cache.MetaNamespaceKeyFunc)
|
||||||
stopCh := make(chan struct{})
|
stopCh := make(chan struct{})
|
||||||
reflector := cache.NewReflector(lw, &api.Pod{}, store, 0)
|
reflector := cache.NewReflector(lw, &v1.Pod{}, store, 0)
|
||||||
reflector.RunUntil(stopCh)
|
reflector.RunUntil(stopCh)
|
||||||
return &PodStore{Store: store, stopCh: stopCh, Reflector: reflector}
|
return &PodStore{Store: store, stopCh: stopCh, Reflector: reflector}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PodStore) List() []*api.Pod {
|
func (s *PodStore) List() []*v1.Pod {
|
||||||
objects := s.Store.List()
|
objects := s.Store.List()
|
||||||
pods := make([]*api.Pod, 0)
|
pods := make([]*v1.Pod, 0)
|
||||||
for _, o := range objects {
|
for _, o := range objects {
|
||||||
pods = append(pods, o.(*api.Pod))
|
pods = append(pods, o.(*v1.Pod))
|
||||||
}
|
}
|
||||||
return pods
|
return pods
|
||||||
}
|
}
|
||||||
|
@@ -27,8 +27,10 @@ import (
|
|||||||
apierrs "k8s.io/kubernetes/pkg/api/errors"
|
apierrs "k8s.io/kubernetes/pkg/api/errors"
|
||||||
"k8s.io/kubernetes/pkg/api/resource"
|
"k8s.io/kubernetes/pkg/api/resource"
|
||||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
|
||||||
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||||
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||||
"k8s.io/kubernetes/pkg/fields"
|
"k8s.io/kubernetes/pkg/fields"
|
||||||
"k8s.io/kubernetes/pkg/labels"
|
"k8s.io/kubernetes/pkg/labels"
|
||||||
"k8s.io/kubernetes/pkg/util/sets"
|
"k8s.io/kubernetes/pkg/util/sets"
|
||||||
@@ -45,6 +47,7 @@ const (
|
|||||||
|
|
||||||
type RCConfig struct {
|
type RCConfig struct {
|
||||||
Client clientset.Interface
|
Client clientset.Interface
|
||||||
|
InternalClient internalclientset.Interface
|
||||||
Image string
|
Image string
|
||||||
Command []string
|
Command []string
|
||||||
Name string
|
Name string
|
||||||
@@ -57,8 +60,8 @@ type RCConfig struct {
|
|||||||
CpuLimit int64 // millicores
|
CpuLimit int64 // millicores
|
||||||
MemRequest int64 // bytes
|
MemRequest int64 // bytes
|
||||||
MemLimit int64 // bytes
|
MemLimit int64 // bytes
|
||||||
ReadinessProbe *api.Probe
|
ReadinessProbe *v1.Probe
|
||||||
DNSPolicy *api.DNSPolicy
|
DNSPolicy *v1.DNSPolicy
|
||||||
|
|
||||||
// Env vars, set the same for every pod.
|
// Env vars, set the same for every pod.
|
||||||
Env map[string]string
|
Env map[string]string
|
||||||
@@ -74,12 +77,12 @@ type RCConfig struct {
|
|||||||
// Ports to declare in the container as host and container ports.
|
// Ports to declare in the container as host and container ports.
|
||||||
HostPorts map[string]int
|
HostPorts map[string]int
|
||||||
|
|
||||||
Volumes []api.Volume
|
Volumes []v1.Volume
|
||||||
VolumeMounts []api.VolumeMount
|
VolumeMounts []v1.VolumeMount
|
||||||
|
|
||||||
// Pointer to a list of pods; if non-nil, will be set to a list of pods
|
// Pointer to a list of pods; if non-nil, will be set to a list of pods
|
||||||
// created by this RC by RunRC.
|
// created by this RC by RunRC.
|
||||||
CreatedPods *[]*api.Pod
|
CreatedPods *[]*v1.Pod
|
||||||
|
|
||||||
// Maximum allowable container failures. If exceeded, RunRC returns an error.
|
// Maximum allowable container failures. If exceeded, RunRC returns an error.
|
||||||
// Defaults to replicas*0.1 if unspecified.
|
// Defaults to replicas*0.1 if unspecified.
|
||||||
@@ -159,7 +162,7 @@ func (p PodDiff) String(ignorePhases sets.String) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Diff computes a PodDiff given 2 lists of pods.
|
// Diff computes a PodDiff given 2 lists of pods.
|
||||||
func Diff(oldPods []*api.Pod, curPods []*api.Pod) PodDiff {
|
func Diff(oldPods []*v1.Pod, curPods []*v1.Pod) PodDiff {
|
||||||
podInfoMap := PodDiff{}
|
podInfoMap := PodDiff{}
|
||||||
|
|
||||||
// New pods will show up in the curPods list but not in oldPods. They have oldhostname/phase == nonexist.
|
// New pods will show up in the curPods list but not in oldPods. They have oldhostname/phase == nonexist.
|
||||||
@@ -192,27 +195,27 @@ func RunDeployment(config DeploymentConfig) error {
|
|||||||
|
|
||||||
func (config *DeploymentConfig) create() error {
|
func (config *DeploymentConfig) create() error {
|
||||||
deployment := &extensions.Deployment{
|
deployment := &extensions.Deployment{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
Name: config.Name,
|
Name: config.Name,
|
||||||
},
|
},
|
||||||
Spec: extensions.DeploymentSpec{
|
Spec: extensions.DeploymentSpec{
|
||||||
Replicas: int32(config.Replicas),
|
Replicas: func(i int) *int32 { x := int32(i); return &x }(config.Replicas),
|
||||||
Selector: &unversioned.LabelSelector{
|
Selector: &unversioned.LabelSelector{
|
||||||
MatchLabels: map[string]string{
|
MatchLabels: map[string]string{
|
||||||
"name": config.Name,
|
"name": config.Name,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Template: api.PodTemplateSpec{
|
Template: v1.PodTemplateSpec{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
Labels: map[string]string{"name": config.Name},
|
Labels: map[string]string{"name": config.Name},
|
||||||
},
|
},
|
||||||
Spec: api.PodSpec{
|
Spec: v1.PodSpec{
|
||||||
Containers: []api.Container{
|
Containers: []v1.Container{
|
||||||
{
|
{
|
||||||
Name: config.Name,
|
Name: config.Name,
|
||||||
Image: config.Image,
|
Image: config.Image,
|
||||||
Command: config.Command,
|
Command: config.Command,
|
||||||
Ports: []api.ContainerPort{{ContainerPort: 80}},
|
Ports: []v1.ContainerPort{{ContainerPort: 80}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -244,27 +247,27 @@ func RunReplicaSet(config ReplicaSetConfig) error {
|
|||||||
|
|
||||||
func (config *ReplicaSetConfig) create() error {
|
func (config *ReplicaSetConfig) create() error {
|
||||||
rs := &extensions.ReplicaSet{
|
rs := &extensions.ReplicaSet{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
Name: config.Name,
|
Name: config.Name,
|
||||||
},
|
},
|
||||||
Spec: extensions.ReplicaSetSpec{
|
Spec: extensions.ReplicaSetSpec{
|
||||||
Replicas: int32(config.Replicas),
|
Replicas: func(i int) *int32 { x := int32(i); return &x }(config.Replicas),
|
||||||
Selector: &unversioned.LabelSelector{
|
Selector: &unversioned.LabelSelector{
|
||||||
MatchLabels: map[string]string{
|
MatchLabels: map[string]string{
|
||||||
"name": config.Name,
|
"name": config.Name,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Template: api.PodTemplateSpec{
|
Template: v1.PodTemplateSpec{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
Labels: map[string]string{"name": config.Name},
|
Labels: map[string]string{"name": config.Name},
|
||||||
},
|
},
|
||||||
Spec: api.PodSpec{
|
Spec: v1.PodSpec{
|
||||||
Containers: []api.Container{
|
Containers: []v1.Container{
|
||||||
{
|
{
|
||||||
Name: config.Name,
|
Name: config.Name,
|
||||||
Image: config.Image,
|
Image: config.Image,
|
||||||
Command: config.Command,
|
Command: config.Command,
|
||||||
Ports: []api.ContainerPort{{ContainerPort: 80}},
|
Ports: []v1.ContainerPort{{ContainerPort: 80}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -295,30 +298,30 @@ func RunRC(config RCConfig) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (config *RCConfig) create() error {
|
func (config *RCConfig) create() error {
|
||||||
dnsDefault := api.DNSDefault
|
dnsDefault := v1.DNSDefault
|
||||||
if config.DNSPolicy == nil {
|
if config.DNSPolicy == nil {
|
||||||
config.DNSPolicy = &dnsDefault
|
config.DNSPolicy = &dnsDefault
|
||||||
}
|
}
|
||||||
rc := &api.ReplicationController{
|
rc := &v1.ReplicationController{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
Name: config.Name,
|
Name: config.Name,
|
||||||
},
|
},
|
||||||
Spec: api.ReplicationControllerSpec{
|
Spec: v1.ReplicationControllerSpec{
|
||||||
Replicas: int32(config.Replicas),
|
Replicas: func(i int) *int32 { x := int32(i); return &x }(config.Replicas),
|
||||||
Selector: map[string]string{
|
Selector: map[string]string{
|
||||||
"name": config.Name,
|
"name": config.Name,
|
||||||
},
|
},
|
||||||
Template: &api.PodTemplateSpec{
|
Template: &v1.PodTemplateSpec{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
Labels: map[string]string{"name": config.Name},
|
Labels: map[string]string{"name": config.Name},
|
||||||
},
|
},
|
||||||
Spec: api.PodSpec{
|
Spec: v1.PodSpec{
|
||||||
Containers: []api.Container{
|
Containers: []v1.Container{
|
||||||
{
|
{
|
||||||
Name: config.Name,
|
Name: config.Name,
|
||||||
Image: config.Image,
|
Image: config.Image,
|
||||||
Command: config.Command,
|
Command: config.Command,
|
||||||
Ports: []api.ContainerPort{{ContainerPort: 80}},
|
Ports: []v1.ContainerPort{{ContainerPort: 80}},
|
||||||
ReadinessProbe: config.ReadinessProbe,
|
ReadinessProbe: config.ReadinessProbe,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -339,11 +342,11 @@ func (config *RCConfig) create() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *RCConfig) applyTo(template *api.PodTemplateSpec) {
|
func (config *RCConfig) applyTo(template *v1.PodTemplateSpec) {
|
||||||
if config.Env != nil {
|
if config.Env != nil {
|
||||||
for k, v := range config.Env {
|
for k, v := range config.Env {
|
||||||
c := &template.Spec.Containers[0]
|
c := &template.Spec.Containers[0]
|
||||||
c.Env = append(c.Env, api.EnvVar{Name: k, Value: v})
|
c.Env = append(c.Env, v1.EnvVar{Name: k, Value: v})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config.Labels != nil {
|
if config.Labels != nil {
|
||||||
@@ -360,32 +363,32 @@ func (config *RCConfig) applyTo(template *api.PodTemplateSpec) {
|
|||||||
if config.Ports != nil {
|
if config.Ports != nil {
|
||||||
for k, v := range config.Ports {
|
for k, v := range config.Ports {
|
||||||
c := &template.Spec.Containers[0]
|
c := &template.Spec.Containers[0]
|
||||||
c.Ports = append(c.Ports, api.ContainerPort{Name: k, ContainerPort: int32(v)})
|
c.Ports = append(c.Ports, v1.ContainerPort{Name: k, ContainerPort: int32(v)})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config.HostPorts != nil {
|
if config.HostPorts != nil {
|
||||||
for k, v := range config.HostPorts {
|
for k, v := range config.HostPorts {
|
||||||
c := &template.Spec.Containers[0]
|
c := &template.Spec.Containers[0]
|
||||||
c.Ports = append(c.Ports, api.ContainerPort{Name: k, ContainerPort: int32(v), HostPort: int32(v)})
|
c.Ports = append(c.Ports, v1.ContainerPort{Name: k, ContainerPort: int32(v), HostPort: int32(v)})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config.CpuLimit > 0 || config.MemLimit > 0 {
|
if config.CpuLimit > 0 || config.MemLimit > 0 {
|
||||||
template.Spec.Containers[0].Resources.Limits = api.ResourceList{}
|
template.Spec.Containers[0].Resources.Limits = v1.ResourceList{}
|
||||||
}
|
}
|
||||||
if config.CpuLimit > 0 {
|
if config.CpuLimit > 0 {
|
||||||
template.Spec.Containers[0].Resources.Limits[api.ResourceCPU] = *resource.NewMilliQuantity(config.CpuLimit, resource.DecimalSI)
|
template.Spec.Containers[0].Resources.Limits[v1.ResourceCPU] = *resource.NewMilliQuantity(config.CpuLimit, resource.DecimalSI)
|
||||||
}
|
}
|
||||||
if config.MemLimit > 0 {
|
if config.MemLimit > 0 {
|
||||||
template.Spec.Containers[0].Resources.Limits[api.ResourceMemory] = *resource.NewQuantity(config.MemLimit, resource.DecimalSI)
|
template.Spec.Containers[0].Resources.Limits[v1.ResourceMemory] = *resource.NewQuantity(config.MemLimit, resource.DecimalSI)
|
||||||
}
|
}
|
||||||
if config.CpuRequest > 0 || config.MemRequest > 0 {
|
if config.CpuRequest > 0 || config.MemRequest > 0 {
|
||||||
template.Spec.Containers[0].Resources.Requests = api.ResourceList{}
|
template.Spec.Containers[0].Resources.Requests = v1.ResourceList{}
|
||||||
}
|
}
|
||||||
if config.CpuRequest > 0 {
|
if config.CpuRequest > 0 {
|
||||||
template.Spec.Containers[0].Resources.Requests[api.ResourceCPU] = *resource.NewMilliQuantity(config.CpuRequest, resource.DecimalSI)
|
template.Spec.Containers[0].Resources.Requests[v1.ResourceCPU] = *resource.NewMilliQuantity(config.CpuRequest, resource.DecimalSI)
|
||||||
}
|
}
|
||||||
if config.MemRequest > 0 {
|
if config.MemRequest > 0 {
|
||||||
template.Spec.Containers[0].Resources.Requests[api.ResourceMemory] = *resource.NewQuantity(config.MemRequest, resource.DecimalSI)
|
template.Spec.Containers[0].Resources.Requests[v1.ResourceMemory] = *resource.NewQuantity(config.MemRequest, resource.DecimalSI)
|
||||||
}
|
}
|
||||||
if len(config.Volumes) > 0 {
|
if len(config.Volumes) > 0 {
|
||||||
template.Spec.Volumes = config.Volumes
|
template.Spec.Volumes = config.Volumes
|
||||||
@@ -405,7 +408,7 @@ type RCStartupStatus struct {
|
|||||||
Unknown int
|
Unknown int
|
||||||
Inactive int
|
Inactive int
|
||||||
FailedContainers int
|
FailedContainers int
|
||||||
Created []*api.Pod
|
Created []*v1.Pod
|
||||||
ContainerRestartNodes sets.String
|
ContainerRestartNodes sets.String
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,10 +417,10 @@ func (s *RCStartupStatus) String(name string) string {
|
|||||||
name, len(s.Created), s.Expected, s.Running, s.Pending, s.Waiting, s.Inactive, s.Terminating, s.Unknown, s.RunningButNotReady)
|
name, len(s.Created), s.Expected, s.Running, s.Pending, s.Waiting, s.Inactive, s.Terminating, s.Unknown, s.RunningButNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ComputeRCStartupStatus(pods []*api.Pod, expected int) RCStartupStatus {
|
func ComputeRCStartupStatus(pods []*v1.Pod, expected int) RCStartupStatus {
|
||||||
startupStatus := RCStartupStatus{
|
startupStatus := RCStartupStatus{
|
||||||
Expected: expected,
|
Expected: expected,
|
||||||
Created: make([]*api.Pod, 0, expected),
|
Created: make([]*v1.Pod, 0, expected),
|
||||||
ContainerRestartNodes: sets.NewString(),
|
ContainerRestartNodes: sets.NewString(),
|
||||||
}
|
}
|
||||||
for _, p := range pods {
|
for _, p := range pods {
|
||||||
@@ -426,10 +429,10 @@ func ComputeRCStartupStatus(pods []*api.Pod, expected int) RCStartupStatus {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
startupStatus.Created = append(startupStatus.Created, p)
|
startupStatus.Created = append(startupStatus.Created, p)
|
||||||
if p.Status.Phase == api.PodRunning {
|
if p.Status.Phase == v1.PodRunning {
|
||||||
ready := false
|
ready := false
|
||||||
for _, c := range p.Status.Conditions {
|
for _, c := range p.Status.Conditions {
|
||||||
if c.Type == api.PodReady && c.Status == api.ConditionTrue {
|
if c.Type == v1.PodReady && c.Status == v1.ConditionTrue {
|
||||||
ready = true
|
ready = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -444,15 +447,15 @@ func ComputeRCStartupStatus(pods []*api.Pod, expected int) RCStartupStatus {
|
|||||||
startupStatus.FailedContainers = startupStatus.FailedContainers + v.Restarts
|
startupStatus.FailedContainers = startupStatus.FailedContainers + v.Restarts
|
||||||
startupStatus.ContainerRestartNodes.Insert(p.Spec.NodeName)
|
startupStatus.ContainerRestartNodes.Insert(p.Spec.NodeName)
|
||||||
}
|
}
|
||||||
} else if p.Status.Phase == api.PodPending {
|
} else if p.Status.Phase == v1.PodPending {
|
||||||
if p.Spec.NodeName == "" {
|
if p.Spec.NodeName == "" {
|
||||||
startupStatus.Waiting++
|
startupStatus.Waiting++
|
||||||
} else {
|
} else {
|
||||||
startupStatus.Pending++
|
startupStatus.Pending++
|
||||||
}
|
}
|
||||||
} else if p.Status.Phase == api.PodSucceeded || p.Status.Phase == api.PodFailed {
|
} else if p.Status.Phase == v1.PodSucceeded || p.Status.Phase == v1.PodFailed {
|
||||||
startupStatus.Inactive++
|
startupStatus.Inactive++
|
||||||
} else if p.Status.Phase == api.PodUnknown {
|
} else if p.Status.Phase == v1.PodUnknown {
|
||||||
startupStatus.Unknown++
|
startupStatus.Unknown++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -481,7 +484,7 @@ func (config *RCConfig) start() error {
|
|||||||
if timeout <= 0 {
|
if timeout <= 0 {
|
||||||
timeout = 5 * time.Minute
|
timeout = 5 * time.Minute
|
||||||
}
|
}
|
||||||
oldPods := make([]*api.Pod, 0)
|
oldPods := make([]*v1.Pod, 0)
|
||||||
oldRunning := 0
|
oldRunning := 0
|
||||||
lastChange := time.Now()
|
lastChange := time.Now()
|
||||||
for oldRunning != config.Replicas {
|
for oldRunning != config.Replicas {
|
||||||
@@ -537,8 +540,8 @@ func (config *RCConfig) start() error {
|
|||||||
|
|
||||||
if oldRunning != config.Replicas {
|
if oldRunning != config.Replicas {
|
||||||
// List only pods from a given replication controller.
|
// List only pods from a given replication controller.
|
||||||
options := api.ListOptions{LabelSelector: label}
|
options := v1.ListOptions{LabelSelector: label.String()}
|
||||||
if pods, err := config.Client.Core().Pods(api.NamespaceAll).List(options); err == nil {
|
if pods, err := config.Client.Core().Pods(v1.NamespaceAll).List(options); err == nil {
|
||||||
|
|
||||||
for _, pod := range pods.Items {
|
for _, pod := range pods.Items {
|
||||||
config.RCConfigLog("Pod %s\t%s\t%s\t%s", pod.Name, pod.Spec.NodeName, pod.Status.Phase, pod.DeletionTimestamp)
|
config.RCConfigLog("Pod %s\t%s\t%s\t%s", pod.Name, pod.Spec.NodeName, pod.Status.Phase, pod.DeletionTimestamp)
|
||||||
@@ -555,7 +558,7 @@ func (config *RCConfig) start() error {
|
|||||||
// Optionally waits for pods to start running (if waitForRunning == true).
|
// Optionally waits for pods to start running (if waitForRunning == true).
|
||||||
// The number of replicas must be non-zero.
|
// The number of replicas must be non-zero.
|
||||||
func StartPods(c clientset.Interface, replicas int, namespace string, podNamePrefix string,
|
func StartPods(c clientset.Interface, replicas int, namespace string, podNamePrefix string,
|
||||||
pod api.Pod, waitForRunning bool, logFunc func(fmt string, args ...interface{})) error {
|
pod v1.Pod, waitForRunning bool, logFunc func(fmt string, args ...interface{})) error {
|
||||||
// no pod to start
|
// no pod to start
|
||||||
if replicas < 1 {
|
if replicas < 1 {
|
||||||
panic("StartPods: number of replicas must be non-zero")
|
panic("StartPods: number of replicas must be non-zero")
|
||||||
@@ -596,7 +599,7 @@ waitLoop:
|
|||||||
continue waitLoop
|
continue waitLoop
|
||||||
}
|
}
|
||||||
for _, p := range pods {
|
for _, p := range pods {
|
||||||
if p.Status.Phase != api.PodRunning {
|
if p.Status.Phase != v1.PodRunning {
|
||||||
continue waitLoop
|
continue waitLoop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -620,17 +623,17 @@ type TestNodePreparer interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PrepareNodeStrategy interface {
|
type PrepareNodeStrategy interface {
|
||||||
PreparePatch(node *api.Node) []byte
|
PreparePatch(node *v1.Node) []byte
|
||||||
CleanupNode(node *api.Node) *api.Node
|
CleanupNode(node *v1.Node) *v1.Node
|
||||||
}
|
}
|
||||||
|
|
||||||
type TrivialNodePrepareStrategy struct{}
|
type TrivialNodePrepareStrategy struct{}
|
||||||
|
|
||||||
func (*TrivialNodePrepareStrategy) PreparePatch(*api.Node) []byte {
|
func (*TrivialNodePrepareStrategy) PreparePatch(*v1.Node) []byte {
|
||||||
return []byte{}
|
return []byte{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*TrivialNodePrepareStrategy) CleanupNode(node *api.Node) *api.Node {
|
func (*TrivialNodePrepareStrategy) CleanupNode(node *v1.Node) *v1.Node {
|
||||||
nodeCopy := *node
|
nodeCopy := *node
|
||||||
return &nodeCopy
|
return &nodeCopy
|
||||||
}
|
}
|
||||||
@@ -647,20 +650,20 @@ func NewLabelNodePrepareStrategy(labelKey string, labelValue string) *LabelNodeP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LabelNodePrepareStrategy) PreparePatch(*api.Node) []byte {
|
func (s *LabelNodePrepareStrategy) PreparePatch(*v1.Node) []byte {
|
||||||
labelString := fmt.Sprintf("{\"%v\":\"%v\"}", s.labelKey, s.labelValue)
|
labelString := fmt.Sprintf("{\"%v\":\"%v\"}", s.labelKey, s.labelValue)
|
||||||
patch := fmt.Sprintf(`{"metadata":{"labels":%v}}`, labelString)
|
patch := fmt.Sprintf(`{"metadata":{"labels":%v}}`, labelString)
|
||||||
return []byte(patch)
|
return []byte(patch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LabelNodePrepareStrategy) CleanupNode(node *api.Node) *api.Node {
|
func (s *LabelNodePrepareStrategy) CleanupNode(node *v1.Node) *v1.Node {
|
||||||
objCopy, err := api.Scheme.DeepCopy(*node)
|
objCopy, err := api.Scheme.DeepCopy(*node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &api.Node{}
|
return &v1.Node{}
|
||||||
}
|
}
|
||||||
nodeCopy, ok := (objCopy).(*api.Node)
|
nodeCopy, ok := (objCopy).(*v1.Node)
|
||||||
if !ok {
|
if !ok {
|
||||||
return &api.Node{}
|
return &v1.Node{}
|
||||||
}
|
}
|
||||||
if node.Labels != nil && len(node.Labels[s.labelKey]) != 0 {
|
if node.Labels != nil && len(node.Labels[s.labelKey]) != 0 {
|
||||||
delete(nodeCopy.Labels, s.labelKey)
|
delete(nodeCopy.Labels, s.labelKey)
|
||||||
@@ -668,7 +671,7 @@ func (s *LabelNodePrepareStrategy) CleanupNode(node *api.Node) *api.Node {
|
|||||||
return nodeCopy
|
return nodeCopy
|
||||||
}
|
}
|
||||||
|
|
||||||
func DoPrepareNode(client clientset.Interface, node *api.Node, strategy PrepareNodeStrategy) error {
|
func DoPrepareNode(client clientset.Interface, node *v1.Node, strategy PrepareNodeStrategy) error {
|
||||||
var err error
|
var err error
|
||||||
patch := strategy.PreparePatch(node)
|
patch := strategy.PreparePatch(node)
|
||||||
if len(patch) == 0 {
|
if len(patch) == 0 {
|
||||||
@@ -693,7 +696,7 @@ func DoCleanupNode(client clientset.Interface, nodeName string, strategy Prepare
|
|||||||
return fmt.Errorf("Skipping cleanup of Node: failed to get Node %v: %v", nodeName, err)
|
return fmt.Errorf("Skipping cleanup of Node: failed to get Node %v: %v", nodeName, err)
|
||||||
}
|
}
|
||||||
updatedNode := strategy.CleanupNode(node)
|
updatedNode := strategy.CleanupNode(node)
|
||||||
if api.Semantic.DeepEqual(node, updatedNode) {
|
if v1.Semantic.DeepEqual(node, updatedNode) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if _, err = client.Core().Nodes().Update(updatedNode); err == nil {
|
if _, err = client.Core().Nodes().Update(updatedNode); err == nil {
|
||||||
@@ -750,27 +753,27 @@ func (c *TestPodCreator) CreatePods() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakePodSpec() api.PodSpec {
|
func MakePodSpec() v1.PodSpec {
|
||||||
return api.PodSpec{
|
return v1.PodSpec{
|
||||||
Containers: []api.Container{{
|
Containers: []v1.Container{{
|
||||||
Name: "pause",
|
Name: "pause",
|
||||||
Image: "kubernetes/pause",
|
Image: "kubernetes/pause",
|
||||||
Ports: []api.ContainerPort{{ContainerPort: 80}},
|
Ports: []v1.ContainerPort{{ContainerPort: 80}},
|
||||||
Resources: api.ResourceRequirements{
|
Resources: v1.ResourceRequirements{
|
||||||
Limits: api.ResourceList{
|
Limits: v1.ResourceList{
|
||||||
api.ResourceCPU: resource.MustParse("100m"),
|
v1.ResourceCPU: resource.MustParse("100m"),
|
||||||
api.ResourceMemory: resource.MustParse("500Mi"),
|
v1.ResourceMemory: resource.MustParse("500Mi"),
|
||||||
},
|
},
|
||||||
Requests: api.ResourceList{
|
Requests: v1.ResourceList{
|
||||||
api.ResourceCPU: resource.MustParse("100m"),
|
v1.ResourceCPU: resource.MustParse("100m"),
|
||||||
api.ResourceMemory: resource.MustParse("500Mi"),
|
v1.ResourceMemory: resource.MustParse("500Mi"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeCreatePod(client clientset.Interface, namespace string, podTemplate *api.Pod) error {
|
func makeCreatePod(client clientset.Interface, namespace string, podTemplate *v1.Pod) error {
|
||||||
var err error
|
var err error
|
||||||
for attempt := 0; attempt < retries; attempt++ {
|
for attempt := 0; attempt < retries; attempt++ {
|
||||||
if _, err := client.Core().Pods(namespace).Create(podTemplate); err == nil {
|
if _, err := client.Core().Pods(namespace).Create(podTemplate); err == nil {
|
||||||
@@ -781,7 +784,7 @@ func makeCreatePod(client clientset.Interface, namespace string, podTemplate *ap
|
|||||||
return fmt.Errorf("Terminal error while creating pod, won't retry: %v", err)
|
return fmt.Errorf("Terminal error while creating pod, won't retry: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createPod(client clientset.Interface, namespace string, podCount int, podTemplate *api.Pod) error {
|
func createPod(client clientset.Interface, namespace string, podCount int, podTemplate *v1.Pod) error {
|
||||||
var createError error
|
var createError error
|
||||||
lock := sync.Mutex{}
|
lock := sync.Mutex{}
|
||||||
createPodFunc := func(i int) {
|
createPodFunc := func(i int) {
|
||||||
@@ -800,16 +803,16 @@ func createPod(client clientset.Interface, namespace string, podCount int, podTe
|
|||||||
return createError
|
return createError
|
||||||
}
|
}
|
||||||
|
|
||||||
func createController(client clientset.Interface, controllerName, namespace string, podCount int, podTemplate *api.Pod) error {
|
func createController(client clientset.Interface, controllerName, namespace string, podCount int, podTemplate *v1.Pod) error {
|
||||||
rc := &api.ReplicationController{
|
rc := &v1.ReplicationController{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
Name: controllerName,
|
Name: controllerName,
|
||||||
},
|
},
|
||||||
Spec: api.ReplicationControllerSpec{
|
Spec: v1.ReplicationControllerSpec{
|
||||||
Replicas: int32(podCount),
|
Replicas: func(i int) *int32 { x := int32(i); return &x }(podCount),
|
||||||
Selector: map[string]string{"name": controllerName},
|
Selector: map[string]string{"name": controllerName},
|
||||||
Template: &api.PodTemplateSpec{
|
Template: &v1.PodTemplateSpec{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
Labels: map[string]string{"name": controllerName},
|
Labels: map[string]string{"name": controllerName},
|
||||||
},
|
},
|
||||||
Spec: podTemplate.Spec,
|
Spec: podTemplate.Spec,
|
||||||
@@ -826,15 +829,15 @@ func createController(client clientset.Interface, controllerName, namespace stri
|
|||||||
return fmt.Errorf("Terminal error while creating rc, won't retry: %v", err)
|
return fmt.Errorf("Terminal error while creating rc, won't retry: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCustomCreatePodStrategy(podTemplate *api.Pod) TestPodCreateStrategy {
|
func NewCustomCreatePodStrategy(podTemplate *v1.Pod) TestPodCreateStrategy {
|
||||||
return func(client clientset.Interface, namespace string, podCount int) error {
|
return func(client clientset.Interface, namespace string, podCount int) error {
|
||||||
return createPod(client, namespace, podCount, podTemplate)
|
return createPod(client, namespace, podCount, podTemplate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSimpleCreatePodStrategy() TestPodCreateStrategy {
|
func NewSimpleCreatePodStrategy() TestPodCreateStrategy {
|
||||||
basePod := &api.Pod{
|
basePod := &v1.Pod{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
GenerateName: "simple-pod-",
|
GenerateName: "simple-pod-",
|
||||||
},
|
},
|
||||||
Spec: MakePodSpec(),
|
Spec: MakePodSpec(),
|
||||||
@@ -844,8 +847,8 @@ func NewSimpleCreatePodStrategy() TestPodCreateStrategy {
|
|||||||
|
|
||||||
func NewSimpleWithControllerCreatePodStrategy(controllerName string) TestPodCreateStrategy {
|
func NewSimpleWithControllerCreatePodStrategy(controllerName string) TestPodCreateStrategy {
|
||||||
return func(client clientset.Interface, namespace string, podCount int) error {
|
return func(client clientset.Interface, namespace string, podCount int) error {
|
||||||
basePod := &api.Pod{
|
basePod := &v1.Pod{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
GenerateName: controllerName + "-pod-",
|
GenerateName: controllerName + "-pod-",
|
||||||
Labels: map[string]string{"name": controllerName},
|
Labels: map[string]string{"name": controllerName},
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user