Skip to content

Commit

Permalink
Add fields for kopia cache, log and config directory in repository se…
Browse files Browse the repository at this point in the history
…rver CR (#2144)

* add unit tests for repository server controller

* test

* add prerequisites for tests

* add server ready test

* remove unwanted changes

* fix lint issues

* automate creation of repo server CRD

* address review comments

* add symbolic link

* address comments

* remove custom resource

* renaming cli to kubecli

* add more tests

* fix lint issue

* Add tests for Immutability

Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>

* Fix naming conventions

Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>

* Update variables

Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>

* add tests related to secrets

* add secrets configuration for testutils

* add base suite for repository server controller

* use constants for location secret keys

* add unit tests

* make the constants local to the package

* add licence headers

* change package name

* fix build errors

* fix licence headers

* fix lint issues

* remove secret manager changes

* remove unused functions and move to next PR

* add utility functions

* add tests for secrets and cache settings

* fix build issues

* move secret creation utils under test suite

* resolve conflicts

* add fields for log,cache and config directory

* add fields for log,cache and config directory

* add tests for newly added fields

* fix test

* resolve conflicts with master

* - add the cache directory, log directory, configFilePath settings under a new field
- remove naked returns from the functions

* start comments with a space

* correct comments in repositoryserver_types.go, move GetIntOrDefault to pkg/utils/utils.go

---------

Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>
Co-authored-by: Rajat Gupta <37516416+r4rajat@users.noreply.github.com>
Co-authored-by: Rajat Gupta <rajat.gupta@veeam.com>
  • Loading branch information
3 people committed Aug 16, 2023
1 parent e29bc47 commit 7663904
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 24 deletions.
12 changes: 12 additions & 0 deletions pkg/apis/cr/v1alpha1/repositoryserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ type Repository struct {
// PasswordSecretRef has the password required to connect to kopia repository
PasswordSecretRef corev1.SecretReference `json:"passwordSecretRef"`
CacheSizeSettings CacheSizeSettings `json:"cacheSizeSettings,omitempty"`
Configuration Configuration `json:"configuration,omitempty"`
}

// Configuration can be used to specify the optional fields used
// for repository operations
type Configuration struct {
// CacheDirectory is an optional field to specify kopia cache directory
CacheDirectory string `json:"cacheDirectory,omitempty"`
// LogDirectory is an optional field to specify kopia log directory
LogDirectory string `json:"logDirectory,omitempty"`
// ConfigFilePath is an optional field to specify kopia config file path
ConfigFilePath string `json:"configFilePath,omitempty"`
}

// CacheSizeSettings are the metadata/content cache size details
Expand Down
17 changes: 17 additions & 0 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.

55 changes: 39 additions & 16 deletions pkg/controllers/repositoryserver/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
package repositoryserver

import (
"strconv"

"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, err := h.getRepositoryCacheSettings()
if err != nil {
return err
}
contentCacheMB, metadataCacheMB := h.getRepositoryCacheSettings()

repoConfiguration := h.getRepositoryConfiguration()
args := command.RepositoryCommandArgs{
CommandArgs: &command.CommandArgs{
RepoPassword: string(h.RepositoryServerSecrets.repositoryPassword.Data[reposerver.RepoPasswordKey]),
ConfigFilePath: command.DefaultConfigFilePath,
LogDirectory: command.DefaultLogDirectory,
ConfigFilePath: repoConfiguration.ConfigFilePath,
LogDirectory: repoConfiguration.LogDirectory,
},
CacheDirectory: command.DefaultCacheDirectory,
CacheDirectory: repoConfiguration.CacheDirectory,
Hostname: h.RepositoryServer.Spec.Repository.Hostname,
ContentCacheMB: contentCacheMB,
MetadataCacheMB: metadataCacheMB,
Expand All @@ -52,19 +52,42 @@ func (h *RepoServerHandler) connectToKopiaRepository() error {
)
}

func (h *RepoServerHandler) getRepositoryCacheSettings() (contentCacheMB, metadataCacheMB int, err error) {
contentCacheMB, metadataCacheMB = command.GetGeneralCacheSizeSettings()
func (h *RepoServerHandler) getRepositoryConfiguration() v1alpha1.Configuration {
configuration := v1alpha1.Configuration{
ConfigFilePath: command.DefaultConfigFilePath,
LogDirectory: command.DefaultLogDirectory,
CacheDirectory: command.DefaultCacheDirectory,
}

if h.RepositoryServer.Spec.Repository.Configuration.ConfigFilePath != "" {
configuration.ConfigFilePath = h.RepositoryServer.Spec.Repository.Configuration.ConfigFilePath
}
if h.RepositoryServer.Spec.Repository.Configuration.LogDirectory != "" {
configuration.LogDirectory = h.RepositoryServer.Spec.Repository.Configuration.LogDirectory
}
if h.RepositoryServer.Spec.Repository.Configuration.CacheDirectory != "" {
configuration.CacheDirectory = h.RepositoryServer.Spec.Repository.Configuration.CacheDirectory
}
return configuration
}

func (h *RepoServerHandler) getRepositoryCacheSettings() (int, int) {
defaultContentCacheMB, defaultMetadataCacheMB := command.GetGeneralCacheSizeSettings()
contentCacheMB := defaultContentCacheMB
metadataCacheMB := defaultMetadataCacheMB
var err error
if h.RepositoryServer.Spec.Repository.CacheSizeSettings.Content != "" {
contentCacheMB, err = strconv.Atoi(h.RepositoryServer.Spec.Repository.CacheSizeSettings.Content)
contentCacheMB, err = utils.GetIntOrDefault(h.RepositoryServer.Spec.Repository.CacheSizeSettings.Content, defaultContentCacheMB)
if err != nil {
return
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})
}
}
if h.RepositoryServer.Spec.Repository.CacheSizeSettings.Metadata != "" {
metadataCacheMB, err = strconv.Atoi(h.RepositoryServer.Spec.Repository.CacheSizeSettings.Metadata)
metadataCacheMB, err = utils.GetIntOrDefault(h.RepositoryServer.Spec.Repository.CacheSizeSettings.Metadata, defaultMetadataCacheMB)
if err != nil {
return
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})
}
}
return

