simplify if and else for code

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud
2017-01-17 11:52:53 +08:00
parent 6fbc554c6b
commit 6300361961

View File

@@ -426,7 +426,8 @@ func (kl *Kubelet) setNodeAddress(node *v1.Node) error {
if kl.externalCloudProvider { if kl.externalCloudProvider {
// We rely on the external cloud provider to supply the addresses. // We rely on the external cloud provider to supply the addresses.
return nil return nil
} else if kl.cloud != nil { }
if kl.cloud != nil {
instances, ok := kl.cloud.Instances() instances, ok := kl.cloud.Instances()
if !ok { if !ok {
return fmt.Errorf("failed to get instances from cloud provider") return fmt.Errorf("failed to get instances from cloud provider")
@@ -603,7 +604,9 @@ func (kl *Kubelet) setNodeStatusVersionInfo(node *v1.Node) {
verinfo, err := kl.cadvisor.VersionInfo() verinfo, err := kl.cadvisor.VersionInfo()
if err != nil { if err != nil {
glog.Errorf("Error getting version info: %v", err) glog.Errorf("Error getting version info: %v", err)
} else { return
}
node.Status.NodeInfo.KernelVersion = verinfo.KernelVersion node.Status.NodeInfo.KernelVersion = verinfo.KernelVersion
node.Status.NodeInfo.OSImage = verinfo.ContainerOsVersion node.Status.NodeInfo.OSImage = verinfo.ContainerOsVersion
@@ -616,7 +619,6 @@ func (kl *Kubelet) setNodeStatusVersionInfo(node *v1.Node) {
node.Status.NodeInfo.KubeletVersion = version.Get().String() node.Status.NodeInfo.KubeletVersion = version.Get().String()
// TODO: kube-proxy might be different version from kubelet in the future // TODO: kube-proxy might be different version from kubelet in the future
node.Status.NodeInfo.KubeProxyVersion = version.Get().String() node.Status.NodeInfo.KubeProxyVersion = version.Get().String()
}
} }
// Set daemonEndpoints for the node. // Set daemonEndpoints for the node.
@@ -631,7 +633,9 @@ func (kl *Kubelet) setNodeStatusImages(node *v1.Node) {
containerImages, err := kl.imageManager.GetImageList() containerImages, err := kl.imageManager.GetImageList()
if err != nil { if err != nil {
glog.Errorf("Error getting image list: %v", err) glog.Errorf("Error getting image list: %v", err)
} else { node.Status.Images = imagesOnNode
return
}
// sort the images from max to min, and only set top N images into the node status. // sort the images from max to min, and only set top N images into the node status.
sort.Sort(sliceutils.ByImageSize(containerImages)) sort.Sort(sliceutils.ByImageSize(containerImages))
if maxImagesInNodeStatus < len(containerImages) { if maxImagesInNodeStatus < len(containerImages) {
@@ -649,7 +653,7 @@ func (kl *Kubelet) setNodeStatusImages(node *v1.Node) {
SizeBytes: image.Size, SizeBytes: image.Size,
}) })
} }
}
node.Status.Images = imagesOnNode node.Status.Images = imagesOnNode
} }
@@ -779,15 +783,13 @@ func (kl *Kubelet) setNodeMemoryPressureCondition(node *v1.Node) {
condition.LastTransitionTime = currentTime condition.LastTransitionTime = currentTime
kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasInsufficientMemory") kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasInsufficientMemory")
} }
} else { } else if condition.Status != v1.ConditionFalse {
if condition.Status != v1.ConditionFalse {
condition.Status = v1.ConditionFalse condition.Status = v1.ConditionFalse
condition.Reason = "KubeletHasSufficientMemory" condition.Reason = "KubeletHasSufficientMemory"
condition.Message = "kubelet has sufficient memory available" condition.Message = "kubelet has sufficient memory available"
condition.LastTransitionTime = currentTime condition.LastTransitionTime = currentTime
kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasSufficientMemory") kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasSufficientMemory")
} }
}
if newCondition { if newCondition {
node.Status.Conditions = append(node.Status.Conditions, *condition) node.Status.Conditions = append(node.Status.Conditions, *condition)
@@ -837,15 +839,13 @@ func (kl *Kubelet) setNodeDiskPressureCondition(node *v1.Node) {
condition.LastTransitionTime = currentTime condition.LastTransitionTime = currentTime
kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasDiskPressure") kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasDiskPressure")
} }
} else { } else if condition.Status != v1.ConditionFalse {
if condition.Status != v1.ConditionFalse {
condition.Status = v1.ConditionFalse condition.Status = v1.ConditionFalse
condition.Reason = "KubeletHasNoDiskPressure" condition.Reason = "KubeletHasNoDiskPressure"
condition.Message = "kubelet has no disk pressure" condition.Message = "kubelet has no disk pressure"
condition.LastTransitionTime = currentTime condition.LastTransitionTime = currentTime
kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasNoDiskPressure") kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasNoDiskPressure")
} }
}
if newCondition { if newCondition {
node.Status.Conditions = append(node.Status.Conditions, *condition) node.Status.Conditions = append(node.Status.Conditions, *condition)