Skip to content

Commit

Permalink
Merge pull request #2017 from barney-s/better-status
Browse files Browse the repository at this point in the history
Compositions: Better Status fields for plan
  • Loading branch information
google-oss-prow[bot] authored Jun 17, 2024
2 parents 03645e9 + 51b7983 commit 56d2909
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 42 deletions.
31 changes: 25 additions & 6 deletions experiments/compositions/composition/api/v1alpha1/plan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

type Stage struct {
Expand All @@ -33,19 +34,37 @@ type PlanSpec struct {
Stages map[string]Stage `json:"stages,omitempty"`
}

type ResourceStatus struct {
Group string `json:"group,omitempty"`
Version string `json:"version,omitempty"`
Kind string `json:"kind"`
Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
IsHealthy bool `json:"isHealthy"`
}

// StageStatus captures the status of a stage
type StageStatus struct {
ResourceCount int `json:"resourceCount"`
ResourceCount int `json:"resourceCount"`
AppliedCount int `json:"appliedCount,omitempty"`
LastApplied []ResourceStatus `json:"lastApplied,omitempty"`
}

// PlanStatus defines the observed state of Plan
type PlanStatus struct {
// Facade's generation last we successfully reconciled
InputGeneration int64 `json:"inputGeneration,omitempty"`
// Facade's generation last succesfully reconciled
InputGeneration int64 `json:"inputGeneration"`
// Composition generation last succesfully reconciled
CompositionGeneration int64 `json:"compositionGeneration"`
// Composition UID
CompositionUID types.UID `json:"compositionUID,omitempty"`

// Plan generation we last successfully reconciled
Generation int64 `json:"generation,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
Stages map[string]StageStatus `json:"stages,omitempty"`
Generation int64 `json:"generation,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
Stages map[string]*StageStatus `json:"stages,omitempty"`
LastPruned []ResourceStatus `json:"lastPruned,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ spec:
status:
description: PlanStatus defines the observed state of Plan
properties:
compositionGeneration:
description: Composition generation last succesfully reconciled
format: int64
type: integer
compositionUID:
description: Composition UID
type: string
conditions:
items:
description: "Condition contains details for one aspect of the current
Expand Down Expand Up @@ -140,19 +147,68 @@ spec:
format: int64
type: integer
inputGeneration:
description: Facade's generation last we successfully reconciled
description: Facade's generation last succesfully reconciled
format: int64
type: integer
lastPruned:
items:
properties:
group:
type: string
isHealthy:
type: boolean
kind:
type: string
name:
type: string
namespace:
type: string
status:
type: string
version:
type: string
required:
- isHealthy
- kind
type: object
type: array
stages:
additionalProperties:
description: StageStatus captures the status of a stage
properties:
appliedCount:
type: integer
lastApplied:
items:
properties:
group:
type: string
isHealthy:
type: boolean
kind:
type: string
name:
type: string
namespace:
type: string
status:
type: string
version:
type: string
required:
- isHealthy
- kind
type: object
type: array
resourceCount:
type: integer
required:
- resourceCount
type: object
type: object
required:
- compositionGeneration
- inputGeneration
type: object
type: object
served: true
Expand Down
10 changes: 5 additions & 5 deletions experiments/compositions/composition/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/go-logr/logr v1.4.1
github.com/gobuffalo/flect v1.0.2
github.com/google/go-cmp v0.6.0
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.29.0
github.com/onsi/ginkgo/v2 v2.14.0
github.com/onsi/gomega v1.30.0
golang.org/x/time v0.5.0
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.34.0
Expand All @@ -21,9 +21,9 @@ require (
k8s.io/klog/v2 v2.120.1
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
sigs.k8s.io/cli-utils v0.35.0
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/kubebuilder-declarative-pattern v0.13.0
sigs.k8s.io/kubebuilder-declarative-pattern/applylib v0.0.0-20240207064257-f3dc5066cc9d
sigs.k8s.io/controller-runtime v0.17.2
sigs.k8s.io/kubebuilder-declarative-pattern v0.15.0-beta.1.0.20240614185435-a248ed1e894c
sigs.k8s.io/kubebuilder-declarative-pattern/applylib v0.0.0-20240614185435-a248ed1e894c
sigs.k8s.io/kustomize/kstatus v0.0.2-0.20200509233124-065f70705d4d
tailscale.com v1.62.0
)
Expand Down
28 changes: 14 additions & 14 deletions experiments/compositions/composition/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (r *ExpanderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
Stages: map[string]compositionv1alpha1.Stage{},
},
Status: compositionv1alpha1.PlanStatus{
Stages: map[string]compositionv1alpha1.StageStatus{},
Stages: map[string]*compositionv1alpha1.StageStatus{},
},
}
if err := ctrl.SetControllerReference(&inputcr, &plancr, r.Scheme); err != nil {
Expand All @@ -148,7 +148,10 @@ func (r *ExpanderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c

// Create a new status for comparison later
newStatus := compositionv1alpha1.PlanStatus{
Stages: map[string]compositionv1alpha1.StageStatus{},
Stages: map[string]*compositionv1alpha1.StageStatus{},
// TODO: Accumulates LastPruned.
// Ideally we need to reset if input/composition gen changes
LastPruned: plancr.Status.LastPruned,
}

// Try updating status before returning
Expand Down Expand Up @@ -271,19 +274,21 @@ func (r *ExpanderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
logger.Info("Successfully loaded manifests for applying")
if newStatus.Stages == nil {
newStatus.Stages = map[string]compositionv1alpha1.StageStatus{}
newStatus.Stages = map[string]*compositionv1alpha1.StageStatus{}
}
newStatus.Stages[expander] = compositionv1alpha1.StageStatus{ResourceCount: applier.Count()}
newStatus.Stages[expander] = &compositionv1alpha1.StageStatus{ResourceCount: applier.Count()}

// Prune only for the last expander section
prune := false
if index == len(compositionCR.Spec.Expanders)-1 {
prune = true
}

_, err = applier.Apply(oldAppliers, prune) // Apply Manifests
err = applier.Apply(oldAppliers, prune) // Apply Manifests
applier.UpdateStageStatus(&newStatus)
if err != nil {
r.Recorder.Event(&inputcr, "Warning", "ApplyFailed", fmt.Sprintf("error applying manifests for expander, name: %s", expander))
r.Recorder.Event(&inputcr, "Warning", "ApplyFailed",
fmt.Sprintf("error applying manifests for expander, name: %s", expander))
logger.Error(err, "Unable to apply manifests")
// Inject plan.ERROR Condition
newStatus.AppendErrorCondition(expander, err.Error(), "FailedApplyingManifests")
Expand Down Expand Up @@ -327,6 +332,8 @@ func (r *ExpanderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
newStatus.AppendCondition(compositionv1alpha1.Ready, metav1.ConditionTrue, message, "ProcessedAllStages")
newStatus.InputGeneration = inputcr.GetGeneration()
newStatus.Generation = plancr.GetGeneration()
newStatus.CompositionGeneration = compositionCR.GetGeneration()
newStatus.CompositionUID = compositionCR.GetUID()
if waitRequested {
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
}
Expand Down
Loading

0 comments on commit 56d2909

Please sign in to comment.