Fix the nil pointer dereference when counting RS replicas

pull/6/head
Janet Kuo 2016-02-24 16:09:20 -08:00
parent c031697848
commit 069e77d504
1 changed files with 19 additions and 13 deletions

View File

@ -325,7 +325,9 @@ func SetFromReplicaSetTemplate(deployment *extensions.Deployment, template api.P
func GetReplicaCountForReplicaSets(replicaSets []*extensions.ReplicaSet) int { func GetReplicaCountForReplicaSets(replicaSets []*extensions.ReplicaSet) int {
totalReplicaCount := 0 totalReplicaCount := 0
for _, rs := range replicaSets { for _, rs := range replicaSets {
totalReplicaCount += rs.Spec.Replicas if rs != nil {
totalReplicaCount += rs.Spec.Replicas
}
} }
return totalReplicaCount return totalReplicaCount
} }
@ -334,7 +336,9 @@ func GetReplicaCountForReplicaSets(replicaSets []*extensions.ReplicaSet) int {
func GetActualReplicaCountForReplicaSets(replicaSets []*extensions.ReplicaSet) int { func GetActualReplicaCountForReplicaSets(replicaSets []*extensions.ReplicaSet) int {
totalReplicaCount := 0 totalReplicaCount := 0
for _, rs := range replicaSets { for _, rs := range replicaSets {
totalReplicaCount += rs.Status.Replicas if rs != nil {
totalReplicaCount += rs.Status.Replicas
}
} }
return totalReplicaCount return totalReplicaCount
} }
@ -379,17 +383,19 @@ func IsPodAvailable(pod *api.Pod, minReadySeconds int) bool {
func GetPodsForReplicaSets(c clientset.Interface, replicaSets []*extensions.ReplicaSet) ([]api.Pod, error) { func GetPodsForReplicaSets(c clientset.Interface, replicaSets []*extensions.ReplicaSet) ([]api.Pod, error) {
allPods := map[string]api.Pod{} allPods := map[string]api.Pod{}
for _, rs := range replicaSets { for _, rs := range replicaSets {
selector, err := unversioned.LabelSelectorAsSelector(rs.Spec.Selector) if rs != nil {
if err != nil { selector, err := unversioned.LabelSelectorAsSelector(rs.Spec.Selector)
return nil, fmt.Errorf("invalid label selector: %v", err) if err != nil {
} return nil, fmt.Errorf("invalid label selector: %v", err)
options := api.ListOptions{LabelSelector: selector} }
podList, err := c.Core().Pods(rs.ObjectMeta.Namespace).List(options) options := api.ListOptions{LabelSelector: selector}
if err != nil { podList, err := c.Core().Pods(rs.ObjectMeta.Namespace).List(options)
return nil, fmt.Errorf("error listing pods: %v", err) if err != nil {
} return nil, fmt.Errorf("error listing pods: %v", err)
for _, pod := range podList.Items { }
allPods[pod.Name] = pod for _, pod := range podList.Items {
allPods[pod.Name] = pod
}
} }
} }
requiredPods := []api.Pod{} requiredPods := []api.Pod{}