Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/kubectl: preallocate slice #26019

Merged
merged 1 commit into from
May 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/kubectl/bash_comp_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func AddJsonFilenameFlag(cmd *cobra.Command, value *[]string, usage string) {
cmd.Flags().StringSliceVarP(value, "filename", "f", *value, usage)
annotations := []string{}
annotations := make([]string, 0, len(resource.FileExtensions))
for _, ext := range resource.FileExtensions {
annotations = append(annotations, strings.TrimLeft(ext, "."))
}
Expand Down
31 changes: 16 additions & 15 deletions pkg/kubectl/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func DescribeResourceQuotas(quotas *api.ResourceQuotaList, w io.Writer) {
for _, q := range quotas.Items {
fmt.Fprintf(w, "\n Name:\t%s\n", q.Name)
if len(q.Spec.Scopes) > 0 {
scopes := []string{}
scopes := make([]string, 0, len(q.Spec.Scopes))
for _, scope := range q.Spec.Scopes {
scopes = append(scopes, string(scope))
}
Expand All @@ -297,7 +297,7 @@ func DescribeResourceQuotas(quotas *api.ResourceQuotaList, w io.Writer) {
fmt.Fprintf(w, " Resource\tUsed\tHard\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 {
resources = append(resources, resource)
}
Expand Down Expand Up @@ -433,7 +433,7 @@ func describeQuota(resourceQuota *api.ResourceQuota) (string, error) {
fmt.Fprintf(out, "Name:\t%s\n", resourceQuota.Name)
fmt.Fprintf(out, "Namespace:\t%s\n", resourceQuota.Namespace)
if len(resourceQuota.Spec.Scopes) > 0 {
scopes := []string{}
scopes := make([]string, 0, len(resourceQuota.Spec.Scopes))
for _, scope := range resourceQuota.Spec.Scopes {
scopes = append(scopes, string(scope))
}
Expand All @@ -449,7 +449,7 @@ func describeQuota(resourceQuota *api.ResourceQuota) (string, error) {
fmt.Fprintf(out, "Resource\tUsed\tHard\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 {
resources = append(resources, resource)
}
Expand Down Expand Up @@ -920,7 +920,7 @@ func describeContainers(label string, containers []api.Container, containerStatu
}

func describeContainerPorts(cPorts []api.ContainerPort) string {
ports := []string{}
ports := make([]string, 0, len(cPorts))
for _, cPort := range cPorts {
ports = append(ports, fmt.Sprintf("%d/%s", cPort.ContainerPort, cPort.Protocol))
}
Expand Down Expand Up @@ -1471,7 +1471,7 @@ func describeEndpoints(ep *api.Endpoints, events *api.EventList) (string, error)
for i := range ep.Subsets {
subset := &ep.Subsets[i]

addresses := []string{}
addresses := make([]string, 0, len(subset.Addresses))
for _, addr := range subset.Addresses {
addresses = append(addresses, addr.IP)
}
Expand All @@ -1481,7 +1481,7 @@ func describeEndpoints(ep *api.Endpoints, events *api.EventList) (string, error)
}
fmt.Fprintf(out, " Addresses:\t%s\n", addressesString)

notReadyAddresses := []string{}
notReadyAddresses := make([]string, 0, len(subset.NotReadyAddresses))
for _, addr := range subset.NotReadyAddresses {
notReadyAddresses = append(notReadyAddresses, addr.IP)
}
Expand Down Expand Up @@ -1655,7 +1655,7 @@ func describeNode(node *api.Node, nodeNonTerminatedPodsList *api.PodList, events
c.Message)
}
}
var addresses []string
addresses := make([]string, 0, len(node.Status.Addresses))
for _, address := range node.Status.Addresses {
addresses = append(addresses, address.Address)
}
Expand Down Expand Up @@ -1974,7 +1974,7 @@ func getDaemonSetsForLabels(c client.DaemonSetInterface, labelsToMatch labels.La

func printReplicationControllersByLabels(matchingRCs []*api.ReplicationController) string {
// Format the matching RC's into strings.
var rcStrings []string
rcStrings := make([]string, 0, len(matchingRCs))
for _, controller := range matchingRCs {
rcStrings = append(rcStrings, fmt.Sprintf("%s (%d/%d replicas created)", controller.Name, controller.Status.Replicas, controller.Spec.Replicas))
}
Expand All @@ -1988,7 +1988,7 @@ func printReplicationControllersByLabels(matchingRCs []*api.ReplicationControlle

func printReplicaSetsByLabels(matchingRSs []*extensions.ReplicaSet) string {
// Format the matching ReplicaSets into strings.
var rsStrings []string
rsStrings := make([]string, 0, len(matchingRSs))
for _, rs := range matchingRSs {
rsStrings = append(rsStrings, fmt.Sprintf("%s (%d/%d replicas created)", rs.Name, rs.Status.Replicas, rs.Spec.Replicas))
}
Expand Down Expand Up @@ -2138,7 +2138,7 @@ func describeNetworkPolicy(networkPolicy *extensions.NetworkPolicy) (string, err

// newErrNoDescriber creates a new ErrNoDescriber with the names of the provided types.
func newErrNoDescriber(types ...reflect.Type) error {
names := []string{}
names := make([]string, 0, len(types))
for _, t := range types {
names = append(names, t.String())
}
Expand Down Expand Up @@ -2177,7 +2177,7 @@ func (d *Describers) DescribeObject(exact interface{}, extra ...interface{}) (st
return fns[0].Describe(exact, extra...)
}

types := []reflect.Type{}
types := make([]reflect.Type, 0, len(extra))
for _, obj := range extra {
types = append(types, reflect.TypeOf(obj))
}
Expand All @@ -2202,14 +2202,15 @@ func (d *Describers) Add(fns ...interface{}) error {
if ft.Kind() != reflect.Func {
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)
}
if ft.NumOut() != 2 {
return fmt.Errorf("expected two 'out' params - (string, error), got: %v", ft)
}
types := []reflect.Type{}
for i := 0; i < ft.NumIn(); i++ {
types := make([]reflect.Type, 0, numIn)
for i := 0; i < numIn; i++ {
types = append(types, ft.In(i))
}
if ft.Out(0) != reflect.TypeOf(string("")) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
// Sort the revisionToChangeCause map by revision
var revisions []int64
revisions := make([]int64, 0, len(historyInfo.RevisionToTemplate))
for r := range historyInfo.RevisionToTemplate {
revisions = append(revisions, r)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type NamespaceInfo struct {
}

func listOfImages(spec *api.PodSpec) []string {
var images []string
images := make([]string, 0, len(spec.Containers))
for _, container := range spec.Containers {
images = append(images, container.Image)
}
Expand All @@ -64,7 +64,7 @@ func NewThirdPartyResourceMapper(gvs []unversioned.GroupVersion, gvks []unversio
}, nil
}
}
groupVersions := []string{}
groupVersions := make([]string, 0, len(gvs))
for ix := range gvs {
groupVersions = append(groupVersions, gvs[ix].String())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ func (BasicPod) Generate(genericParams map[string]interface{}) (runtime.Object,
}

func parseEnvs(envArray []string) ([]api.EnvVar, error) {
envs := []api.EnvVar{}
envs := make([]api.EnvVar, 0, len(envArray))
for _, env := range envArray {
pos := strings.Index(env, "=")
if pos == -1 {
Expand Down