You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original issue URL: kptdev/kpt#3896
Original issue user: https://github.com/henderiw
Original issue created at: 2023-03-28T14:58:17Z
Original issue last updated at: 2023-04-04T20:46:21Z
Original issue body: ### Describe your problem
Could it be possible to add a number of condition methods to the kptfile?
I did a small library to do this but it make more sense if this is part of the standard library.
here is the code.
// SetConditions sets the conditions in the kptfile. It either updates the entry if it exists
// or appends the entry if it does not exist.
func (r *kptFile) SetConditions(c ...kptv1.Condition) {
// validate is the status is set, if not initialize the condition slice
if r.GetKptFile().Status == nil {
r.GetKptFile().Status = &kptv1.Status{
Conditions: []kptv1.Condition{},
}
}
// for each nex condition check if the kind
for _, new := range c {
exists := false
for i, existing := range r.GetKptFile().Status.Conditions {
if existing.Type != new.Type {
continue
}
r.GetKptFile().Status.Conditions[i] = new
exists = true
}
if !exists {
r.GetKptFile().Status.Conditions = append(r.GetKptFile().Status.Conditions, new)
}
}
}
// DeleteCondition deletes the condition equal to the conditionType if it exists
func (r *kptFile) DeleteCondition(ct string) {
if r.GetKptFile().Status == nil || len(r.GetKptFile().Status.Conditions) == 0 {
return
}
for idx, c := range r.GetKptFile().Status.Conditions {
if c.Type == ct {
r.GetKptFile().Status.Conditions = append(r.GetKptFile().Status.Conditions[:idx], r.GetKptFile().Status.Conditions[idx+1:]...)
}
}
}
// GetCondition returns the condition for the given ConditionType if it exists,
// otherwise returns nil
func (r *kptFile) GetCondition(ct string) *kptv1.Condition {
if r.GetKptFile().Status == nil || len(r.GetKptFile().Status.Conditions) == 0 {
return nil
}
for _, c := range r.GetKptFile().Status.Conditions {
if c.Type == ct {
return &c
}
}
return nil
}
// GetConditions returns all the conditions in the kptfile. if not initialized it
// returns an emoty slice
func (r *kptFile) GetConditions() []kptv1.Condition {
if r.GetKptFile().Status == nil || len(r.GetKptFile().Status.Conditions) == 0 {
return []kptv1.Condition{}
}
return r.GetKptFile().Status.Conditions
}
Original issue comments:
Comment user: https://github.com/mortent
Comment created at: 2023-03-29T18:05:15Z
Comment last updated at: 2023-03-29T18:05:15Z
Comment body: Yeah, I agree this would be useful. If we aligned the conditions with meta.condition as you suggested in kptdev/kpt#3657 we probably wouldn't need it, but I haven't had a chance to look into that yet.
Comment user: https://github.com/johnbelamaric
Comment created at: 2023-04-04T20:46:21Z
Comment last updated at: 2023-04-04T20:46:21Z
Comment body: @henderiw, this is the code you want to upstream at some point, correct?
Triage Comments:
Nice to have. This is mostly already there. The idea was to avoid adding libraries but in the meantime a different approach was taken.
Original issue URL: kptdev/kpt#3896
Original issue user: https://github.com/henderiw
Original issue created at: 2023-03-28T14:58:17Z
Original issue last updated at: 2023-04-04T20:46:21Z
Original issue body: ### Describe your problem
Could it be possible to add a number of condition methods to the kptfile?
I did a small library to do this but it make more sense if this is part of the standard library.
here is the code.
// SetConditions sets the conditions in the kptfile. It either updates the entry if it exists
// or appends the entry if it does not exist.
func (r *kptFile) SetConditions(c ...kptv1.Condition) {
// validate is the status is set, if not initialize the condition slice
if r.GetKptFile().Status == nil {
r.GetKptFile().Status = &kptv1.Status{
Conditions: []kptv1.Condition{},
}
}
}
// DeleteCondition deletes the condition equal to the conditionType if it exists
func (r *kptFile) DeleteCondition(ct string) {
if r.GetKptFile().Status == nil || len(r.GetKptFile().Status.Conditions) == 0 {
return
}
}
// GetCondition returns the condition for the given ConditionType if it exists,
// otherwise returns nil
func (r *kptFile) GetCondition(ct string) *kptv1.Condition {
if r.GetKptFile().Status == nil || len(r.GetKptFile().Status.Conditions) == 0 {
return nil
}
}
// GetConditions returns all the conditions in the kptfile. if not initialized it
// returns an emoty slice
func (r *kptFile) GetConditions() []kptv1.Condition {
if r.GetKptFile().Status == nil || len(r.GetKptFile().Status.Conditions) == 0 {
return []kptv1.Condition{}
}
return r.GetKptFile().Status.Conditions
}
tests:
func TestGetCondition(t *testing.T) {
kind: Kptfile
metadata:
name: pkg-upf
annotations:
config.kubernetes.io/local-config: "true"
info:
description: upf package example
`
}
func TestSetConditions(t *testing.T) {
kind: Kptfile
metadata:
name: pkg-upf
annotations:
config.kubernetes.io/local-config: "true"
info:
description: upf package example
`
}
Original issue comments:
Comment user: https://github.com/mortent
Comment created at: 2023-03-29T18:05:15Z
Comment last updated at: 2023-03-29T18:05:15Z
Comment body: Yeah, I agree this would be useful. If we aligned the conditions with meta.condition as you suggested in kptdev/kpt#3657 we probably wouldn't need it, but I haven't had a chance to look into that yet.
Comment user: https://github.com/johnbelamaric
Comment created at: 2023-04-04T20:46:21Z
Comment last updated at: 2023-04-04T20:46:21Z
Comment body: @henderiw, this is the code you want to upstream at some point, correct?
https://github.com/nephio-project/nephio/tree/main/krm-functions/lib/kptfile/v1
The text was updated successfully, but these errors were encountered: