Skip to content

Commit

Permalink
Merge pull request #5785 from oasisprotocol/kostko/feature/detached-c…
Browse files Browse the repository at this point in the history
…omps-autoenable

Fix handling of detached bundles
  • Loading branch information
kostko committed Jul 23, 2024
2 parents c2b4d72 + bfdf208 commit ca0105a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
1 change: 1 addition & 0 deletions .changelog/5785.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/runtime/bundle: Use manifest hash at bundle load time
5 changes: 5 additions & 0 deletions .changelog/5785.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go/runtime: Automatically enable all configured detached components

Since they are explicitly configured there should be no need to enable
them twice. This just defaults detached components to be enabled and one
needs to explicitly disable them.
16 changes: 12 additions & 4 deletions go/runtime/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
type Bundle struct {
Manifest *Manifest
Data map[string][]byte

// manifestHash is the original manifest hash of the bundle at time the bundle was loaded.
manifestHash hash.Hash
}

// Validate validates the runtime bundle for well-formedness.
Expand Down Expand Up @@ -277,6 +280,9 @@ func (bnd *Bundle) Write(fn string) error {
return fmt.Errorf("runtime/bundle: failed to write bundle: %w", err)
}

// Update the manifest hash.
bnd.manifestHash = bnd.Manifest.Hash()

return nil
}

Expand All @@ -298,12 +304,12 @@ func (bnd *Bundle) ExplodedPath(dataDir, fn string) string {
case false:
// DATADIR/runtimes/bundles/manifestHash
subDir = filepath.Join(ExplodedPath(dataDir),
bnd.Manifest.Hash().String(),
bnd.manifestHash.String(),
)
case true:
// DATADIR/runtimes/bundles/detached/manifestHash
subDir = filepath.Join(DetachedExplodedPath(dataDir),
bnd.Manifest.Hash().String(),
bnd.manifestHash.String(),
)
}

Expand Down Expand Up @@ -378,6 +384,7 @@ func (bnd *Bundle) WriteExploded(dataDir string) error {
func (bnd *Bundle) Close() error {
bnd.Manifest = nil
bnd.Data = nil
bnd.manifestHash.Empty()
return nil
}

Expand Down Expand Up @@ -436,8 +443,9 @@ func Open(fn string) (*Bundle, error) {

// Ensure the bundle is well-formed.
bnd := &Bundle{
Manifest: &manifest,
Data: data,
Manifest: &manifest,
Data: data,
manifestHash: manifest.Hash(),
}
if err = bnd.Validate(); err != nil {
return nil, err
Expand Down
56 changes: 56 additions & 0 deletions go/runtime/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package config

import (
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

"github.com/oasisprotocol/oasis-core/go/runtime/bundle/component"
)

func TestComponentConfig(t *testing.T) {
require := require.New(t)

cfg := Config{
Components: []ComponentConfig{
{
ID: component.ID{Kind: component.ROFL, Name: "foo-test"},
Disabled: false,
},
},
}

compCfg, ok := cfg.GetComponent(component.ID{Kind: component.ROFL, Name: "foo-test"})
require.True(ok)
require.EqualValues(compCfg.ID.Kind, component.ROFL)
require.EqualValues(compCfg.ID.Name, "foo-test")
require.False(compCfg.Disabled)

compCfg, ok = cfg.GetComponent(component.ID{Kind: component.ROFL, Name: "does-not-exist"})
require.False(ok)
require.EqualValues(compCfg, ComponentConfig{})

// Deserialization.
yamlCfg := `
components:
- rofl.foo-test
- id: rofl.another
disabled: true
`
var decCfg Config
err := yaml.Unmarshal([]byte(yamlCfg), &decCfg)
require.NoError(err, "yaml.Unmarshal")

compCfg, ok = decCfg.GetComponent(component.ID{Kind: component.ROFL, Name: "foo-test"})
require.True(ok)
require.EqualValues(compCfg.ID.Kind, component.ROFL)
require.EqualValues(compCfg.ID.Name, "foo-test")
require.False(compCfg.Disabled)

compCfg, ok = decCfg.GetComponent(component.ID{Kind: component.ROFL, Name: "another"})
require.True(ok)
require.EqualValues(compCfg.ID.Kind, component.ROFL)
require.EqualValues(compCfg.ID.Name, "another")
require.True(compCfg.Disabled)
}
9 changes: 7 additions & 2 deletions go/runtime/registry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ func newConfig( //nolint: gocyclo
}

rtBnd := &runtimeHost.RuntimeBundle{
Bundle: bnd,
ExplodedDataDir: dataDir,
Bundle: bnd,
ExplodedDataDir: dataDir,
ExplodedDetachedDirs: make(map[component.ID]string),
}

// Merge in detached components.
Expand Down Expand Up @@ -321,6 +322,10 @@ func newConfig( //nolint: gocyclo
if config.GlobalConfig.Mode != config.ModeCompute {
enabled = false
}
// Detached components are explicit and they should be enabled by default.
if _, ok := rtBnd.ExplodedDetachedDirs[comp.ID()]; ok {
enabled = true
}

// Check for any overrides in the node configuration.
compCfg, ok := config.GlobalConfig.Runtime.GetComponent(comp.ID())
Expand Down

0 comments on commit ca0105a

Please sign in to comment.