diff --git a/pkg/kubectl/bash_comp_utils.go b/pkg/kubectl/bash_comp_utils.go index a89fef07a06..e3eaf30e4bb 100644 --- a/pkg/kubectl/bash_comp_utils.go +++ b/pkg/kubectl/bash_comp_utils.go @@ -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, ".")) } diff --git a/pkg/kubectl/describe.go b/pkg/kubectl/describe.go index 634959e8699..c73e405347b 100644 --- a/pkg/kubectl/describe.go +++ b/pkg/kubectl/describe.go @@ -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)) } @@ -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) } @@ -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)) } @@ -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) } @@ -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)) } @@ -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) } @@ -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) } @@ -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) } @@ -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)) } @@ -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)) } @@ -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()) } @@ -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)) } @@ -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("")) { diff --git a/pkg/kubectl/history.go b/pkg/kubectl/history.go index dece14fda8c..938f0329508 100644 --- a/pkg/kubectl/history.go +++ b/pkg/kubectl/history.go @@ -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) } diff --git a/pkg/kubectl/kubectl.go b/pkg/kubectl/kubectl.go index dd13dd458bf..9f5cb22ff8e 100644 --- a/pkg/kubectl/kubectl.go +++ b/pkg/kubectl/kubectl.go @@ -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) } @@ -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()) } diff --git a/pkg/kubectl/run.go b/pkg/kubectl/run.go index 19a732c7e60..4e076c07195 100644 --- a/pkg/kubectl/run.go +++ b/pkg/kubectl/run.go @@ -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 {