2014-06-06 23:40:48 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-06-12 00:01:02 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2014-06-14 01:11:32 +00:00
|
|
|
"fmt"
|
2015-09-17 19:41:06 +00:00
|
|
|
"math"
|
2015-02-26 20:15:50 +00:00
|
|
|
"os"
|
2014-11-24 05:07:36 +00:00
|
|
|
"reflect"
|
2014-09-04 17:55:30 +00:00
|
|
|
"regexp"
|
2014-12-24 00:03:26 +00:00
|
|
|
"strconv"
|
2015-01-17 00:34:47 +00:00
|
|
|
"strings"
|
2014-06-06 23:40:48 +00:00
|
|
|
"time"
|
2014-06-25 03:51:57 +00:00
|
|
|
|
2015-11-10 06:28:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/intstr"
|
2016-01-15 07:32:10 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/runtime"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2015-09-16 18:52:07 +00:00
|
|
|
// For any test of the style:
|
|
|
|
// ...
|
|
|
|
// <- time.After(timeout):
|
|
|
|
// t.Errorf("Timed out")
|
|
|
|
// The value for timeout should effectively be "forever." Obviously we don't want our tests to truly lock up forever, but 30s
|
|
|
|
// is long enough that it is effectively forever for the things that can slow down a run on a heavily contended machine
|
|
|
|
// (GC, seeks, etc), but not so long as to make a developer ctrl-c a test run if they do happen to break that test.
|
|
|
|
var ForeverTestTimeout = time.Second * 30
|
|
|
|
|
2015-02-28 00:49:51 +00:00
|
|
|
// NeverStop may be passed to Until to make it never stop.
|
|
|
|
var NeverStop <-chan struct{} = make(chan struct{})
|
|
|
|
|
2015-09-02 23:49:05 +00:00
|
|
|
// Forever is syntactic sugar on top of Until
|
|
|
|
func Forever(f func(), period time.Duration) {
|
|
|
|
Until(f, period, NeverStop)
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:05:33 +00:00
|
|
|
// Until loops until stop channel is closed, running f every period.
|
2014-11-28 20:14:49 +00:00
|
|
|
// Catches any panics, and keeps going. f may not be invoked if
|
2015-08-24 01:59:15 +00:00
|
|
|
// stop channel is already closed. Pass NeverStop to Until if you
|
|
|
|
// don't want it stop.
|
2014-11-28 20:14:49 +00:00
|
|
|
func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
2015-11-05 00:31:02 +00:00
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
for {
|
|
|
|
func() {
|
2016-01-15 07:32:10 +00:00
|
|
|
defer runtime.HandleCrash()
|
2014-06-06 23:40:48 +00:00
|
|
|
f()
|
|
|
|
}()
|
2015-11-05 00:31:02 +00:00
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
case <-time.After(period):
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 06:28:45 +00:00
|
|
|
func GetIntOrPercentValue(intOrStr *intstr.IntOrString) (int, bool, error) {
|
|
|
|
switch intOrStr.Type {
|
|
|
|
case intstr.Int:
|
2015-11-18 18:15:16 +00:00
|
|
|
return intOrStr.IntValue(), false, nil
|
2015-11-10 06:28:45 +00:00
|
|
|
case intstr.String:
|
|
|
|
s := strings.Replace(intOrStr.StrVal, "%", "", -1)
|
2015-09-17 19:41:06 +00:00
|
|
|
v, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
2015-11-10 06:28:45 +00:00
|
|
|
return 0, false, fmt.Errorf("invalid value %q: %v", intOrStr.StrVal, err)
|
2015-09-17 19:41:06 +00:00
|
|
|
}
|
2015-11-18 18:15:16 +00:00
|
|
|
return int(v), true, nil
|
2015-09-17 19:41:06 +00:00
|
|
|
}
|
|
|
|
return 0, false, fmt.Errorf("invalid value: neither int nor percentage")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetValueFromPercent(percent int, value int) int {
|
|
|
|
return int(math.Ceil(float64(percent) * (float64(value)) / 100))
|
|
|
|
}
|
|
|
|
|
2014-09-04 17:55:30 +00:00
|
|
|
// Takes a list of strings and compiles them into a list of regular expressions
|
2014-10-08 13:23:25 +00:00
|
|
|
func CompileRegexps(regexpStrings []string) ([]*regexp.Regexp, error) {
|
2014-09-04 17:55:30 +00:00
|
|
|
regexps := []*regexp.Regexp{}
|
|
|
|
for _, regexpStr := range regexpStrings {
|
|
|
|
r, err := regexp.Compile(regexpStr)
|
|
|
|
if err != nil {
|
|
|
|
return []*regexp.Regexp{}, err
|
|
|
|
}
|
|
|
|
regexps = append(regexps, r)
|
|
|
|
}
|
|
|
|
return regexps, nil
|
|
|
|
}
|
2014-12-24 00:03:26 +00:00
|
|
|
|
2015-06-24 18:10:10 +00:00
|
|
|
// Detects if using systemd as the init system
|
|
|
|
// Please note that simply reading /proc/1/cmdline can be misleading because
|
|
|
|
// some installation of various init programs can automatically make /sbin/init
|
|
|
|
// a symlink or even a renamed version of their main program.
|
|
|
|
// TODO(dchen1107): realiably detects the init system using on the system:
|
|
|
|
// systemd, upstart, initd, etc.
|
|
|
|
func UsingSystemdInitSystem() bool {
|
2015-06-24 19:56:36 +00:00
|
|
|
if _, err := os.Stat("/run/systemd/system"); err == nil {
|
2015-06-24 18:10:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-11-24 05:07:36 +00:00
|
|
|
// Tests whether all pointer fields in a struct are nil. This is useful when,
|
|
|
|
// for example, an API struct is handled by plugins which need to distinguish
|
|
|
|
// "no plugin accepted this spec" from "this spec is empty".
|
|
|
|
//
|
|
|
|
// This function is only valid for structs and pointers to structs. Any other
|
|
|
|
// type will cause a panic. Passing a typed nil pointer will return true.
|
|
|
|
func AllPtrFieldsNil(obj interface{}) bool {
|
|
|
|
v := reflect.ValueOf(obj)
|
|
|
|
if !v.IsValid() {
|
|
|
|
panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
|
|
|
|
}
|
|
|
|
if v.Kind() == reflect.Ptr {
|
|
|
|
if v.IsNil() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2015-01-17 00:34:47 +00:00
|
|
|
|
2015-05-28 18:45:08 +00:00
|
|
|
func FileExists(filename string) (bool, error) {
|
2015-06-05 21:49:26 +00:00
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return false, err
|
2015-05-28 18:45:08 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
2015-09-10 18:54:40 +00:00
|
|
|
|
2015-09-14 18:37:27 +00:00
|
|
|
// borrowed from ioutil.ReadDir
|
|
|
|
// ReadDir reads the directory named by dirname and returns
|
2015-09-17 14:04:06 +00:00
|
|
|
// a list of directory entries, minus those with lstat errors
|
2015-09-18 14:50:30 +00:00
|
|
|
func ReadDirNoExit(dirname string) ([]os.FileInfo, []error, error) {
|
2015-09-14 18:37:27 +00:00
|
|
|
if dirname == "" {
|
|
|
|
dirname = "."
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(dirname)
|
|
|
|
if err != nil {
|
2015-09-18 14:50:30 +00:00
|
|
|
return nil, nil, err
|
2015-09-14 18:37:27 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
names, err := f.Readdirnames(-1)
|
|
|
|
list := make([]os.FileInfo, 0, len(names))
|
2015-09-18 14:50:30 +00:00
|
|
|
errs := make([]error, 0, len(names))
|
2015-09-14 18:37:27 +00:00
|
|
|
for _, filename := range names {
|
|
|
|
fip, lerr := os.Lstat(dirname + "/" + filename)
|
|
|
|
if os.IsNotExist(lerr) {
|
|
|
|
// File disappeared between readdir + stat.
|
|
|
|
// Just treat it as if it didn't exist.
|
|
|
|
continue
|
|
|
|
}
|
2015-09-18 14:50:30 +00:00
|
|
|
|
|
|
|
list = append(list, fip)
|
|
|
|
errs = append(errs, lerr)
|
2015-09-14 18:37:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 14:50:30 +00:00
|
|
|
return list, errs, nil
|
2015-09-14 18:37:27 +00:00
|
|
|
}
|
2016-01-06 01:58:51 +00:00
|
|
|
|
|
|
|
// IntPtr returns a pointer to an int
|
|
|
|
func IntPtr(i int) *int {
|
|
|
|
o := i
|
|
|
|
return &o
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntPtrDerefOr derefrence the int ptr and returns it i not nil,
|
|
|
|
// else returns def.
|
|
|
|
func IntPtrDerefOr(ptr *int, def int) int {
|
|
|
|
if ptr != nil {
|
|
|
|
return *ptr
|
|
|
|
}
|
|
|
|
return def
|
|
|
|
}
|