Skip to content

Commit

Permalink
add gometaliner into travis build (#254)
Browse files Browse the repository at this point in the history
and gradually tighten the linting rules. for now unused and golint are left out.

some fixes are included in this PR.

using gometaliner instead of just golint because:

it covers more cases, e.g. the typo checkings.
it also allows for disabling rules w.r.t regex, e.g. when you don't want to enforce all the comment rules enforced by golint

* Part of #53
  • Loading branch information
jimexist authored and jlewi committed Dec 29, 2017
1 parent 420f9aa commit 433b234
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 32 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
language: go

go:
# we cannot enable `gotyp` in gometalinter while at the same time
# keep 1.8 here. so I (jiay) went with removing `gotype` for now.
# see open issues:
# - https://github.com/alecthomas/gometalinter/issues/358
# - https://github.com/golang/go/issues/21712
# TODO jiayu: re-evaluate the pending issues and add back `gotype`
- 1.8
- 1.9

install:
# get coveralls.io support
- go get github.com/mattn/goveralls
- go get -u github.com/alecthomas/gometalinter
- gometalinter --install

script:
- gometalinter --config=linter_config.json ./pkg/...
# We customize the build step because by default
# Travis runs go test -v ./... which will include the vendor
# directory.
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# K8s Custom Resource and Operator For TensorFlow jobs

[![Build Status](https://travis-ci.org/tensorflow/k8s.svg?branch=master)](https://travis-ci.org/tensorflow/k8s)

[![Coverage Status](https://coveralls.io/repos/github/tensorflow/k8s/badge.svg?branch=master)](https://coveralls.io/github/tensorflow/k8s?branch=master)

[![Go Report Card](https://goreportcard.com/badge/github.com/tensorflow/k8s)](https://goreportcard.com/report/github.com/tensorflow/k8s)
[Prow Test Dashboard](https://k8s-testgrid.appspot.com/sig-big-data)

[Prow Jobs](https://prow.k8s.io/?repo=tensorflow%2Fk8s)

## Overview
Expand Down
25 changes: 25 additions & 0 deletions linter_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"Vendor": true,
"DisableAll": true,
"Enable": [
"vet",
"safesql",
"errcheck",
"goconst",
"goimports",
"varcheck",
"gas",
"staticcheck",
"gosimple",
"lll",
"unconvert",
"misspell",
"unconvert"
],
"Aggregate": true,
"WarnUnmatchedNolint": true,
"LineLength": 240,
"Exclude": ["comment or be unexported", "comment on exported"],
"Deadline": "300s",
"Skip": ["bin", "py"]
}
12 changes: 9 additions & 3 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ func (c *Controller) watch(watchVersion string) (<-chan *Event, <-chan error) {
}
if resp.StatusCode != http.StatusOK {
log.Infof("WatchClusters response: %+v", resp)
resp.Body.Close()
if err := resp.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
errCh <- errors.New("invalid status code: " + resp.Status)
return
}
Expand All @@ -326,7 +328,9 @@ func (c *Controller) watch(watchVersion string) (<-chan *Event, <-chan error) {
}

if st != nil {
resp.Body.Close()
if err := resp.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}

if st.Code == http.StatusGone {
// event history is outdated.
Expand All @@ -353,7 +357,9 @@ func (c *Controller) watch(watchVersion string) (<-chan *Event, <-chan error) {
eventCh <- ev
}

resp.Body.Close()
if err := resp.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}
}()

Expand Down
2 changes: 1 addition & 1 deletion pkg/spec/tf_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type ChiefSpec struct {

// Validate checks that the TfJobSpec is valid.
func (c *TfJobSpec) Validate() error {
if c.TerminationPolicy == nil || c.TerminationPolicy.Chief == nil {
if c.TerminationPolicy == nil || c.TerminationPolicy.Chief == nil {
return fmt.Errorf("invalid termination policy: %v", c.TerminationPolicy)
}
// Check that each replica has a TensorFlow container.
Expand Down
10 changes: 5 additions & 5 deletions pkg/spec/tf_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func TestSetDefaults(t *testing.T) {

func TestValidate(t *testing.T) {
type testCase struct {
in *TfJobSpec
in *TfJobSpec
expectingError bool
}

Expand All @@ -405,7 +405,7 @@ func TestValidate(t *testing.T) {
},
},
TfReplicaType: MASTER,
Replicas: proto.Int32(1),
Replicas: proto.Int32(1),
},
},
TfImage: "tensorflow/tensorflow:1.3.0",
Expand All @@ -426,7 +426,7 @@ func TestValidate(t *testing.T) {
},
},
TfReplicaType: WORKER,
Replicas: proto.Int32(1),
Replicas: proto.Int32(1),
},
},
TfImage: "tensorflow/tensorflow:1.3.0",
Expand All @@ -447,13 +447,13 @@ func TestValidate(t *testing.T) {
},
},
TfReplicaType: WORKER,
Replicas: proto.Int32(1),
Replicas: proto.Int32(1),
},
},
TfImage: "tensorflow/tensorflow:1.3.0",
TerminationPolicy: &TerminationPolicySpec{
Chief: &ChiefSpec{
ReplicaName: "WORKER",
ReplicaName: "WORKER",
ReplicaIndex: 0,
},
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/trainer/replicas.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ func transformClusterSpecForDefaultPS(clusterSpec ClusterSpec) string {
jobs := []string{}
for _, jobType := range keys {
hosts := []string{}
for _, h := range clusterSpec[jobType] {
hosts = append(hosts, h)
}
hosts = append(hosts, clusterSpec[jobType]...)
s := jobType + "|" + strings.Join(hosts, ";")
jobs = append(jobs, s)
}
Expand Down Expand Up @@ -341,7 +339,9 @@ func (s *TFReplicaSet) Delete() error {
}
} else {
log.V(1).Infof("Delete ConfigMaps %v:%v", s.Job.job.Metadata.Namespace, s.defaultPSConfigMapName())
s.ClientSet.CoreV1().ConfigMaps(s.Job.job.Metadata.Namespace).Delete(s.defaultPSConfigMapName(), &meta_v1.DeleteOptions{})
if err = s.ClientSet.CoreV1().ConfigMaps(s.Job.job.Metadata.Namespace).Delete(s.defaultPSConfigMapName(), &meta_v1.DeleteOptions{}); err != nil {
log.Errorf("error deleting config maps %v:%v: %v", s.Job.job.Metadata.Namespace, s.defaultPSConfigMapName(), err)
}
}

if failures {
Expand Down Expand Up @@ -406,7 +406,7 @@ func replicaStatusFromPodList(l v1.PodList, name spec.ContainerName) spec.Replic
return spec.ReplicaStateUnknown
}

func (s *TFReplicaSet) GetSingleReplicaStatus(index int32) (spec.ReplicaState) {
func (s *TFReplicaSet) GetSingleReplicaStatus(index int32) spec.ReplicaState {
j, err := s.ClientSet.BatchV1().Jobs(s.Job.job.Metadata.Namespace).Get(s.jobName(index), meta_v1.GetOptions{})

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/trainer/replicas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"encoding/json"

"github.com/tensorflow/k8s/pkg/spec"
"github.com/tensorflow/k8s/pkg/util"
tfJobFake "github.com/tensorflow/k8s/pkg/util/k8sutil/fake"
Expand Down
8 changes: 2 additions & 6 deletions pkg/trainer/tensorboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,14 @@ func (s *TBReplicaSet) getDeploymentSpecTemplate(image string) v1.PodTemplateSpe
VolumeMounts: make([]v1.VolumeMount, 0),
}

for _, v := range s.Spec.VolumeMounts {
c.VolumeMounts = append(c.VolumeMounts, v)
}
c.VolumeMounts = append(c.VolumeMounts, s.Spec.VolumeMounts...)

ps := &v1.PodSpec{
Containers: []v1.Container{*c},
Volumes: make([]v1.Volume, 0),
}

for _, v := range s.Spec.Volumes {
ps.Volumes = append(ps.Volumes, v)
}
ps.Volumes = append(ps.Volumes, s.Spec.Volumes...)

return v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Expand Down
6 changes: 3 additions & 3 deletions pkg/trainer/training.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type TrainingJob struct {

// ClusterSpec represents a cluster TensorFlow specification.
// https://www.tensorflow.org/deploy/distributed#create_a_tftrainclusterspec_to_describe_the_cluster
// It is a map from job names to network addressess.
// It is a map from job names to network addresses.
type ClusterSpec map[string][]string

type TaskSpec struct {
Expand Down Expand Up @@ -179,7 +179,7 @@ func (j *TrainingJob) GetStatus() (spec.ReplicaState, []*spec.TfReplicaStatus, e

replicaStatuses = append(replicaStatuses, &rStatus)

if string(r.Spec.TfReplicaType) == string(chief.ReplicaName) {
if string(r.Spec.TfReplicaType) == chief.ReplicaName {
chiefState = r.GetSingleReplicaStatus(int32(chief.ReplicaIndex))
}
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func (j *TrainingJob) reconcile(config *spec.ControllerConfig) {
}

// updateTPRStatus will update the status of the TPR with c.Status if c.Status
// doesn't match c.Cluster.status. So you can change c.Status in order to propogate
// doesn't match c.Cluster.status. So you can change c.Status in order to propagate
// changes to the TPR status.
if err := j.updateTPRStatus(); err != nil {
log.Warningf("Job %v; failed to update TPR status error: %v", j.job.Metadata.Name, err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/trainer/training_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"sync"

"github.com/gogo/protobuf/proto"
"github.com/tensorflow/k8s/pkg/spec"
tfJobFake "github.com/tensorflow/k8s/pkg/util/k8sutil/fake"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"sync"
)

func TestIsRetryableTerminationState(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/k8sutil/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
package fake

import (
"github.com/tensorflow/k8s/pkg/spec"
"net/http"
"time"

"github.com/tensorflow/k8s/pkg/spec"
)

type TfJobClientFake struct{}
Expand Down
10 changes: 7 additions & 3 deletions pkg/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

"github.com/tensorflow/k8s/pkg/spec"

log "github.com/golang/glog"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
log "github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -56,10 +56,14 @@ func GetClusterConfig() (*rest.Config, error) {
if err != nil {
panic(err)
}
os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0])
if err := os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0]); err != nil {
return nil, err
}
}
if len(os.Getenv("KUBERNETES_SERVICE_PORT")) == 0 {
os.Setenv("KUBERNETES_SERVICE_PORT", "443")
if err := os.Setenv("KUBERNETES_SERVICE_PORT", "443"); err != nil {
panic(err)
}
}
return rest.InClusterConfig()
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package util
import (
"encoding/json"
"fmt"
log "github.com/golang/glog"
"math/rand"
"time"

log "github.com/golang/glog"
)

// Pformat returns a pretty format output of any value that can be marshalled to JSON.
Expand Down

0 comments on commit 433b234

Please sign in to comment.