Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Kopia storage helpers #2655

Merged
merged 39 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
680e782
Add safecli dependency
plar Feb 3, 2024
72011e7
add new flag implementations based on the safecli package for the Kop…
plar Feb 3, 2024
0f635ad
apply go fmt
plar Feb 4, 2024
d0a6dd1
Add common Kopia args and flags
plar Feb 6, 2024
ca853c1
Add Kopia storage core flags
plar Feb 6, 2024
9850f4a
Fix Apply and test.Suit
plar Feb 12, 2024
246e1c1
Remove variadic args for Common and Cache flags
plar Feb 12, 2024
419652c
Merge branch 'kopia-cli-core-infra' into kopia-cli-common-flags
plar Feb 12, 2024
24707e5
pkg/kopia/cli/internal/flag is implemented in the safecli@v0.0.4 now
plar Feb 17, 2024
c029fc1
Merge branch 'master' into kopia-cli-core-infra
plar Feb 17, 2024
cff7220
Re-implement Kopia common args and opts; Sync with the master
plar Feb 17, 2024
550d124
Add pkg/kopia/cli package
plar Feb 17, 2024
fc918a0
go mod tidy
plar Feb 17, 2024
9530c91
Merge branch 'kopia-cli-core-infra' into kopia-cli-common-flags
plar Feb 17, 2024
75fb036
Merge branch 'kopia-cli-common-flags' into kopia-cli-storage-core-flags
plar Feb 17, 2024
9fdf94b
Add Kopia storage helpers
plar Feb 17, 2024
2c6cb6e
Convert common flags from vars to funcs
plar Feb 26, 2024
1a3ee2d
Add safecli dependency
plar Feb 3, 2024
386e7e9
add new flag implementations based on the safecli package for the Kop…
plar Feb 3, 2024
b815f63
apply go fmt
plar Feb 4, 2024
bcafabb
Fix Apply and test.Suit
plar Feb 12, 2024
6d61bb4
pkg/kopia/cli/internal/flag is implemented in the safecli@v0.0.4 now
plar Feb 17, 2024
e6ddb8f
Add pkg/kopia/cli package
plar Feb 17, 2024
4e7ffd7
go mod tidy
plar Feb 17, 2024
f16aea7
Update safecli to v0.0.5
plar Feb 27, 2024
dcd6425
Update safecli to v0.0.6
plar Feb 28, 2024
d3dfb31
Sync with upstream
plar Feb 28, 2024
d2ac987
Merge branch 'kopia-cli-common-flags' into kopia-cli-storage-core-flags
plar Feb 28, 2024
b093421
Fix tests
plar Feb 28, 2024
6e71143
Merge branch 'kopia-cli-common-flags' into kopia-cli-storage-core-flags
plar Feb 28, 2024
de3a52b
Add Location.IsPointInTypeSupported
plar Feb 28, 2024
722b279
Add tests for Location.IsPointInTypeSupported
plar Feb 28, 2024
44c133e
Fix formatting
plar Feb 29, 2024
1897a1e
Merge branch 'kopia-cli-core-infra' into kopia-cli-common-flags
plar Feb 29, 2024
33b1342
organize imports
plar Feb 29, 2024
83292af
Merge branch 'kopia-cli-common-flags' into kopia-cli-storage-core-flags
plar Feb 29, 2024
516a14c
organize imports
plar Feb 29, 2024
cd03d1e
Merge branch 'master' into kopia-cli-storage-core-flags
plar Mar 5, 2024
8cf36c7
Merge branch 'master' into kopia-cli-storage-core-flags
mergify[bot] Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)

require github.com/kanisterio/safecli v0.0.6

