Skip to content

Commit

Permalink
Merge pull request #183 from fluxcd/suspend
Browse files Browse the repository at this point in the history
Do not mark suspended resource as not ready
  • Loading branch information
stefanprodan authored Nov 26, 2020
2 parents 200d4c0 + bed1418 commit 6a55d5d
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 48 deletions.
2 changes: 1 addition & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/fluxcd/pkg/apis/meta v0.4.0
github.com/fluxcd/pkg/runtime v0.3.0
github.com/fluxcd/pkg/runtime v0.3.1
k8s.io/api v0.19.3
k8s.io/apimachinery v0.19.3
sigs.k8s.io/controller-runtime v0.6.3
Expand Down
96 changes: 94 additions & 2 deletions api/go.sum

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion api/v1beta1/kustomization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ type KustomizationSpec struct {

// This flag tells the controller to suspend subsequent kustomize executions,
// it does not apply to already started executions. Defaults to false.
// +kubebuilder:default:=false
// +kubebuilder:validation:Optional
// +optional
Suspend bool `json:"suspend,omitempty"`
Suspend bool `json:"suspend"`

// TargetNamespace sets or overrides the namespace in the
// kustomization.yaml file.
Expand Down Expand Up @@ -233,6 +235,7 @@ const (
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description=""
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description=""
// +kubebuilder:printcolumn:name="Suspended",type="boolean",JSONPath=".spec.suspend",description=""
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""

// Kustomization is the Schema for the kustomizations API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ spec:
- jsonPath: .status.conditions[?(@.type=="Ready")].message
name: Status
type: string
- jsonPath: .spec.suspend
name: Suspended
type: boolean
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
Expand Down Expand Up @@ -170,6 +173,7 @@ spec:
- name
type: object
suspend:
default: false
description: This flag tells the controller to suspend subsequent
kustomize executions, it does not apply to already started executions.
Defaults to false.
Expand Down
44 changes: 19 additions & 25 deletions controllers/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,12 @@ func (r *KustomizationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
return r.reconcileDelete(ctx, log, kustomization)
}

// Return early if the Kustomization is suspended.
if kustomization.Spec.Suspend {
msg := "Kustomization is suspended, skipping reconciliation"
kustomization = kustomizev1.KustomizationNotReady(kustomization, "", meta.SuspendedReason, msg)
if err := r.updateStatus(ctx, req, kustomization.Status); err != nil {
log.Error(err, "unable to update status")
return ctrl.Result{Requeue: true}, err
}
r.recordReadiness(kustomization)
log.Info(msg)
log.Info("Reconciliation is suspended for this object")
return ctrl.Result{}, nil
}

// record reconciliation duration
if r.MetricsRecorder != nil {
objRef, err := reference.GetReference(r.Scheme, &kustomization)
if err != nil {
return ctrl.Result{}, err
}
defer r.MetricsRecorder.RecordDuration(*objRef, reconcileStart)
}

// set the reconciliation status to progressing
kustomization = kustomizev1.KustomizationProgressing(kustomization)
if err := r.updateStatus(ctx, req, kustomization.Status); err != nil {
log.Error(err, "unable to update status to progressing")
return ctrl.Result{Requeue: true}, err
}
r.recordReadiness(kustomization)

// resolve source reference
source, err := r.getSource(ctx, kustomization)
if err != nil {
Expand Down Expand Up @@ -246,6 +223,23 @@ func (r *KustomizationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
log.Info("All dependencies area ready, proceeding with reconciliation")
}

// record reconciliation duration
if r.MetricsRecorder != nil {
objRef, err := reference.GetReference(r.Scheme, &kustomization)
if err != nil {
return ctrl.Result{}, err
}
defer r.MetricsRecorder.RecordDuration(*objRef, reconcileStart)
}

// set the reconciliation status to progressing
kustomization = kustomizev1.KustomizationProgressing(kustomization)
if err := r.updateStatus(ctx, req, kustomization.Status); err != nil {
log.Error(err, "unable to update status to progressing")
return ctrl.Result{Requeue: true}, err
}
r.recordReadiness(kustomization)

// reconcile kustomization by applying the latest revision
reconciledKustomization, reconcileErr := r.reconcile(*kustomization.DeepCopy(), source)
if reconcileErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ replace github.com/fluxcd/kustomize-controller/api => ./api
require (
github.com/fluxcd/kustomize-controller/api v0.3.0
github.com/fluxcd/pkg/apis/meta v0.4.0
github.com/fluxcd/pkg/runtime v0.3.0
github.com/fluxcd/pkg/runtime v0.3.1
github.com/fluxcd/pkg/testserver v0.0.2
github.com/fluxcd/pkg/untar v0.0.5
github.com/fluxcd/source-controller/api v0.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ github.com/fluxcd/pkg/apis/meta v0.3.0 h1:o2YkfGgf0j8sKeZs8cBmmmMKLA7kEoS1qYViOi
github.com/fluxcd/pkg/apis/meta v0.3.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0=
github.com/fluxcd/pkg/apis/meta v0.4.0 h1:JChqB9GGgorW9HWKxirTVV0rzrcLyzBaVjinmqZ0iHA=
github.com/fluxcd/pkg/apis/meta v0.4.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0=
github.com/fluxcd/pkg/runtime v0.3.0 h1:WpeTmDT2meIe4NsU081I8zmUGgTYs3bIMRgs9F3Lj90=
github.com/fluxcd/pkg/runtime v0.3.0/go.mod h1:gPe6JgfPB4EDh5gaVkuI0SPuATk3PmclbFa1kPcZrKE=
github.com/fluxcd/pkg/runtime v0.3.1 h1:UI+FQd1OgipZ6N8YxXHtKYMAu1NRFWaR/Gp1M3T6RZA=
github.com/fluxcd/pkg/runtime v0.3.1/go.mod h1:wg33L6k5FkGEYoZta7hbUlBIscM5VZd/PfCbaEi+wK4=
github.com/fluxcd/pkg/testserver v0.0.2 h1:SoaMtO9cE5p/wl2zkGudzflnEHd9mk68CGjZOo7w0Uk=
github.com/fluxcd/pkg/testserver v0.0.2/go.mod h1:pgUZTh9aQ44FSTQo+5NFlh7YMbUfdz1B80DalW7k96Y=
github.com/fluxcd/pkg/untar v0.0.5 h1:UGI3Ch1UIEIaqQvMicmImL1s9npQa64DJ/ozqHKB7gk=
Expand Down
21 changes: 5 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
crtlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
"github.com/fluxcd/kustomize-controller/controllers"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/logger"
"github.com/fluxcd/pkg/runtime/metrics"
"github.com/fluxcd/pkg/runtime/probes"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
"github.com/fluxcd/kustomize-controller/controllers"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -113,7 +114,7 @@ func main() {
os.Exit(1)
}

setupChecks(mgr)
probes.SetupChecks(mgr, setupLog)

if err = (&controllers.KustomizationReconciler{
Client: mgr.GetClient(),
Expand All @@ -138,15 +139,3 @@ func main() {
os.Exit(1)
}
}

func setupChecks(mgr ctrl.Manager) {
if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create ready check")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create health check")
os.Exit(1)
}
}

0 comments on commit 6a55d5d

Please sign in to comment.