use istio client-go library instead of knative (#1661)

use istio client-go library instead of knative
bump kubernetes dependency version
change code coverage to codecov
This commit is contained in:
zryfish
2019-12-13 11:26:18 +08:00
committed by GitHub
parent f249a6e081
commit ea88c8803d
2071 changed files with 354531 additions and 108336 deletions

21
vendor/sigs.k8s.io/structured-merge-diff/value/doc.go generated vendored Normal file
View File

@@ -0,0 +1,21 @@
/*
Copyright 2018 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 value defines types for an in-memory representation of yaml or json
// objects, organized for convenient comparison with a schema (as defined by
// the sibling schema package). Functions for reading and writing the objects
// are also provided.
package value

View File

@@ -0,0 +1,149 @@
/*
Copyright 2019 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 value
import (
"bytes"
"fmt"
jsoniter "github.com/json-iterator/go"
)
var (
readPool = jsoniter.NewIterator(jsoniter.ConfigCompatibleWithStandardLibrary).Pool()
writePool = jsoniter.NewStream(jsoniter.ConfigCompatibleWithStandardLibrary, nil, 1024).Pool()
)
// FromJSONFast is a helper function for reading a JSON document
func FromJSONFast(input []byte) (Value, error) {
iter := readPool.BorrowIterator(input)
defer readPool.ReturnIterator(iter)
return ReadJSONIter(iter)
}
func ReadJSONIter(iter *jsoniter.Iterator) (Value, error) {
next := iter.WhatIsNext()
switch next {
case jsoniter.InvalidValue:
iter.ReportError("reading an object", "got invalid token")
return Value{}, iter.Error
case jsoniter.StringValue:
str := String(iter.ReadString())
return Value{StringValue: &str}, nil
case jsoniter.NumberValue:
number := iter.ReadNumber()
isFloat := false
for _, c := range number {
if c == 'e' || c == 'E' || c == '.' {
isFloat = true
break
}
}
if isFloat {
f, err := number.Float64()
if err != nil {
iter.ReportError("parsing as float", err.Error())
return Value{}, err
}
return Value{FloatValue: (*Float)(&f)}, nil
}
i, err := number.Int64()
if err != nil {
iter.ReportError("parsing as float", err.Error())
return Value{}, err
}
return Value{IntValue: (*Int)(&i)}, nil
case jsoniter.NilValue:
iter.ReadNil()
return Value{Null: true}, nil
case jsoniter.BoolValue:
b := Boolean(iter.ReadBool())
return Value{BooleanValue: &b}, nil
case jsoniter.ArrayValue:
list := &List{}
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
v, err := ReadJSONIter(iter)
if err != nil {
iter.Error = err
return false
}
list.Items = append(list.Items, v)
return true
})
return Value{ListValue: list}, iter.Error
case jsoniter.ObjectValue:
m := &Map{}
iter.ReadObjectCB(func(iter *jsoniter.Iterator, key string) bool {
v, err := ReadJSONIter(iter)
if err != nil {
iter.Error = err
return false
}
m.Items = append(m.Items, Field{Name: key, Value: v})
return true
})
return Value{MapValue: m}, iter.Error
default:
return Value{}, fmt.Errorf("unexpected object type %v", next)
}
}
// ToJSONFast is a helper function for producing a JSon document.
func (v *Value) ToJSONFast() ([]byte, error) {
buf := bytes.Buffer{}
stream := writePool.BorrowStream(&buf)
defer writePool.ReturnStream(stream)
v.WriteJSONStream(stream)
err := stream.Flush()
return buf.Bytes(), err
}
func (v *Value) WriteJSONStream(stream *jsoniter.Stream) {
switch {
case v.Null:
stream.WriteNil()
case v.FloatValue != nil:
stream.WriteFloat64(float64(*v.FloatValue))
case v.IntValue != nil:
stream.WriteInt64(int64(*v.IntValue))
case v.BooleanValue != nil:
stream.WriteBool(bool(*v.BooleanValue))
case v.StringValue != nil:
stream.WriteString(string(*v.StringValue))
case v.ListValue != nil:
stream.WriteArrayStart()
for i := range v.ListValue.Items {
if i > 0 {
stream.WriteMore()
}
v.ListValue.Items[i].WriteJSONStream(stream)
}
stream.WriteArrayEnd()
case v.MapValue != nil:
stream.WriteObjectStart()
for i := range v.MapValue.Items {
if i > 0 {
stream.WriteMore()
}
stream.WriteObjectField(v.MapValue.Items[i].Name)
v.MapValue.Items[i].Value.WriteJSONStream(stream)
}
stream.WriteObjectEnd()
default:
stream.Write([]byte("invalid_value"))
}
}

View File

@@ -0,0 +1,234 @@
/*
Copyright 2018 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 value
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
)
// FromYAML is a helper function for reading a YAML document; it attempts to
// preserve order of keys within maps/structs. This is as a convenience to
// humans keeping YAML documents, not because there is a behavior difference.
//
// Known bug: objects with top-level arrays don't parse correctly.
func FromYAML(input []byte) (Value, error) {
var decoded interface{}
if len(input) == 4 && string(input) == "null" {
// Special case since the yaml package doesn't accurately
// preserve this.
return Value{Null: true}, nil
}
// This attempts to enable order sensitivity; note the yaml package is
// broken for documents that have root-level arrays, hence the two-step
// approach. TODO: This is a horrific hack. Is it worth it?
var ms yaml.MapSlice
if err := yaml.Unmarshal(input, &ms); err == nil {
decoded = ms
} else if err := yaml.Unmarshal(input, &decoded); err != nil {
return Value{}, err
}
v, err := FromUnstructured(decoded)
if err != nil {
return Value{}, fmt.Errorf("failed to interpret (%v):\n%s", err, input)
}
return v, nil
}
// FromJSON is a helper function for reading a JSON document
func FromJSON(input []byte) (Value, error) {
var decoded interface{}
if err := json.Unmarshal(input, &decoded); err != nil {
return Value{}, err
}
v, err := FromUnstructured(decoded)
if err != nil {
return Value{}, fmt.Errorf("failed to interpret (%v):\n%s", err, input)
}
return v, nil
}
// FromUnstructured will convert a go interface to a Value.
// It's most commonly expected to be used with map[string]interface{} as the
// input. `in` must not have any structures with cycles in them.
// yaml.MapSlice may be used for order-preservation.
func FromUnstructured(in interface{}) (Value, error) {
if in == nil {
return Value{Null: true}, nil
}
switch t := in.(type) {
case map[interface{}]interface{}:
m := Map{}
for rawKey, rawVal := range t {
k, ok := rawKey.(string)
if !ok {
return Value{}, fmt.Errorf("key %#v: not a string", k)
}
v, err := FromUnstructured(rawVal)
if err != nil {
return Value{}, fmt.Errorf("key %v: %v", k, err)
}
m.Set(k, v)
}
return Value{MapValue: &m}, nil
case map[string]interface{}:
m := Map{}
for k, rawVal := range t {
v, err := FromUnstructured(rawVal)
if err != nil {
return Value{}, fmt.Errorf("key %v: %v", k, err)
}
m.Set(k, v)
}
return Value{MapValue: &m}, nil
case yaml.MapSlice:
m := Map{}
for _, item := range t {
k, ok := item.Key.(string)
if !ok {
return Value{}, fmt.Errorf("key %#v is not a string", item.Key)
}
v, err := FromUnstructured(item.Value)
if err != nil {
return Value{}, fmt.Errorf("key %v: %v", k, err)
}
m.Set(k, v)
}
return Value{MapValue: &m}, nil
case []interface{}:
l := List{}
for i, rawVal := range t {
v, err := FromUnstructured(rawVal)
if err != nil {
return Value{}, fmt.Errorf("index %v: %v", i, err)
}
l.Items = append(l.Items, v)
}
return Value{ListValue: &l}, nil
case int:
n := Int(t)
return Value{IntValue: &n}, nil
case int8:
n := Int(t)
return Value{IntValue: &n}, nil
case int16:
n := Int(t)
return Value{IntValue: &n}, nil
case int32:
n := Int(t)
return Value{IntValue: &n}, nil
case int64:
n := Int(t)
return Value{IntValue: &n}, nil
case uint:
n := Int(t)
return Value{IntValue: &n}, nil
case uint8:
n := Int(t)
return Value{IntValue: &n}, nil
case uint16:
n := Int(t)
return Value{IntValue: &n}, nil
case uint32:
n := Int(t)
return Value{IntValue: &n}, nil
case float32:
f := Float(t)
return Value{FloatValue: &f}, nil
case float64:
f := Float(t)
return Value{FloatValue: &f}, nil
case string:
return StringValue(t), nil
case bool:
return BooleanValue(t), nil
default:
return Value{}, fmt.Errorf("type unimplemented: %t", in)
}
}
// ToYAML is a helper function for producing a YAML document; it attempts to
// preserve order of keys within maps/structs. This is as a convenience to
// humans keeping YAML documents, not because there is a behavior difference.
func (v *Value) ToYAML() ([]byte, error) {
return yaml.Marshal(v.ToUnstructured(true))
}
// ToJSON is a helper function for producing a JSon document.
func (v *Value) ToJSON() ([]byte, error) {
return json.Marshal(v.ToUnstructured(false))
}
// ToUnstructured will convert the Value into a go-typed object.
// If preserveOrder is true, then maps will be converted to the yaml.MapSlice
// type. Otherwise, map[string]interface{} must be used-- this destroys
// ordering information and is not recommended if the result of this will be
// serialized. Other types:
// * list -> []interface{}
// * others -> corresponding go type, wrapped in an interface{}
//
// Of note, floats and ints will always come out as float64 and int64,
// respectively.
func (v *Value) ToUnstructured(preserveOrder bool) interface{} {
switch {
case v.FloatValue != nil:
f := float64(*v.FloatValue)
return f
case v.IntValue != nil:
i := int64(*v.IntValue)
return i
case v.StringValue != nil:
return string(*v.StringValue)
case v.BooleanValue != nil:
return bool(*v.BooleanValue)
case v.ListValue != nil:
out := []interface{}{}
for _, item := range v.ListValue.Items {
out = append(out, item.ToUnstructured(preserveOrder))
}
return out
case v.MapValue != nil:
m := v.MapValue
if preserveOrder {
ms := make(yaml.MapSlice, len(m.Items))
for i := range m.Items {
ms[i] = yaml.MapItem{
Key: m.Items[i].Name,
Value: m.Items[i].Value.ToUnstructured(preserveOrder),
}
}
return ms
}
// This case is unavoidably lossy.
out := map[string]interface{}{}
for i := range m.Items {
out[m.Items[i].Name] = m.Items[i].Value.ToUnstructured(preserveOrder)
}
return out
default:
fallthrough
case v.Null == true:
return nil
}
}

361
vendor/sigs.k8s.io/structured-merge-diff/value/value.go generated vendored Normal file
View File

@@ -0,0 +1,361 @@
/*
Copyright 2018 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 value
import (
"fmt"
"sort"
"strings"
)
// A Value is an object; it corresponds to an 'atom' in the schema.
type Value struct {
// Exactly one of the below must be set.
FloatValue *Float
IntValue *Int
StringValue *String
BooleanValue *Boolean
ListValue *List
MapValue *Map
Null bool // represents an explicit `"foo" = null`
}
// Equals returns true iff the two values are equal.
func (v Value) Equals(rhs Value) bool {
return !v.Less(rhs) && !rhs.Less(v)
}
// Less provides a total ordering for Value (so that they can be sorted, even
// if they are of different types).
func (v Value) Less(rhs Value) bool {
if v.FloatValue != nil {
if rhs.FloatValue == nil {
// Extra: compare floats and ints numerically.
if rhs.IntValue != nil {
return float64(*v.FloatValue) < float64(*rhs.IntValue)
}
return true
}
return *v.FloatValue < *rhs.FloatValue
} else if rhs.FloatValue != nil {
// Extra: compare floats and ints numerically.
if v.IntValue != nil {
return float64(*v.IntValue) < float64(*rhs.FloatValue)
}
return false
}
if v.IntValue != nil {
if rhs.IntValue == nil {
return true
}
return *v.IntValue < *rhs.IntValue
} else if rhs.IntValue != nil {
return false
}
if v.StringValue != nil {
if rhs.StringValue == nil {
return true
}
return *v.StringValue < *rhs.StringValue
} else if rhs.StringValue != nil {
return false
}
if v.BooleanValue != nil {
if rhs.BooleanValue == nil {
return true
}
if *v.BooleanValue == *rhs.BooleanValue {
return false
}
return *v.BooleanValue == false
} else if rhs.BooleanValue != nil {
return false
}
if v.ListValue != nil {
if rhs.ListValue == nil {
return true
}
return v.ListValue.Less(rhs.ListValue)
} else if rhs.ListValue != nil {
return false
}
if v.MapValue != nil {
if rhs.MapValue == nil {
return true
}
return v.MapValue.Less(rhs.MapValue)
} else if rhs.MapValue != nil {
return false
}
if v.Null {
if !rhs.Null {
return true
}
return false
} else if rhs.Null {
return false
}
// Invalid Value-- nothing is set.
return false
}
type Int int64
type Float float64
type String string
type Boolean bool
// Field is an individual key-value pair.
type Field struct {
Name string
Value Value
}
// List is a list of items.
type List struct {
Items []Value
}
// Less compares two lists lexically.
func (l *List) Less(rhs *List) bool {
i := 0
for {
if i >= len(l.Items) && i >= len(rhs.Items) {
// Lists are the same length and all items are equal.
return false
}
if i >= len(l.Items) {
// LHS is shorter.
return true
}
if i >= len(rhs.Items) {
// RHS is shorter.
return false
}
if l.Items[i].Less(rhs.Items[i]) {
// LHS is less; return
return true
}
if rhs.Items[i].Less(l.Items[i]) {
// RHS is less; return
return false
}
// The items are equal; continue.
i++
}
}
// Map is a map of key-value pairs. It represents both structs and maps. We use
// a list and a go-language map to preserve order.
//
// Set and Get helpers are provided.
type Map struct {
Items []Field
// may be nil; lazily constructed.
// TODO: Direct modifications to Items above will cause serious problems.
index map[string]int
// may be empty; lazily constructed.
// TODO: Direct modifications to Items above will cause serious problems.
order []int
}
func (m *Map) computeOrder() []int {
if len(m.order) != len(m.Items) {
m.order = make([]int, len(m.Items))
for i := range m.order {
m.order[i] = i
}
sort.SliceStable(m.order, func(i, j int) bool {
return m.Items[m.order[i]].Name < m.Items[m.order[j]].Name
})
}
return m.order
}
// Less compares two maps lexically.
func (m *Map) Less(rhs *Map) bool {
var noAllocL, noAllocR [2]int
var morder, rorder []int
// For very short maps (<2 elements) this permits us to avoid
// allocating the order array. We could make this accomodate larger
// maps, but 2 items should be enough to cover most path element
// comparisons, and at some point there will be diminishing returns.
// This has a large effect on the path element deserialization test,
// because everything is sorted / compared, but only once.
switch len(m.Items) {
case 0:
morder = noAllocL[0:0]
case 1:
morder = noAllocL[0:1]
case 2:
morder = noAllocL[0:2]
if m.Items[0].Name > m.Items[1].Name {
morder[0] = 1
} else {
morder[1] = 1
}
default:
morder = m.computeOrder()
}
switch len(rhs.Items) {
case 0:
rorder = noAllocR[0:0]
case 1:
rorder = noAllocR[0:1]
case 2:
rorder = noAllocR[0:2]
if rhs.Items[0].Name > rhs.Items[1].Name {
rorder[0] = 1
} else {
rorder[1] = 1
}
default:
rorder = rhs.computeOrder()
}
i := 0
for {
if i >= len(morder) && i >= len(rorder) {
// Maps are the same length and all items are equal.
return false
}
if i >= len(morder) {
// LHS is shorter.
return true
}
if i >= len(rorder) {
// RHS is shorter.
return false
}
fa, fb := &m.Items[morder[i]], &rhs.Items[rorder[i]]
if fa.Name != fb.Name {
// the map having the field name that sorts lexically less is "less"
return fa.Name < fb.Name
}
if fa.Value.Less(fb.Value) {
// LHS is less; return
return true
}
if fb.Value.Less(fa.Value) {
// RHS is less; return
return false
}
// The items are equal; continue.
i++
}
}
// Get returns the (Field, true) or (nil, false) if it is not present
func (m *Map) Get(key string) (*Field, bool) {
if m.index == nil {
m.index = map[string]int{}
for i := range m.Items {
m.index[m.Items[i].Name] = i
}
}
f, ok := m.index[key]
if !ok {
return nil, false
}
return &m.Items[f], true
}
// Set inserts or updates the given item.
func (m *Map) Set(key string, value Value) {
if f, ok := m.Get(key); ok {
f.Value = value
return
}
m.Items = append(m.Items, Field{Name: key, Value: value})
i := len(m.Items) - 1
m.index[key] = i
m.order = nil
}
// Delete removes the key from the set.
func (m *Map) Delete(key string) {
items := []Field{}
for i := range m.Items {
if m.Items[i].Name != key {
items = append(items, m.Items[i])
}
}
m.Items = items
m.index = nil // Since the list has changed
m.order = nil
}
// StringValue returns s as a scalar string Value.
func StringValue(s string) Value {
s2 := String(s)
return Value{StringValue: &s2}
}
// IntValue returns i as a scalar numeric (integer) Value.
func IntValue(i int) Value {
i2 := Int(i)
return Value{IntValue: &i2}
}
// FloatValue returns f as a scalar numeric (float) Value.
func FloatValue(f float64) Value {
f2 := Float(f)
return Value{FloatValue: &f2}
}
// BooleanValue returns b as a scalar boolean Value.
func BooleanValue(b bool) Value {
b2 := Boolean(b)
return Value{BooleanValue: &b2}
}
// String returns a human-readable representation of the value.
func (v Value) String() string {
switch {
case v.FloatValue != nil:
return fmt.Sprintf("%v", *v.FloatValue)
case v.IntValue != nil:
return fmt.Sprintf("%v", *v.IntValue)
case v.StringValue != nil:
return fmt.Sprintf("%q", *v.StringValue)
case v.BooleanValue != nil:
return fmt.Sprintf("%v", *v.BooleanValue)
case v.ListValue != nil:
strs := []string{}
for _, item := range v.ListValue.Items {
strs = append(strs, item.String())
}
return "[" + strings.Join(strs, ",") + "]"
case v.MapValue != nil:
strs := []string{}
for _, i := range v.MapValue.Items {
strs = append(strs, fmt.Sprintf("%v=%v", i.Name, i.Value))
}
return "{" + strings.Join(strs, ";") + "}"
default:
fallthrough
case v.Null == true:
return "null"
}
}