Skip to content

Commit

Permalink
unify specs readiness/controller (#1639)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Kalantar <kalantar@us.ibm.com>
  • Loading branch information
kalantar authored Sep 18, 2023
1 parent b059c62 commit 467287a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 35 deletions.
32 changes: 19 additions & 13 deletions base/readiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type readinessInputs struct {
Namespace *string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
// Name of the object
Name string `json:"name" yaml:"name"`
// Condition is label of condition to check for value of "True"
Condition *string `json:"condition" yaml:"condition"`
// Conditions is list of conditions to check for value of "True"
Conditions []string `json:"conditions" yaml:"conditions"`
// Timeout is maximum time spent trying to find object and check condition
Timeout *string `json:"timeout" yaml:"timeout"`
}
Expand Down Expand Up @@ -133,22 +133,28 @@ func checkObjectExistsAndConditionTrue(t *readinessTask, restCfg *rest.Config) e
return err
}

// if no condition to check was specified, we can return now
if t.With.Condition == nil {
// if no conditios to check were specified, we can return now
if len(t.With.Conditions) == 0 {
return nil
}

// otherwise, find the condition and check that it is "True"
log.Logger.Trace("looking for condition: ", *t.With.Condition)
// set err to nil; will set if there is a problem finding conditions
err = nil
var cs *string
for _, condition := range t.With.Conditions {
// otherwise, find the condition and check that it is "True"
log.Logger.Trace("looking for condition: ", condition)

cs, err := getConditionStatus(obj, *t.With.Condition)
if err != nil {
return err
}
if strings.EqualFold(*cs, string(corev1.ConditionTrue)) {
return nil
cs, err = getConditionStatus(obj, condition)
if err != nil {
continue
}
if strings.EqualFold(*cs, string(corev1.ConditionTrue)) {
return nil
}
err = errors.New("condition status not True")
}
return errors.New("condition status not True")
return err
}

func gvr(objRef *readinessInputs) schema.GroupVersionResource {
Expand Down
2 changes: 1 addition & 1 deletion base/readiness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (t *readinessTaskBuilder) withTimeout(timeout string) *readinessTaskBuilder
}

func (t *readinessTaskBuilder) withCondition(condition string) *readinessTaskBuilder {
t.With.Condition = &condition
t.With.Conditions = append(t.With.Conditions, condition)
return t
}

Expand Down
10 changes: 1 addition & 9 deletions controllers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ const (
// GroupVersionResourceConditions is a Kubernetes resource type along with a list of conditions
type GroupVersionResourceConditions struct {
schema.GroupVersionResource
Conditions []Condition `json:"conditions,omitempty"`
}

// Condition is the condition within resource status
type Condition struct {
// Name of the condition
Name string `json:"name"`
// Status of the condition
Status string `json:"status"`
Conditions []string `json:"conditions,omitempty"`
}

// Config defines the configuration of the controllers
Expand Down
7 changes: 3 additions & 4 deletions controllers/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ func TestReadConfig(t *testing.T) {
Version: "v1beta1",
Resource: "inferenceservices",
},
Conditions: []Condition{{
Name: "Ready",
Status: "True",
}},
Conditions: []string{
"Ready",
},
})
} else {
assert.Error(t, err)
Expand Down
4 changes: 2 additions & 2 deletions controllers/routemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func conditionsSatisfied(u *unstructured.Unstructured, gvrShort string, config *
return false
}
name, found, err := unstructured.NestedString(condition, "type")
if !found || err != nil || !strings.EqualFold(name, c.Name) {
if !found || err != nil || !strings.EqualFold(name, c) {
log.Logger.Trace("condition with no type")
continue
}
Expand All @@ -443,7 +443,7 @@ func conditionsSatisfied(u *unstructured.Unstructured, gvrShort string, config *
}

// check if condition status equals the required value specified in config
if !strings.EqualFold(status, c.Status) {
if !strings.EqualFold(status, "True") {
log.Logger.Info("condition not satisfied")
return false
}
Expand Down
7 changes: 3 additions & 4 deletions controllers/routemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ func TestConditionsSatisfied(t *testing.T) {
u.SetGeneration(13)
config := &Config{
ResourceTypes: map[string]GroupVersionResourceConditions{"foo": {
Conditions: []Condition{{
Name: "bar",
Status: "True",
}},
Conditions: []string{
"bar",
},
}},
}
var gen12 = int64(12)
Expand Down
3 changes: 1 addition & 2 deletions testdata/controllers/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ resourceTypes:
Version: v1beta1
Resource: inferenceservices
conditions:
- name: Ready
status: "True"
- Ready
vs:
Group: networking.istio.io
Version: v1beta1
Expand Down

0 comments on commit 467287a

Please sign in to comment.