Skip to content

Commit

Permalink
Merge pull request #798 from kaleido-io/namespace
Browse files Browse the repository at this point in the history
Define config schema for namespaces.predefined
  • Loading branch information
peterbroadhurst authored May 13, 2022
2 parents 43ab040 + 55a0287 commit 3cf5f35
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/firefly.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func getOrchestrator() orchestrator.Orchestrator {
if _utOrchestrator != nil {
return _utOrchestrator
}
return orchestrator.NewOrchestrator()
return orchestrator.NewOrchestrator(true)
}

// Execute is called by the main method of the package
Expand Down
2 changes: 1 addition & 1 deletion docs/config_docs_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

func TestGenerateConfigDocs(t *testing.T) {
// Initialize config of all plugins
orchestrator.NewOrchestrator()
orchestrator.NewOrchestrator(false)
apiserver.InitConfig()
f, err := os.Create(filepath.Join("reference", "config.md"))
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions docs/config_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import (
"path/filepath"
"testing"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly/internal/apiserver"
"github.com/hyperledger/firefly/internal/orchestrator"
"github.com/hyperledger/firefly-common/pkg/config"
"github.com/stretchr/testify/assert"
)

func TestConfigDocsUpToDate(t *testing.T) {
// Initialize config of all plugins
orchestrator.NewOrchestrator()
orchestrator.NewOrchestrator(false)
apiserver.InitConfig()
generatedConfig, err := config.GenerateConfigMarkdown(context.Background(), config.GetKnownKeys())
assert.NoError(t, err)
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,13 @@ nav_order: 3
|default|The default namespace - must be in the predefined list|`string`|`<nil>`
|predefined|A list of namespaces to ensure exists, without requiring a broadcast from the network|List `string`|`<nil>`

## namespaces.predefined[]

|Key|Description|Type|Default Value|
|---|-----------|----|-------------|
|description|A description for the namespace|`string`|`<nil>`
|name|The name of the namespace (must be unique)|`string`|`<nil>`

## node

|Key|Description|Type|Default Value|
Expand Down
2 changes: 0 additions & 2 deletions internal/coreconfig/coreconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package coreconfig

import (
"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly/pkg/core"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -328,7 +327,6 @@ func setDefaults() {
viper.SetDefault(string(MessageWriterBatchTimeout), "10ms")
viper.SetDefault(string(MessageWriterCount), 5)
viper.SetDefault(string(NamespacesDefault), "default")
viper.SetDefault(string(NamespacesPredefined), fftypes.JSONObjectArray{{"name": "default", "description": "Default predefined namespace"}})
viper.SetDefault(string(OrchestratorStartupAttempts), 5)
viper.SetDefault(string(OpUpdateRetryInitDelay), "250ms")
viper.SetDefault(string(OpUpdateRetryMaxDelay), "1m")
Expand Down
6 changes: 4 additions & 2 deletions internal/coremsgs/en_config_descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ var (
ConfigMetricsReadTimeout = ffc("config.metrics.readTimeout", "The maximum time to wait when reading from an HTTP connection", i18n.TimeDurationType)
ConfigMetricsWriteTimeout = ffc("config.metrics.writeTimeout", "The maximum time to wait when writing to an HTTP connection", i18n.TimeDurationType)

ConfigNamespacesDefault = ffc("config.namespaces.default", "The default namespace - must be in the predefined list", i18n.StringType)
ConfigNamespacesPredefined = ffc("config.namespaces.predefined", "A list of namespaces to ensure exists, without requiring a broadcast from the network", "List "+i18n.StringType)
ConfigNamespacesDefault = ffc("config.namespaces.default", "The default namespace - must be in the predefined list", i18n.StringType)
ConfigNamespacesPredefined = ffc("config.namespaces.predefined", "A list of namespaces to ensure exists, without requiring a broadcast from the network", "List "+i18n.StringType)
ConfigNamespacesPredefinedName = ffc("config.namespaces.predefined[].name", "The name of the namespace (must be unique)", i18n.StringType)
ConfigNamespacesPredefinedDescription = ffc("config.namespaces.predefined[].description", "A description for the namespace", i18n.StringType)

ConfigNodeDescription = ffc("config.node.description", "The description of this FireFly node", i18n.StringType)
ConfigNodeName = ffc("config.node.name", "The name of this FireFly node", i18n.StringType)
Expand Down
33 changes: 28 additions & 5 deletions internal/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ import (
"github.com/hyperledger/firefly/pkg/tokens"
)

const (
// NamespacePredefined is the list of pre-defined namespaces
NamespacePredefined = "predefined"
// NamespaceName is a short name for a pre-defined namespace
NamespaceName = "name"
// NamespaceName is a long description for a pre-defined namespace
NamespaceDescription = "description"
)

var (
namespaceConfig = config.RootSection("namespaces")
blockchainConfig = config.RootSection("blockchain")
databaseConfig = config.RootSection("database")
identityConfig = config.RootSection("identity")
Expand Down Expand Up @@ -178,9 +188,10 @@ type orchestrator struct {
adminEvents adminevents.Manager
sharedDownload shareddownload.Manager
txHelper txcommon.Helper
predefinedNS config.ArraySection
}

func NewOrchestrator() Orchestrator {
func NewOrchestrator(withDefaults bool) Orchestrator {
or := &orchestrator{}

// Initialize the config on all the factories
Expand All @@ -192,9 +203,21 @@ func NewOrchestrator() Orchestrator {
dxfactory.InitConfig(dataexchangeConfig)
tifactory.InitConfig(tokensConfig)

or.InitNamespaceConfig(withDefaults)

return or
}

func (or *orchestrator) InitNamespaceConfig(withDefaults bool) {
or.predefinedNS = namespaceConfig.SubArray(NamespacePredefined)
or.predefinedNS.AddKnownKey(NamespaceName)
or.predefinedNS.AddKnownKey(NamespaceDescription)
if withDefaults {
namespaceConfig.AddKnownKey(NamespacePredefined+".0."+NamespaceName, "default")
namespaceConfig.AddKnownKey(NamespacePredefined+".0."+NamespaceDescription, "Default predefined namespace")
}
}

func (or *orchestrator) Init(ctx context.Context, cancelCtx context.CancelFunc) (err error) {
or.ctx = ctx
or.cancelCtx = cancelCtx
Expand Down Expand Up @@ -599,9 +622,8 @@ func (or *orchestrator) initComponents(ctx context.Context) (err error) {
return nil
}

func (or *orchestrator) getPrefdefinedNamespaces(ctx context.Context) ([]*core.Namespace, error) {
func (or *orchestrator) getPredefinedNamespaces(ctx context.Context) ([]*core.Namespace, error) {
defaultNS := config.GetString(coreconfig.NamespacesDefault)
predefined := config.GetObjectArray(coreconfig.NamespacesPredefined)
namespaces := []*core.Namespace{
{
Name: core.SystemNamespace,
Expand All @@ -610,7 +632,8 @@ func (or *orchestrator) getPrefdefinedNamespaces(ctx context.Context) ([]*core.N
},
}
foundDefault := false
for i, nsObject := range predefined {
for i := 0; i < or.predefinedNS.ArraySize(); i++ {
nsObject := or.predefinedNS.ArrayEntry(i)
name := nsObject.GetString("name")
err := core.ValidateFFNameField(ctx, name, fmt.Sprintf("namespaces.predefined[%d].name", i))
if err != nil {
Expand Down Expand Up @@ -640,7 +663,7 @@ func (or *orchestrator) getPrefdefinedNamespaces(ctx context.Context) ([]*core.N
}

func (or *orchestrator) initNamespaces(ctx context.Context) error {
predefined, err := or.getPrefdefinedNamespaces(ctx)
predefined, err := or.getPredefinedNamespaces(ctx)
if err != nil {
return err
}
Expand Down
13 changes: 6 additions & 7 deletions internal/orchestrator/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ func newTestOrchestrator() *testOrchestrator {
tor.mti.On("Name").Return("mock-tk").Maybe()
tor.mcm.On("Name").Return("mock-cm").Maybe()
tor.mmi.On("Name").Return("mock-mm").Maybe()
tor.orchestrator.InitNamespaceConfig(true)
return tor
}

func TestNewOrchestrator(t *testing.T) {
or := NewOrchestrator()
or := NewOrchestrator(true)
assert.NotNil(t, or)
}

Expand Down Expand Up @@ -708,13 +709,11 @@ func TestInitNamespacesDefaultMissing(t *testing.T) {

func TestInitNamespacesDupName(t *testing.T) {
or := newTestOrchestrator()
config.Set(coreconfig.NamespacesPredefined, fftypes.JSONObjectArray{
{"name": "ns1"},
{"name": "ns2"},
{"name": "ns2"},
})
namespaceConfig.AddKnownKey("predefined.0.name", "ns1")
namespaceConfig.AddKnownKey("predefined.1.name", "ns2")
namespaceConfig.AddKnownKey("predefined.2.name", "ns2")
config.Set(coreconfig.NamespacesDefault, "ns1")
nsList, err := or.getPrefdefinedNamespaces(context.Background())
nsList, err := or.getPredefinedNamespaces(context.Background())
assert.NoError(t, err)
assert.Len(t, nsList, 3)
assert.Equal(t, core.SystemNamespace, nsList[0].Name)
Expand Down

0 comments on commit 3cf5f35

Please sign in to comment.