return contentCacheMB, metadataCacheMB
}
40 changes: 32 additions & 8 deletions pkg/controllers/repositoryserver/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func (s *RepoServerControllerSuite) TestCacheSizeConfiguration(c *C) {
}

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

Expand All @@ -46,8 +45,7 @@ func (s *RepoServerControllerSuite) TestCacheSizeConfiguration(c *C) {
Metadata: "1000",
Content: "1100",
}
contentCacheMB, metadataCacheMB, err = repoServerHandler.getRepositoryCacheSettings()
c.Assert(err, IsNil)
contentCacheMB, metadataCacheMB = repoServerHandler.getRepositoryCacheSettings()
c.Assert(contentCacheMB, Equals, 1100)
c.Assert(metadataCacheMB, Equals, 1000)

Expand All @@ -56,8 +54,7 @@ func (s *RepoServerControllerSuite) TestCacheSizeConfiguration(c *C) {
Metadata: "1000",
Content: "",
}
contentCacheMB, metadataCacheMB, err = repoServerHandler.getRepositoryCacheSettings()
c.Assert(err, IsNil)
contentCacheMB, metadataCacheMB = repoServerHandler.getRepositoryCacheSettings()
c.Assert(contentCacheMB, Equals, defaultcontentCacheMB)
c.Assert(metadataCacheMB, Equals, 1000)

Expand All @@ -66,8 +63,35 @@ func (s *RepoServerControllerSuite) TestCacheSizeConfiguration(c *C) {
Metadata: "",
Content: "1100",
}
contentCacheMB, metadataCacheMB, err = repoServerHandler.getRepositoryCacheSettings()
c.Assert(err, IsNil)
contentCacheMB, metadataCacheMB = repoServerHandler.getRepositoryCacheSettings()
c.Assert(contentCacheMB, Equals, 1100)
c.Assert(metadataCacheMB, Equals, defaultmetadataCacheMB)
}

func (s *RepoServerControllerSuite) TestConfigFileAndLogDirectoryConfiguration(c *C) {
repositoryServer := testutil.GetTestKopiaRepositoryServerCR(s.repoServerControllerNamespace)
setRepositoryServerSecretsInCR(&s.repoServerSecrets, &repositoryServer)

repoServerHandler := RepoServerHandler{
Req: reconcile.Request{},
Reconciler: s.DefaultRepoServerReconciler,
KubeCli: s.kubeCli,
RepositoryServer: &repositoryServer,
}

// Check if default values for log directory,config file path and cache directory are set
configuration := repoServerHandler.getRepositoryConfiguration()
c.Assert(configuration.ConfigFilePath, Equals, command.DefaultConfigFilePath)
c.Assert(configuration.LogDirectory, Equals, command.DefaultLogDirectory)
c.Assert(configuration.CacheDirectory, Equals, command.DefaultCacheDirectory)

// Check if custom values for log directory,config file path and cache directory are set
repositoryServer.Spec.Repository.Configuration.ConfigFilePath = "/tmp/test-config"
repositoryServer.Spec.Repository.Configuration.LogDirectory = "/tmp/test-log-directory"
repositoryServer.Spec.Repository.Configuration.CacheDirectory = "/tmp/test-cache-directory"

configuration = repoServerHandler.getRepositoryConfiguration()
c.Assert(configuration.ConfigFilePath, Equals, "/tmp/test-config")
c.Assert(configuration.LogDirectory, Equals, "/tmp/test-log-directory")
c.Assert(configuration.CacheDirectory, Equals, "/tmp/test-cache-directory")
}
17 changes: 17 additions & 0 deletions pkg/customresource/repositoryserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ spec:
- content
- metadata
type: object
configuration:
description: Configuration can be used to specify the optional
fields used for repository operations
properties:
cacheDirectory:
description: CacheDirectory is an optional field to specify
kopia cache directory
type: string
configFilePath:
description: ConfigFilePath is an optional field to specify
kopia config file path
type: string
logDirectory:
description: LogDirectory is an optional field to specify
kopia log directory
type: string
type: object
hostname:
description: If specified, these values will be used by the controller
to override default hostname when connecting to the repository
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ func GetEnvAsStringOrDefault(envKey string, def string) string {
return def
}

func GetIntOrDefault(value string, defaultValue int) (int, error) {
v, err := strconv.Atoi(value)
if err != nil {
v = defaultValue
return v, errors.New("conversion to integer failed, using default value for the field")
}
return v, nil
}

// DurationToString formats the given duration into a short format which eludes trailing zero units in the string.
func DurationToString(d time.Duration) string {
s := d.String()
Expand Down

0 comments on commit 7663904

Please sign in to comment.