Skip to content

Commit 91d2ec1

Browse files
authored
Rewrite conditions for ResourceGraphDefinition leveraging ConditionSet (#544)
This updates ResourceGraphDefinition to use the following conditions structure: ``` Ready ├─ ResourceGraphAccepted - This controller has accepted the spec.schema and spec.resources. ├─ KindReady - The CRD status created on behalf of this RGD. └─ ControllerReady - The status of the controller thread reconciling this resource. ``` --------- Signed-off-by: Scott Nichols <[email protected]>
1 parent 2689634 commit 91d2ec1

File tree

27 files changed

+584
-530
lines changed

27 files changed

+584
-530
lines changed

ATTRIBUTION.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ License version 2.0, we include the full text of the package's License below.
3939
* `k8s.io/apiserver`
4040
* `k8s.io/client-go`
4141
* `k8s.io/kube-openapi`
42+
* `k8s.io/utils`
4243
* `sigs.k8s.io/controller-runtime`
4344
* `sigs.k8s.io/release-utils`
4445

@@ -3421,6 +3422,8 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34213422
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34223423
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34233424

3425+
3426+
34243427
### sigs.k8s.io/controller-runtime
34253428

34263429
License Identifier: Apache-2.0
@@ -3799,7 +3802,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37993802
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38003803
SOFTWARE.
38013804

3805+
#### k8s.io/utils
38023806

3807+
License Identifier: Apache-2.0
38033808

38043809
#### github.com/Azure/go-ansiterm
38053810

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package apis
15+
package v1alpha1
1616

1717
import (
1818
"testing"

api/v1alpha1/conditions.go

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
// ConditionType is a type of condition for a resource.
2323
type ConditionType string
2424

25+
func (c ConditionType) String() string { return string(c) }
26+
2527
const (
2628
// ResourceGraphDefinitionConditionTypeGraphVerified indicates the state of the directed
2729
// acyclic graph (DAG) that kro uses to manage the resources in a
@@ -38,19 +40,6 @@ const (
3840
ResourceGraphDefinitionConditionTypeReconcilerReady ConditionType = "ReconcilerReady"
3941
)
4042

41-
const (
42-
InstanceConditionTypeReady ConditionType = "Ready"
43-
44-
// InstanceConditionTypeProgressing used for Creating Deleting Migrating
45-
InstanceConditionTypeProgressing ConditionType = "Progressing"
46-
47-
// InstanceConditionTypeDegraded used in unexpected situation, behaviour, need human intervention
48-
InstanceConditionTypeDegraded ConditionType = "Degraded"
49-
50-
// InstanceConditionTypeError used in something is wrong but i'm going to try again
51-
InstanceConditionTypeError ConditionType = "Error"
52-
)
53-
5443
// Condition is the common struct used by all CRDs managed by ACK service
5544
// controllers to indicate terminal states of the CR and its backend AWS
5645
// service API resource
@@ -76,18 +65,39 @@ type Condition struct {
7665
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
7766
}
7867

79-
// NewCondition returns a new Condition instance.
80-
func NewCondition(t ConditionType, status metav1.ConditionStatus, reason, message string) Condition {
81-
return Condition{
82-
Type: t,
83-
Status: status,
84-
LastTransitionTime: &metav1.Time{Time: metav1.Now().Time},
85-
Reason: &reason,
86-
Message: &message,
68+
func (c *Condition) IsTrue() bool {
69+
if c == nil {
70+
return false
8771
}
72+
return c.Status == metav1.ConditionTrue
8873
}
8974

90-
func SetCondition(conditions []Condition, condition Condition) []Condition {
75+
func (c *Condition) IsFalse() bool {
76+
if c == nil {
77+
return false
78+
}
79+
return c.Status == metav1.ConditionFalse
80+
}
81+
82+
func (c *Condition) IsUnknown() bool {
83+
if c == nil {
84+
return true
85+
}
86+
return c.Status == metav1.ConditionUnknown
87+
}
88+
89+
func (c *Condition) GetStatus() metav1.ConditionStatus {
90+
if c == nil {
91+
return metav1.ConditionUnknown
92+
}
93+
return c.Status
94+
}
95+
96+
// Conditions is a list of conditions.
97+
type Conditions []Condition
98+
99+
// Set sets the provided condition into the conditions list, if it exists already the condition is replaced.
100+
func (conditions Conditions) Set(condition Condition) []Condition {
91101
for i, c := range conditions {
92102
if c.Type == condition.Type {
93103
conditions[i] = condition
@@ -97,7 +107,8 @@ func SetCondition(conditions []Condition, condition Condition) []Condition {
97107
return append(conditions, condition)
98108
}
99109

100-
func HasCondition(conditions []Condition, t ConditionType) bool {
110+
// Has returns true if the conditions list contains the given condition type.
111+
func (conditions Conditions) Has(t ConditionType) bool {
101112
return slices.ContainsFunc(conditions, func(c Condition) bool {
102113
return c.Type == t
103114
})

api/v1alpha1/types.go renamed to api/v1alpha1/resourcegraphdefinition_types.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ type ResourceGraphDefinitionStatus struct {
151151
// TopologicalOrder is the topological order of the resourcegraphdefinition graph
152152
TopologicalOrder []string `json:"topologicalOrder,omitempty"`
153153
// Conditions represent the latest available observations of an object's state
154-
Conditions []Condition `json:"conditions,omitempty"`
154+
Conditions Conditions `json:"conditions,omitempty"`
155155
// Resources represents the resources, and their information (dependencies for now)
156156
Resources []ResourceInformation `json:"resources,omitempty"`
157157
}
@@ -190,6 +190,14 @@ type ResourceGraphDefinition struct {
190190
Status ResourceGraphDefinitionStatus `json:"status,omitempty"`
191191
}
192192

193+
func (o *ResourceGraphDefinition) GetConditions() []Condition {
194+
return o.Status.Conditions
195+
}
196+
197+
func (o *ResourceGraphDefinition) SetConditions(conditions []Condition) {
198+
o.Status.Conditions = conditions
199+
}
200+
193201
//+kubebuilder:object:root=true
194202

195203
// ResourceGraphDefinitionList contains a list of ResourceGraphDefinition

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/condition.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@
1818
package apis
1919

2020
import (
21-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"github.com/kro-run/kro/api/v1alpha1"
2222
"sigs.k8s.io/controller-runtime/pkg/client"
2323
)
2424

25-
// Condition aliases the upstream type and adds additional helper methods
26-
type Condition metav1.Condition
27-
2825
type Object interface {
2926
client.Object
30-
GetConditions() []Condition
31-
SetConditions([]Condition)
27+
GetConditions() []v1alpha1.Condition
28+
SetConditions([]v1alpha1.Condition)
3229
}
3330

34-
// ConditionType is an upper-camel-cased condition type.
35-
type ConditionType string
36-
3731
const (
3832
// ConditionReady specifies that the resource is ready.
3933
// For long-running resources.
@@ -42,31 +36,3 @@ const (
4236
// For resource which run to completion.
4337
ConditionSucceeded = "Succeeded"
4438
)
45-
46-
func (c *Condition) IsTrue() bool {
47-
if c == nil {
48-
return false
49-
}
50-
return c.Status == metav1.ConditionTrue
51-
}
52-
53-
func (c *Condition) IsFalse() bool {
54-
if c == nil {
55-
return false
56-
}
57-
return c.Status == metav1.ConditionFalse
58-
}
59-
60-
func (c *Condition) IsUnknown() bool {
61-
if c == nil {
62-
return true
63-
}
64-
return c.Status == metav1.ConditionUnknown
65-
}
66-
67-
func (c *Condition) GetStatus() metav1.ConditionStatus {
68-
if c == nil {
69-
return metav1.ConditionUnknown
70-
}
71-
return c.Status
72-
}

0 commit comments

Comments
 (0)