Skip to content

Commit

Permalink
Add conditions to owner.status.resource[x] (#816)
Browse files Browse the repository at this point in the history
* Remove duplication across workload and deliverable errors and conditions
  • Loading branch information
emmjohnson authored Apr 21, 2022
1 parent ff0ef79 commit 61b10e9
Show file tree
Hide file tree
Showing 31 changed files with 1,055 additions and 851 deletions.
76 changes: 76 additions & 0 deletions config/crd/bases/carto.run_deliverables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,82 @@ spec:
Delivery was processed.
items:
properties:
conditions:
description: 'Conditions describing this resource''s reconcile
state. The top level condition is of type `Ready`, and follows
these Kubernetes conventions: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties'
items:
description: "Condition contains details for one aspect of
the current state of this API Resource. --- This struct
is intended for direct use as an array at the field path
.status.conditions. For example, type FooStatus struct{
// Represents the observations of a foo's current state.
// Known .status.conditions.type are: \"Available\", \"Progressing\",
and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge
// +listType=map // +listMapKey=type Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields
}"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should
be when the underlying condition changed. If that is
not known, then using the time when the API field changed
is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance,
if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the
current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier
indicating the reason for the condition's last transition.
Producers of specific condition types may define expected
values and meanings for this field, and whether the
values are considered a guaranteed API. The value should
be a CamelCase string. This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False,
Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across
resources like Available, but because arbitrary conditions
can be useful (see .node.status.conditions), the ability
to deconflict is important. The regex it matches is
(dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
inputs:
description: Inputs are references to resources that were used
to template the object in StampedRef
Expand Down
76 changes: 76 additions & 0 deletions config/crd/bases/carto.run_workloads.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,82 @@ spec:
as the Supply Chain was processed.
items:
properties:
conditions:
description: 'Conditions describing this resource''s reconcile
state. The top level condition is of type `Ready`, and follows
these Kubernetes conventions: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties'
items:
description: "Condition contains details for one aspect of
the current state of this API Resource. --- This struct
is intended for direct use as an array at the field path
.status.conditions. For example, type FooStatus struct{
// Represents the observations of a foo's current state.
// Known .status.conditions.type are: \"Available\", \"Progressing\",
and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge
// +listType=map // +listMapKey=type Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields
}"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should
be when the underlying condition changed. If that is
not known, then using the time when the API field changed
is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance,
if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the
current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier
indicating the reason for the condition's last transition.
Producers of specific condition types may define expected
values and meanings for this field, and whether the
values are considered a guaranteed API. The value should
be a CamelCase string. This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False,
Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across
resources like Available, but because arbitrary conditions
can be useful (see .node.status.conditions), the ability
to deconflict is important. The regex it matches is
(dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
inputs:
description: Inputs are references to resources that were used
to template the object in StampedRef
Expand Down
15 changes: 15 additions & 0 deletions pkg/apis/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ const (
FieldSelectorOpDoesNotExist FieldSelectorOperator = "DoesNotExist"
)

const (
OwnerReady = "Ready"
OwnerResourcesSubmitted = "ResourcesSubmitted"
)

const (
ResourceReady = "Ready"
ResourceSubmitted = "ResourceSubmitted"
)

type OwnerStatus struct {
// ObservedGeneration refers to the metadata.Generation of the spec that resulted in
// the current `status`.
Expand Down Expand Up @@ -214,6 +224,11 @@ type RealizedResource struct {

// Outputs are values from the object in StampedRef that can be consumed by other resources
Outputs []Output `json:"outputs,omitempty"`

// Conditions describing this resource's reconcile state. The top level condition is
// of type `Ready`, and follows these Kubernetes conventions:
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

type Input struct {
Expand Down
4 changes: 1 addition & 3 deletions pkg/apis/v1alpha1/deliverable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import (
)

const (
DeliverableReady = "Ready"
DeliverableDeliveryReady = "DeliveryReady"
DeliverableResourcesSubmitted = "ResourcesSubmitted"
DeliverableDeliveryReady = "DeliveryReady"
)

const (
Expand Down
4 changes: 1 addition & 3 deletions pkg/apis/v1alpha1/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
)

const (
WorkloadReady = "Ready"
WorkloadSupplyChainReady = "SupplyChainReady"
WorkloadResourceSubmitted = "ResourcesSubmitted"
WorkloadSupplyChainReady = "SupplyChainReady"
)

const (
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/v1alpha1/zz_generated.deepcopy.go

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

7 changes: 3 additions & 4 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/vmware-tanzu/cartographer/pkg/apis/v1alpha1"
"github.com/vmware-tanzu/cartographer/pkg/controller/deliverable"
"github.com/vmware-tanzu/cartographer/pkg/controller/delivery"
"github.com/vmware-tanzu/cartographer/pkg/controller/runnable"
"github.com/vmware-tanzu/cartographer/pkg/controller/supplychain"
"github.com/vmware-tanzu/cartographer/pkg/controller/workload"
"github.com/vmware-tanzu/cartographer/pkg/controllers"
"github.com/vmware-tanzu/cartographer/pkg/utils"
)

Expand Down Expand Up @@ -83,15 +82,15 @@ func (cmd *Command) Execute(ctx context.Context) error {
}

func registerControllers(mgr manager.Manager) error {
if err := (&workload.Reconciler{}).SetupWithManager(mgr); err != nil {
if err := (&controllers.WorkloadReconciler{}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to register workload controller: %w", err)
}

if err := (&supplychain.Reconciler{}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to register supply chain controller: %w", err)
}

if err := (&deliverable.Reconciler{}).SetupWithManager(mgr); err != nil {
if err := (&controllers.DeliverableReconciler{}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to register deliverable controller: %w", err)
}

Expand Down
130 changes: 130 additions & 0 deletions pkg/conditions/deliverable_conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2021 VMware
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package conditions

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/vmware-tanzu/cartographer/pkg/apis/v1alpha1"
cerrors "github.com/vmware-tanzu/cartographer/pkg/errors"
"github.com/vmware-tanzu/cartographer/pkg/templates"
)

// -- Delivery conditions

func DeliveryReadyCondition() metav1.Condition {
return metav1.Condition{
Type: v1alpha1.DeliverableDeliveryReady,
Status: metav1.ConditionTrue,
Reason: v1alpha1.ReadyDeliveryReason,
}
}

func DeliverableMissingLabelsCondition() metav1.Condition {
return metav1.Condition{
Type: v1alpha1.DeliverableDeliveryReady,
Status: metav1.ConditionFalse,
Reason: v1alpha1.DeliverableLabelsMissingDeliveryReason,
Message: "deliverable has no labels to match to delivery",
}
}

func DeliveryNotFoundCondition(labels map[string]string) metav1.Condition {
return metav1.Condition{
Type: v1alpha1.DeliverableDeliveryReady,
Status: metav1.ConditionFalse,
Reason: v1alpha1.NotFoundDeliveryReadyReason,
Message: fmt.Sprintf("no delivery found where full selector is satisfied by labels: %+v", labels),
}
}

func TooManyDeliveryMatchesCondition() metav1.Condition {
return metav1.Condition{
Type: v1alpha1.DeliverableDeliveryReady,
Status: metav1.ConditionFalse,
Reason: v1alpha1.MultipleMatchesDeliveryReadyReason,
Message: "deliverable may only match a single delivery's selector",
}
}

func MissingReadyInDeliveryCondition(deliveryReadyCondition metav1.Condition) metav1.Condition {
return metav1.Condition{
Type: v1alpha1.DeliverableDeliveryReady,
Status: metav1.ConditionFalse,
Reason: deliveryReadyCondition.Reason,
Message: deliveryReadyCondition.Message,
}
}

// -- Resource conditions

func TemplateStampFailureByObservedGenerationCondition(err error) metav1.Condition {
return metav1.Condition{
Type: v1alpha1.OwnerResourcesSubmitted,
Status: metav1.ConditionFalse,
Reason: v1alpha1.TemplateStampFailureResourcesSubmittedReason,
Message: fmt.Sprintf("resource [%s] cannot satisfy observedCompletion without observedGeneration in object status", err.(cerrors.RetrieveOutputError).GetResourceName()),
}
}

func DeploymentConditionNotMetCondition(err error) metav1.Condition {
return metav1.Condition{
Type: v1alpha1.OwnerResourcesSubmitted,
Status: metav1.ConditionUnknown,
Reason: v1alpha1.DeploymentConditionNotMetResourcesSubmittedReason,
Message: fmt.Sprintf("resource [%s] condition not met: %s", err.(cerrors.RetrieveOutputError).GetResourceName(), err.(cerrors.RetrieveOutputError).Err.Error()),
}
}

func DeploymentFailedConditionMetCondition(err error) metav1.Condition {
return metav1.Condition{
Type: v1alpha1.OwnerResourcesSubmitted,
Status: metav1.ConditionFalse,
Reason: v1alpha1.DeploymentFailedConditionMetResourcesSubmittedReason,
Message: fmt.Sprintf("resource [%s] failed condition met: %s", err.(cerrors.RetrieveOutputError).GetResourceName(), err.(cerrors.RetrieveOutputError).Err.Error()),
}
}

func AddConditionForDeliverableError(conditionManager *ConditionManager, isOwner bool, err error) {
switch typedErr := err.(type) {
case cerrors.GetTemplateError:
(*conditionManager).AddPositive(TemplateObjectRetrievalFailureCondition(isOwner, typedErr))
case cerrors.StampError:
(*conditionManager).AddPositive(TemplateStampFailureCondition(isOwner, typedErr))
case cerrors.ApplyStampedObjectError:
(*conditionManager).AddPositive(TemplateRejectedByAPIServerCondition(isOwner, typedErr))
case cerrors.RetrieveOutputError:
switch typedErr.Err.(type) {
case templates.ObservedGenerationError:
(*conditionManager).AddPositive(TemplateStampFailureByObservedGenerationCondition(typedErr))
case templates.DeploymentFailedConditionMetError:
(*conditionManager).AddPositive(DeploymentFailedConditionMetCondition(typedErr))
case templates.DeploymentConditionError:
(*conditionManager).AddPositive(DeploymentConditionNotMetCondition(typedErr))
case templates.JsonPathError:
(*conditionManager).AddPositive(MissingValueAtPathCondition(isOwner, typedErr.StampedObject, typedErr.JsonPathExpression()))
default:
(*conditionManager).AddPositive(UnknownResourceErrorCondition(isOwner, typedErr))
}
case cerrors.ResolveTemplateOptionError:
(*conditionManager).AddPositive(ResolveTemplateOptionsErrorCondition(isOwner, typedErr))
case cerrors.TemplateOptionsMatchError:
(*conditionManager).AddPositive(TemplateOptionsMatchErrorCondition(isOwner, typedErr))
default:
(*conditionManager).AddPositive(UnknownResourceErrorCondition(isOwner, typedErr))
}
}
Loading

0 comments on commit 61b10e9

Please sign in to comment.