From 54ea01555b183eb10fa689ab51729612cdba8689 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 14 Oct 2020 11:55:18 +0530 Subject: [PATCH] Support providing k8s native resource through --objects flag in kanctl (#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 Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- pkg/kanctl/actionset.go | 6 +-- pkg/kanctl/actionset_test.go | 78 ++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 pkg/kanctl/actionset_test.go diff --git a/pkg/kanctl/actionset.go b/pkg/kanctl/actionset.go index 24e14ac69b..579f3d8afa 100644 --- a/pkg/kanctl/actionset.go +++ b/pkg/kanctl/actionset.go @@ -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], diff --git a/pkg/kanctl/actionset_test.go b/pkg/kanctl/actionset_test.go new file mode 100644 index 0000000000..d6123049a1 --- /dev/null +++ b/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) + } +}