require (
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.18 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kanisterio/safecli v0.0.6 h1:Mq99jK7A/SBiHKZalUIlsk4qaSVZJNbBWb8rjaJv/Jk=
github.com/kanisterio/safecli v0.0.6/go.mod h1:KBraqj8mdv2cwAr9wecknGUb8jztTzUik0r7uE6yRA8=
github.com/kastenhq/check v0.0.0-20180626002341-0264cfcea734 h1:qulsCaCv+O2y9/sQ9nd5KChnAgFOWakTHQ9ZADjs6DQ=
github.com/kastenhq/check v0.0.0-20180626002341-0264cfcea734/go.mod h1:rdqSnvOJuKCPFW/h2rVLuXOAkRnHHdp9PZcKx4HCoDM=
github.com/kastenhq/stow v0.2.6-kasten.1.0.20231101232131-9321daa23aae h1:2cl4yuAJpdmLCx7G8eIsfNlQBLEfw0JDj6mTTyqc5qg=
Expand Down
26 changes: 26 additions & 0 deletions pkg/kopia/cli/args/cache_args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package args

// Cache provides the cache arguments for Kopia CLI.
type Cache struct {
CacheDirectory string // the directory where cache is stored. Default is "/tmp/kopia-cache".
ContentCacheSizeLimitMB int // the maximum size of the content cache in MB.
MetadataCacheSizeLimitMB int // the maximum size of the metadata cache in MB.

// unused?
ContentCacheSizeMB int // the size of the content cache in MB.
MetadataCacheSizeMB int // the size of the metadata cache in MB.
}
23 changes: 23 additions & 0 deletions pkg/kopia/cli/args/common_args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package args

// Common provides the common arguments for Kopia CLI.
type Common struct {
ConfigFilePath string // the path to the config file.
LogDirectory string // the directory where logs are stored.
LogLevel string // the level of logging. Default is "error".
RepoPassword string // the password for the repository.
}
21 changes: 21 additions & 0 deletions pkg/kopia/cli/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cli

// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import (
_ "github.com/kanisterio/safecli"
)

// This package contains the implementation of the Kopia CLI using github.com/kanisterio/safecli.
31 changes: 31 additions & 0 deletions pkg/kopia/cli/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"github.com/pkg/errors"
)

// Common errors
var (
// ErrInvalidID is returned when the ID is empty.
ErrInvalidID = errors.New("invalid ID")
)

// storage errors
var (
// ErrUnsupportedStorage is returned when the storage is not supported.
ErrUnsupportedStorage = errors.New("unsupported storage")
)
28 changes: 28 additions & 0 deletions pkg/kopia/cli/internal/args/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package args

import (
"github.com/kanisterio/kanister/pkg/kopia/cli"
"github.com/kanisterio/safecli/command"
)

// ID creates a new ID argument.
func ID(id string) command.Applier {
if id == "" {
return command.NewErrorArgument(cli.ErrInvalidID)
}
return command.NewArgument(id)
}
39 changes: 39 additions & 0 deletions pkg/kopia/cli/internal/args/args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package args_test

import (
"testing"

"github.com/kanisterio/kanister/pkg/kopia/cli"
"github.com/kanisterio/kanister/pkg/kopia/cli/internal/args"
"github.com/kanisterio/safecli/test"
"gopkg.in/check.v1"
)

func TestArgs(t *testing.T) { check.TestingT(t) }

var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTest{
{
Name: "Invalid ID",
Argument: args.ID(""),
ExpectedErr: cli.ErrInvalidID,
},
{
Name: "ID",
Argument: args.ID("id12345"),
ExpectedCLI: []string{"cmd", "id12345"},
},
}})
61 changes: 61 additions & 0 deletions pkg/kopia/cli/internal/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package internal

import (
"strconv"
"strings"

rs "github.com/kanisterio/kanister/pkg/secrets/repositoryserver"
)

// Location is a map of key-value pairs that represent different storage properties.
type Location map[string][]byte

// Type returns the location type.
func (l Location) Type() rs.LocType {
return rs.LocType(string(l[rs.TypeKey]))
}

// Region returns the location region.
func (l Location) Region() string {
return string(l[rs.RegionKey])
}

// BucketName returns the location bucket name.
func (l Location) BucketName() string {
return string(l[rs.BucketKey])
}

// Endpoint returns the location endpoint.
func (l Location) Endpoint() string {
return string(l[rs.EndpointKey])
}

// Prefix returns the location prefix.
func (l Location) Prefix() string {
return string(l[rs.PrefixKey])
}

