Skip to content

Commit

Permalink
Enhancement: Add suspend/resume buttons (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: Laszlo Fogas <[email protected]>
  • Loading branch information
dzsak and laszlocph authored Apr 8, 2024
1 parent 9df18da commit a989ed2
Show file tree
Hide file tree
Showing 15 changed files with 741 additions and 9 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/onsi/gomega v1.31.1 // indirect
github.com/opencontainers/go-digest v1.0.1-0.20231025023718-d50d2fec9c98 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
Expand Down Expand Up @@ -136,6 +137,7 @@ require (
github.com/fluxcd/image-reflector-controller/api v0.31.2
github.com/fluxcd/kustomize-controller/api v1.2.2
github.com/fluxcd/notification-controller/api v1.2.4
github.com/fluxcd/pkg/runtime v0.44.1
github.com/fluxcd/source-controller/api v1.2.4
github.com/go-chi/chi v1.5.5
github.com/go-logr/logr v1.4.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ github.com/fluxcd/pkg/apis/kustomize v1.3.0 h1:qvB46CfaOWcL1SyR2RiVWN/j7/035D0Ot
github.com/fluxcd/pkg/apis/kustomize v1.3.0/go.mod h1:PCXf5kktTzNav0aH2Ns3jsowqwmA9xTcsrEOoPzx/K8=
github.com/fluxcd/pkg/apis/meta v1.3.0 h1:KxeEc6olmSZvQ5pBONPE4IKxyoWQbqTJF1X6K5nIXpU=
github.com/fluxcd/pkg/apis/meta v1.3.0/go.mod h1:3Ui8xFkoU4sYehqmscjpq7NjqH2YN1A2iX2okbO3/yA=
github.com/fluxcd/pkg/runtime v0.44.1 h1:XuPTcNIgn/NsoIo/A6qfPZaD9E7cbnJTDbeNw8O1SZQ=
github.com/fluxcd/pkg/runtime v0.44.1/go.mod h1:s1AhSOTCEBPaTfz/GdBD/Ws66uOByIuNP4Znrq+is9M=
github.com/fluxcd/source-controller/api v1.2.4 h1:XjKTWhSSeLGsogWnTcLl5sUnyMlC5TKDbbBgP9SyJ5c=
github.com/fluxcd/source-controller/api v1.2.4/go.mod h1:j3QSHpIPBP5sjaGIkVtsgWCx8JcOmcsutRmdJmRMOZg=
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
Expand Down Expand Up @@ -282,6 +284,8 @@ github.com/onsi/ginkgo/v2 v2.14.0 h1:vSmGj2Z5YPb9JwCWT6z6ihcUvDhuXLc3sJiqd3jMKAY
github.com/onsi/ginkgo/v2 v2.14.0/go.mod h1:JkUdW7JkN0V6rFvsHcJ478egV3XH9NxpD27Hal/PhZw=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0=
github.com/opencontainers/go-digest v1.0.1-0.20231025023718-d50d2fec9c98 h1:H55sU3giNgBkIvmAo0vI/AAFwVTwfWsf6MN3+9H6U8o=
github.com/opencontainers/go-digest v1.0.1-0.20231025023718-d50d2fec9c98/go.mod h1:RqnyioA3pIEZMkSbOIcrw32YSgETfn/VrLuEikEdPNU=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
Expand Down
26 changes: 26 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,32 @@ func stopLogs(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{}"))
}

func suspend(w http.ResponseWriter, r *http.Request) {
resource := r.URL.Query().Get("resource")
namespace := r.URL.Query().Get("namespace")
name := r.URL.Query().Get("name")
config, _ := r.Context().Value("config").(*rest.Config)

reconcileCommand := flux.NewSuspendCommand(resource)
go reconcileCommand.Run(config, namespace, name)

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}

func resume(w http.ResponseWriter, r *http.Request) {
resource := r.URL.Query().Get("resource")
namespace := r.URL.Query().Get("namespace")
name := r.URL.Query().Get("name")
config, _ := r.Context().Value("config").(*rest.Config)

reconcileCommand := flux.NewResumeCommand(resource)
go reconcileCommand.Run(config, namespace, name)

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}

func reconcile(w http.ResponseWriter, r *http.Request) {
resource := r.URL.Query().Get("resource")
namespace := r.URL.Query().Get("namespace")
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func SetupRouter(
r.Get("/api/describePod", describePod)
r.Get("/api/logs", streamLogs)
r.Get("/api/stopLogs", stopLogs)
r.Post("/api/suspend", suspend)
r.Post("/api/resume", resume)
r.Post("/api/reconcile", reconcile)
r.Get("/ws/", func(w http.ResponseWriter, r *http.Request) {
streaming.ServeWs(clientHub, w, r)
Expand Down
40 changes: 40 additions & 0 deletions pkg/flux/helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,54 @@ func (h helmReleaseAdapter) asClientObject() client.Object {
return h.HelmRelease
}

func (h helmReleaseAdapter) deepCopyClientObject() client.Object {
return h.HelmRelease.DeepCopy()
}

func (obj helmReleaseAdapter) isSuspended() bool {
return obj.HelmRelease.Spec.Suspend
}

func (obj helmReleaseAdapter) setSuspended() {
obj.HelmRelease.Spec.Suspend = true
}

func (obj helmReleaseAdapter) setUnsuspended() {
obj.HelmRelease.Spec.Suspend = false
}

func (obj helmReleaseAdapter) getObservedGeneration() int64 {
return obj.HelmRelease.Status.ObservedGeneration
}

func (obj helmReleaseAdapter) isStatic() bool {
return false
}

func (obj helmReleaseAdapter) lastHandledReconcileRequest() string {
return obj.Status.GetLastHandledReconcileRequest()
}

func (obj helmReleaseAdapter) successMessage() string {
return fmt.Sprintf("applied revision %s", obj.Status.LastAppliedRevision)
}

type helmReleaseListAdapter struct {
*helmv2beta1.HelmReleaseList
}

func (h helmReleaseListAdapter) asClientList() client.ObjectList {
return h.HelmReleaseList
}

func (h helmReleaseListAdapter) len() int {
return len(h.HelmReleaseList.Items)
}

func (a helmReleaseListAdapter) item(i int) suspendable {
return &helmReleaseAdapter{&a.HelmReleaseList.Items[i]}
}

func (a helmReleaseListAdapter) resumeItem(i int) resumable {
return &helmReleaseAdapter{&a.HelmReleaseList.Items[i]}
}
40 changes: 40 additions & 0 deletions pkg/flux/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,54 @@ func (a kustomizationAdapter) asClientObject() client.Object {
return a.Kustomization
}

func (a kustomizationAdapter) deepCopyClientObject() client.Object {
return a.Kustomization.DeepCopy()
}

func (obj kustomizationAdapter) isSuspended() bool {
return obj.Kustomization.Spec.Suspend
}

func (obj kustomizationAdapter) setSuspended() {
obj.Kustomization.Spec.Suspend = true
}

func (obj kustomizationAdapter) setUnsuspended() {
obj.Kustomization.Spec.Suspend = false
}

func (obj kustomizationAdapter) getObservedGeneration() int64 {
return obj.Kustomization.Status.ObservedGeneration
}

func (obj kustomizationAdapter) isStatic() bool {
return false
}

func (obj kustomizationAdapter) lastHandledReconcileRequest() string {
return obj.Status.GetLastHandledReconcileRequest()
}

func (obj kustomizationAdapter) successMessage() string {
return fmt.Sprintf("applied revision %s", obj.Status.LastAppliedRevision)
}

type kustomizationListAdapter struct {
*kustomizationv1.KustomizationList
}

func (a kustomizationListAdapter) asClientList() client.ObjectList {
return a.KustomizationList
}

func (a kustomizationListAdapter) len() int {
return len(a.KustomizationList.Items)
}

func (a kustomizationListAdapter) item(i int) suspendable {
return &kustomizationAdapter{&a.KustomizationList.Items[i]}
}

func (a kustomizationListAdapter) resumeItem(i int) resumable {
return &kustomizationAdapter{&a.KustomizationList.Items[i]}
}
3 changes: 1 addition & 2 deletions pkg/flux/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
apiruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -104,7 +103,7 @@ func NewReconcileCommand(resource string) *reconcileCommand {
}

func (r *reconcileCommand) Run(config *rest.Config, namespace, name string) {
scheme := apiruntime.NewScheme()
scheme := runtime.NewScheme()
sourcev1.AddToScheme(scheme)
sourcev1beta2.AddToScheme(scheme)
kustomizationv1.AddToScheme(scheme)
Expand Down
Loading

0 comments on commit a989ed2

Please sign in to comment.