Skip to content

Commit

Permalink
fix: don't list resources that have already been duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
Telemaco019 committed Jul 3, 2024
1 parent 7de33aa commit 7cb3a3f
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 5 additions & 6 deletions pkg/deployments/client.go → pkg/clients/deployment_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/pods/client.go → pkg/clients/pod_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package pods
package clients

import (
"context"
Expand All @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package pods
package clients

import (
"context"
Expand Down
11 changes: 5 additions & 6 deletions pkg/statefulsets/client.go → pkg/clients/statefulset_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions pkg/core/selectors.go
Original file line number Diff line number Diff line change
@@ -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),
}
}
2 changes: 1 addition & 1 deletion pkg/test/mocks/pod_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 7cb3a3f

Please sign in to comment.