1
0
mirror of synced 2026-02-06 19:05:55 +00:00

Add graceful shutdown when interrupted

If implemented this permit restoring a clean state in case of signal
interruption.

Signed-off-by: Soule BA <soule@weave.works>
This commit is contained in:
Soule BA
2022-01-12 11:50:19 +01:00
parent f7d9ee90cd
commit 306f8f5715
23 changed files with 134 additions and 800 deletions

View File

@@ -19,10 +19,11 @@ package main
import (
"fmt"
"os"
"os/signal"
"github.com/spf13/cobra"
"github.com/fluxcd/flux2/internal/kustomization"
"github.com/fluxcd/flux2/internal/build"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
)
@@ -33,7 +34,7 @@ var buildKsCmd = &cobra.Command{
Long: `The build command queries the Kubernetes API and fetches the specified Flux Kustomization.
It then uses the fetched in cluster flux kustomization to perform needed transformation on the local kustomization.yaml
pointed at by --path. The local kustomization.yaml is generated if it does not exist. Finally it builds the overlays using the local kustomization.yaml, and write the resulting multi-doc YAML to stdout.`,
Example: `# Create a new overlay.
Example: `# Build the local manifests as they were built on the cluster
flux build kustomization my-app --path ./path/to/local/manifests`,
ValidArgsFunction: resourceNamesCompletionFunc(kustomizev1.GroupVersion.WithKind(kustomizev1.KustomizationKind)),
RunE: buildKsCmdRun,
@@ -64,17 +65,35 @@ func buildKsCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("invalid resource path %q", buildKsArgs.path)
}
builder, err := kustomization.NewBuilder(kubeconfigArgs, name, buildKsArgs.path, kustomization.WithTimeout(rootArgs.timeout))
builder, err := build.NewBuilder(kubeconfigArgs, name, buildKsArgs.path, build.WithTimeout(rootArgs.timeout))
if err != nil {
return err
}
manifests, err := builder.Build()
if err != nil {
return err
}
// create a signal channel
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt)
cmd.Print(string(manifests))
errChan := make(chan error)
go func() {
manifests, err := builder.Build()
if err != nil {
errChan <- err
}
cmd.Print(string(manifests))
errChan <- nil
}()
select {
case <-sigc:
fmt.Println("Build cancelled... exiting.")
return builder.Cancel()
case err := <-errChan:
if err != nil {
return err
}
}
return nil

View File

@@ -19,10 +19,11 @@ package main
import (
"fmt"
"os"
"os/signal"
"github.com/spf13/cobra"
"github.com/fluxcd/flux2/internal/kustomization"
"github.com/fluxcd/flux2/internal/build"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
)
@@ -31,7 +32,7 @@ var diffKsCmd = &cobra.Command{
Aliases: []string{"ks"},
Short: "Diff Kustomization",
Long: `The diff command does a build, then it performs a server-side dry-run and output the diff.`,
Example: `# Create a new overlay.
Example: `# Preview changes local changes as they were applied on the cluster
flux diff kustomization my-app --path ./path/to/local/manifests`,
ValidArgsFunction: resourceNamesCompletionFunc(kustomizev1.GroupVersion.WithKind(kustomizev1.KustomizationKind)),
RunE: diffKsCmdRun,
@@ -44,7 +45,7 @@ type diffKsFlags struct {
var diffKsArgs diffKsFlags
func init() {
diffKsCmd.Flags().StringVar(&diffKsArgs.path, "path", "", "Name of a file containing a file to add to the kustomization file.)")
diffKsCmd.Flags().StringVar(&diffKsArgs.path, "path", "", "Path to a local directory that matches the specified Kustomization.spec.path.)")
diffCmd.AddCommand(diffKsCmd)
}
@@ -62,17 +63,35 @@ func diffKsCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("invalid resource path %q", diffKsArgs.path)
}
builder, err := kustomization.NewBuilder(kubeconfigArgs, name, diffKsArgs.path, kustomization.WithTimeout(rootArgs.timeout))
builder, err := build.NewBuilder(kubeconfigArgs, name, diffKsArgs.path, build.WithTimeout(rootArgs.timeout))
if err != nil {
return err
}
output, err := builder.Diff()
if err != nil {
return err
}
// create a signal channel
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt)
cmd.Print(output)
errChan := make(chan error)
go func() {
output, err := builder.Diff()
if err != nil {
errChan <- err
}
cmd.Print(output)
errChan <- nil
}()
select {
case <-sigc:
fmt.Println("Build cancelled... exiting.")
return builder.Cancel()
case err := <-errChan:
if err != nil {
return err
}
}
return nil

View File

@@ -25,7 +25,7 @@ import (
"strings"
"testing"
"github.com/fluxcd/flux2/internal/kustomization"
"github.com/fluxcd/flux2/internal/build"
"github.com/fluxcd/pkg/ssa"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
@@ -85,7 +85,7 @@ func TestDiffKustomization(t *testing.T) {
"fluxns": allocateNamespace("flux-system"),
}
b, _ := kustomization.NewBuilder(kubeconfigArgs, "podinfo", "")
b, _ := build.NewBuilder(kubeconfigArgs, "podinfo", "")
resourceManager, err := b.Manager()
if err != nil {

View File

@@ -71,4 +71,4 @@ spec:
memory: 512Mi
requests:
cpu: 100m
memory: 64Mi
memory: 64Mi

View File

@@ -17,4 +17,4 @@ spec:
type: Utilization
# scale up if usage is above
# 99% of the requested CPU (100m)
averageUtilization: 99
averageUtilization: 99

View File

@@ -12,4 +12,4 @@ spec:
sourceRef:
kind: GitRepository
name: podinfo
targetNamespace: default
targetNamespace: default

View File

@@ -13,4 +13,4 @@ spec:
interval: 30s
ref:
branch: master
url: https://github.com/stefanprodan/podinfo
url: https://github.com/stefanprodan/podinfo

View File

@@ -71,4 +71,4 @@ spec:
memory: 512Mi
requests:
cpu: 100m
memory: 64Mi
memory: 64Mi

View File

@@ -17,4 +17,4 @@ spec:
type: Utilization
# scale up if usage is above
# 99% of the requested CPU (100m)
averageUtilization: 99
averageUtilization: 99

View File

@@ -14,4 +14,4 @@ spec:
- port: 9999
targetPort: grpc
protocol: TCP
name: grpc
name: grpc

View File

@@ -75,4 +75,4 @@ spec:
memory: 512Mi
requests:
cpu: 100m
memory: 64Mi
memory: 64Mi

View File

@@ -8,4 +8,4 @@ secretGenerator:
- literals:
- username=admin
- password=1f2d1e2e67df
name: secret-basic-auth
name: secret-basic-auth

View File

@@ -9,4 +9,4 @@ metadata:
kustomize.toolkit.fluxcd.io/namespace: {{ .fluxns }}
name: db-user-pass-bkbd782d2c
namespace: default
type: Opaque
type: Opaque

View File

@@ -18,4 +18,4 @@ spec:
- port: 9999
targetPort: grpc
protocol: TCP
name: grpc
name: grpc

View File

@@ -8,4 +8,4 @@ metadata:
kustomize.toolkit.fluxcd.io/namespace: {{ .fluxns }}
name: podinfo-token-77t89m9b67
namespace: default
type: Opaque
type: Opaque