generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [INCOMPATIBLE CHANGE] add delete waves This commit adds delete waves, which can be controlled through the new annotation <operator-name>/delete-order; by default, all objects have a delete order of zero, and therefore are deleted in one wave. Note that delete waves are independent of apply waves. This commit brings some incompatible changes: 1. The annotation <operator-name>/order was renamed to <operator-name>/apply-order; the according constant in pkg/types was renamed as well. 2. The InventoryItem type was enhanced with new fields; consumers therefore *must* regenerate their CRDs. * exlude purge order from inventory for now * add new annotation <operator-name>/status-hint this can be used to tweak the status detection (done by kstatus); currently there are two options available that can be passed as value of this annotation (comma-separated): 'has-observed-generation', and 'has-ready-condition', which tells kstatus that the object does have an observedGeneration field in the status, resp. a condition of type Ready, even if it is not (yet) present in the status. * bump controller-tools * update generated artifacts * update docs
- Loading branch information
1 parent
97834ce
commit b86915b
Showing
16 changed files
with
650 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package kstatus | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/iancoleman/strcase" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status" | ||
|
||
"github.com/sap/component-operator-runtime/pkg/types" | ||
) | ||
|
||
const conditionTypeReady = "Ready" | ||
|
||
type statusAnalyzer struct { | ||
reconcilerName string | ||
} | ||
|
||
func NewStatusAnalyzer(reconcilerName string) StatusAnalyzer { | ||
return &statusAnalyzer{ | ||
reconcilerName: reconcilerName, | ||
} | ||
} | ||
|
||
func (s *statusAnalyzer) ComputeStatus(object *unstructured.Unstructured) (Status, error) { | ||
if hint, ok := object.GetAnnotations()[s.reconcilerName+"/"+types.AnnotationKeySuffixStatusHint]; ok { | ||
object = object.DeepCopy() | ||
|
||
for _, hint := range strings.Split(hint, ",") { | ||
switch strcase.ToKebab(hint) { | ||
case types.StatusHintHasObservedGeneration: | ||
_, found, err := unstructured.NestedInt64(object.Object, "status", "observedGeneration") | ||
if err != nil { | ||
return UnknownStatus, err | ||
} | ||
if !found { | ||
if err := unstructured.SetNestedField(object.Object, -1, "status", "observedGeneration"); err != nil { | ||
return UnknownStatus, err | ||
} | ||
} | ||
case types.StatusHintHasReadyCondition: | ||
foundReadyCondition := false | ||
conditions, found, err := unstructured.NestedSlice(object.Object, "status", "conditions") | ||
if err != nil { | ||
return UnknownStatus, err | ||
} | ||
if !found { | ||
conditions = make([]any, 0) | ||
} | ||
for _, condition := range conditions { | ||
if condition, ok := condition.(map[string]any); ok { | ||
condType, found, err := unstructured.NestedString(condition, "type") | ||
if err != nil { | ||
return UnknownStatus, err | ||
} | ||
if found && condType == conditionTypeReady { | ||
foundReadyCondition = true | ||
break | ||
} | ||
} | ||
} | ||
if !foundReadyCondition { | ||
conditions = append(conditions, map[string]any{ | ||
"type": conditionTypeReady, | ||
"status": string(corev1.ConditionUnknown), | ||
}) | ||
if err := unstructured.SetNestedSlice(object.Object, conditions, "status", "conditions"); err != nil { | ||
return UnknownStatus, err | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
res, err := kstatus.Compute(object) | ||
if err != nil { | ||
return UnknownStatus, err | ||
} | ||
|
||
switch object.GroupVersionKind() { | ||
case schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "Job"}: | ||
// other than kstatus we want to consider jobs as InProgress if its pods are still running, resp. did not (yet) finish successfully | ||
if res.Status == kstatus.CurrentStatus { | ||
done := false | ||
objc, err := kstatus.GetObjectWithConditions(object.UnstructuredContent()) | ||
if err != nil { | ||
return UnknownStatus, err | ||
} | ||
for _, cond := range objc.Status.Conditions { | ||
if cond.Type == string(batchv1.JobComplete) && cond.Status == corev1.ConditionTrue { | ||
done = true | ||
break | ||
} | ||
if cond.Type == string(batchv1.JobFailed) && cond.Status == corev1.ConditionTrue { | ||
done = true | ||
break | ||
} | ||
} | ||
if !done { | ||
res.Status = kstatus.InProgressStatus | ||
} | ||
} | ||
} | ||
|
||
return Status(res.Status), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package kstatus | ||
|
||
func (s Status) String() string { | ||
return string(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package kstatus | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status" | ||
) | ||
|
||
// TODO: the StatusAnalyzer interface should be public. | ||
|
||
// The StatusAnalyzer interface models types which allow to extract a kstatus-compatible status from an object. | ||
type StatusAnalyzer interface { | ||
ComputeStatus(object *unstructured.Unstructured) (Status, error) | ||
} | ||
|
||
type Status kstatus.Status | ||
|
||
const ( | ||
InProgressStatus Status = Status(kstatus.InProgressStatus) | ||
FailedStatus Status = Status(kstatus.FailedStatus) | ||
CurrentStatus Status = Status(kstatus.CurrentStatus) | ||
TerminatingStatus Status = Status(kstatus.TerminatingStatus) | ||
NotFoundStatus Status = Status(kstatus.NotFoundStatus) | ||
UnknownStatus Status = Status(kstatus.UnknownStatus) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.