Skip to content

Commit

Permalink
Restructure ML Storage CR
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-vanyasin committed Nov 27, 2023
1 parent 391840c commit 923d4e2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
25 changes: 18 additions & 7 deletions pkg/apis/ml/v1alpha1/storage_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,33 @@ import (
)

type ArangoMLStorageSpec struct {
Mode *ArangoMLStorageSpecMode `json:"mode,omitempty"`
// Mode defines how storage implementation should be deployed
Mode *ArangoMLStorageSpecMode `json:"mode,omitempty"`
// Backed defines how storage is implemented
Backend *ArangoMLStorageSpecBackend `json:"backend,omitempty"`
}

func (s *ArangoMLStorageSpec) GetMode() *ArangoMLStorageSpecMode {
if s == nil || s.Mode == nil {
return &ArangoMLStorageSpecMode{}
}
return s.Mode
}

func (s *ArangoMLStorageSpec) GetBackend() *ArangoMLStorageSpecBackend {
if s == nil || s.Backend == nil {
return &ArangoMLStorageSpecBackend{}
}
return s.Backend
}

func (s *ArangoMLStorageSpec) Validate() error {
if s == nil {
s = &ArangoMLStorageSpec{}
}

if err := shared.WithErrors(
shared.PrefixResourceError("backend", s.Backend.Validate()),
); err != nil {
return err
}

if err := shared.WithErrors(shared.PrefixResourceErrors("spec",
shared.PrefixResourceError("backend", s.Backend.Validate()),
shared.PrefixResourceError("mode", s.Mode.Validate()),
)); err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/ml/v1alpha1/storage_spec_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ import (
)

type ArangoMLStorageSpecBackend struct {
// S3 backend implements storage as a proxy to the provided S3 API endpoint
S3 *ArangoMLStorageSpecBackendS3 `json:"s3,omitempty"`
}

func (s *ArangoMLStorageSpecBackend) GetS3() *ArangoMLStorageSpecBackendS3 {
if s == nil || s.S3 == nil {
return &ArangoMLStorageSpecBackendS3{}
}
return s.S3
}

func (s *ArangoMLStorageSpecBackend) Validate() error {
if s == nil {
return errors.Newf("Backend is not specified")
Expand Down
25 changes: 14 additions & 11 deletions pkg/apis/ml/v1alpha1/storage_spec_backend_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package v1alpha1
import (
"net/url"

"github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)

Expand Down Expand Up @@ -55,61 +56,63 @@ func (s *ArangoMLStorageSpecBackendS3) Validate() error {
s = &ArangoMLStorageSpecBackendS3{}
}

var errs []error

if s.GetBucketName() == "" {
return errors.New("bucketName must be not empty")
errs = append(errs, errors.New("bucketName must be not empty"))
}

if s.GetEndpoint() == "" {
return errors.New("endpoint must be not empty")
errs = append(errs, errors.New("endpoint must be not empty"))
}

if _, err := url.Parse(s.GetEndpoint()); err != nil {
return errors.Newf("invalid endpoint URL was provided: %s", err.Error())
errs = append(errs, errors.Newf("invalid endpoint URL was provided: %s", err.Error()))
}

if s.GetCredentialsSecretName() == "" {
return errors.New("credentialsSecretName must be not empty")
errs = append(errs, errors.New("credentialsSecretName must be not empty"))
}
return nil
return shared.WithErrors(errs...)
}

func (s *ArangoMLStorageSpecBackendS3) GetEndpoint() string {
if s.Endpoint == nil {
if s == nil || s.Endpoint == nil {
return ""
}
return *s.Endpoint
}

func (s *ArangoMLStorageSpecBackendS3) GetBucketName() string {
if s.BucketName == nil {
if s == nil || s.BucketName == nil {
return ""
}
return *s.BucketName
}

func (s *ArangoMLStorageSpecBackendS3) GetCredentialsSecretName() string {
if s.CredentialsSecretName == nil {
if s == nil || s.CredentialsSecretName == nil {
return ""
}
return *s.CredentialsSecretName
}

func (s *ArangoMLStorageSpecBackendS3) GetAllowInsecure() bool {
if s.AllowInsecure == nil {
if s == nil || s.AllowInsecure == nil {
return false
}
return *s.AllowInsecure
}

func (s *ArangoMLStorageSpecBackendS3) GetCASecretName() string {
if s.CASecretName == nil {
if s == nil || s.CASecretName == nil {
return ""
}
return *s.CASecretName
}

func (s *ArangoMLStorageSpecBackendS3) GetRegion() string {
if s.Region == nil {
if s == nil || s.Region == nil {
return ""
}
return *s.Region
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/ml/v1alpha1/storage_spec_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ import (
)

type ArangoMLStorageSpecMode struct {
// Sidecar mode runs the storage implementation as a sidecar
Sidecar *ArangoMLStorageSpecModeSidecar `json:"sidecar,omitempty"`
}

func (s *ArangoMLStorageSpecMode) GetSidecar() *ArangoMLStorageSpecModeSidecar {
if s == nil || s.Sidecar == nil {
return &ArangoMLStorageSpecModeSidecar{}
}
return s.Sidecar
}

func (s *ArangoMLStorageSpecMode) Validate() error {
if s == nil {
return errors.Newf("Mode is not defined")
Expand Down
19 changes: 13 additions & 6 deletions pkg/apis/ml/v1alpha1/storage_spec_mode_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)

var (
defaultRequestsCPU = resource.MustParse("100m")
defaultRequestsMemory = resource.MustParse("100Mi")
defaultLimitsCPU = resource.MustParse("200m")
defaultLimitsMemory = resource.MustParse("200Mi")
)

type ArangoMLStorageSpecModeSidecar struct {
// ListenPort defines on which port the sidecar container will be listening for connections
// +doc/default: 9201
Expand All @@ -49,27 +56,27 @@ func (s *ArangoMLStorageSpecModeSidecar) Validate() error {
}

func (s *ArangoMLStorageSpecModeSidecar) GetListenPort() uint16 {
if s.ListenPort == nil {
if s == nil || s.ListenPort == nil {
return 9201
}
return *s.ListenPort
}

func (s *ArangoMLStorageSpecModeSidecar) GetResources() core.ResourceRequirements {
var resources core.ResourceRequirements
if s.Resources != nil {
if s != nil && s.Resources != nil {
resources = *s.Resources
}

if len(resources.Requests) == 0 {
resources.Requests = make(core.ResourceList)
resources.Requests[core.ResourceCPU] = resource.MustParse("100m")
resources.Requests[core.ResourceMemory] = resource.MustParse("100Mi")
resources.Requests[core.ResourceCPU] = defaultRequestsCPU
resources.Requests[core.ResourceMemory] = defaultRequestsMemory
}
if len(resources.Limits) == 0 {
resources.Limits = make(core.ResourceList)
resources.Limits[core.ResourceCPU] = resource.MustParse("200m")
resources.Limits[core.ResourceMemory] = resource.MustParse("200Mi")
resources.Limits[core.ResourceCPU] = defaultLimitsCPU
resources.Limits[core.ResourceMemory] = defaultLimitsMemory
}
return resources
}
9 changes: 9 additions & 0 deletions pkg/apis/ml/v1alpha1/storage_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ import (
func Test_ArangoMLStorageSpec(t *testing.T) {
s := ArangoMLStorageSpec{}
require.Error(t, s.Validate())
require.NotNil(t, s.GetMode())
require.NotNil(t, s.GetBackend())

require.NotNil(t, s.Mode.GetSidecar())
s.Mode = &ArangoMLStorageSpecMode{}

require.NotNil(t, s.Backend.GetS3())
s.Backend = &ArangoMLStorageSpecBackend{}
require.Error(t, s.Validate())

require.NotNil(t, s.Mode.Sidecar.GetListenPort())
require.NotNil(t, s.Mode.Sidecar.GetResources())
s.Mode.Sidecar = &ArangoMLStorageSpecModeSidecar{}

require.Error(t, s.Backend.S3.Validate())
s.Backend.S3 = &ArangoMLStorageSpecBackendS3{
Endpoint: util.NewType("http://test.s3.example.com"),
BucketName: util.NewType("bucket"),
Expand Down

0 comments on commit 923d4e2

Please sign in to comment.