diff --git a/cmd/flux/suspend_helmrelease.go b/cmd/flux/suspend_helmrelease.go index 6536f15f..fd76f5d4 100644 --- a/cmd/flux/suspend_helmrelease.go +++ b/cmd/flux/suspend_helmrelease.go @@ -17,14 +17,8 @@ limitations under the License. package main import ( - "context" - "fmt" - - "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" - - "github.com/fluxcd/flux2/internal/utils" helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" + "github.com/spf13/cobra" ) var suspendHrCmd = &cobra.Command{ @@ -35,43 +29,20 @@ var suspendHrCmd = &cobra.Command{ Example: ` # Suspend reconciliation for an existing Helm release flux suspend hr podinfo `, - RunE: suspendHrCmdRun, + RunE: suspendCommand{ + apiType: helmReleaseType, + object: &helmReleaseAdapter{&helmv2.HelmRelease{}}, + }.run, } func init() { suspendCmd.AddCommand(suspendHrCmd) } -func suspendHrCmdRun(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("HelmRelease name is required") - } - name := args[0] - - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) - defer cancel() - - kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) - if err != nil { - return err - } - - namespacedName := types.NamespacedName{ - Namespace: rootArgs.namespace, - Name: name, - } - var helmRelease helmv2.HelmRelease - err = kubeClient.Get(ctx, namespacedName, &helmRelease) - if err != nil { - return err - } - - logger.Actionf("suspending HelmRelease %s in %s namespace", name, rootArgs.namespace) - helmRelease.Spec.Suspend = true - if err := kubeClient.Update(ctx, &helmRelease); err != nil { - return err - } - logger.Successf("HelmRelease suspended") +func (obj helmReleaseAdapter) isSuspended() bool { + return obj.HelmRelease.Spec.Suspend +} - return nil +func (obj helmReleaseAdapter) setSuspended() { + obj.HelmRelease.Spec.Suspend = true } diff --git a/cmd/flux/suspend_kustomization.go b/cmd/flux/suspend_kustomization.go index dd2a808e..ecb336c4 100644 --- a/cmd/flux/suspend_kustomization.go +++ b/cmd/flux/suspend_kustomization.go @@ -17,13 +17,8 @@ limitations under the License. package main import ( - "context" - "fmt" - - "github.com/fluxcd/flux2/internal/utils" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1" "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" ) var suspendKsCmd = &cobra.Command{ @@ -34,43 +29,20 @@ var suspendKsCmd = &cobra.Command{ Example: ` # Suspend reconciliation for an existing Kustomization flux suspend ks podinfo `, - RunE: suspendKsCmdRun, + RunE: suspendCommand{ + apiType: kustomizationType, + object: kustomizationAdapter{&kustomizev1.Kustomization{}}, + }.run, } func init() { suspendCmd.AddCommand(suspendKsCmd) } -func suspendKsCmdRun(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("kustomization name is required") - } - name := args[0] - - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) - defer cancel() - - kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) - if err != nil { - return err - } - - namespacedName := types.NamespacedName{ - Namespace: rootArgs.namespace, - Name: name, - } - var kustomization kustomizev1.Kustomization - err = kubeClient.Get(ctx, namespacedName, &kustomization) - if err != nil { - return err - } - - logger.Actionf("suspending kustomization %s in %s namespace", name, rootArgs.namespace) - kustomization.Spec.Suspend = true - if err := kubeClient.Update(ctx, &kustomization); err != nil { - return err - } - logger.Successf("kustomization suspended") +func (obj kustomizationAdapter) isSuspended() bool { + return obj.Kustomization.Spec.Suspend +} - return nil +func (obj kustomizationAdapter) setSuspended() { + obj.Kustomization.Spec.Suspend = true } diff --git a/cmd/flux/suspend_source_bucket.go b/cmd/flux/suspend_source_bucket.go index dd0b9f36..3b44c8ad 100644 --- a/cmd/flux/suspend_source_bucket.go +++ b/cmd/flux/suspend_source_bucket.go @@ -17,13 +17,8 @@ limitations under the License. package main import ( - "context" - "fmt" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" - - "github.com/fluxcd/flux2/internal/utils" "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" ) var suspendSourceBucketCmd = &cobra.Command{ @@ -33,43 +28,20 @@ var suspendSourceBucketCmd = &cobra.Command{ Example: ` # Suspend reconciliation for an existing Bucket flux suspend source bucket podinfo `, - RunE: suspendSourceBucketCmdRun, + RunE: suspendCommand{ + apiType: bucketType, + object: bucketAdapter{&sourcev1.Bucket{}}, + }.run, } func init() { suspendSourceCmd.AddCommand(suspendSourceBucketCmd) } -func suspendSourceBucketCmdRun(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("source name is required") - } - name := args[0] - - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) - defer cancel() - - kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) - if err != nil { - return err - } - - namespacedName := types.NamespacedName{ - Namespace: rootArgs.namespace, - Name: name, - } - var bucket sourcev1.Bucket - err = kubeClient.Get(ctx, namespacedName, &bucket) - if err != nil { - return err - } - - logger.Actionf("suspending source %s in %s namespace", name, rootArgs.namespace) - bucket.Spec.Suspend = true - if err := kubeClient.Update(ctx, &bucket); err != nil { - return err - } - logger.Successf("source suspended") +func (obj bucketAdapter) isSuspended() bool { + return obj.Bucket.Spec.Suspend +} - return nil +func (obj bucketAdapter) setSuspended() { + obj.Bucket.Spec.Suspend = true } diff --git a/cmd/flux/suspend_source_chart.go b/cmd/flux/suspend_source_chart.go index 28d2c0d9..45715ebf 100644 --- a/cmd/flux/suspend_source_chart.go +++ b/cmd/flux/suspend_source_chart.go @@ -17,13 +17,9 @@ limitations under the License. package main import ( - "context" - "fmt" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" - "github.com/fluxcd/flux2/internal/utils" "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" ) var suspendSourceHelmChartCmd = &cobra.Command{ @@ -33,43 +29,20 @@ var suspendSourceHelmChartCmd = &cobra.Command{ Example: ` # Suspend reconciliation for an existing HelmChart flux suspend source chart podinfo `, - RunE: suspendSourceHelmChartCmdRun, + RunE: suspendCommand{ + apiType: helmChartType, + object: helmChartAdapter{&sourcev1.HelmChart{}}, + }.run, } func init() { suspendSourceCmd.AddCommand(suspendSourceHelmChartCmd) } -func suspendSourceHelmChartCmdRun(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("source name is required") - } - name := args[0] - - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) - defer cancel() - - kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) - if err != nil { - return err - } - - namespacedName := types.NamespacedName{ - Namespace: rootArgs.namespace, - Name: name, - } - var chart sourcev1.HelmChart - err = kubeClient.Get(ctx, namespacedName, &chart) - if err != nil { - return err - } - - logger.Actionf("suspending source %s in %s namespace", name, rootArgs.namespace) - chart.Spec.Suspend = true - if err := kubeClient.Update(ctx, &chart); err != nil { - return err - } - logger.Successf("source suspended") +func (obj helmChartAdapter) isSuspended() bool { + return obj.HelmChart.Spec.Suspend +} - return nil +func (obj helmChartAdapter) setSuspended() { + obj.HelmChart.Spec.Suspend = true } diff --git a/cmd/flux/suspend_source_git.go b/cmd/flux/suspend_source_git.go index 4d20cc26..810ff1bf 100644 --- a/cmd/flux/suspend_source_git.go +++ b/cmd/flux/suspend_source_git.go @@ -17,13 +17,9 @@ limitations under the License. package main import ( - "context" - "fmt" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" - "github.com/fluxcd/flux2/internal/utils" "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" ) var suspendSourceGitCmd = &cobra.Command{ @@ -33,43 +29,20 @@ var suspendSourceGitCmd = &cobra.Command{ Example: ` # Suspend reconciliation for an existing GitRepository flux suspend source git podinfo `, - RunE: suspendSourceGitCmdRun, + RunE: suspendCommand{ + apiType: gitRepositoryType, + object: gitRepositoryAdapter{&sourcev1.GitRepository{}}, + }.run, } func init() { suspendSourceCmd.AddCommand(suspendSourceGitCmd) } -func suspendSourceGitCmdRun(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("source name is required") - } - name := args[0] - - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) - defer cancel() - - kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) - if err != nil { - return err - } - - namespacedName := types.NamespacedName{ - Namespace: rootArgs.namespace, - Name: name, - } - var repository sourcev1.GitRepository - err = kubeClient.Get(ctx, namespacedName, &repository) - if err != nil { - return err - } - - logger.Actionf("suspending source %s in %s namespace", name, rootArgs.namespace) - repository.Spec.Suspend = true - if err := kubeClient.Update(ctx, &repository); err != nil { - return err - } - logger.Successf("source suspended") +func (obj gitRepositoryAdapter) isSuspended() bool { + return obj.GitRepository.Spec.Suspend +} - return nil +func (obj gitRepositoryAdapter) setSuspended() { + obj.GitRepository.Spec.Suspend = true } diff --git a/cmd/flux/suspend_source_helm.go b/cmd/flux/suspend_source_helm.go index 2b4d00b5..c2a672f9 100644 --- a/cmd/flux/suspend_source_helm.go +++ b/cmd/flux/suspend_source_helm.go @@ -17,13 +17,9 @@ limitations under the License. package main import ( - "context" - "fmt" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" - "github.com/fluxcd/flux2/internal/utils" "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" ) var suspendSourceHelmCmd = &cobra.Command{ @@ -33,43 +29,20 @@ var suspendSourceHelmCmd = &cobra.Command{ Example: ` # Suspend reconciliation for an existing HelmRepository flux suspend source helm bitnami `, - RunE: suspendSourceHelmCmdRun, + RunE: suspendCommand{ + apiType: helmRepositoryType, + object: helmRepositoryAdapter{&sourcev1.HelmRepository{}}, + }.run, } func init() { suspendSourceCmd.AddCommand(suspendSourceHelmCmd) } -func suspendSourceHelmCmdRun(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("source name is required") - } - name := args[0] - - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) - defer cancel() - - kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) - if err != nil { - return err - } - - namespacedName := types.NamespacedName{ - Namespace: rootArgs.namespace, - Name: name, - } - var repository sourcev1.HelmRepository - err = kubeClient.Get(ctx, namespacedName, &repository) - if err != nil { - return err - } - - logger.Actionf("suspending source %s in %s namespace", name, rootArgs.namespace) - repository.Spec.Suspend = true - if err := kubeClient.Update(ctx, &repository); err != nil { - return err - } - logger.Successf("source suspended") +func (obj helmRepositoryAdapter) isSuspended() bool { + return obj.HelmRepository.Spec.Suspend +} - return nil +func (obj helmRepositoryAdapter) setSuspended() { + obj.HelmRepository.Spec.Suspend = true }