Skip to content

Commit

Permalink
etcd3/store: watcher implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
hongchaodeng committed Apr 18, 2016
1 parent e81663c commit e18b4e6
Show file tree
Hide file tree
Showing 4 changed files with 601 additions and 3 deletions.
50 changes: 50 additions & 0 deletions pkg/storage/etcd3/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 etcd3

import (
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/storage/storagepb"
)

type event struct {
key string
value []byte
rev int64
isDeleted bool
isCreated bool
}

func parseKV(kv *storagepb.KeyValue) *event {
return &event{
key: string(kv.Key),
value: kv.Value,
rev: kv.ModRevision,
isDeleted: false,
isCreated: kv.ModRevision == kv.CreateRevision,
}
}

func parseEvent(e *clientv3.Event) *event {
return &event{
key: string(e.Kv.Key),
value: e.Kv.Value,
rev: e.Kv.ModRevision,
isDeleted: e.Type == clientv3.EventTypeDelete,
isCreated: e.IsCreate(),
}
}
18 changes: 15 additions & 3 deletions pkg/storage/etcd3/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type store struct {
codec runtime.Codec
versioner storage.Versioner
pathPrefix string
watcher *watcher
}

type elemForDecode struct {
Expand All @@ -57,11 +58,13 @@ type objState struct {
}

func newStore(c *clientv3.Client, codec runtime.Codec, prefix string) *store {
versioner := etcd.APIObjectVersioner{}
return &store{
client: c,
versioner: etcd.APIObjectVersioner{},
versioner: versioner,
codec: codec,
pathPrefix: prefix,
watcher: newWatcher(c, codec, versioner),
}
}

Expand Down Expand Up @@ -315,12 +318,21 @@ func (s *store) List(ctx context.Context, key, resourceVersion string, filter st

// Watch implements storage.Interface.Watch.
func (s *store) Watch(ctx context.Context, key string, resourceVersion string, filter storage.FilterFunc) (watch.Interface, error) {
panic("TODO: unimplemented")
return s.watch(ctx, key, resourceVersion, filter, false)
}

// WatchList implements storage.Interface.WatchList.
func (s *store) WatchList(ctx context.Context, key string, resourceVersion string, filter storage.FilterFunc) (watch.Interface, error) {
panic("TODO: unimplemented")
return s.watch(ctx, key, resourceVersion, filter, true)
}

func (s *store) watch(ctx context.Context, key string, rv string, filter storage.FilterFunc, recursive bool) (watch.Interface, error) {
rev, err := storage.ParseWatchResourceVersion(rv)
if err != nil {
return nil, err
}
key = keyWithPrefix(s.pathPrefix, key)
return s.watcher.Watch(ctx, key, int64(rev), recursive, filter)
}

func (s *store) getState(getResp *clientv3.GetResponse, key string, v reflect.Value, ignoreNotFound bool) (*objState, error) {
Expand Down
Loading

1 comment on commit e18b4e6

@k8s-teamcity-mesosphere

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity OSS :: Kubernetes Mesos :: 4 - Smoke Tests Build 21684 outcome was FAILURE
Summary: Exit code 1 (new) Build time: 00:47:15

Please sign in to comment.