Skip to content

Commit

Permalink
Suspend command
Browse files Browse the repository at this point in the history
  • Loading branch information
dzsak committed Mar 27, 2024
1 parent 41ec15d commit e9966c4
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 5 deletions.
13 changes: 13 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ 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 reconcile(w http.ResponseWriter, r *http.Request) {
resource := r.URL.Query().Get("resource")
namespace := r.URL.Query().Get("namespace")
Expand Down
1 change: 1 addition & 0 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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/reconcile", reconcile)
r.Get("/ws/", func(w http.ResponseWriter, r *http.Request) {
streaming.ServeWs(clientHub, w, r)
Expand Down
24 changes: 24 additions & 0 deletions pkg/flux/helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,38 @@ 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) 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]}
}
24 changes: 24 additions & 0 deletions pkg/flux/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,38 @@ 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) 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]}
}
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 @@ -92,7 +91,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
73 changes: 73 additions & 0 deletions pkg/flux/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

sourcev1 "github.com/fluxcd/source-controller/api/v1"
sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2"
sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -33,10 +34,18 @@ func (a gitRepositoryAdapter) asClientObject() client.Object {
return a.GitRepository
}

func (a gitRepositoryAdapter) deepCopyClientObject() client.Object {
return a.GitRepository.DeepCopy()
}

func (obj gitRepositoryAdapter) isSuspended() bool {
return obj.GitRepository.Spec.Suspend
}

func (obj gitRepositoryAdapter) setSuspended() {
obj.GitRepository.Spec.Suspend = true
}

func (obj gitRepositoryAdapter) lastHandledReconcileRequest() string {
return obj.Status.GetLastHandledReconcileRequest()
}
Expand All @@ -45,6 +54,22 @@ func (obj gitRepositoryAdapter) successMessage() string {
return fmt.Sprintf("fetched revision %s", obj.Status.Artifact.Revision)
}

type gitRepositoryListAdapter struct {
*sourcev1.GitRepositoryList
}

func (a gitRepositoryListAdapter) asClientList() client.ObjectList {
return a.GitRepositoryList
}

func (a gitRepositoryListAdapter) len() int {
return len(a.GitRepositoryList.Items)
}

func (a gitRepositoryListAdapter) item(i int) suspendable {
return &gitRepositoryAdapter{&a.GitRepositoryList.Items[i]}
}

type ociRepositoryAdapter struct {
*sourcev1beta2.OCIRepository
}
Expand All @@ -53,10 +78,18 @@ func (a ociRepositoryAdapter) asClientObject() client.Object {
return a.OCIRepository
}

func (a ociRepositoryAdapter) deepCopyClientObject() client.Object {
return a.OCIRepository.DeepCopy()
}

func (obj ociRepositoryAdapter) isSuspended() bool {
return obj.OCIRepository.Spec.Suspend
}

func (obj ociRepositoryAdapter) setSuspended() {
obj.OCIRepository.Spec.Suspend = true
}

func (obj ociRepositoryAdapter) lastHandledReconcileRequest() string {
return obj.Status.GetLastHandledReconcileRequest()
}
Expand All @@ -65,6 +98,22 @@ func (obj ociRepositoryAdapter) successMessage() string {
return fmt.Sprintf("fetched revision %s", obj.Status.Artifact.Revision)
}

type ociRepositoryListAdapter struct {
*sourcev1b2.OCIRepositoryList
}

func (a ociRepositoryListAdapter) asClientList() client.ObjectList {
return a.OCIRepositoryList
}

func (a ociRepositoryListAdapter) len() int {
return len(a.OCIRepositoryList.Items)
}

func (a ociRepositoryListAdapter) item(i int) suspendable {
return &ociRepositoryAdapter{&a.OCIRepositoryList.Items[i]}
}

type bucketAdapter struct {
*sourcev1beta2.Bucket
}
Expand All @@ -73,14 +122,38 @@ func (a bucketAdapter) asClientObject() client.Object {
return a.Bucket
}

func (a bucketAdapter) deepCopyClientObject() client.Object {
return a.Bucket.DeepCopy()
}

func (obj bucketAdapter) isSuspended() bool {
return obj.Bucket.Spec.Suspend
}

func (obj bucketAdapter) setSuspended() {
obj.Bucket.Spec.Suspend = true
}

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

func (obj bucketAdapter) successMessage() string {
return fmt.Sprintf("fetched revision %s", obj.Status.Artifact.Revision)
}

type bucketListAdapter struct {
*sourcev1b2.BucketList
}

func (a bucketListAdapter) asClientList() client.ObjectList {
return a.BucketList
}

func (a bucketListAdapter) len() int {
return len(a.BucketList.Items)
}

func (a bucketListAdapter) item(i int) suspendable {
return &bucketAdapter{&a.BucketList.Items[i]}
}
Loading

0 comments on commit e9966c4

Please sign in to comment.