Skip to content

Commit

Permalink
Change the data type for metadata and content fields in v1alpha1.Cach…
Browse files Browse the repository at this point in the history
…eSizeSettings (#2237)

* change the data type to *int for metadata and content cache size in the repository server CR

* remove unnecessary trailing space in the function

* generate deep copy functions using codegen

* update repository server CRD with data type changes

* add omit empty for the fields

Signed-off-by: Amruta Kale <amruta.kale@veeam.com>

* add changes after execution of make codegen

* add description about unit to be used for metadata and cache settings

* change the copyright information

---------

Signed-off-by: Amruta Kale <amruta.kale@veeam.com>
  • Loading branch information
kale-amruta authored Aug 31, 2023
1 parent 0af2e9d commit a52fa96
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 70 deletions.
6 changes: 4 additions & 2 deletions pkg/apis/cr/v1alpha1/repositoryserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ type Configuration struct {
// CacheSizeSettings are the metadata/content cache size details
// that can be used while establishing connection to the kopia repository
type CacheSizeSettings struct {
Metadata string `json:"metadata"`
Content string `json:"content"`
// Metadata size should be in specified in MB
Metadata *int `json:"metadata,omitempty"`
// Content size should be in specified in MB
Content *int `json:"content,omitempty"`
}

// Server details required for starting the repository proxy server and initializing the repository client users
Expand Down
16 changes: 13 additions & 3 deletions pkg/apis/cr/v1alpha1/zz_generated.deepcopy.go

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

35 changes: 13 additions & 22 deletions pkg/controllers/repositoryserver/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ package repositoryserver

import (
"github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/kopia/command"
"github.com/kanisterio/kanister/pkg/kopia/repository"
reposerver "github.com/kanisterio/kanister/pkg/secrets/repositoryserver"
"github.com/kanisterio/kanister/pkg/utils"
)

func (h *RepoServerHandler) connectToKopiaRepository() error {
contentCacheMB, metadataCacheMB := h.getRepositoryCacheSettings()

repoConfiguration := h.getRepositoryConfiguration()
cacheSizeSettings := h.getRepositoryCacheSettings()
args := command.RepositoryCommandArgs{
CommandArgs: &command.CommandArgs{
RepoPassword: string(h.RepositoryServerSecrets.repositoryPassword.Data[reposerver.RepoPasswordKey]),
Expand All @@ -35,8 +32,8 @@ func (h *RepoServerHandler) connectToKopiaRepository() error {
},
CacheDirectory: repoConfiguration.CacheDirectory,
Hostname: h.RepositoryServer.Spec.Repository.Hostname,
ContentCacheMB: contentCacheMB,
MetadataCacheMB: metadataCacheMB,
ContentCacheMB: *cacheSizeSettings.Content,
MetadataCacheMB: *cacheSizeSettings.Metadata,
Username: h.RepositoryServer.Spec.Repository.Username,
// TODO(Amruta): Generate path for respository
RepoPathPrefix: h.RepositoryServer.Spec.Repository.RootPath,
Expand Down Expand Up @@ -71,23 +68,17 @@ func (h *RepoServerHandler) getRepositoryConfiguration() v1alpha1.Configuration
return configuration
}

func (h *RepoServerHandler) getRepositoryCacheSettings() (int, int) {
func (h *RepoServerHandler) getRepositoryCacheSettings() v1alpha1.CacheSizeSettings {
defaultContentCacheMB, defaultMetadataCacheMB := command.GetGeneralCacheSizeSettings()
contentCacheMB := defaultContentCacheMB
metadataCacheMB := defaultMetadataCacheMB
var err error
if h.RepositoryServer.Spec.Repository.CacheSizeSettings.Content != "" {
contentCacheMB, err = utils.GetIntOrDefault(h.RepositoryServer.Spec.Repository.CacheSizeSettings.Content, defaultContentCacheMB)
if err != nil {
h.Logger.Error(err, "cache content size should be an integer, using default value", field.M{"contentSize": h.RepositoryServer.Spec.Repository.CacheSizeSettings.Content, "default_value": defaultContentCacheMB})
}
cacheSizeSettings := v1alpha1.CacheSizeSettings{
Metadata: &defaultMetadataCacheMB,
Content: &defaultContentCacheMB,
}
if h.RepositoryServer.Spec.Repository.CacheSizeSettings.Metadata != "" {
metadataCacheMB, err = utils.GetIntOrDefault(h.RepositoryServer.Spec.Repository.CacheSizeSettings.Metadata, defaultMetadataCacheMB)
if err != nil {
h.Logger.Error(err, "cache metadata size should be an integer, using default value", field.M{"metadataSize": h.RepositoryServer.Spec.Repository.CacheSizeSettings.Metadata, "default_value": defaultMetadataCacheMB})
}
if h.RepositoryServer.Spec.Repository.CacheSizeSettings.Content != nil {
cacheSizeSettings.Content = h.RepositoryServer.Spec.Repository.CacheSizeSettings.Content
}

return contentCacheMB, metadataCacheMB
if h.RepositoryServer.Spec.Repository.CacheSizeSettings.Metadata != nil {
cacheSizeSettings.Metadata = h.RepositoryServer.Spec.Repository.CacheSizeSettings.Metadata
}
return cacheSizeSettings
}
38 changes: 20 additions & 18 deletions pkg/controllers/repositoryserver/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,38 @@ func (s *RepoServerControllerSuite) TestCacheSizeConfiguration(c *C) {
}

// Test if Default cache size settings are set
contentCacheMB, metadataCacheMB := repoServerHandler.getRepositoryCacheSettings()
c.Assert(contentCacheMB, Equals, defaultcontentCacheMB)
c.Assert(metadataCacheMB, Equals, defaultmetadataCacheMB)
cacheSizeSettings := repoServerHandler.getRepositoryCacheSettings()
c.Assert(*cacheSizeSettings.Content, Equals, defaultcontentCacheMB)
c.Assert(*cacheSizeSettings.Metadata, Equals, defaultmetadataCacheMB)

customCacheMetadataSize := 1000
customCacheContentSize := 1100
// Test if configfured cache size settings are set
repositoryServer.Spec.Repository.CacheSizeSettings = v1alpha1.CacheSizeSettings{
Metadata: "1000",
Content: "1100",
Metadata: &customCacheMetadataSize,
Content: &customCacheContentSize,
}
contentCacheMB, metadataCacheMB = repoServerHandler.getRepositoryCacheSettings()
c.Assert(contentCacheMB, Equals, 1100)
c.Assert(metadataCacheMB, Equals, 1000)
cacheSizeSettings = repoServerHandler.getRepositoryCacheSettings()
c.Assert(*cacheSizeSettings.Content, Equals, 1100)
c.Assert(*cacheSizeSettings.Metadata, Equals, 1000)

// Check if default Content Cache size is set
repositoryServer.Spec.Repository.CacheSizeSettings = v1alpha1.CacheSizeSettings{
Metadata: "1000",
Content: "",
Metadata: &customCacheMetadataSize,
Content: nil,
}
contentCacheMB, metadataCacheMB = repoServerHandler.getRepositoryCacheSettings()
c.Assert(contentCacheMB, Equals, defaultcontentCacheMB)
c.Assert(metadataCacheMB, Equals, 1000)
cacheSizeSettings = repoServerHandler.getRepositoryCacheSettings()
c.Assert(*cacheSizeSettings.Content, Equals, defaultcontentCacheMB)
c.Assert(*cacheSizeSettings.Metadata, Equals, 1000)

// Check if default Metadata Cache size is set
repositoryServer.Spec.Repository.CacheSizeSettings = v1alpha1.CacheSizeSettings{
Metadata: "",
Content: "1100",
Metadata: nil,
Content: &customCacheContentSize,
}
contentCacheMB, metadataCacheMB = repoServerHandler.getRepositoryCacheSettings()
c.Assert(contentCacheMB, Equals, 1100)
c.Assert(metadataCacheMB, Equals, defaultmetadataCacheMB)
cacheSizeSettings = repoServerHandler.getRepositoryCacheSettings()
c.Assert(*cacheSizeSettings.Content, Equals, 1100)
c.Assert(*cacheSizeSettings.Metadata, Equals, defaultmetadataCacheMB)
}

func (s *RepoServerControllerSuite) TestConfigFileAndLogDirectoryConfiguration(c *C) {
Expand Down
11 changes: 6 additions & 5 deletions pkg/customresource/repositoryserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.12.0
name: repositoryservers.cr.kanister.io
spec:
group: cr.kanister.io
Expand Down Expand Up @@ -44,12 +46,11 @@ spec:
to the kopia repository
properties:
content:
type: string
description: Content size should be in specified in MB
type: integer
metadata:
type: string
required:
- content
- metadata
description: Metadata size should be in specified in MB
type: integer
type: object
configuration:
description: Configuration can be used to specify the optional
Expand Down
36 changes: 16 additions & 20 deletions pkg/param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"

"github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/client/clientset/versioned"
"github.com/kanisterio/kanister/pkg/kopia/command"
Expand Down Expand Up @@ -300,38 +301,33 @@ func fetchRepositoryServer(ctx context.Context, cli kubernetes.Interface, crCli
return nil, errors.Wrap(err, "Error Fetching Repository Server Service")
}
repositoryServerAddress := fmt.Sprintf("https://%s.%s.%s:%d", repositoryServerService.Name, repositoryServerService.Namespace, clusterLocalDomain, repositoryServerService.Spec.Ports[0].Port)
contentCacheMB, metadataCacheMB, err := getKopiaRepositoryCacheSize(r)
if err != nil {
return nil, err
}
cacheSizeSettings := getKopiaRepositoryCacheSize(r)

return &RepositoryServer{
Name: r.Name,
Namespace: r.Namespace,
ServerInfo: r.Status.ServerInfo,
Username: r.Spec.Server.UserAccess.Username,
Credentials: repoServerSecrets,
Address: repositoryServerAddress,
ContentCacheMB: contentCacheMB,
MetadataCacheMB: metadataCacheMB,
ContentCacheMB: *cacheSizeSettings.Content,
MetadataCacheMB: *cacheSizeSettings.Metadata,
}, nil
}

func getKopiaRepositoryCacheSize(rs *crv1alpha1.RepositoryServer) (int, int, error) {
var err error
contentCacheMB, metadataCacheMB := command.GetGeneralCacheSizeSettings()
if rs.Spec.Repository.CacheSizeSettings.Content != "" {
contentCacheMB, err = strconv.Atoi(rs.Spec.Repository.CacheSizeSettings.Content)
if err != nil {
return 0, 0, errors.Wrap(err, "Error Parsing Content Cache Size")
}
func getKopiaRepositoryCacheSize(rs *crv1alpha1.RepositoryServer) v1alpha1.CacheSizeSettings {
defaultContentCacheMB, defaultMetadataCacheMB := command.GetGeneralCacheSizeSettings()
cacheSizeSettings := v1alpha1.CacheSizeSettings{
Metadata: &defaultMetadataCacheMB,
Content: &defaultContentCacheMB,
}
if rs.Spec.Repository.CacheSizeSettings.Metadata != "" {
metadataCacheMB, err = strconv.Atoi(rs.Spec.Repository.CacheSizeSettings.Metadata)
if err != nil {
return 0, 0, errors.Wrap(err, "Error Parsing Metadata Cache Size")
}
if rs.Spec.Repository.CacheSizeSettings.Content != nil {
cacheSizeSettings.Content = rs.Spec.Repository.CacheSizeSettings.Content
}
if rs.Spec.Repository.CacheSizeSettings.Metadata != nil {
cacheSizeSettings.Metadata = rs.Spec.Repository.CacheSizeSettings.Metadata
}
return contentCacheMB, metadataCacheMB, nil
return cacheSizeSettings
}

func fetchCredential(ctx context.Context, cli kubernetes.Interface, c crv1alpha1.Credential) (*Credential, error) {
Expand Down

0 comments on commit a52fa96

Please sign in to comment.