Refactor suspend commands
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
committed by
Hidde Beydals
parent
5b268f62a3
commit
ef579fe596
@@ -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{
|
func (obj helmReleaseAdapter) setSuspended() {
|
||||||
Namespace: rootArgs.namespace,
|
obj.HelmRelease.Spec.Suspend = true
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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{
|
func (obj kustomizationAdapter) setSuspended() {
|
||||||
Namespace: rootArgs.namespace,
|
obj.Kustomization.Spec.Suspend = true
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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{
|
func (obj bucketAdapter) setSuspended() {
|
||||||
Namespace: rootArgs.namespace,
|
obj.Bucket.Spec.Suspend = true
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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{
|
func (obj helmChartAdapter) setSuspended() {
|
||||||
Namespace: rootArgs.namespace,
|
obj.HelmChart.Spec.Suspend = true
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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{
|
func (obj gitRepositoryAdapter) setSuspended() {
|
||||||
Namespace: rootArgs.namespace,
|
obj.GitRepository.Spec.Suspend = true
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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{
|
func (obj helmRepositoryAdapter) setSuspended() {
|
||||||
Namespace: rootArgs.namespace,
|
obj.HelmRepository.Spec.Suspend = true
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user