Repository: iter8-tools/iter8
Pull request #1639 (2023-09-15 18:48:08)

unify specs readiness/controller


Patch:
diff --git a/base/readiness.go b/base/readiness.go
index 356381c8..f66c2575 100644
--- a/base/readiness.go
+++ b/base/readiness.go
@@ -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"`
 }
@@ -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 {
diff --git a/controllers/config.go b/controllers/config.go
index 49ba4b98..80cf333e 100644
--- a/controllers/config.go
+++ b/controllers/config.go
@@ -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
diff --git a/controllers/routemap.go b/controllers/routemap.go
index 3993fc11..cad8e732 100644
--- a/controllers/routemap.go
+++ b/controllers/routemap.go
@@ -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
 			}
@@ -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
 			}


Test patch:
diff --git a/base/readiness_test.go b/base/readiness_test.go
index 60fd0f32..84aacf78 100644
--- a/base/readiness_test.go
+++ b/base/readiness_test.go
@@ -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
 }
 
diff --git a/controllers/config_test.go b/controllers/config_test.go
index 9883890b..501bce55 100644
--- a/controllers/config_test.go
+++ b/controllers/config_test.go
@@ -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)
diff --git a/controllers/routemap_test.go b/controllers/routemap_test.go
index 97c061b2..e2b537dd 100644
--- a/controllers/routemap_test.go
+++ b/controllers/routemap_test.go
@@ -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)
diff --git a/testdata/controllers/config.yaml b/testdata/controllers/config.yaml
index b9d21319..d792438f 100644
--- a/testdata/controllers/config.yaml
+++ b/testdata/controllers/config.yaml
@@ -19,8 +19,7 @@ resourceTypes:
     Version: v1beta1
     Resource: inferenceservices
     conditions:
-    - name: Ready
-      status: "True"
+    - Ready
   vs:
     Group: networking.istio.io
     Version: v1beta1