-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
634 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Enhancement: Named Service Registration | ||
|
||
move away from hardcoding service IP addresses and rely upon name resolution instead. It delegates the address lookup to a static in-memory service registry, which can be re-implemented in multiple forms. | ||
|
||
https://github.com/cs3org/reva/pull/1509 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// 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. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package registry | ||
|
||
import ( | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
// Config configures a registry | ||
type Config struct { | ||
Services map[string]map[string]*service `mapstructure:"services"` | ||
} | ||
|
||
// service implements the Service interface. Attributes are exported so that mapstructure can unmarshal values onto them. | ||
type service struct { | ||
Name string `mapstructure:"name"` | ||
Nodes []node `mapstructure:"nodes"` | ||
} | ||
|
||
type node struct { | ||
Address string `mapstructure:"address"` | ||
Metadata map[string]string `mapstructure:"metadata"` | ||
} | ||
|
||
// ParseConfig translates Config file values into a Config struct for consumers. | ||
func ParseConfig(m map[string]interface{}) (*Config, error) { | ||
c := &Config{} | ||
if err := mapstructure.Decode(m, c); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(c.Services) == 0 { | ||
c.Services = make(map[string]map[string]*service) | ||
} | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// 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. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package registry | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
/* | ||
config example: | ||
--- | ||
services: | ||
authprovider: | ||
basic: | ||
name: auth-basic | ||
nodes: | ||
- address: 0.0.0.0:1234 | ||
metadata: | ||
version: v0.1.0 | ||
bearer: | ||
name: auth-bearer | ||
nodes: | ||
- address: 0.0.0.0:5678 | ||
metadata: | ||
version: v0.1.0 | ||
*/ | ||
func TestParseConfig(t *testing.T) { | ||
type args struct { | ||
m map[string]interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *Config | ||
wantErr bool | ||
}{ | ||
{name: "parse config", args: args{map[string]interface{}{ | ||
"services": map[string]map[string]interface{}{ | ||
"authprovider": map[string]interface{}{ | ||
"basic": map[string]interface{}{ | ||
"name": "auth-basic", | ||
"nodes": []map[string]interface{}{ | ||
{ | ||
"address": "0.0.0.0:1234", | ||
"metadata": map[string]string{"version": "v0.1.0"}, | ||
}, | ||
}, | ||
}, | ||
"bearer": map[string]interface{}{ | ||
"name": "auth-bearer", | ||
"nodes": []map[string]interface{}{ | ||
{ | ||
"address": "0.0.0.0:5678", | ||
"metadata": map[string]string{"version": "v0.1.0"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}}, want: &Config{ | ||
Services: map[string]map[string]*service{ | ||
"authprovider": map[string]*service{ | ||
"basic": &service{ | ||
Name: "auth-basic", | ||
Nodes: []node{{ | ||
Address: "0.0.0.0:1234", | ||
Metadata: map[string]string{"version": "v0.1.0"}, | ||
}}, | ||
}, | ||
"bearer": &service{ | ||
Name: "auth-bearer", | ||
Nodes: []node{{ | ||
Address: "0.0.0.0:5678", | ||
Metadata: map[string]string{"version": "v0.1.0"}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
}, wantErr: false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ParseConfig(tt.args.m) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ParseConfig() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ParseConfig() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// 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. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package memory | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/cs3org/reva/pkg/registry" | ||
) | ||
|
||
// Registry implements the Registry interface. | ||
type Registry struct { | ||
// m protects async access to the services map. | ||
sync.Mutex | ||
// services map a service name with a set of nodes. | ||
services map[string]registry.Service | ||
} | ||
|
||
// Add implements the Registry interface. If the service is already known in this registry it will only update the nodes. | ||
func (r *Registry) Add(svc registry.Service) error { | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
// append the nodes if the service is already registered. | ||
if _, ok := r.services[svc.Name()]; ok { | ||
s := service{ | ||
name: svc.Name(), | ||
nodes: make([]node, 0), | ||
} | ||
|
||
s.mergeNodes(svc.Nodes(), r.services[svc.Name()].Nodes()) | ||
|
||
r.services[svc.Name()] = s | ||
return nil | ||
} | ||
|
||
r.services[svc.Name()] = svc | ||
return nil | ||
} | ||
|
||
// GetService implements the Registry interface. There is currently no load balance being done, but it should not be | ||
// hard to add. | ||
func (r *Registry) GetService(name string) (registry.Service, error) { | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
if service, ok := r.services[name]; ok { | ||
return service, nil | ||
} | ||
|
||
return nil, fmt.Errorf("service %v not found", name) | ||
} | ||
|
||
// New returns an implementation of the Registry interface. | ||
func New(m map[string]interface{}) registry.Registry { | ||
// c, err := registry.ParseConfig(m) | ||
// if err != nil { | ||
// return nil | ||
// } | ||
|
||
return &Registry{ | ||
services: map[string]registry.Service{}, | ||
} | ||
} |
Oops, something went wrong.