mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-13 13:14:05 +00:00
All types stored in etcd are now API objects.
This means I made an api.ContainerManifestList, and added a JSONBase to endpoints (and changed Name -> JSONBase.ID).
This commit is contained in:
@@ -127,19 +127,19 @@ func (s *endpointsStore) Merge(source string, change interface{}) error {
|
||||
case ADD:
|
||||
glog.Infof("Adding new endpoint from source %s : %v", source, update.Endpoints)
|
||||
for _, value := range update.Endpoints {
|
||||
endpoints[value.Name] = value
|
||||
endpoints[value.ID] = value
|
||||
}
|
||||
case REMOVE:
|
||||
glog.Infof("Removing an endpoint %v", update)
|
||||
for _, value := range update.Endpoints {
|
||||
delete(endpoints, value.Name)
|
||||
delete(endpoints, value.ID)
|
||||
}
|
||||
case SET:
|
||||
glog.Infof("Setting endpoints %v", update)
|
||||
// Clear the old map entries by just creating a new map
|
||||
endpoints = make(map[string]api.Endpoints)
|
||||
for _, value := range update.Endpoints {
|
||||
endpoints[value.Name] = value
|
||||
endpoints[value.ID] = value
|
||||
}
|
||||
default:
|
||||
glog.Infof("Received invalid update type: %v", update)
|
||||
|
@@ -83,7 +83,7 @@ func (s sortedEndpoints) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
func (s sortedEndpoints) Less(i, j int) bool {
|
||||
return s[i].Name < s[j].Name
|
||||
return s[i].ID < s[j].ID
|
||||
}
|
||||
|
||||
type EndpointsHandlerMock struct {
|
||||
@@ -216,8 +216,14 @@ func TestNewMultipleSourcesEndpointsMultipleHandlersAddedAndNotified(t *testing.
|
||||
handler2 := NewEndpointsHandlerMock()
|
||||
config.RegisterHandler(handler)
|
||||
config.RegisterHandler(handler2)
|
||||
endpointsUpdate1 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "foo", Endpoints: []string{"endpoint1", "endpoint2"}})
|
||||
endpointsUpdate2 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "bar", Endpoints: []string{"endpoint3", "endpoint4"}})
|
||||
endpointsUpdate1 := CreateEndpointsUpdate(ADD, api.Endpoints{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
Endpoints: []string{"endpoint1", "endpoint2"},
|
||||
})
|
||||
endpointsUpdate2 := CreateEndpointsUpdate(ADD, api.Endpoints{
|
||||
JSONBase: api.JSONBase{ID: "bar"},
|
||||
Endpoints: []string{"endpoint3", "endpoint4"},
|
||||
})
|
||||
handler.Wait(2)
|
||||
handler2.Wait(2)
|
||||
channelOne <- endpointsUpdate1
|
||||
@@ -236,8 +242,14 @@ func TestNewMultipleSourcesEndpointsMultipleHandlersAddRemoveSetAndNotified(t *t
|
||||
handler2 := NewEndpointsHandlerMock()
|
||||
config.RegisterHandler(handler)
|
||||
config.RegisterHandler(handler2)
|
||||
endpointsUpdate1 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "foo", Endpoints: []string{"endpoint1", "endpoint2"}})
|
||||
endpointsUpdate2 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "bar", Endpoints: []string{"endpoint3", "endpoint4"}})
|
||||
endpointsUpdate1 := CreateEndpointsUpdate(ADD, api.Endpoints{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
Endpoints: []string{"endpoint1", "endpoint2"},
|
||||
})
|
||||
endpointsUpdate2 := CreateEndpointsUpdate(ADD, api.Endpoints{
|
||||
JSONBase: api.JSONBase{ID: "bar"},
|
||||
Endpoints: []string{"endpoint3", "endpoint4"},
|
||||
})
|
||||
handler.Wait(2)
|
||||
handler2.Wait(2)
|
||||
channelOne <- endpointsUpdate1
|
||||
@@ -248,7 +260,10 @@ func TestNewMultipleSourcesEndpointsMultipleHandlersAddRemoveSetAndNotified(t *t
|
||||
handler2.ValidateEndpoints(t, endpoints)
|
||||
|
||||
// Add one more
|
||||
endpointsUpdate3 := CreateEndpointsUpdate(ADD, api.Endpoints{Name: "foobar", Endpoints: []string{"endpoint5", "endpoint6"}})
|
||||
endpointsUpdate3 := CreateEndpointsUpdate(ADD, api.Endpoints{
|
||||
JSONBase: api.JSONBase{ID: "foobar"},
|
||||
Endpoints: []string{"endpoint5", "endpoint6"},
|
||||
})
|
||||
handler.Wait(1)
|
||||
handler2.Wait(1)
|
||||
channelTwo <- endpointsUpdate3
|
||||
@@ -257,7 +272,10 @@ func TestNewMultipleSourcesEndpointsMultipleHandlersAddRemoveSetAndNotified(t *t
|
||||
handler2.ValidateEndpoints(t, endpoints)
|
||||
|
||||
// Update the "foo" service with new endpoints
|
||||
endpointsUpdate1 = CreateEndpointsUpdate(ADD, api.Endpoints{Name: "foo", Endpoints: []string{"endpoint77"}})
|
||||
endpointsUpdate1 = CreateEndpointsUpdate(ADD, api.Endpoints{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
Endpoints: []string{"endpoint77"},
|
||||
})
|
||||
handler.Wait(1)
|
||||
handler2.Wait(1)
|
||||
channelOne <- endpointsUpdate1
|
||||
@@ -266,7 +284,7 @@ func TestNewMultipleSourcesEndpointsMultipleHandlersAddRemoveSetAndNotified(t *t
|
||||
handler2.ValidateEndpoints(t, endpoints)
|
||||
|
||||
// Remove "bar" service
|
||||
endpointsUpdate2 = CreateEndpointsUpdate(REMOVE, api.Endpoints{Name: "bar"})
|
||||
endpointsUpdate2 = CreateEndpointsUpdate(REMOVE, api.Endpoints{JSONBase: api.JSONBase{ID: "bar"}})
|
||||
handler.Wait(1)
|
||||
handler2.Wait(1)
|
||||
channelTwo <- endpointsUpdate2
|
||||
|
@@ -34,7 +34,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -127,7 +126,7 @@ func (s ConfigSourceEtcd) GetServices() ([]api.Service, []api.Endpoints, error)
|
||||
// and create a Service entry for it.
|
||||
for i, node := range response.Node.Nodes {
|
||||
var svc api.Service
|
||||
err = json.Unmarshal([]byte(node.Value), &svc)
|
||||
err = api.DecodeInto([]byte(node.Value), &svc)
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to load Service: %s (%#v)", node.Value, err)
|
||||
continue
|
||||
@@ -154,7 +153,9 @@ func (s ConfigSourceEtcd) GetEndpoints(service string) (api.Endpoints, error) {
|
||||
return api.Endpoints{}, err
|
||||
}
|
||||
// Parse all the endpoint specifications in this value.
|
||||
return parseEndpoints(response.Node.Value)
|
||||
var e api.Endpoints
|
||||
err = api.DecodeInto([]byte(response.Node.Value), &e)
|
||||
return e, err
|
||||
}
|
||||
|
||||
// etcdResponseToService takes an etcd response and pulls it apart to find service.
|
||||
@@ -163,19 +164,13 @@ func etcdResponseToService(response *etcd.Response) (*api.Service, error) {
|
||||
return nil, fmt.Errorf("invalid response from etcd: %#v", response)
|
||||
}
|
||||
var svc api.Service
|
||||
err := json.Unmarshal([]byte(response.Node.Value), &svc)
|
||||
err := api.DecodeInto([]byte(response.Node.Value), &svc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &svc, err
|
||||
}
|
||||
|
||||
func parseEndpoints(jsonString string) (api.Endpoints, error) {
|
||||
var e api.Endpoints
|
||||
err := json.Unmarshal([]byte(jsonString), &e)
|
||||
return e, err
|
||||
}
|
||||
|
||||
func (s ConfigSourceEtcd) WatchForChanges() {
|
||||
glog.Info("Setting up a watch for new services")
|
||||
watchChannel := make(chan *etcd.Response)
|
||||
@@ -220,7 +215,7 @@ func (s ConfigSourceEtcd) ProcessChange(response *etcd.Response) {
|
||||
func (s ConfigSourceEtcd) ProcessEndpointResponse(response *etcd.Response) {
|
||||
glog.Infof("Processing a change in endpoint configuration... %s", *response)
|
||||
var endpoints api.Endpoints
|
||||
err := json.Unmarshal([]byte(response.Node.Value), &endpoints)
|
||||
err := api.DecodeInto([]byte(response.Node.Value), &endpoints)
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to parse service out of etcd key: %v : %+v", response.Node.Value, err)
|
||||
return
|
||||
|
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. 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 config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
const TomcatContainerEtcdKey = "/registry/services/tomcat/endpoints/tomcat-3bd5af34"
|
||||
const TomcatService = "tomcat"
|
||||
const TomcatContainerID = "tomcat-3bd5af34"
|
||||
|
||||
func validateJSONParsing(t *testing.T, jsonString string, expectedEndpoints api.Endpoints, expectError bool) {
|
||||
endpoints, err := parseEndpoints(jsonString)
|
||||
if err == nil && expectError {
|
||||
t.Errorf("validateJSONParsing did not get expected error when parsing %s", jsonString)
|
||||
}
|
||||
if err != nil && !expectError {
|
||||
t.Errorf("validateJSONParsing got unexpected error %+v when parsing %s", err, jsonString)
|
||||
}
|
||||
if !reflect.DeepEqual(expectedEndpoints, endpoints) {
|
||||
t.Errorf("Didn't get expected endpoints %+v got: %+v", expectedEndpoints, endpoints)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseJsonEndpoints(t *testing.T) {
|
||||
validateJSONParsing(t, "", api.Endpoints{}, true)
|
||||
endpoints := api.Endpoints{
|
||||
Name: "foo",
|
||||
Endpoints: []string{"foo", "bar", "baz"},
|
||||
}
|
||||
data, err := json.Marshal(endpoints)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %#v", err)
|
||||
}
|
||||
validateJSONParsing(t, string(data), endpoints, false)
|
||||
// validateJSONParsing(t, "[{\"port\":8000,\"name\":\"mysql\",\"machine\":\"foo\"},{\"port\":9000,\"name\":\"mysql\",\"machine\":\"bar\"}]", []string{"foo:8000", "bar:9000"}, false)
|
||||
}
|
@@ -102,7 +102,7 @@ func (s ConfigSourceFile) Run() {
|
||||
newEndpoints := make([]api.Endpoints, len(config.Services))
|
||||
for i, service := range config.Services {
|
||||
newServices[i] = api.Service{JSONBase: api.JSONBase{ID: service.Name}, Port: service.Port}
|
||||
newEndpoints[i] = api.Endpoints{Name: service.Name, Endpoints: service.Endpoints}
|
||||
newEndpoints[i] = api.Endpoints{JSONBase: api.JSONBase{ID: service.Name}, Endpoints: service.Endpoints}
|
||||
}
|
||||
if !reflect.DeepEqual(lastServices, newServices) {
|
||||
serviceUpdate := ServiceUpdate{Op: SET, Services: newServices}
|
||||
|
Reference in New Issue
Block a user