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..f309b3a --- /dev/null +++ b/pkg/core/selectors.go @@ -0,0 +1,31 @@ +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 }