Skip to content

Commit

Permalink
lint: Enable goimports
Browse files Browse the repository at this point in the history
goimports checks import lines, adding missing ones and removing
unreferenced ones:
https://godoc.org/golang.org/x/tools/cmd/goimports

It also requires named imports for packages whose
import paths don't match their package names:
- golang/go#28428
- https://go-review.googlesource.com/c/tools/+/145699/

Also standardized named imports of common Kubernetes packaages.

Part of #217

Signed-off-by: Andrew Seigner <siggy@buoyant.io>
  • Loading branch information
siggy committed Feb 24, 2019
1 parent e300309 commit 7886306
Show file tree
Hide file tree
Showing 33 changed files with 287 additions and 287 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ linters:
- deadcode
- depguard
- gofmt
- goimports
- golint
- gosimple
- govet
Expand All @@ -22,7 +23,6 @@ linters:
# - gochecknoinits
# - goconst
# - gocyclo
# - goimports
# - gosec
# - interfacer
# - lll
Expand Down
74 changes: 37 additions & 37 deletions cli/cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/linkerd/linkerd2/pkg/healthcheck"
"github.com/linkerd/linkerd2/pkg/k8s"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
k8sMeta "k8s.io/apimachinery/pkg/api/meta"
k8sResource "k8s.io/apimachinery/pkg/api/resource"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -54,7 +54,7 @@ func runInjectCmd(inputs []io.Reader, errWriter, outWriter io.Writer, options *i

// objMeta provides a generic struct to parse the names of Kubernetes objects
type objMeta struct {
metaV1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
}

func newInjectOptions() *injectOptions {
Expand Down Expand Up @@ -118,7 +118,7 @@ func uninjectAndInject(inputs []io.Reader, errWriter, outWriter io.Writer, optio
/* Given a ObjectMeta, update ObjectMeta in place with the new labels and
* annotations.
*/
func injectObjectMeta(t *metaV1.ObjectMeta, k8sLabels map[string]string, options *injectOptions, report *injectReport) bool {
func injectObjectMeta(t *metav1.ObjectMeta, k8sLabels map[string]string, options *injectOptions, report *injectReport) bool {
report.injectDisabled = injectDisabled(t)
if report.injectDisabled {
return false
Expand All @@ -145,7 +145,7 @@ func injectObjectMeta(t *metaV1.ObjectMeta, k8sLabels map[string]string, options
* and init-container injected. If the pod is unsuitable for having them
* injected, return false.
*/
func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameOverride string, options *injectOptions, report *injectReport) bool {
func injectPodSpec(t *corev1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameOverride string, options *injectOptions, report *injectReport) bool {
report.hostNetwork = t.HostNetwork
report.sidecar = healthcheck.HasExistingSidecars(t)
report.udp = checkUDPPorts(t)
Expand Down Expand Up @@ -196,19 +196,19 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
IntVal: int32(options.proxyMetricsPort),
}

proxyProbe := v1.Probe{
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
proxyProbe := corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/metrics",
Port: metricsPort,
},
},
InitialDelaySeconds: 10,
}

resources := v1.ResourceRequirements{
Requests: v1.ResourceList{},
Limits: v1.ResourceList{},
resources := corev1.ResourceRequirements{
Requests: corev1.ResourceList{},
Limits: corev1.ResourceList{},
}

if options.proxyCPURequest != "" {
Expand All @@ -231,15 +231,15 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
if options.disableExternalProfiles {
profileSuffixes = "svc.cluster.local."
}
sidecar := v1.Container{
sidecar := corev1.Container{
Name: k8s.ProxyContainerName,
Image: options.taggedProxyImage(),
ImagePullPolicy: v1.PullPolicy(options.imagePullPolicy),
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
SecurityContext: &v1.SecurityContext{
ImagePullPolicy: corev1.PullPolicy(options.imagePullPolicy),
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &options.proxyUID,
},
Ports: []v1.ContainerPort{
Ports: []corev1.ContainerPort{
{
Name: "linkerd-proxy",
ContainerPort: int32(options.inboundPort),
Expand All @@ -250,7 +250,7 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
},
},
Resources: resources,
Env: []v1.EnvVar{
Env: []corev1.EnvVar{
{Name: "LINKERD2_PROXY_LOG", Value: options.proxyLogLevel},
{
Name: "LINKERD2_PROXY_CONTROL_URL",
Expand All @@ -263,7 +263,7 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
{Name: "LINKERD2_PROXY_DESTINATION_PROFILE_SUFFIXES", Value: profileSuffixes},
{
Name: PodNamespaceEnvVarName,
ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"}},
ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.namespace"}},
},
{Name: "LINKERD2_PROXY_INBOUND_ACCEPT_KEEPALIVE", Value: fmt.Sprintf("%dms", defaultKeepaliveMs)},
{Name: "LINKERD2_PROXY_OUTBOUND_CONNECT_KEEPALIVE", Value: fmt.Sprintf("%dms", defaultKeepaliveMs)},
Expand All @@ -282,7 +282,7 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
for _, container := range t.Containers {
if capacity, ok := options.proxyOutboundCapacity[container.Image]; ok {
sidecar.Env = append(sidecar.Env,
v1.EnvVar{
corev1.EnvVar{
Name: "LINKERD2_PROXY_OUTBOUND_ROUTER_CAPACITY",
Value: fmt.Sprintf("%d", capacity),
},
Expand All @@ -294,19 +294,19 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
if options.enableTLS() {
yes := true

configMapVolume := v1.Volume{
configMapVolume := corev1.Volume{
Name: k8s.TLSTrustAnchorVolumeName,
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{Name: k8s.TLSTrustAnchorConfigMapName},
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: k8s.TLSTrustAnchorConfigMapName},
Optional: &yes,
},
},
}
secretVolume := v1.Volume{
secretVolume := corev1.Volume{
Name: k8s.TLSSecretsVolumeName,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: identity.ToSecretName(),
Optional: &yes,
},
Expand All @@ -316,7 +316,7 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
base := "/var/linkerd-io"
configMapBase := base + "/trust-anchors"
secretBase := base + "/identity"
tlsEnvVars := []v1.EnvVar{
tlsEnvVars := []corev1.EnvVar{
{Name: "LINKERD2_PROXY_TLS_TRUST_ANCHORS", Value: configMapBase + "/" + k8s.TLSTrustAnchorFileName},
{Name: "LINKERD2_PROXY_TLS_CERT", Value: secretBase + "/" + k8s.TLSCertFileName},
{Name: "LINKERD2_PROXY_TLS_PRIVATE_KEY", Value: secretBase + "/" + k8s.TLSPrivateKeyFileName},
Expand All @@ -329,7 +329,7 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
}

sidecar.Env = append(sidecar.Env, tlsEnvVars...)
sidecar.VolumeMounts = []v1.VolumeMount{
sidecar.VolumeMounts = []corev1.VolumeMount{
{Name: configMapVolume.Name, MountPath: configMapBase, ReadOnly: true},
{Name: secretVolume.Name, MountPath: secretBase, ReadOnly: true},
}
Expand All @@ -341,15 +341,15 @@ func injectPodSpec(t *v1.PodSpec, identity k8s.TLSIdentity, controlPlaneDNSNameO
if !options.noInitContainer {
nonRoot := false
runAsUser := int64(0)
initContainer := v1.Container{
initContainer := corev1.Container{
Name: k8s.InitContainerName,
Image: options.taggedProxyInitImage(),
ImagePullPolicy: v1.PullPolicy(options.imagePullPolicy),
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
ImagePullPolicy: corev1.PullPolicy(options.imagePullPolicy),
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
Args: initArgs,
SecurityContext: &v1.SecurityContext{
Capabilities: &v1.Capabilities{
Add: []v1.Capability{v1.Capability("NET_ADMIN")},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Add: []corev1.Capability{corev1.Capability("NET_ADMIN")},
},
Privileged: &f,
RunAsNonRoot: &nonRoot,
Expand Down Expand Up @@ -509,18 +509,18 @@ func (resourceTransformerInject) generateReport(injectReports []injectReport, ou
output.Write([]byte("\n"))
}

func checkUDPPorts(t *v1.PodSpec) bool {
func checkUDPPorts(t *corev1.PodSpec) bool {
// Check for ports with `protocol: UDP`, which will not be routed by Linkerd
for _, container := range t.Containers {
for _, port := range container.Ports {
if port.Protocol == v1.ProtocolUDP {
if port.Protocol == corev1.ProtocolUDP {
return true
}
}
}
return false
}

func injectDisabled(t *metaV1.ObjectMeta) bool {
func injectDisabled(t *metav1.ObjectMeta) bool {
return t.GetAnnotations()[k8s.ProxyInjectAnnotation] == k8s.ProxyInjectDisabled
}
24 changes: 12 additions & 12 deletions cli/cmd/inject_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"path/filepath"

"github.com/linkerd/linkerd2/pkg/k8s"
appsV1 "k8s.io/api/apps/v1"
batchV1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
yamlDecoder "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/yaml"
Expand All @@ -37,9 +37,9 @@ type injectReport struct {
type resourceConfig struct {
obj interface{}
om objMeta
meta metaV1.TypeMeta
podSpec *v1.PodSpec
objectMeta *metaV1.ObjectMeta
meta metav1.TypeMeta
podSpec *corev1.PodSpec
objectMeta *metav1.ObjectMeta
dnsNameOverride string
k8sLabels map[string]string
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func ProcessYAML(in io.Reader, out io.Writer, report io.Writer, options *injectO
}

func processList(b []byte, options *injectOptions, rt resourceTransformer) ([]byte, []injectReport, error) {
var sourceList v1.List
var sourceList corev1.List
if err := yaml.Unmarshal(b, &sourceList); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func (conf *resourceConfig) parse(bytes []byte, options *injectOptions, rt resou
conf.objectMeta = &deployment.Spec.Template.ObjectMeta

case "ReplicationController":
var rc v1.ReplicationController
var rc corev1.ReplicationController
if err := yaml.Unmarshal(bytes, &rc); err != nil {
return nil, nil, err
}
Expand All @@ -216,7 +216,7 @@ func (conf *resourceConfig) parse(bytes []byte, options *injectOptions, rt resou
conf.objectMeta = &rs.Spec.Template.ObjectMeta

case "Job":
var job batchV1.Job
var job batchv1.Job
if err := yaml.Unmarshal(bytes, &job); err != nil {
return nil, nil, err
}
Expand All @@ -238,7 +238,7 @@ func (conf *resourceConfig) parse(bytes []byte, options *injectOptions, rt resou
conf.objectMeta = &ds.Spec.Template.ObjectMeta

case "StatefulSet":
var statefulset appsV1.StatefulSet
var statefulset appsv1.StatefulSet
if err := yaml.Unmarshal(bytes, &statefulset); err != nil {
return nil, nil, err
}
Expand All @@ -249,7 +249,7 @@ func (conf *resourceConfig) parse(bytes []byte, options *injectOptions, rt resou
conf.objectMeta = &statefulset.Spec.Template.ObjectMeta

case "Pod":
var pod v1.Pod
var pod corev1.Pod
if err := yaml.Unmarshal(bytes, &pod); err != nil {
return nil, nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions cli/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/linkerd/linkerd2/pkg/k8s"
"github.com/spf13/cobra"
"github.com/wercker/stern/stern"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -105,7 +105,7 @@ func (o *logsOptions) toSternConfig(controlPlaneComponents, availableContainers
return config, nil
}

func getControlPlaneComponentsAndContainers(pods *v1.PodList) ([]string, []string) {
func getControlPlaneComponentsAndContainers(pods *corev1.PodList) ([]string, []string) {
var controlPlaneComponents, containers []string
for _, pod := range pods.Items {
controlPlaneComponents = append(controlPlaneComponents, pod.Labels["linkerd.io/control-plane-component"])
Expand All @@ -127,7 +127,7 @@ func newLogCmdConfig(options *logsOptions, kubeconfigPath, kubeContext string) (
return nil, err
}

podList, err := clientset.CoreV1().Pods(controlPlaneNamespace).List(meta_v1.ListOptions{})
podList, err := clientset.CoreV1().Pods(controlPlaneNamespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions cli/cmd/uninject.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/linkerd/linkerd2/pkg/k8s"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -126,8 +126,8 @@ func (resourceTransformerUninjectSilent) generateReport(uninjectReports []inject

// Given a PodSpec, update the PodSpec in place with the sidecar
// and init-container uninjected
func uninjectPodSpec(t *v1.PodSpec, report *injectReport) {
initContainers := []v1.Container{}
func uninjectPodSpec(t *corev1.PodSpec, report *injectReport) {
initContainers := []corev1.Container{}
for _, container := range t.InitContainers {
if container.Name != k8s.InitContainerName {
initContainers = append(initContainers, container)
Expand All @@ -137,15 +137,15 @@ func uninjectPodSpec(t *v1.PodSpec, report *injectReport) {
}
t.InitContainers = initContainers

containers := []v1.Container{}
containers := []corev1.Container{}
for _, container := range t.Containers {
if container.Name != k8s.ProxyContainerName {
containers = append(containers, container)
}
}
t.Containers = containers

volumes := []v1.Volume{}
volumes := []corev1.Volume{}
for _, volume := range t.Volumes {
// TODO: move those strings to constants
if volume.Name != k8s.TLSTrustAnchorVolumeName && volume.Name != k8s.TLSSecretsVolumeName {
Expand All @@ -155,7 +155,7 @@ func uninjectPodSpec(t *v1.PodSpec, report *injectReport) {
t.Volumes = volumes
}

func uninjectObjectMeta(t *metaV1.ObjectMeta) {
func uninjectObjectMeta(t *metav1.ObjectMeta) {
newAnnotations := make(map[string]string)
for key, val := range t.Annotations {
if key != k8s.CreatedByAnnotation && key != k8s.ProxyVersionAnnotation {
Expand Down
Loading

0 comments on commit 7886306

Please sign in to comment.