From 19bb197d18ef69214b0c16877096618386222c3f Mon Sep 17 00:00:00 2001 From: Prasad Ghangal Date: Wed, 23 Oct 2019 18:20:14 +0530 Subject: [PATCH] Change strategicpatch.JSONMap type to crv1alpha1.JSONMap Signed-off-by: Prasad Ghangal --- pkg/apis/cr/v1alpha1/deepcopy.go | 7 +++++ pkg/apis/cr/v1alpha1/types.go | 5 +++- pkg/apis/cr/v1alpha1/zz_generated.deepcopy.go | 11 +++++++ pkg/function/args.go | 6 ++-- pkg/function/delete_data.go | 4 +-- pkg/function/describe_backups.go | 4 +-- pkg/function/kube_task.go | 4 +-- pkg/function/kube_task_test.go | 3 +- pkg/function/prepare_data.go | 4 +-- pkg/function/restore_data.go | 10 +++---- pkg/function/restore_data_all.go | 7 ++--- pkg/function/restore_data_test.go | 7 ++--- pkg/kube/pod.go | 7 +++-- pkg/kube/pod_test.go | 29 ++++++++++--------- pkg/param/param.go | 3 +- 15 files changed, 65 insertions(+), 46 deletions(-) diff --git a/pkg/apis/cr/v1alpha1/deepcopy.go b/pkg/apis/cr/v1alpha1/deepcopy.go index 77d2007e78..e5e82c7a19 100644 --- a/pkg/apis/cr/v1alpha1/deepcopy.go +++ b/pkg/apis/cr/v1alpha1/deepcopy.go @@ -15,3 +15,10 @@ func (in *Phase) DeepCopyInto(out *Phase) { // TODO: Handle 'Output' map[string]interface{} return } + +// DeepCopyInto handles JSONMap deep copies, copying the receiver, writing into out. in must be non-nil. +// The auto-generated function does not handle the map[string]interface{} type +func (in *JSONMap) DeepCopyInto(out *JSONMap) { + *out = *in + return +} diff --git a/pkg/apis/cr/v1alpha1/types.go b/pkg/apis/cr/v1alpha1/types.go index fec73ad52f..ca6f6df3cb 100644 --- a/pkg/apis/cr/v1alpha1/types.go +++ b/pkg/apis/cr/v1alpha1/types.go @@ -44,6 +44,9 @@ const ( ActionSetResourceNamePlural = "actionsets" ) +// JSONMap contains PodOverride specs. +type JSONMap sp.JSONMap + var _ runtime.Object = (*ActionSet)(nil) // +genclient @@ -101,7 +104,7 @@ type ActionSpec struct { Profile *ObjectReference `json:"profile"` // PodOverride is used to specify pod specs that will override the // default pod specs - PodOverride sp.JSONMap `json:"podOverride,omitempty"` + PodOverride JSONMap `json:"podOverride,omitempty"` // Options will be used to specify additional values // to be used in the Blueprint. Options map[string]string `json:"options"` diff --git a/pkg/apis/cr/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/cr/v1alpha1/zz_generated.deepcopy.go index 3e1143f569..7cc747893f 100644 --- a/pkg/apis/cr/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/cr/v1alpha1/zz_generated.deepcopy.go @@ -173,6 +173,7 @@ func (in *ActionSpec) DeepCopyInto(out *ActionSpec) { *out = new(ObjectReference) **out = **in } + out.PodOverride = in.PodOverride.DeepCopy() if in.Options != nil { in, out := &in.Options, &out.Options *out = make(map[string]string, len(*in)) @@ -406,6 +407,16 @@ func (in *Credential) DeepCopy() *Credential { return out } +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new JSONMap. +func (in JSONMap) DeepCopy() JSONMap { + if in == nil { + return nil + } + out := new(JSONMap) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *KeyPair) DeepCopyInto(out *KeyPair) { *out = *in diff --git a/pkg/function/args.go b/pkg/function/args.go index d0cf4553bf..b112ad2235 100644 --- a/pkg/function/args.go +++ b/pkg/function/args.go @@ -17,8 +17,8 @@ package function import ( "github.com/mitchellh/mapstructure" "github.com/pkg/errors" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/kube" "github.com/kanisterio/kanister/pkg/param" ) @@ -51,8 +51,8 @@ func ArgExists(args map[string]interface{}, argName string) bool { } // GetPodSpecOverride merges PodOverride specs passed in args and TemplateParams and returns combined Override specs -func GetPodSpecOverride(tp param.TemplateParams, args map[string]interface{}, argName string) (sp.JSONMap, error) { - var podOverride sp.JSONMap +func GetPodSpecOverride(tp param.TemplateParams, args map[string]interface{}, argName string) (crv1alpha1.JSONMap, error) { + var podOverride crv1alpha1.JSONMap var err error if err = OptArg(args, KubeTaskPodOverrideArg, &podOverride, tp.PodOverride); err != nil { return nil, err diff --git a/pkg/function/delete_data.go b/pkg/function/delete_data.go index 8fd587575a..4f341409cf 100644 --- a/pkg/function/delete_data.go +++ b/pkg/function/delete_data.go @@ -20,10 +20,10 @@ import ( "github.com/pkg/errors" v1 "k8s.io/api/core/v1" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" kanister "github.com/kanisterio/kanister/pkg" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/format" "github.com/kanisterio/kanister/pkg/kube" "github.com/kanisterio/kanister/pkg/param" @@ -60,7 +60,7 @@ func (*deleteDataFunc) Name() string { return "DeleteData" } -func deleteData(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, reclaimSpace bool, namespace, encryptionKey string, targetPaths, deleteTags, deleteIdentifiers []string, jobPrefix string, podOverride sp.JSONMap) (map[string]interface{}, error) { +func deleteData(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, reclaimSpace bool, namespace, encryptionKey string, targetPaths, deleteTags, deleteIdentifiers []string, jobPrefix string, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) { options := &kube.PodOptions{ Namespace: namespace, GenerateName: jobPrefix, diff --git a/pkg/function/describe_backups.go b/pkg/function/describe_backups.go index 52f098a594..0c65954d51 100644 --- a/pkg/function/describe_backups.go +++ b/pkg/function/describe_backups.go @@ -20,10 +20,10 @@ import ( "github.com/pkg/errors" v1 "k8s.io/api/core/v1" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" kanister "github.com/kanisterio/kanister/pkg" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/format" "github.com/kanisterio/kanister/pkg/kube" "github.com/kanisterio/kanister/pkg/param" @@ -58,7 +58,7 @@ func (*DescribeBackupsFunc) Name() string { return "DescribeBackups" } -func describeBackups(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, encryptionKey, targetPaths, jobPrefix string, podOverride sp.JSONMap) (map[string]interface{}, error) { +func describeBackups(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, encryptionKey, targetPaths, jobPrefix string, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) { namespace, err := kube.GetControllerNamespace() if err != nil { return nil, errors.Wrapf(err, "Failed to get controller namespace") diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index 51fdc13794..77800a1249 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -19,10 +19,10 @@ import ( "github.com/pkg/errors" "k8s.io/api/core/v1" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" kanister "github.com/kanisterio/kanister/pkg" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/consts" "github.com/kanisterio/kanister/pkg/field" "github.com/kanisterio/kanister/pkg/kube" @@ -50,7 +50,7 @@ func (*kubeTaskFunc) Name() string { return "KubeTask" } -func kubeTask(ctx context.Context, cli kubernetes.Interface, namespace, image string, command []string, podOverride sp.JSONMap) (map[string]interface{}, error) { +func kubeTask(ctx context.Context, cli kubernetes.Interface, namespace, image string, command []string, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) { var serviceAccount string var err error if namespace == "" { diff --git a/pkg/function/kube_task_test.go b/pkg/function/kube_task_test.go index 112b0f715e..ac1ac6f436 100644 --- a/pkg/function/kube_task_test.go +++ b/pkg/function/kube_task_test.go @@ -22,7 +22,6 @@ import ( . "gopkg.in/check.v1" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" kanister "github.com/kanisterio/kanister/pkg" @@ -127,7 +126,7 @@ func (s *KubeTaskSuite) TestKubeTask(c *C) { StatefulSet: ¶m.StatefulSetParams{ Namespace: s.namespace, }, - PodOverride: sp.JSONMap{ + PodOverride: crv1alpha1.JSONMap{ "containers": []map[string]interface{}{ { "name": "container", diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index 090bf594cf..e3098f252e 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -21,10 +21,10 @@ import ( "github.com/pkg/errors" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" kanister "github.com/kanisterio/kanister/pkg" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/format" "github.com/kanisterio/kanister/pkg/kube" "github.com/kanisterio/kanister/pkg/param" @@ -75,7 +75,7 @@ func getVolumes(tp param.TemplateParams) (map[string]string, error) { return vols, nil } -func prepareData(ctx context.Context, cli kubernetes.Interface, namespace, serviceAccount, image string, vols map[string]string, podOverride sp.JSONMap, command ...string) (map[string]interface{}, error) { +func prepareData(ctx context.Context, cli kubernetes.Interface, namespace, serviceAccount, image string, vols map[string]string, podOverride crv1alpha1.JSONMap, command ...string) (map[string]interface{}, error) { // Validate volumes for pvc := range vols { if _, err := cli.CoreV1().PersistentVolumeClaims(namespace).Get(pvc, metav1.GetOptions{}); err != nil { diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index af124fe54a..77088fdd7c 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -20,10 +20,10 @@ import ( "github.com/pkg/errors" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" kanister "github.com/kanisterio/kanister/pkg" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/format" "github.com/kanisterio/kanister/pkg/kube" "github.com/kanisterio/kanister/pkg/param" @@ -66,10 +66,10 @@ func (*restoreDataFunc) Name() string { return "RestoreData" } -func validateAndGetOptArgs(args map[string]interface{}, tp param.TemplateParams) (string, string, string, map[string]string, string, string, sp.JSONMap, error) { +func validateAndGetOptArgs(args map[string]interface{}, tp param.TemplateParams) (string, string, string, map[string]string, string, string, crv1alpha1.JSONMap, error) { var restorePath, encryptionKey, pod, tag, id string var vols map[string]string - var podOverride sp.JSONMap + var podOverride crv1alpha1.JSONMap var err error if err = OptArg(args, RestoreDataRestorePathArg, &restorePath, "/"); err != nil { @@ -124,7 +124,7 @@ func fetchPodVolumes(pod string, tp param.TemplateParams) (map[string]string, er } func restoreData(ctx context.Context, cli kubernetes.Interface, tp param.TemplateParams, namespace, encryptionKey, backupArtifactPrefix, restorePath, backupTag, backupID, jobPrefix, image string, - vols map[string]string, podOverride sp.JSONMap) (map[string]interface{}, error) { + vols map[string]string, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) { // Validate volumes for pvc := range vols { if _, err := cli.CoreV1().PersistentVolumeClaims(namespace).Get(pvc, metav1.GetOptions{}); err != nil { @@ -178,7 +178,7 @@ func restoreDataPodFunc(cli kubernetes.Interface, tp param.TemplateParams, names func (*restoreDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) { var namespace, image, backupArtifactPrefix, backupTag, backupID string - var podOverride sp.JSONMap + var podOverride crv1alpha1.JSONMap var err error if err = Arg(args, RestoreDataNamespaceArg, &namespace); err != nil { return nil, err diff --git a/pkg/function/restore_data_all.go b/pkg/function/restore_data_all.go index af522a6870..02ebc5d6b0 100644 --- a/pkg/function/restore_data_all.go +++ b/pkg/function/restore_data_all.go @@ -22,9 +22,8 @@ import ( "github.com/pkg/errors" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" - kanister "github.com/kanisterio/kanister/pkg" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/kube" "github.com/kanisterio/kanister/pkg/param" "github.com/kanisterio/kanister/pkg/restic" @@ -62,10 +61,10 @@ func (*restoreDataAllFunc) Name() string { return "RestoreDataAll" } -func validateAndGetRestoreAllOptArgs(args map[string]interface{}, tp param.TemplateParams) (string, string, []string, sp.JSONMap, error) { +func validateAndGetRestoreAllOptArgs(args map[string]interface{}, tp param.TemplateParams) (string, string, []string, crv1alpha1.JSONMap, error) { var restorePath, encryptionKey, pods string var ps []string - var podOverride sp.JSONMap + var podOverride crv1alpha1.JSONMap var err error if err = OptArg(args, RestoreDataAllRestorePathArg, &restorePath, "/"); err != nil { diff --git a/pkg/function/restore_data_test.go b/pkg/function/restore_data_test.go index f8d8c3adef..df92d398df 100644 --- a/pkg/function/restore_data_test.go +++ b/pkg/function/restore_data_test.go @@ -17,8 +17,7 @@ package function import ( . "gopkg.in/check.v1" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" - + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/param" ) @@ -179,7 +178,7 @@ func (s *RestoreDataTestSuite) TestValidateAndGetOptArgs(c *C) { args: map[string]interface{}{ RestoreDataPodArg: "some-pod", RestoreDataBackupIdentifierArg: "backup123", - RestoreDataPodOverrideArg: sp.JSONMap{ + RestoreDataPodOverrideArg: crv1alpha1.JSONMap{ "containers": []map[string]interface{}{ { "name": "container", @@ -190,7 +189,7 @@ func (s *RestoreDataTestSuite) TestValidateAndGetOptArgs(c *C) { }, errChecker: IsNil, tp: param.TemplateParams{ - PodOverride: sp.JSONMap{ + PodOverride: crv1alpha1.JSONMap{ "dnsPolicy": "ClusterFirst", }, }, diff --git a/pkg/kube/pod.go b/pkg/kube/pod.go index c06519bf85..33664d057e 100644 --- a/pkg/kube/pod.go +++ b/pkg/kube/pod.go @@ -26,6 +26,7 @@ import ( sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" "github.com/kanisterio/kanister/pkg/log" "github.com/kanisterio/kanister/pkg/poll" ) @@ -38,7 +39,7 @@ type PodOptions struct { Command []string Volumes map[string]string ServiceAccountName string - PodOverride sp.JSONMap + PodOverride crv1alpha1.JSONMap } // CreatePod creates a pod with a single container based on the specified image @@ -146,7 +147,7 @@ func WaitForPodCompletion(ctx context.Context, cli kubernetes.Interface, namespa } // use Strategic Merge to patch default pod specs with the passed specs -func patchDefaultPodSpecs(defaultPodSpecs v1.PodSpec, override sp.JSONMap) (v1.PodSpec, error) { +func patchDefaultPodSpecs(defaultPodSpecs v1.PodSpec, override crv1alpha1.JSONMap) (v1.PodSpec, error) { // Merge default specs and override specs with StrategicMergePatch mergedPatch, err := strategicMergeJsonPatch(defaultPodSpecs, override) if err != nil { @@ -163,7 +164,7 @@ func patchDefaultPodSpecs(defaultPodSpecs v1.PodSpec, override sp.JSONMap) (v1.P } // CreateAndMergeJsonPatch uses Strategic Merge to merge two Pod spec configuration -func CreateAndMergeJsonPatch(original, override sp.JSONMap) (sp.JSONMap, error) { +func CreateAndMergeJsonPatch(original, override crv1alpha1.JSONMap) (crv1alpha1.JSONMap, error) { // Merge json specs with StrategicMerge mergedPatch, err := strategicMergeJsonPatch(original, override) if err != nil { diff --git a/pkg/kube/pod_test.go b/pkg/kube/pod_test.go index c3d8b2b61b..83b8a3ccb5 100644 --- a/pkg/kube/pod_test.go +++ b/pkg/kube/pod_test.go @@ -26,10 +26,11 @@ import ( "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/testing" + + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" ) type PodSuite struct { @@ -150,8 +151,8 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { } tests := []struct { - BlueprintPodSpecs sp.JSONMap - ActionsetPodSpecs sp.JSONMap + BlueprintPodSpecs crv1alpha1.JSONMap + ActionsetPodSpecs crv1alpha1.JSONMap Expected v1.PodSpec }{ // Blueprint and Actionset PodOverride specs are nil @@ -164,7 +165,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { // Blueprint PodOverride specs are nil { BlueprintPodSpecs: nil, - ActionsetPodSpecs: sp.JSONMap{ + ActionsetPodSpecs: crv1alpha1.JSONMap{ "restartPolicy": "Always", }, Expected: v1.PodSpec{ @@ -198,7 +199,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { // Actionset PodOverride specs are nil { - BlueprintPodSpecs: sp.JSONMap{ + BlueprintPodSpecs: crv1alpha1.JSONMap{ "containers": []map[string]interface{}{ { "name": "container", @@ -238,7 +239,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { // Modify volume mounts { - BlueprintPodSpecs: sp.JSONMap{ + BlueprintPodSpecs: crv1alpha1.JSONMap{ "containers": []map[string]interface{}{ { "name": "container", @@ -251,7 +252,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { }, }, }, - ActionsetPodSpecs: sp.JSONMap{ + ActionsetPodSpecs: crv1alpha1.JSONMap{ "volumes": []map[string]interface{}{ { "name": "data", @@ -296,12 +297,12 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { // Add NodeSelector and Tolerations { - BlueprintPodSpecs: sp.JSONMap{ + BlueprintPodSpecs: crv1alpha1.JSONMap{ "nodeSelector": map[string]interface{}{ "selector-key": "selector-value", }, }, - ActionsetPodSpecs: sp.JSONMap{ + ActionsetPodSpecs: crv1alpha1.JSONMap{ "tolerations": []map[string]interface{}{ { "key": "taint-key", @@ -353,7 +354,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { // Add NodeSelector and Tolerations. Override container command { - BlueprintPodSpecs: sp.JSONMap{ + BlueprintPodSpecs: crv1alpha1.JSONMap{ "nodeSelector": map[string]interface{}{ "selector-key": "selector-value", }, @@ -366,7 +367,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { }, }, }, - ActionsetPodSpecs: sp.JSONMap{ + ActionsetPodSpecs: crv1alpha1.JSONMap{ "containers": []map[string]interface{}{ { "name": "container", @@ -416,7 +417,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { // Override container command { - BlueprintPodSpecs: sp.JSONMap{ + BlueprintPodSpecs: crv1alpha1.JSONMap{ "containers": []map[string]interface{}{ { "name": "container", @@ -456,7 +457,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { // Override blueprint specs with actionset { - BlueprintPodSpecs: sp.JSONMap{ + BlueprintPodSpecs: crv1alpha1.JSONMap{ "containers": []map[string]interface{}{ { "name": "container", @@ -465,7 +466,7 @@ func (s *PodSuite) TestPatchDefaultPodSpecs(c *C) { }, "dnsPolicy": "Default", }, - ActionsetPodSpecs: sp.JSONMap{ + ActionsetPodSpecs: crv1alpha1.JSONMap{ "dnsPolicy": "ClusterFirst", }, Expected: v1.PodSpec{ diff --git a/pkg/param/param.go b/pkg/param/param.go index fbca9a73d7..35495ef301 100644 --- a/pkg/param/param.go +++ b/pkg/param/param.go @@ -23,7 +23,6 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" - sp "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -49,7 +48,7 @@ type TemplateParams struct { Options map[string]string Object map[string]interface{} Phases map[string]*Phase - PodOverride sp.JSONMap + PodOverride crv1alpha1.JSONMap } // StatefulSetParams are params for stateful sets.