Merge pull request #26019 from gyuho/kubectl_slice_append

Automatic merge from submit-queue

pkg/kubectl: preallocate slice
This commit is contained in:
k8s-merge-robot 2016-05-29 15:00:15 -07:00
commit 72479b82e0
5 changed files with 21 additions and 20 deletions

View File

@ -28,7 +28,7 @@ import (
func AddJsonFilenameFlag(cmd *cobra.Command, value *[]string, usage string) { func AddJsonFilenameFlag(cmd *cobra.Command, value *[]string, usage string) {
cmd.Flags().StringSliceVarP(value, "filename", "f", *value, usage) cmd.Flags().StringSliceVarP(value, "filename", "f", *value, usage)
annotations := []string{} annotations := make([]string, 0, len(resource.FileExtensions))
for _, ext := range resource.FileExtensions { for _, ext := range resource.FileExtensions {
annotations = append(annotations, strings.TrimLeft(ext, ".")) annotations = append(annotations, strings.TrimLeft(ext, "."))
} }

View File

@ -280,7 +280,7 @@ func DescribeResourceQuotas(quotas *api.ResourceQuotaList, w io.Writer) {
for _, q := range quotas.Items { for _, q := range quotas.Items {
fmt.Fprintf(w, "\n Name:\t%s\n", q.Name) fmt.Fprintf(w, "\n Name:\t%s\n", q.Name)
if len(q.Spec.Scopes) > 0 { if len(q.Spec.Scopes) > 0 {
scopes := []string{} scopes := make([]string, 0, len(q.Spec.Scopes))
for _, scope := range q.Spec.Scopes { for _, scope := range q.Spec.Scopes {
scopes = append(scopes, string(scope)) scopes = append(scopes, string(scope))
} }
@ -297,7 +297,7 @@ func DescribeResourceQuotas(quotas *api.ResourceQuotaList, w io.Writer) {
fmt.Fprintf(w, " Resource\tUsed\tHard\n") fmt.Fprintf(w, " Resource\tUsed\tHard\n")
fmt.Fprint(w, " --------\t---\t---\n") fmt.Fprint(w, " --------\t---\t---\n")
resources := []api.ResourceName{} resources := make([]api.ResourceName, 0, len(q.Status.Hard))
for resource := range q.Status.Hard { for resource := range q.Status.Hard {
resources = append(resources, resource) resources = append(resources, resource)
} }
@ -433,7 +433,7 @@ func describeQuota(resourceQuota *api.ResourceQuota) (string, error) {
fmt.Fprintf(out, "Name:\t%s\n", resourceQuota.Name) fmt.Fprintf(out, "Name:\t%s\n", resourceQuota.Name)
fmt.Fprintf(out, "Namespace:\t%s\n", resourceQuota.Namespace) fmt.Fprintf(out, "Namespace:\t%s\n", resourceQuota.Namespace)
if len(resourceQuota.Spec.Scopes) > 0 { if len(resourceQuota.Spec.Scopes) > 0 {
scopes := []string{} scopes := make([]string, 0, len(resourceQuota.Spec.Scopes))
for _, scope := range resourceQuota.Spec.Scopes { for _, scope := range resourceQuota.Spec.Scopes {
scopes = append(scopes, string(scope)) scopes = append(scopes, string(scope))
} }
@ -449,7 +449,7 @@ func describeQuota(resourceQuota *api.ResourceQuota) (string, error) {
fmt.Fprintf(out, "Resource\tUsed\tHard\n") fmt.Fprintf(out, "Resource\tUsed\tHard\n")
fmt.Fprintf(out, "--------\t----\t----\n") fmt.Fprintf(out, "--------\t----\t----\n")
resources := []api.ResourceName{} resources := make([]api.ResourceName, 0, len(resourceQuota.Status.Hard))
for resource := range resourceQuota.Status.Hard { for resource := range resourceQuota.Status.Hard {
resources = append(resources, resource) resources = append(resources, resource)
} }
@ -920,7 +920,7 @@ func describeContainers(label string, containers []api.Container, containerStatu
} }
func describeContainerPorts(cPorts []api.ContainerPort) string { func describeContainerPorts(cPorts []api.ContainerPort) string {
ports := []string{} ports := make([]string, 0, len(cPorts))
for _, cPort := range cPorts { for _, cPort := range cPorts {
ports = append(ports, fmt.Sprintf("%d/%s", cPort.ContainerPort, cPort.Protocol)) ports = append(ports, fmt.Sprintf("%d/%s", cPort.ContainerPort, cPort.Protocol))
} }
@ -1471,7 +1471,7 @@ func describeEndpoints(ep *api.Endpoints, events *api.EventList) (string, error)
for i := range ep.Subsets { for i := range ep.Subsets {
subset := &ep.Subsets[i] subset := &ep.Subsets[i]
addresses := []string{} addresses := make([]string, 0, len(subset.Addresses))
for _, addr := range subset.Addresses { for _, addr := range subset.Addresses {
addresses = append(addresses, addr.IP) addresses = append(addresses, addr.IP)
} }
@ -1481,7 +1481,7 @@ func describeEndpoints(ep *api.Endpoints, events *api.EventList) (string, error)
} }
fmt.Fprintf(out, " Addresses:\t%s\n", addressesString) fmt.Fprintf(out, " Addresses:\t%s\n", addressesString)
notReadyAddresses := []string{} notReadyAddresses := make([]string, 0, len(subset.NotReadyAddresses))
for _, addr := range subset.NotReadyAddresses { for _, addr := range subset.NotReadyAddresses {
notReadyAddresses = append(notReadyAddresses, addr.IP) notReadyAddresses = append(notReadyAddresses, addr.IP)
} }
@ -1655,7 +1655,7 @@ func describeNode(node *api.Node, nodeNonTerminatedPodsList *api.PodList, events
c.Message) c.Message)
} }
} }
var addresses []string addresses := make([]string, 0, len(node.Status.Addresses))
for _, address := range node.Status.Addresses { for _, address := range node.Status.Addresses {
addresses = append(addresses, address.Address) addresses = append(addresses, address.Address)
} }
@ -1974,7 +1974,7 @@ func getDaemonSetsForLabels(c client.DaemonSetInterface, labelsToMatch labels.La
func printReplicationControllersByLabels(matchingRCs []*api.ReplicationController) string { func printReplicationControllersByLabels(matchingRCs []*api.ReplicationController) string {
// Format the matching RC's into strings. // Format the matching RC's into strings.
var rcStrings []string rcStrings := make([]string, 0, len(matchingRCs))
for _, controller := range matchingRCs { for _, controller := range matchingRCs {
rcStrings = append(rcStrings, fmt.Sprintf("%s (%d/%d replicas created)", controller.Name, controller.Status.Replicas, controller.Spec.Replicas)) rcStrings = append(rcStrings, fmt.Sprintf("%s (%d/%d replicas created)", controller.Name, controller.Status.Replicas, controller.Spec.Replicas))
} }
@ -1988,7 +1988,7 @@ func printReplicationControllersByLabels(matchingRCs []*api.ReplicationControlle
func printReplicaSetsByLabels(matchingRSs []*extensions.ReplicaSet) string { func printReplicaSetsByLabels(matchingRSs []*extensions.ReplicaSet) string {
// Format the matching ReplicaSets into strings. // Format the matching ReplicaSets into strings.
var rsStrings []string rsStrings := make([]string, 0, len(matchingRSs))
for _, rs := range matchingRSs { for _, rs := range matchingRSs {
rsStrings = append(rsStrings, fmt.Sprintf("%s (%d/%d replicas created)", rs.Name, rs.Status.Replicas, rs.Spec.Replicas)) rsStrings = append(rsStrings, fmt.Sprintf("%s (%d/%d replicas created)", rs.Name, rs.Status.Replicas, rs.Spec.Replicas))
} }
@ -2138,7 +2138,7 @@ func describeNetworkPolicy(networkPolicy *extensions.NetworkPolicy) (string, err
// newErrNoDescriber creates a new ErrNoDescriber with the names of the provided types. // newErrNoDescriber creates a new ErrNoDescriber with the names of the provided types.
func newErrNoDescriber(types ...reflect.Type) error { func newErrNoDescriber(types ...reflect.Type) error {
names := []string{} names := make([]string, 0, len(types))
for _, t := range types { for _, t := range types {
names = append(names, t.String()) names = append(names, t.String())
} }
@ -2177,7 +2177,7 @@ func (d *Describers) DescribeObject(exact interface{}, extra ...interface{}) (st
return fns[0].Describe(exact, extra...) return fns[0].Describe(exact, extra...)
} }
types := []reflect.Type{} types := make([]reflect.Type, 0, len(extra))
for _, obj := range extra { for _, obj := range extra {
types = append(types, reflect.TypeOf(obj)) types = append(types, reflect.TypeOf(obj))
} }
@ -2202,14 +2202,15 @@ func (d *Describers) Add(fns ...interface{}) error {
if ft.Kind() != reflect.Func { if ft.Kind() != reflect.Func {
return fmt.Errorf("expected func, got: %v", ft) return fmt.Errorf("expected func, got: %v", ft)
} }
if ft.NumIn() == 0 { numIn := ft.NumIn()
if numIn == 0 {
return fmt.Errorf("expected at least one 'in' params, got: %v", ft) return fmt.Errorf("expected at least one 'in' params, got: %v", ft)
} }
if ft.NumOut() != 2 { if ft.NumOut() != 2 {
return fmt.Errorf("expected two 'out' params - (string, error), got: %v", ft) return fmt.Errorf("expected two 'out' params - (string, error), got: %v", ft)
} }
types := []reflect.Type{} types := make([]reflect.Type, 0, numIn)
for i := 0; i < ft.NumIn(); i++ { for i := 0; i < numIn; i++ {
types = append(types, ft.In(i)) types = append(types, ft.In(i))
} }
if ft.Out(0) != reflect.TypeOf(string("")) { if ft.Out(0) != reflect.TypeOf(string("")) {

View File

@ -101,7 +101,7 @@ func PrintRolloutHistory(historyInfo HistoryInfo, resource, name string) (string
return fmt.Sprintf("No rollout history found in %s %q", resource, name), nil return fmt.Sprintf("No rollout history found in %s %q", resource, name), nil
} }
// Sort the revisionToChangeCause map by revision // Sort the revisionToChangeCause map by revision
var revisions []int64 revisions := make([]int64, 0, len(historyInfo.RevisionToTemplate))
for r := range historyInfo.RevisionToTemplate { for r := range historyInfo.RevisionToTemplate {
revisions = append(revisions, r) revisions = append(revisions, r)
} }

View File

@ -43,7 +43,7 @@ type NamespaceInfo struct {
} }
func listOfImages(spec *api.PodSpec) []string { func listOfImages(spec *api.PodSpec) []string {
var images []string images := make([]string, 0, len(spec.Containers))
for _, container := range spec.Containers { for _, container := range spec.Containers {
images = append(images, container.Image) images = append(images, container.Image)
} }
@ -64,7 +64,7 @@ func NewThirdPartyResourceMapper(gvs []unversioned.GroupVersion, gvks []unversio
}, nil }, nil
} }
} }
groupVersions := []string{} groupVersions := make([]string, 0, len(gvs))
for ix := range gvs { for ix := range gvs {
groupVersions = append(groupVersions, gvs[ix].String()) groupVersions = append(groupVersions, gvs[ix].String())
} }

View File

@ -827,7 +827,7 @@ func (BasicPod) Generate(genericParams map[string]interface{}) (runtime.Object,
} }
func parseEnvs(envArray []string) ([]api.EnvVar, error) { func parseEnvs(envArray []string) ([]api.EnvVar, error) {
envs := []api.EnvVar{} envs := make([]api.EnvVar, 0, len(envArray))
for _, env := range envArray { for _, env := range envArray {
pos := strings.Index(env, "=") pos := strings.Index(env, "=")
if pos == -1 { if pos == -1 {