Skip to content

Commit 1595e87

Browse files
author
Jianfei Hu
committed
2 parents 256d963 + ac78a55 commit 1595e87

File tree

36 files changed

+1646
-603
lines changed

36 files changed

+1646
-603
lines changed

galley/docker/Dockerfile.galley

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM scratch
1+
FROM istionightly/base_debug
22
ADD galley /usr/local/bin/
33
ENTRYPOINT ["/usr/local/bin/galley"]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain accumulator copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package processing
16+
17+
import (
18+
"istio.io/istio/galley/pkg/runtime/resource"
19+
"istio.io/istio/pkg/log"
20+
)
21+
22+
var scope = log.RegisterScope("processing", "Galley data processing", 0)
23+
24+
// Dispatcher is a Handler that can dispatch events to other Handlers, based on Collection.
25+
type Dispatcher struct {
26+
handlers map[resource.Collection][]Handler
27+
}
28+
29+
var _ Handler = &Dispatcher{}
30+
31+
// Handle implements Handler
32+
func (d *Dispatcher) Handle(e resource.Event) {
33+
handlers, found := d.handlers[e.Entry.ID.Collection]
34+
if !found {
35+
scope.Warnf("Unhandled resource event: %v", e)
36+
return
37+
}
38+
39+
for _, h := range handlers {
40+
h.Handle(e)
41+
}
42+
}
43+
44+
// DispatcherBuilder builds Dispatchers
45+
type DispatcherBuilder struct {
46+
handlers map[resource.Collection][]Handler
47+
}
48+
49+
// NewDispatcherBuilder returns a new dispatcher dispatcher
50+
func NewDispatcherBuilder() *DispatcherBuilder {
51+
return &DispatcherBuilder{
52+
handlers: make(map[resource.Collection][]Handler),
53+
}
54+
}
55+
56+
// Add a new handler for the given Collection
57+
func (d *DispatcherBuilder) Add(t resource.Collection, h Handler) {
58+
handlers := d.handlers[t]
59+
handlers = append(handlers, h)
60+
d.handlers[t] = handlers
61+
}
62+
63+
// Build a Dispatcher
64+
func (d *DispatcherBuilder) Build() *Dispatcher {
65+
r := &Dispatcher{
66+
handlers: d.handlers,
67+
}
68+
d.handlers = nil
69+
return r
70+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package processing_test
16+
17+
import (
18+
"testing"
19+
20+
"istio.io/istio/galley/pkg/runtime/processing"
21+
"istio.io/istio/galley/pkg/runtime/resource"
22+
)
23+
24+
func TestDispatcher_Handle(t *testing.T) {
25+
b := processing.NewDispatcherBuilder()
26+
27+
fh1 := &fakeHandler{}
28+
fh2 := &fakeHandler{}
29+
fh3 := &fakeHandler{}
30+
b.Add(emptyInfo.Collection, fh1)
31+
b.Add(emptyInfo.Collection, fh2)
32+
b.Add(structInfo.Collection, fh3)
33+
d := b.Build()
34+
35+
d.Handle(addRes1V1())
36+
d.Handle(addRes2V1())
37+
if len(fh1.evts) != 2 {
38+
t.Fatalf("unexpected events: %v", fh1.evts)
39+
}
40+
if len(fh2.evts) != 2 {
41+
t.Fatalf("unexpected events: %v", fh2.evts)
42+
}
43+
44+
if len(fh3.evts) != 0 {
45+
t.Fatalf("unexpected events: %v", fh3.evts)
46+
}
47+
48+
d.Handle(addRes3V1())
49+
if len(fh1.evts) != 2 {
50+
t.Fatalf("unexpected events: %v", fh1.evts)
51+
}
52+
if len(fh2.evts) != 2 {
53+
t.Fatalf("unexpected events: %v", fh2.evts)
54+
}
55+
56+
if len(fh3.evts) != 1 {
57+
t.Fatalf("unexpected events: %v", fh3.evts)
58+
}
59+
}
60+
61+
func TestDispatcher_UnhandledEvent(t *testing.T) {
62+
b := processing.NewDispatcherBuilder()
63+
d := b.Build()
64+
65+
d.Handle(addRes1V1())
66+
// no panic
67+
}
68+
69+
type fakeHandler struct {
70+
evts []resource.Event
71+
}
72+
73+
var _ processing.Handler = &fakeHandler{}
74+
75+
func (f *fakeHandler) Handle(e resource.Event) {
76+
f.evts = append(f.evts, e)
77+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain accumulator copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package processing
16+
17+
import (
18+
"istio.io/istio/galley/pkg/runtime/resource"
19+
)
20+
21+
// Handler handles an incoming resource event.
22+
type Handler interface {
23+
Handle(e resource.Event)
24+
}
25+
26+
// HandlerFromFn returns a new Handler, based on the Handler function.
27+
func HandlerFromFn(fn func(e resource.Event)) Handler {
28+
return &fnHandler{
29+
fn: fn,
30+
}
31+
}
32+
33+
var _ Handler = &fnHandler{}
34+
35+
type fnHandler struct {
36+
fn func(e resource.Event)
37+
}
38+
39+
func (h *fnHandler) Handle(e resource.Event) {
40+
h.fn(e)
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package processing_test
16+
17+
import (
18+
"reflect"
19+
"testing"
20+
21+
"istio.io/istio/galley/pkg/runtime/processing"
22+
"istio.io/istio/galley/pkg/runtime/resource"
23+
)
24+
25+
func TestHandlerFromFn(t *testing.T) {
26+
var received resource.Event
27+
h := processing.HandlerFromFn(func(e resource.Event) {
28+
received = e
29+
})
30+
31+
sent := updateRes1V2()
32+
33+
h.Handle(sent)
34+
35+
if !reflect.DeepEqual(received, sent) {
36+
t.Fatalf("Mismatch: got:%v, expected:%v", received, sent)
37+
}
38+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package processing_test
16+
17+
import (
18+
"time"
19+
20+
"github.com/gogo/protobuf/types"
21+
22+
"istio.io/istio/galley/pkg/runtime/resource"
23+
)
24+
25+
var emptyInfo resource.Info
26+
var structInfo resource.Info
27+
28+
func init() {
29+
b := resource.NewSchemaBuilder()
30+
emptyInfo = b.Register("empty", "type.googleapis.com/google.protobuf.Empty")
31+
structInfo = b.Register("struct", "type.googleapis.com/google.protobuf.Struct")
32+
_ = b.Build()
33+
}
34+
35+
func res1V1() resource.Entry {
36+
return resource.Entry{
37+
ID: resource.VersionedKey{
38+
Version: "v1",
39+
Key: resource.Key{
40+
Collection: emptyInfo.Collection,
41+
FullName: resource.FullNameFromNamespaceAndName("ns1", "res1"),
42+
},
43+
},
44+
Metadata: resource.Metadata{
45+
CreateTime: time.Unix(1, 1),
46+
},
47+
Item: &types.Empty{},
48+
}
49+
}
50+
51+
func res2V1() resource.Entry {
52+
return resource.Entry{
53+
ID: resource.VersionedKey{
54+
Version: "v1",
55+
Key: resource.Key{
56+
Collection: emptyInfo.Collection,
57+
FullName: resource.FullNameFromNamespaceAndName("ns1", "res2"),
58+
},
59+
},
60+
Metadata: resource.Metadata{
61+
CreateTime: time.Unix(2, 1),
62+
},
63+
Item: &types.Empty{},
64+
}
65+
}
66+
67+
func res3V1() resource.Entry {
68+
return resource.Entry{
69+
ID: resource.VersionedKey{
70+
Version: "v1",
71+
Key: resource.Key{
72+
Collection: structInfo.Collection,
73+
FullName: resource.FullNameFromNamespaceAndName("ns2", "res1"),
74+
},
75+
},
76+
Metadata: resource.Metadata{
77+
CreateTime: time.Unix(3, 1),
78+
},
79+
Item: &types.Empty{},
80+
}
81+
}
82+
83+
func addRes1V1() resource.Event {
84+
return resource.Event{
85+
Kind: resource.Added,
86+
Entry: res1V1(),
87+
}
88+
}
89+
90+
func addRes2V1() resource.Event {
91+
return resource.Event{
92+
Kind: resource.Added,
93+
Entry: res2V1(),
94+
}
95+
}
96+
97+
func addRes3V1() resource.Event {
98+
return resource.Event{
99+
Kind: resource.Added,
100+
Entry: res3V1(),
101+
}
102+
}
103+
104+
func updateRes1V2() resource.Event {
105+
return resource.Event{
106+
Kind: resource.Updated,
107+
Entry: resource.Entry{
108+
ID: resource.VersionedKey{
109+
Version: "v2",
110+
Key: resource.Key{
111+
Collection: emptyInfo.Collection,
112+
FullName: resource.FullNameFromNamespaceAndName("ns1", "res1"),
113+
},
114+
},
115+
Metadata: resource.Metadata{
116+
CreateTime: time.Unix(1, 2),
117+
},
118+
Item: &types.Empty{},
119+
},
120+
}
121+
}

0 commit comments

Comments
 (0)