From fe3131ba879bfe28c28cf27ea6cf818891fef7f8 Mon Sep 17 00:00:00 2001 From: WanLinghao Date: Mon, 15 Oct 2018 09:54:08 +0800 Subject: [PATCH] clean all unused packages under pkg/util --- hack/.golint_failures | 1 - pkg/util/BUILD | 3 - pkg/util/limitwriter/BUILD | 35 ----- pkg/util/limitwriter/doc.go | 19 --- pkg/util/limitwriter/limitwriter.go | 53 -------- pkg/util/limitwriter/limitwriter_test.go | 93 -------------- pkg/util/template/BUILD | 33 ----- pkg/util/template/template.go | 49 ------- pkg/util/template/template_test.go | 61 --------- pkg/util/threading/BUILD | 33 ----- pkg/util/threading/deadlock-detector.go | 120 ------------------ pkg/util/threading/deadlock-detector_test.go | 127 ------------------- 12 files changed, 627 deletions(-) delete mode 100644 pkg/util/limitwriter/BUILD delete mode 100644 pkg/util/limitwriter/doc.go delete mode 100644 pkg/util/limitwriter/limitwriter.go delete mode 100644 pkg/util/limitwriter/limitwriter_test.go delete mode 100644 pkg/util/template/BUILD delete mode 100644 pkg/util/template/template.go delete mode 100644 pkg/util/template/template_test.go delete mode 100644 pkg/util/threading/BUILD delete mode 100644 pkg/util/threading/deadlock-detector.go delete mode 100644 pkg/util/threading/deadlock-detector_test.go diff --git a/hack/.golint_failures b/hack/.golint_failures index bfbe4bd32d..0872e4dd37 100644 --- a/hack/.golint_failures +++ b/hack/.golint_failures @@ -400,7 +400,6 @@ pkg/util/sysctl pkg/util/sysctl/testing pkg/util/system pkg/util/taints -pkg/util/threading pkg/util/tolerations pkg/util/workqueue/prometheus pkg/version/verflag diff --git a/pkg/util/BUILD b/pkg/util/BUILD index 8849890665..497e36aadb 100644 --- a/pkg/util/BUILD +++ b/pkg/util/BUILD @@ -35,7 +35,6 @@ filegroup( "//pkg/util/ipvs:all-srcs", "//pkg/util/keymutex:all-srcs", "//pkg/util/labels:all-srcs", - "//pkg/util/limitwriter:all-srcs", "//pkg/util/maps:all-srcs", "//pkg/util/metrics:all-srcs", "//pkg/util/mount:all-srcs", @@ -60,8 +59,6 @@ filegroup( "//pkg/util/system:all-srcs", "//pkg/util/tail:all-srcs", "//pkg/util/taints:all-srcs", - "//pkg/util/template:all-srcs", - "//pkg/util/threading:all-srcs", "//pkg/util/tolerations:all-srcs", "//pkg/util/workqueue/prometheus:all-srcs", ], diff --git a/pkg/util/limitwriter/BUILD b/pkg/util/limitwriter/BUILD deleted file mode 100644 index 1c9d11f8cd..0000000000 --- a/pkg/util/limitwriter/BUILD +++ /dev/null @@ -1,35 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -load( - "@io_bazel_rules_go//go:def.bzl", - "go_library", - "go_test", -) - -go_library( - name = "go_default_library", - srcs = [ - "doc.go", - "limitwriter.go", - ], - importpath = "k8s.io/kubernetes/pkg/util/limitwriter", -) - -go_test( - name = "go_default_test", - srcs = ["limitwriter_test.go"], - embed = [":go_default_library"], -) - -filegroup( - name = "package-srcs", - srcs = glob(["**"]), - tags = ["automanaged"], - visibility = ["//visibility:private"], -) - -filegroup( - name = "all-srcs", - srcs = [":package-srcs"], - tags = ["automanaged"], -) diff --git a/pkg/util/limitwriter/doc.go b/pkg/util/limitwriter/doc.go deleted file mode 100644 index 02720adb7e..0000000000 --- a/pkg/util/limitwriter/doc.go +++ /dev/null @@ -1,19 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Package limitwriter provides a writer that only allows a certain number of bytes to be -// written. -package limitwriter // import "k8s.io/kubernetes/pkg/util/limitwriter" diff --git a/pkg/util/limitwriter/limitwriter.go b/pkg/util/limitwriter/limitwriter.go deleted file mode 100644 index c83f4a718e..0000000000 --- a/pkg/util/limitwriter/limitwriter.go +++ /dev/null @@ -1,53 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package limitwriter - -import ( - "errors" - "io" -) - -// New creates a writer that is limited to writing at most n bytes to w. This writer is not -// thread safe. -func New(w io.Writer, n int64) io.Writer { - return &limitWriter{ - w: w, - n: n, - } -} - -// ErrMaximumWrite is returned when all bytes have been written. -var ErrMaximumWrite = errors.New("maximum write") - -type limitWriter struct { - w io.Writer - n int64 -} - -func (w *limitWriter) Write(p []byte) (n int, err error) { - if int64(len(p)) > w.n { - p = p[:w.n] - } - if w.n > 0 { - n, err = w.w.Write(p) - w.n -= int64(n) - } - if w.n == 0 { - err = ErrMaximumWrite - } - return -} diff --git a/pkg/util/limitwriter/limitwriter_test.go b/pkg/util/limitwriter/limitwriter_test.go deleted file mode 100644 index 74c1cdcbb6..0000000000 --- a/pkg/util/limitwriter/limitwriter_test.go +++ /dev/null @@ -1,93 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package limitwriter - -import ( - "reflect" - "testing" -) - -type recordingWriter struct { - Wrote [][]byte -} - -func (r *recordingWriter) Write(data []byte) (int, error) { - r.Wrote = append(r.Wrote, data) - return len(data), nil -} - -func TestLimitWriter(t *testing.T) { - testcases := map[string]struct { - Limit int64 - Writes [][]byte - - ExpectedRecordedWrites [][]byte - ExpectedReportedWrites []int - ExpectedReportedErrors []error - }{ - "empty": {}, - - "empty write": { - Limit: 1000, - Writes: [][]byte{{}}, - ExpectedRecordedWrites: [][]byte{{}}, - ExpectedReportedWrites: []int{0}, - ExpectedReportedErrors: []error{nil}, - }, - - "unlimited write": { - Limit: 1000, - Writes: [][]byte{[]byte(`foo`)}, - ExpectedRecordedWrites: [][]byte{[]byte(`foo`)}, - ExpectedReportedWrites: []int{3}, - ExpectedReportedErrors: []error{nil}, - }, - - "limited write": { - Limit: 5, - Writes: [][]byte{[]byte(``), []byte(`1`), []byte(`23`), []byte(`456789`), []byte(`10`), []byte(``)}, - ExpectedRecordedWrites: [][]byte{[]byte(``), []byte(`1`), []byte(`23`), []byte(`45`)}, - ExpectedReportedWrites: []int{0, 1, 2, 2, 0, 0}, - ExpectedReportedErrors: []error{nil, nil, nil, ErrMaximumWrite, ErrMaximumWrite, ErrMaximumWrite}, - }, - } - - for k, tc := range testcases { - var reportedWrites []int - var reportedErrors []error - - recordingWriter := &recordingWriter{} - - limitwriter := New(recordingWriter, tc.Limit) - - for _, w := range tc.Writes { - n, err := limitwriter.Write(w) - reportedWrites = append(reportedWrites, n) - reportedErrors = append(reportedErrors, err) - } - - if !reflect.DeepEqual(recordingWriter.Wrote, tc.ExpectedRecordedWrites) { - t.Errorf("%s: expected recorded writes %v, got %v", k, tc.ExpectedRecordedWrites, recordingWriter.Wrote) - } - if !reflect.DeepEqual(reportedWrites, tc.ExpectedReportedWrites) { - t.Errorf("%s: expected reported writes %v, got %v", k, tc.ExpectedReportedWrites, reportedWrites) - } - if !reflect.DeepEqual(reportedErrors, tc.ExpectedReportedErrors) { - t.Errorf("%s: expected reported errors %v, got %v", k, tc.ExpectedReportedErrors, reportedErrors) - } - } -} diff --git a/pkg/util/template/BUILD b/pkg/util/template/BUILD deleted file mode 100644 index 3dd139d0bb..0000000000 --- a/pkg/util/template/BUILD +++ /dev/null @@ -1,33 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -load( - "@io_bazel_rules_go//go:def.bzl", - "go_library", - "go_test", -) - -go_test( - name = "go_default_test", - srcs = ["template_test.go"], - embed = [":go_default_library"], - deps = ["//vendor/github.com/stretchr/testify/assert:go_default_library"], -) - -go_library( - name = "go_default_library", - srcs = ["template.go"], - importpath = "k8s.io/kubernetes/pkg/util/template", -) - -filegroup( - name = "package-srcs", - srcs = glob(["**"]), - tags = ["automanaged"], - visibility = ["//visibility:private"], -) - -filegroup( - name = "all-srcs", - srcs = [":package-srcs"], - tags = ["automanaged"], -) diff --git a/pkg/util/template/template.go b/pkg/util/template/template.go deleted file mode 100644 index 2c03911dc2..0000000000 --- a/pkg/util/template/template.go +++ /dev/null @@ -1,49 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package template - -import ( - "bytes" - "go/doc" - "io" - "strings" - "text/template" -) - -func wrap(indent string, s string) string { - var buf bytes.Buffer - doc.ToText(&buf, s, indent, indent+" ", 80-len(indent)) - return buf.String() -} - -// ExecuteTemplate executes templateText with data and output written to w. -func ExecuteTemplate(w io.Writer, templateText string, data interface{}) error { - t := template.New("top") - t.Funcs(template.FuncMap{ - "trim": strings.TrimSpace, - "wrap": wrap, - }) - template.Must(t.Parse(templateText)) - return t.Execute(w, data) -} - -// ExecuteTemplateToString executes templateText with data and output written to string. -func ExecuteTemplateToString(templateText string, data interface{}) (string, error) { - b := bytes.Buffer{} - err := ExecuteTemplate(&b, templateText, data) - return b.String(), err -} diff --git a/pkg/util/template/template_test.go b/pkg/util/template/template_test.go deleted file mode 100644 index f6ce4d08c7..0000000000 --- a/pkg/util/template/template_test.go +++ /dev/null @@ -1,61 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package template - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestWrap(t *testing.T) { - tt := `before ->{{.Long | wrap "**"}}<- after` - data := struct { - Long string - }{ - `Hodor, hodor; hodor hodor; -hodor hodor... Hodor hodor hodor? Hodor. Hodor hodor hodor hodor... -Hodor hodor hodor; hodor hodor hodor! Hodor, hodor. Hodor. Hodor, -HODOR hodor, hodor hodor; hodor hodor; hodor HODOR hodor, hodor hodor? -Hodor. Hodor hodor - hodor hodor. Hodor hodor HODOR! Hodor hodor - hodor... -Hodor hodor HODOR hodor, hodor hodor hodor! Hodor, hodor... Hodor hodor -hodor hodor hodor hodor! Hodor, hodor; hodor hodor. Hodor.`, - } - output, _ := ExecuteTemplateToString(tt, data) - t.Logf("%q", output) - - assert.Equal(t, `before ->**Hodor, hodor; hodor hodor; hodor hodor... Hodor hodor hodor? Hodor. Hodor -**hodor hodor hodor... Hodor hodor hodor; hodor hodor hodor! Hodor, hodor. -**Hodor. Hodor, HODOR hodor, hodor hodor; hodor hodor; hodor HODOR hodor, hodor -**hodor? Hodor. Hodor hodor - hodor hodor. Hodor hodor HODOR! Hodor hodor - -**hodor... Hodor hodor HODOR hodor, hodor hodor hodor! Hodor, hodor... Hodor -**hodor hodor hodor hodor hodor! Hodor, hodor; hodor hodor. Hodor. -<- after`, output) -} - -func TestTrim(t *testing.T) { - tt := `before ->{{.Messy | trim }}<- after` - data := struct { - Messy string - }{ - "\t stuff\n \r ", - } - output, _ := ExecuteTemplateToString(tt, data) - t.Logf("%q", output) - - assert.Equal(t, `before ->stuff<- after`, output) -} diff --git a/pkg/util/threading/BUILD b/pkg/util/threading/BUILD deleted file mode 100644 index 4f0f898681..0000000000 --- a/pkg/util/threading/BUILD +++ /dev/null @@ -1,33 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -load( - "@io_bazel_rules_go//go:def.bzl", - "go_library", - "go_test", -) - -go_library( - name = "go_default_library", - srcs = ["deadlock-detector.go"], - importpath = "k8s.io/kubernetes/pkg/util/threading", - deps = ["//vendor/github.com/golang/glog:go_default_library"], -) - -go_test( - name = "go_default_test", - srcs = ["deadlock-detector_test.go"], - embed = [":go_default_library"], -) - -filegroup( - name = "package-srcs", - srcs = glob(["**"]), - tags = ["automanaged"], - visibility = ["//visibility:private"], -) - -filegroup( - name = "all-srcs", - srcs = [":package-srcs"], - tags = ["automanaged"], -) diff --git a/pkg/util/threading/deadlock-detector.go b/pkg/util/threading/deadlock-detector.go deleted file mode 100644 index 535dd906bc..0000000000 --- a/pkg/util/threading/deadlock-detector.go +++ /dev/null @@ -1,120 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package threading - -import ( - "os" - "sync" - "time" - - "github.com/golang/glog" -) - -type rwMutexToLockableAdapter struct { - rw *sync.RWMutex -} - -func (r *rwMutexToLockableAdapter) Lock() { - r.rw.RLock() -} - -func (r *rwMutexToLockableAdapter) Unlock() { - r.rw.RUnlock() -} - -type deadlockDetector struct { - name string - lock sync.Locker - maxLockPeriod time.Duration - exiter exiter - exitChannelFn func() <-chan time.Time - // Really only useful for testing - stopChannel <-chan bool -} - -// DeadlockWatchdogReadLock creates a watchdog on read/write mutex. If the mutex can not be acquired -// for read access within 'maxLockPeriod', the program exits via glog.Exitf() or os.Exit() if that fails -// 'name' is a semantic name that is useful for the user and is printed on exit. -func DeadlockWatchdogReadLock(lock *sync.RWMutex, name string, maxLockPeriod time.Duration) { - DeadlockWatchdog(&rwMutexToLockableAdapter{lock}, name, maxLockPeriod) -} - -func DeadlockWatchdog(lock sync.Locker, name string, maxLockPeriod time.Duration) { - if maxLockPeriod <= 0 { - panic("maxLockPeriod is <= 0, that can't be what you wanted") - } - detector := &deadlockDetector{ - lock: lock, - name: name, - maxLockPeriod: maxLockPeriod, - exitChannelFn: func() <-chan time.Time { return time.After(maxLockPeriod) }, - stopChannel: make(chan bool), - } - go detector.run() -} - -// Useful for injecting tests -type exiter interface { - Exitf(format string, args ...interface{}) -} - -type realExiter struct{} - -func (realExiter) Exitf(format string, args ...interface{}) { - func() { - defer func() { - // Let's just be extra sure we die, even if Exitf panics - if r := recover(); r != nil { - glog.Errorf(format, args...) - os.Exit(2) - } - }() - glog.Exitf(format, args...) - }() -} - -func (d *deadlockDetector) run() { - for { - if !d.runOnce() { - return - } - time.Sleep(d.maxLockPeriod / 2) - } -} - -func (d *deadlockDetector) runOnce() bool { - ch := make(chan bool, 1) - go func() { - d.lock.Lock() - d.lock.Unlock() - - ch <- true - }() - exitCh := d.exitChannelFn() - select { - case <-exitCh: - d.exiter.Exitf("Deadlock on %s, exiting", d.name) - // return is here for when we use a fake exiter in testing - return false - case <-ch: - glog.V(6).Infof("%s is not deadlocked", d.name) - case <-d.stopChannel: - glog.V(4).Infof("Stopping deadlock detector for %s", d.name) - return false - } - return true -} diff --git a/pkg/util/threading/deadlock-detector_test.go b/pkg/util/threading/deadlock-detector_test.go deleted file mode 100644 index 51444aa0b2..0000000000 --- a/pkg/util/threading/deadlock-detector_test.go +++ /dev/null @@ -1,127 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package threading - -import ( - "sync" - "testing" - "time" -) - -type fakeExiter struct { - format string - args []interface{} - exited bool -} - -func (f *fakeExiter) Exitf(format string, args ...interface{}) { - f.format = format - f.args = args - f.exited = true -} - -func TestMaxLockPeriod(t *testing.T) { - lock := &sync.RWMutex{} - panicked := false - func() { - defer func() { - if r := recover(); r != nil { - panicked = true - } - }() - DeadlockWatchdogReadLock(lock, "test lock", 0) - }() - if !panicked { - t.Errorf("expected a panic for a zero max lock period") - } -} - -func TestDeadlockWatchdogLocked(t *testing.T) { - lock := &sync.RWMutex{} - lock.Lock() - - exitCh := make(chan time.Time, 1) - fake := fakeExiter{} - - detector := &deadlockDetector{ - lock: &rwMutexToLockableAdapter{lock}, - name: "test deadlock", - exitChannelFn: func() <-chan time.Time { return exitCh }, - exiter: &fake, - } - - exitCh <- time.Time{} - - detector.run() - - if !fake.exited { - t.Errorf("expected to have exited") - } - - if len(fake.args) != 1 || fake.args[0].(string) != detector.name { - t.Errorf("unexpected args: %v", fake.args) - } -} - -func TestDeadlockWatchdogUnlocked(t *testing.T) { - lock := &sync.RWMutex{} - - fake := fakeExiter{} - - detector := &deadlockDetector{ - lock: &rwMutexToLockableAdapter{lock}, - name: "test deadlock", - exitChannelFn: func() <-chan time.Time { return time.After(time.Second * 5) }, - exiter: &fake, - } - - for i := 0; i < 100; i++ { - detector.runOnce() - } - - if fake.exited { - t.Errorf("expected to have not exited") - } -} - -func TestDeadlockWatchdogLocking(t *testing.T) { - lock := &sync.RWMutex{} - - fake := fakeExiter{} - - go func() { - for { - lock.Lock() - lock.Unlock() - } - }() - - detector := &deadlockDetector{ - lock: &rwMutexToLockableAdapter{lock}, - name: "test deadlock", - exitChannelFn: func() <-chan time.Time { return time.After(time.Second * 5) }, - exiter: &fake, - } - - for i := 0; i < 100; i++ { - detector.runOnce() - } - - if fake.exited { - t.Errorf("expected to have not exited") - } -}