Skip to content

Commit

Permalink
Introduce runtime2/config. (istio#2447)
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue.

Introduce runtime2/config.

This is the first part of a major refactoring of the runtime code. This part solely deals with config.

- Collects all config related code under runtime2/config package.
- Introduces a strongly-typed object model to ease use at the client-code.
- Adds perf counters for config state changes.

Some further details can be found here: https://docs.google.com/document/d/1tYlZuDQYkoikNcuD_DEA4SCi_t-SNwURkEyA8ND_mbw

NOTE: This PR contains code for some of the pending PRs:
istio#2448
istio#2450

The important parts to look at are pkg/runtime2/config and pkg/expr changes.
  • Loading branch information
ozevren authored and istio-merge-robot committed Jan 10, 2018
1 parent 695ccff commit 0428f25
Show file tree
Hide file tree
Showing 18 changed files with 3,000 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mixer/pkg/attribute/list.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions mixer/pkg/expr/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
srcs = [
"evaluator.go",
"expr.go",
"finder.go",
"func.go",
],
visibility = ["//visibility:public"],
Expand All @@ -22,6 +23,7 @@ go_test(
size = "small",
srcs = [
"expr_test.go",
"finter_test.go",
],
library = ":go_default_library",
deps = [
Expand Down
65 changes: 65 additions & 0 deletions mixer/pkg/expr/finder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2018 Istio 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 expr

import (
"fmt"
"sort"

configpb "istio.io/istio/mixer/pkg/config/proto"
"istio.io/istio/mixer/pkg/pool"
)

// NewFinder returns a new AttributeDescriptorFinder instance, based on the given attributes
func NewFinder(attributes map[string]*configpb.AttributeManifest_AttributeInfo) AttributeDescriptorFinder {
return finder{
attributes: attributes,
}
}

// finder exposes expr.AttributeDescriptorFinder
type finder struct {
attributes map[string]*configpb.AttributeManifest_AttributeInfo
}

var _ AttributeDescriptorFinder = finder{}

// GetAttribute finds an attribute by name. returns nil if not found.
func (a finder) GetAttribute(name string) *configpb.AttributeManifest_AttributeInfo {
return a.attributes[name]
}

func (a finder) String() string {
b := pool.GetBuffer()

// Sort by attribute names for stable ordering.
i := 0
names := make([]string, len(a.attributes))
for name := range a.attributes {
names[i] = name
i++
}
sort.Strings(names)

fmt.Fprintln(b, "Attributes:")
for _, n := range names {
fmt.Fprintf(b, " %s: %s", n, a.attributes[n].ValueType.String())
fmt.Fprintln(b)
}

s := b.String()
pool.PutBuffer(b)
return s
}
59 changes: 59 additions & 0 deletions mixer/pkg/expr/finder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2018 Istio 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 expr

import (
"fmt"
"testing"

descriptorpb "istio.io/api/mixer/v1/config/descriptor"
configpb "istio.io/istio/mixer/pkg/config/proto"
)

func TestFinder(t *testing.T) {

var finder AttributeDescriptorFinder = finder{
attributes: map[string]*configpb.AttributeManifest_AttributeInfo{
"foo": {
ValueType: descriptorpb.DOUBLE,
},
"baz": {
ValueType: descriptorpb.INT64,
},
},
}

foo := finder.GetAttribute("foo")
if foo == nil || foo.ValueType != descriptorpb.DOUBLE {
t.Fail()
}

bar := finder.GetAttribute("bar")
if bar != nil {
t.Fail()
}

expected := `Attributes:
baz: INT64
foo: DOUBLE
`
s := fmt.Sprintf("%v", finder)
if s != expected {
t.Log(s)
t.Log("!=")
t.Logf(expected)
t.Fatal("finder.String() mismatch")
}
}
Loading

0 comments on commit 0428f25

Please sign in to comment.