Skip to content

Commit

Permalink
chore(conf): Refactor configuration of use case (chainloop-dev#1676)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
  • Loading branch information
javirln authored Dec 18, 2024
1 parent ce478cf commit c36f98f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
10 changes: 10 additions & 0 deletions app/controlplane/cmd/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func wireApp(*conf.Bootstrap, credentials.ReaderWriter, log.Logger, sdk.Availabl
newPolicyProviderConfig,
newNatsConnection,
auditor.NewAuditLogPublisher,
newCASServerOptions,
),
)
}
Expand All @@ -86,3 +87,12 @@ func serviceOpts(l log.Logger) []service.NewOpt {
service.WithLogger(l),
}
}

func newCASServerOptions(in *conf.Bootstrap_CASServer) *biz.CASServerDefaultOpts {
if in == nil {
return &biz.CASServerDefaultOpts{}
}
return &biz.CASServerDefaultOpts{
DefaultEntryMaxSize: in.GetDefaultEntryMaxSize(),
}
}
12 changes: 11 additions & 1 deletion app/controlplane/cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions app/controlplane/pkg/biz/casbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"time"

"code.cloudfoundry.org/bytefmt"
conf "github.com/chainloop-dev/chainloop/app/controlplane/internal/conf/controlplane/config/v1"
backend "github.com/chainloop-dev/chainloop/pkg/blobmanager"
"github.com/chainloop-dev/chainloop/pkg/blobmanager/azureblob"
"github.com/chainloop-dev/chainloop/pkg/blobmanager/oci"
Expand Down Expand Up @@ -119,13 +118,18 @@ type CASBackendUseCase struct {
MaxBytesDefault int64
}

func NewCASBackendUseCase(repo CASBackendRepo, credsRW credentials.ReaderWriter, providers backend.Providers, c *conf.Bootstrap_CASServer, l log.Logger) (*CASBackendUseCase, error) {
// CASServerDefaultOpts holds the default options for the CAS server
type CASServerDefaultOpts struct {
DefaultEntryMaxSize string
}

func NewCASBackendUseCase(repo CASBackendRepo, credsRW credentials.ReaderWriter, providers backend.Providers, c *CASServerDefaultOpts, l log.Logger) (*CASBackendUseCase, error) {
if l == nil {
l = log.NewStdLogger(io.Discard)
}

var maxBytesDefault uint64 = 100 * 1024 * 1024 // 100MB
if c.GetDefaultEntryMaxSize() != "" {
if c != nil && c.DefaultEntryMaxSize != "" {
var err error
maxBytesDefault, err = bytefmt.ToBytes(c.DefaultEntryMaxSize)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions app/controlplane/pkg/biz/casbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"testing"

conf "github.com/chainloop-dev/chainloop/app/controlplane/internal/conf/controlplane/config/v1"
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz"
bizMocks "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz/mocks"
backends "github.com/chainloop-dev/chainloop/pkg/blobmanager"
Expand Down Expand Up @@ -178,7 +177,7 @@ func (s *casBackendTestSuite) TestNewCASBackendUseCase() {

tests := []struct {
name string
config *conf.Bootstrap_CASServer
config *biz.CASServerDefaultOpts
expectError bool
errorMsg string
wantSize int64 // Expected size in bytes after parsing
Expand All @@ -191,23 +190,23 @@ func (s *casBackendTestSuite) TestNewCASBackendUseCase() {
},
{
name: "valid size - megabytes",
config: &conf.Bootstrap_CASServer{
config: &biz.CASServerDefaultOpts{
DefaultEntryMaxSize: "100MB",
},
expectError: false,
wantSize: 100 * 1024 * 1024,
},
{
name: "valid size - gigabytes",
config: &conf.Bootstrap_CASServer{
config: &biz.CASServerDefaultOpts{
DefaultEntryMaxSize: "2GB",
},
expectError: false,
wantSize: 2 * 1024 * 1024 * 1024,
},
{
name: "invalid size format",
config: &conf.Bootstrap_CASServer{
config: &biz.CASServerDefaultOpts{
DefaultEntryMaxSize: "invalid",
},
expectError: true,
Expand All @@ -216,7 +215,7 @@ func (s *casBackendTestSuite) TestNewCASBackendUseCase() {
},
{
name: "negative size",
config: &conf.Bootstrap_CASServer{
config: &biz.CASServerDefaultOpts{
DefaultEntryMaxSize: "-100MB",
},
expectError: true,
Expand All @@ -225,7 +224,7 @@ func (s *casBackendTestSuite) TestNewCASBackendUseCase() {
},
{
name: "zero size",
config: &conf.Bootstrap_CASServer{
config: &biz.CASServerDefaultOpts{
DefaultEntryMaxSize: "0",
},
expectError: true,
Expand All @@ -234,7 +233,7 @@ func (s *casBackendTestSuite) TestNewCASBackendUseCase() {
},
{
name: "missing unit",
config: &conf.Bootstrap_CASServer{
config: &biz.CASServerDefaultOpts{
DefaultEntryMaxSize: "100",
},
expectError: true,
Expand Down
6 changes: 6 additions & 0 deletions app/controlplane/pkg/biz/testhelpers/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ func NewCASBackendConfig() *conf.Bootstrap_CASServer {
}
}

func NewCASServerOptions(in *conf.Bootstrap_CASServer) *biz.CASServerDefaultOpts {
return &biz.CASServerDefaultOpts{
DefaultEntryMaxSize: in.GetDefaultEntryMaxSize(),
}
}

func NewPromSpec() []*conf.PrometheusIntegrationSpec {
return []*conf.PrometheusIntegrationSpec{}
}
Expand Down
1 change: 1 addition & 0 deletions app/controlplane/pkg/biz/testhelpers/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func WireTestData(*TestDatabase, *testing.T, log.Logger, credentials.ReaderWrite
newNatsConnection,
auditor.NewAuditLogPublisher,
NewCASBackendConfig,
NewCASServerOptions,
),
)
}
Expand Down
3 changes: 2 additions & 1 deletion app/controlplane/pkg/biz/testhelpers/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c36f98f

Please sign in to comment.