Skip to content

Commit

Permalink
Support providing k8s native resource through --objects flag in kanctl (
Browse files Browse the repository at this point in the history
#638)

* Support providing k8s native resource through
--objects flag in kanctl command, while creating
actionset.
For ex
```
kanctl create actionset \
--action backup \
--namespace kanister \
--blueprint etcd-blueprint \
--objects /v1/pods/kube-system/etcd-minikube \
--profile kube-system/s3-profile-xhgl9
```

* Use format v1/resources/namespace/name to provide resources from core api group

* Add test for parseGenericObjectRef

* Fix golint

Co-authored-by: Pavan Navarathna <pavan@kasten.io>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 14, 2020
1 parent dd80704 commit 54ea015
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/kanctl/actionset.go
Expand Up @@ -430,10 +430,10 @@ func parseGenericObjects(objs []string) ([]crv1alpha1.ObjectReference, error) {

func parseGenericObjectReference(s string) (crv1alpha1.ObjectReference, error) {
// Looking for group/version/resource/namespace/name
reg := regexp.MustCompile(`([\w-.]+)/([\w-.]+)/([\w-.]+)/([\w-.]+)/([\w-.]+)`)
reg := regexp.MustCompile(`([\w-.]*/)*([\w-.]+)/([\w-.]+)/([\w-.]+)/([\w-.]+)`)
m := reg.FindStringSubmatch(s)
if len(m) != 6 {
return crv1alpha1.ObjectReference{}, errors.Errorf("Expected group/version/resource/namespace/name. Got %s %d", s, len(m))
if len(m) != 6 && len(m) != 5 {
return crv1alpha1.ObjectReference{}, errors.Errorf("Expected group/version/resource/namespace/name or in the case of core group's resources version/resource/namespace/name . Got %s %d", s, len(m))
}
return crv1alpha1.ObjectReference{
Group: m[1],
Expand Down
78 changes: 78 additions & 0 deletions pkg/kanctl/actionset_test.go
@@ -0,0 +1,78 @@
// Copyright 2020 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 kanctl

import (
"testing"

. "gopkg.in/check.v1"

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
)

type KanctlTestSuite struct{}

var _ = Suite(&KanctlTestSuite{})

func Test(t *testing.T) { TestingT(t) }

func (k *KanctlTestSuite) TestParseGenericObjectReference(c *C) {
for _, tc := range []struct {
objectFlag string
expected crv1alpha1.ObjectReference
err Checker
}{
// not core group
{
objectFlag: "apps/v1/deployments/namespace/name",
expected: crv1alpha1.ObjectReference{
APIVersion: "v1",
Group: "apps/",
Resource: "deployments",
Name: "name",
Namespace: "namespace",
},
err: IsNil,
},
// core group
{
objectFlag: "v1/pods/kube-system/etcd-minikube ",
expected: crv1alpha1.ObjectReference{
APIVersion: "v1",
Group: "",
Resource: "pods",
Name: "etcd-minikube",
Namespace: "kube-system",
},
err: IsNil,
},
// CRs
{
objectFlag: "cr.kanister.io/v1alpha1/profiles/kanister/s3-profile-5fx9w",
expected: crv1alpha1.ObjectReference{
APIVersion: "v1alpha1",
Group: "cr.kanister.io/",
Resource: "profiles",
Name: "s3-profile-5fx9w",
Namespace: "kanister",
},
err: IsNil,
},
} {
a, err := parseGenericObjectReference(tc.objectFlag)
c.Check(err, tc.err)
c.Assert(a, DeepEquals, tc.expected)
}
}

0 comments on commit 54ea015

Please sign in to comment.