From 6590234a59e5652db1e179a84f82f40e9a57967f Mon Sep 17 00:00:00 2001 From: Michele Zanotti Date: Wed, 3 Jul 2024 21:26:43 +0200 Subject: [PATCH] fix: don't list resources that have already been duplicated --- .DS_Store | Bin 0 -> 6148 bytes CHANGELOG.md | 6 +++ .../deployment_client.go} | 11 ++-- pkg/{pods/client.go => clients/pod_client.go} | 8 +-- .../pod_configurator.go} | 2 +- .../statefulset_client.go} | 11 ++-- pkg/cmd/deployment.go | 4 +- pkg/cmd/pod.go | 4 +- pkg/cmd/statefulset.go | 4 +- pkg/core/client.go | 2 +- pkg/core/selectors.go | 47 ++++++++++++++++++ pkg/test/mocks/pod_client.go | 2 +- pkg/utils/select.go | 2 +- 13 files changed, 77 insertions(+), 26 deletions(-) create mode 100644 .DS_Store rename pkg/{deployments/client.go => clients/deployment_client.go} (89%) rename pkg/{pods/client.go => clients/pod_client.go} (90%) rename pkg/{pods/configurator.go => clients/pod_configurator.go} (99%) rename pkg/{statefulsets/client.go => clients/statefulset_client.go} (89%) create mode 100644 pkg/core/selectors.go diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d18a21b16bd984c8bd9dfac612c509bc1a6836a8 GIT binary patch literal 6148 zcmeHK!AiqG5Z!H~O(;SR3OxqA7OYwe;w8lT14i_qQj?}?FlI}WnnNk%tUu(J_&v_- zZi-R`Pa<{(X5VCXW|MshI~m3p_omSV;V1VyY_Fnl36kGdo^?WqHDTq7*9ahQk@ zu3wj;$?+E%z;|b`hzT}i*~j-6j*~dcJDoRPs~cL=NAZlnsp)ynwOT&ux?qLwG(6UO1opNt%fC z4t&)jRWczlKnxHAE5d+2{>5S{|Cd2D5Cg=(|73t0eZTL)-gIqU+9lRn2lNIM1>;JEixePf7p) literal 0 HcmV?d00001 diff --git a/CHANGELOG.md b/CHANGELOG.md index c974d72..e6d91d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## v0.2.1 + +### Fixes + +* In interactive selection, don't list resources that have already been duplicated. + ## v0.2.0 ### New features diff --git a/pkg/deployments/client.go b/pkg/clients/deployment_client.go similarity index 89% rename from pkg/deployments/client.go rename to pkg/clients/deployment_client.go index a615976..cb4eb4f 100644 --- a/pkg/deployments/client.go +++ b/pkg/clients/deployment_client.go @@ -14,13 +14,12 @@ * limitations under the License. */ -package deployments +package clients import ( "context" "fmt" "github.com/telemaco019/duplik8s/pkg/core" - "github.com/telemaco019/duplik8s/pkg/pods" "github.com/telemaco019/duplik8s/pkg/utils" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -32,7 +31,7 @@ type DeploymentClient struct { ctx context.Context } -func NewClient(opts utils.KubeOptions) (*DeploymentClient, error) { +func NewDeploymentClient(opts utils.KubeOptions) (*DeploymentClient, error) { clientset, err := utils.NewClientset(opts.Kubeconfig, opts.Kubecontext) if err != nil { return nil, err @@ -43,8 +42,8 @@ func NewClient(opts utils.KubeOptions) (*DeploymentClient, error) { }, nil } -func (c *DeploymentClient) List(namespace string) ([]core.DuplicableObject, error) { - deployments, err := c.clientset.AppsV1().Deployments(namespace).List(c.ctx, metav1.ListOptions{}) +func (c *DeploymentClient) ListDuplicable(namespace string) ([]core.DuplicableObject, error) { + deployments, err := c.clientset.AppsV1().Deployments(namespace).List(c.ctx, core.NewDuplicableListOptions()) if err != nil { return nil, err } @@ -85,7 +84,7 @@ func (c *DeploymentClient) Duplicate(obj core.DuplicableObject, opts core.PodOve } // override the spec of the deployment's pod - configurator := pods.NewConfigurator(c.clientset, opts) + configurator := NewConfigurator(c.clientset, opts) err = configurator.OverrideSpec(c.ctx, obj.Namespace, &newDeploy.Spec.Template.Spec) if err != nil { return err diff --git a/pkg/pods/client.go b/pkg/clients/pod_client.go similarity index 90% rename from pkg/pods/client.go rename to pkg/clients/pod_client.go index 8a0ace4..2ea0db8 100644 --- a/pkg/pods/client.go +++ b/pkg/clients/pod_client.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package pods +package clients import ( "context" @@ -31,7 +31,7 @@ type PodClient struct { ctx context.Context } -func NewClient(opts utils.KubeOptions) (*PodClient, error) { +func NewPodClient(opts utils.KubeOptions) (*PodClient, error) { clientset, err := utils.NewClientset(opts.Kubeconfig, opts.Kubecontext) if err != nil { return nil, err @@ -42,8 +42,8 @@ func NewClient(opts utils.KubeOptions) (*PodClient, error) { }, nil } -func (c *PodClient) List(namespace string) ([]core.DuplicableObject, error) { - pods, err := c.clientset.CoreV1().Pods(namespace).List(c.ctx, metav1.ListOptions{}) +func (c *PodClient) ListDuplicable(namespace string) ([]core.DuplicableObject, error) { + pods, err := c.clientset.CoreV1().Pods(namespace).List(c.ctx, core.NewDuplicableListOptions()) if err != nil { return nil, err } diff --git a/pkg/pods/configurator.go b/pkg/clients/pod_configurator.go similarity index 99% rename from pkg/pods/configurator.go rename to pkg/clients/pod_configurator.go index 9ea5dcd..4bde007 100644 --- a/pkg/pods/configurator.go +++ b/pkg/clients/pod_configurator.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package pods +package clients import ( "context" diff --git a/pkg/statefulsets/client.go b/pkg/clients/statefulset_client.go similarity index 89% rename from pkg/statefulsets/client.go rename to pkg/clients/statefulset_client.go index d4c74e8..6a7f2c6 100644 --- a/pkg/statefulsets/client.go +++ b/pkg/clients/statefulset_client.go @@ -14,13 +14,12 @@ * limitations under the License. */ -package statefulsets +package clients import ( "context" "fmt" "github.com/telemaco019/duplik8s/pkg/core" - "github.com/telemaco019/duplik8s/pkg/pods" "github.com/telemaco019/duplik8s/pkg/utils" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -32,7 +31,7 @@ type StatefulSetClient struct { ctx context.Context } -func NewClient(opts utils.KubeOptions) (*StatefulSetClient, error) { +func NewStatefulSetClient(opts utils.KubeOptions) (*StatefulSetClient, error) { clientset, err := utils.NewClientset(opts.Kubeconfig, opts.Kubecontext) if err != nil { return nil, err @@ -43,8 +42,8 @@ func NewClient(opts utils.KubeOptions) (*StatefulSetClient, error) { }, nil } -func (c *StatefulSetClient) List(namespace string) ([]core.DuplicableObject, error) { - statefulSets, err := c.clientset.AppsV1().StatefulSets(namespace).List(c.ctx, metav1.ListOptions{}) +func (c *StatefulSetClient) ListDuplicable(namespace string) ([]core.DuplicableObject, error) { + statefulSets, err := c.clientset.AppsV1().StatefulSets(namespace).List(c.ctx, core.NewDuplicableListOptions()) if err != nil { return nil, err } @@ -85,7 +84,7 @@ func (c *StatefulSetClient) Duplicate(obj core.DuplicableObject, opts core.PodOv } // override the spec of the statefulset's pod - configurator := pods.NewConfigurator(c.clientset, opts) + configurator := NewConfigurator(c.clientset, opts) err = configurator.OverrideSpec(c.ctx, obj.Namespace, &newStatefulSet.Spec.Template.Spec) if err != nil { return err diff --git a/pkg/cmd/deployment.go b/pkg/cmd/deployment.go index 6c56427..060eaed 100644 --- a/pkg/cmd/deployment.go +++ b/pkg/cmd/deployment.go @@ -18,15 +18,15 @@ package cmd import ( "github.com/spf13/cobra" + "github.com/telemaco019/duplik8s/pkg/clients" "github.com/telemaco019/duplik8s/pkg/core" - "github.com/telemaco019/duplik8s/pkg/deployments" "github.com/telemaco019/duplik8s/pkg/utils" ) func NewDeployCmd(client core.Duplik8sClient) *cobra.Command { factory := func(opts utils.KubeOptions) (core.Duplik8sClient, error) { if client == nil { - return deployments.NewClient(opts) + return clients.NewDeploymentClient(opts) } return client, nil } diff --git a/pkg/cmd/pod.go b/pkg/cmd/pod.go index cbc7e16..24646fe 100644 --- a/pkg/cmd/pod.go +++ b/pkg/cmd/pod.go @@ -18,15 +18,15 @@ package cmd import ( "github.com/spf13/cobra" + "github.com/telemaco019/duplik8s/pkg/clients" "github.com/telemaco019/duplik8s/pkg/core" - "github.com/telemaco019/duplik8s/pkg/pods" "github.com/telemaco019/duplik8s/pkg/utils" ) func NewPodCmd(podClient core.Duplik8sClient) *cobra.Command { factory := func(opts utils.KubeOptions) (core.Duplik8sClient, error) { if podClient == nil { - return pods.NewClient(opts) + return clients.NewPodClient(opts) } return podClient, nil } diff --git a/pkg/cmd/statefulset.go b/pkg/cmd/statefulset.go index 5417ea9..65cde3b 100644 --- a/pkg/cmd/statefulset.go +++ b/pkg/cmd/statefulset.go @@ -18,15 +18,15 @@ package cmd import ( "github.com/spf13/cobra" + "github.com/telemaco019/duplik8s/pkg/clients" "github.com/telemaco019/duplik8s/pkg/core" - "github.com/telemaco019/duplik8s/pkg/statefulsets" "github.com/telemaco019/duplik8s/pkg/utils" ) func NewStatefulSetCmd(client core.Duplik8sClient) *cobra.Command { factory := func(opts utils.KubeOptions) (core.Duplik8sClient, error) { if client == nil { - return statefulsets.NewClient(opts) + return clients.NewStatefulSetClient(opts) } return client, nil } diff --git a/pkg/core/client.go b/pkg/core/client.go index 6764c99..619347a 100644 --- a/pkg/core/client.go +++ b/pkg/core/client.go @@ -17,6 +17,6 @@ package core type Duplik8sClient interface { - List(namespace string) ([]DuplicableObject, error) + ListDuplicable(namespace string) ([]DuplicableObject, error) Duplicate(obj DuplicableObject, opts PodOverrideOptions) error } diff --git a/pkg/core/selectors.go b/pkg/core/selectors.go new file mode 100644 index 0000000..e21cce5 --- /dev/null +++ b/pkg/core/selectors.go @@ -0,0 +1,47 @@ +/* + * Copyright 2024 Michele Zanotti + * + * 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 core + +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + +func NewDuplicableListOptions() metav1.ListOptions { + selector := metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: LABEL_DUPLICATED, + Operator: metav1.LabelSelectorOpDoesNotExist, + }, + }, + } + return metav1.ListOptions{ + LabelSelector: metav1.FormatLabelSelector(&selector), + } +} + +func NewDuplicatedListOptions() metav1.ListOptions { + selector := metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: LABEL_DUPLICATED, + Operator: metav1.LabelSelectorOpExists, + }, + }, + } + return metav1.ListOptions{ + LabelSelector: metav1.FormatLabelSelector(&selector), + } +} diff --git a/pkg/test/mocks/pod_client.go b/pkg/test/mocks/pod_client.go index e340d27..58535a8 100644 --- a/pkg/test/mocks/pod_client.go +++ b/pkg/test/mocks/pod_client.go @@ -51,7 +51,7 @@ func NewPodClient( } } -func (c *PodClient) List(_ string) ([]core.DuplicableObject, error) { +func (c *PodClient) ListDuplicable(_ string) ([]core.DuplicableObject, error) { return c.ListPodsResult.Objs, c.ListPodsResult.Err } diff --git a/pkg/utils/select.go b/pkg/utils/select.go index 19fcde7..ae677d8 100644 --- a/pkg/utils/select.go +++ b/pkg/utils/select.go @@ -24,7 +24,7 @@ import ( func SelectItem(client core.Duplik8sClient, namespace, selectMessage string) (core.DuplicableObject, error) { var selected = core.DuplicableObject{} - objs, err := client.List(namespace) + objs, err := client.ListDuplicable(namespace) if err != nil { return selected, err }