Merge pull request #791 from SomtochiAma/refactor-reconcile-commands

Refactor suspend commands
pull/780/head v0.7.3
Hidde Beydals 4 years ago committed by GitHub
commit 29c46a9892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,14 +17,8 @@ limitations under the License.
package main package main
import ( 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" helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/spf13/cobra"
) )
var suspendHrCmd = &cobra.Command{ var suspendHrCmd = &cobra.Command{
@ -35,43 +29,20 @@ var suspendHrCmd = &cobra.Command{
Example: ` # Suspend reconciliation for an existing Helm release Example: ` # Suspend reconciliation for an existing Helm release
flux suspend hr podinfo flux suspend hr podinfo
`, `,
RunE: suspendHrCmdRun, RunE: suspendCommand{
apiType: helmReleaseType,
object: &helmReleaseAdapter{&helmv2.HelmRelease{}},
}.run,
} }
func init() { func init() {
suspendCmd.AddCommand(suspendHrCmd) suspendCmd.AddCommand(suspendHrCmd)
} }
func suspendHrCmdRun(cmd *cobra.Command, args []string) error { func (obj helmReleaseAdapter) isSuspended() bool {
if len(args) < 1 { return obj.HelmRelease.Spec.Suspend
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")
return nil func (obj helmReleaseAdapter) setSuspended() {
obj.HelmRelease.Spec.Suspend = true
} }

@ -17,13 +17,8 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt"
"github.com/fluxcd/flux2/internal/utils"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
) )
var suspendKsCmd = &cobra.Command{ var suspendKsCmd = &cobra.Command{
@ -34,43 +29,20 @@ var suspendKsCmd = &cobra.Command{
Example: ` # Suspend reconciliation for an existing Kustomization Example: ` # Suspend reconciliation for an existing Kustomization
flux suspend ks podinfo flux suspend ks podinfo
`, `,
RunE: suspendKsCmdRun, RunE: suspendCommand{
apiType: kustomizationType,
object: kustomizationAdapter{&kustomizev1.Kustomization{}},
}.run,
} }
func init() { func init() {
suspendCmd.AddCommand(suspendKsCmd) suspendCmd.AddCommand(suspendKsCmd)
} }
func suspendKsCmdRun(cmd *cobra.Command, args []string) error { func (obj kustomizationAdapter) isSuspended() bool {
if len(args) < 1 { return obj.Kustomization.Spec.Suspend
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")
return nil func (obj kustomizationAdapter) setSuspended() {
obj.Kustomization.Spec.Suspend = true
} }

@ -17,13 +17,8 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/flux2/internal/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
) )
var suspendSourceBucketCmd = &cobra.Command{ var suspendSourceBucketCmd = &cobra.Command{
@ -33,43 +28,20 @@ var suspendSourceBucketCmd = &cobra.Command{
Example: ` # Suspend reconciliation for an existing Bucket Example: ` # Suspend reconciliation for an existing Bucket
flux suspend source bucket podinfo flux suspend source bucket podinfo
`, `,
RunE: suspendSourceBucketCmdRun, RunE: suspendCommand{
apiType: bucketType,
object: bucketAdapter{&sourcev1.Bucket{}},
}.run,
} }
func init() { func init() {
suspendSourceCmd.AddCommand(suspendSourceBucketCmd) suspendSourceCmd.AddCommand(suspendSourceBucketCmd)
} }
func suspendSourceBucketCmdRun(cmd *cobra.Command, args []string) error { func (obj bucketAdapter) isSuspended() bool {
if len(args) < 1 { return obj.Bucket.Spec.Suspend
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")
return nil func (obj bucketAdapter) setSuspended() {
obj.Bucket.Spec.Suspend = true
} }

@ -17,13 +17,9 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/flux2/internal/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
) )
var suspendSourceHelmChartCmd = &cobra.Command{ var suspendSourceHelmChartCmd = &cobra.Command{
@ -33,43 +29,20 @@ var suspendSourceHelmChartCmd = &cobra.Command{
Example: ` # Suspend reconciliation for an existing HelmChart Example: ` # Suspend reconciliation for an existing HelmChart
flux suspend source chart podinfo flux suspend source chart podinfo
`, `,
RunE: suspendSourceHelmChartCmdRun, RunE: suspendCommand{
apiType: helmChartType,
object: helmChartAdapter{&sourcev1.HelmChart{}},
}.run,
} }
func init() { func init() {
suspendSourceCmd.AddCommand(suspendSourceHelmChartCmd) suspendSourceCmd.AddCommand(suspendSourceHelmChartCmd)
} }
func suspendSourceHelmChartCmdRun(cmd *cobra.Command, args []string) error { func (obj helmChartAdapter) isSuspended() bool {
if len(args) < 1 { return obj.HelmChart.Spec.Suspend
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")
return nil func (obj helmChartAdapter) setSuspended() {
obj.HelmChart.Spec.Suspend = true
} }

@ -17,13 +17,9 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/flux2/internal/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
) )
var suspendSourceGitCmd = &cobra.Command{ var suspendSourceGitCmd = &cobra.Command{
@ -33,43 +29,20 @@ var suspendSourceGitCmd = &cobra.Command{
Example: ` # Suspend reconciliation for an existing GitRepository Example: ` # Suspend reconciliation for an existing GitRepository
flux suspend source git podinfo flux suspend source git podinfo
`, `,
RunE: suspendSourceGitCmdRun, RunE: suspendCommand{
apiType: gitRepositoryType,
object: gitRepositoryAdapter{&sourcev1.GitRepository{}},
}.run,
} }
func init() { func init() {
suspendSourceCmd.AddCommand(suspendSourceGitCmd) suspendSourceCmd.AddCommand(suspendSourceGitCmd)
} }
func suspendSourceGitCmdRun(cmd *cobra.Command, args []string) error { func (obj gitRepositoryAdapter) isSuspended() bool {
if len(args) < 1 { return obj.GitRepository.Spec.Suspend
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")
return nil func (obj gitRepositoryAdapter) setSuspended() {
obj.GitRepository.Spec.Suspend = true
} }

@ -17,13 +17,9 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/flux2/internal/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
) )
var suspendSourceHelmCmd = &cobra.Command{ var suspendSourceHelmCmd = &cobra.Command{
@ -33,43 +29,20 @@ var suspendSourceHelmCmd = &cobra.Command{
Example: ` # Suspend reconciliation for an existing HelmRepository Example: ` # Suspend reconciliation for an existing HelmRepository
flux suspend source helm bitnami flux suspend source helm bitnami
`, `,
RunE: suspendSourceHelmCmdRun, RunE: suspendCommand{
apiType: helmRepositoryType,
object: helmRepositoryAdapter{&sourcev1.HelmRepository{}},
}.run,
} }
func init() { func init() {
suspendSourceCmd.AddCommand(suspendSourceHelmCmd) suspendSourceCmd.AddCommand(suspendSourceHelmCmd)
} }
func suspendSourceHelmCmdRun(cmd *cobra.Command, args []string) error { func (obj helmRepositoryAdapter) isSuspended() bool {
if len(args) < 1 { return obj.HelmRepository.Spec.Suspend
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")
return nil func (obj helmRepositoryAdapter) setSuspended() {
obj.HelmRepository.Spec.Suspend = true
} }

Loading…
Cancel
Save