// IsInsecureEndpoint returns true if the location endpoint is insecure/http.
func (l Location) IsInsecureEndpoint() bool {
return strings.HasPrefix(l.Endpoint(), "http:")
}

// HasSkipSSLVerify returns true if the location has skip SSL verification.
func (l Location) HasSkipSSLVerify() bool {
v, _ := strconv.ParseBool(string(l[rs.SkipSSLVerifyKey]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we handle error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've implemented the same logic as it is in the current implementation:

// pkg/kopia/command/storage/secret_utils.go
func checkSkipSSLVerifyFromMap(m map[string][]byte) bool {
	v := string(m[repositoryserver.SkipSSLVerifyKey])
	return v == "true"
}

strconv.ParseBool returns false in the case of error.

return v
}
82 changes: 82 additions & 0 deletions pkg/kopia/cli/internal/location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package internal_test

import (
"testing"

"github.com/kanisterio/kanister/pkg/kopia/cli/internal"
rs "github.com/kanisterio/kanister/pkg/secrets/repositoryserver"
"gopkg.in/check.v1"
plar marked this conversation as resolved.
Show resolved Hide resolved
)

func TestLocation(t *testing.T) { check.TestingT(t) }

type LocationSuite struct{}

var _ = check.Suite(&LocationSuite{})

func (s *LocationSuite) TestLocation(c *check.C) {
type expected struct {
Type rs.LocType
Region string
BucketName string
Endpoint string
Prefix string
IsInsecure bool
HasSkipSSLVerify bool
}

tests := []struct {
name string
location internal.Location
expected expected
}{
{
name: "Test with no fields",
location: internal.Location{},
expected: expected{},
},
{
name: "Test with all fields",
location: internal.Location{
rs.TypeKey: []byte("Type1"),
rs.RegionKey: []byte("Region1"),
rs.BucketKey: []byte("Bucket1"),
rs.EndpointKey: []byte("http://Endpoint1"),
rs.PrefixKey: []byte("Prefix1"),
rs.SkipSSLVerifyKey: []byte("true"),
},
expected: expected{
Type: "Type1",
Region: "Region1",
BucketName: "Bucket1",
Endpoint: "http://Endpoint1",
Prefix: "Prefix1",
IsInsecure: true,
HasSkipSSLVerify: true,
},
},
}
for _, test := range tests {
c.Check(test.location.Type(), check.Equals, test.expected.Type)
c.Check(test.location.Region(), check.Equals, test.expected.Region)
c.Check(test.location.BucketName(), check.Equals, test.expected.BucketName)
c.Check(test.location.Endpoint(), check.Equals, test.expected.Endpoint)
c.Check(test.location.Prefix(), check.Equals, test.expected.Prefix)
c.Check(test.location.IsInsecureEndpoint(), check.Equals, test.expected.IsInsecure)
c.Check(test.location.HasSkipSSLVerify(), check.Equals, test.expected.HasSkipSSLVerify)
}
}
45 changes: 45 additions & 0 deletions pkg/kopia/cli/internal/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package storage

import (
"context"
"io"

"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/log"
)

// NopLogger is a logger that does nothing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor Author

@plar plar Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Nop logger is used inside s3 storage as a default logger if no logger is provided.
It helps to avoid code cluttering and remove some code bumps.

You don't need to write something like below:

    if logger != nil {
        logger.Print("Removing trailing slashes from the endpoint")
    }

and can simply use:

    logger.Print("Removing trailing slashes from the endpoint")

// TODO: Move to log package
hairyhum marked this conversation as resolved.
Show resolved Hide resolved
type NopLogger struct{}

// Print does nothing.
func (NopLogger) Print(msg string, fields ...field.M) {
}

// PrintTo does nothing.
func (NopLogger) PrintTo(w io.Writer, msg string, fields ...field.M) {
}

// WithContext does nothing.
func (NopLogger) WithContext(ctx context.Context) log.Logger {
return &NopLogger{}
}

// WithError does nothing.
func (NopLogger) WithError(err error) log.Logger {
return &NopLogger{}
}
Loading