Skip to content

Commit

Permalink
[KS-370] Add ConfigForCapability to CapabilitiesRegistry (#640)
Browse files Browse the repository at this point in the history
* [KS-370] Add ConfigForCapability to CapabilityRegistry

* Address feedback: use durationpb rather than uint32

* Rename ExecuteConfig -> DefaultConfig

* Add IsLocal to capabilityInfo

* Add IsLocal to CapabilityInfo

* Rename GetLocalNode -> LocalNode

* Fix MustNewCapabilityInfo

* Pass through IsLocal to GRPC call
  • Loading branch information
cedric-cordenier authored Jul 23, 2024
1 parent 17a92dc commit e407ecd
Show file tree
Hide file tree
Showing 15 changed files with 1,132 additions and 382 deletions.
79 changes: 64 additions & 15 deletions pkg/capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ type CapabilityInfo struct {
CapabilityType CapabilityType
Description string
DON *DON
IsLocal bool
}

// Parse out the version from the ID.
Expand Down Expand Up @@ -250,24 +251,12 @@ const (
idMaxLength = 128
)

// NewCapabilityInfo returns a new CapabilityInfo.
func NewCapabilityInfo(
id string,
capabilityType CapabilityType,
description string,
) (CapabilityInfo, error) {
return NewRemoteCapabilityInfo(id, capabilityType, description, nil)
}

// NewRemoteCapabilityInfo returns a new CapabilityInfo for remote capabilities.
// This is largely intended for internal use by the registry syncer.
// Capability developers should use `NewCapabilityInfo` instead as this
// omits the requirement to pass in the DON Info.
func NewRemoteCapabilityInfo(
func newCapabilityInfo(
id string,
capabilityType CapabilityType,
description string,
don *DON,
isLocal bool,
) (CapabilityInfo, error) {
if len(id) > idMaxLength {
return CapabilityInfo{}, fmt.Errorf("invalid id: %s exceeds max length %d", id, idMaxLength)
Expand All @@ -285,17 +274,45 @@ func NewRemoteCapabilityInfo(
CapabilityType: capabilityType,
Description: description,
DON: don,
IsLocal: isLocal,
}, nil
}

// NewCapabilityInfo returns a new CapabilityInfo.
func NewCapabilityInfo(
id string,
capabilityType CapabilityType,
description string,
) (CapabilityInfo, error) {
return newCapabilityInfo(id, capabilityType, description, nil, true)
}

// NewRemoteCapabilityInfo returns a new CapabilityInfo for remote capabilities.
// This is largely intended for internal use by the registry syncer.
// Capability developers should use `NewCapabilityInfo` instead as this
// omits the requirement to pass in the DON Info.
func NewRemoteCapabilityInfo(
id string,
capabilityType CapabilityType,
description string,
don *DON,
) (CapabilityInfo, error) {
return newCapabilityInfo(id, capabilityType, description, don, false)
}

// MustNewCapabilityInfo returns a new CapabilityInfo,
// `panic`ing if we could not instantiate a CapabilityInfo.
func MustNewCapabilityInfo(
id string,
capabilityType CapabilityType,
description string,
) CapabilityInfo {
return MustNewRemoteCapabilityInfo(id, capabilityType, description, nil)
c, err := NewCapabilityInfo(id, capabilityType, description)
if err != nil {
panic(err)
}

return c
}

// MustNewRemoteCapabilityInfo returns a new CapabilityInfo,
Expand Down Expand Up @@ -367,3 +384,35 @@ outerLoop:

return &values.List{Underlying: vs}, nil
}

const (
DefaultRegistrationRefresh = 30 * time.Second
DefaultRegistrationExpiry = 2 * time.Minute
DefaultMessageExpiry = 2 * time.Minute
)

type RemoteTriggerConfig struct {
RegistrationRefresh time.Duration
RegistrationExpiry time.Duration
MinResponsesToAggregate uint32
MessageExpiry time.Duration
}

// NOTE: consider splitting this config into values stored in Registry (KS-118)
// and values defined locally by Capability owners.
func (c *RemoteTriggerConfig) ApplyDefaults() {
if c.RegistrationRefresh == 0 {
c.RegistrationRefresh = DefaultRegistrationRefresh
}
if c.RegistrationExpiry == 0 {
c.RegistrationExpiry = DefaultRegistrationExpiry
}
if c.MessageExpiry == 0 {
c.MessageExpiry = DefaultMessageExpiry
}
}

type CapabilityConfiguration struct {
DefaultConfig *values.Map
RemoteTriggerConfig RemoteTriggerConfig
}
279 changes: 144 additions & 135 deletions pkg/capabilities/pb/capabilities.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/capabilities/pb/capabilities.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ message CapabilityInfoReply {
CapabilityType capability_type = 2;
string description = 3;
string version = 4;
bool isLocal = 5;
}

message RequestMetadata {
Expand Down
1 change: 1 addition & 0 deletions pkg/capabilities/pb/generate.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//go:generate protoc --go_out=../../ --go_opt=paths=source_relative --go-grpc_out=../../ --go-grpc_opt=paths=source_relative --proto_path=../../ capabilities/pb/capabilities.proto values/pb/values.proto
//go:generate protoc --go_out=../../ --go_opt=paths=source_relative --go-grpc_out=../../ --go-grpc_opt=paths=source_relative --proto_path=../../ capabilities/pb/registry.proto values/pb/values.proto
package pb
Loading

0 comments on commit e407ecd

Please sign in to comment.