|
| 1 | +package tableconvertor |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + configv1 "github.com/openshift/api/config/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 11 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 12 | + "k8s.io/apiserver/pkg/registry/rest" |
| 13 | +) |
| 14 | + |
| 15 | +var clusterOperatorGVK = schema.GroupVersionKind{configv1.GroupName, "v1", "ClusterOperator"} |
| 16 | + |
| 17 | +func withClusterOperatorColumns(c *convertor, gvk schema.GroupVersionKind) rest.TableConvertor { |
| 18 | + if gvk != clusterOperatorGVK { |
| 19 | + return c |
| 20 | + } |
| 21 | + |
| 22 | + c.headers = append(c.headers, metav1.TableColumnDefinition{ |
| 23 | + Name: "Message", |
| 24 | + Type: "string", |
| 25 | + Description: "A message describing the status of the operator", |
| 26 | + Priority: 0, |
| 27 | + }) |
| 28 | + c.additionalColumns = append(c.additionalColumns, clusterOperatorConditionMessage{}) |
| 29 | + |
| 30 | + return c |
| 31 | +} |
| 32 | + |
| 33 | +type clusterOperatorConditionMessage struct { |
| 34 | +} |
| 35 | + |
| 36 | +func (c clusterOperatorConditionMessage) FindResults(data interface{}) ([][]reflect.Value, error) { |
| 37 | + obj := data.(map[string]interface{}) |
| 38 | + unstructuredConds, _, _ := unstructured.NestedFieldNoCopy(obj, "status", "conditions") |
| 39 | + var conds []configv1.ClusterOperatorStatusCondition |
| 40 | + bs, err := json.Marshal(unstructuredConds) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + if err := json.Unmarshal(bs, &conds); err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + var available, degraded, progressing *configv1.ClusterOperatorStatusCondition |
| 49 | + for i := range conds { |
| 50 | + cond := &conds[i] |
| 51 | + switch { |
| 52 | + case cond.Type == configv1.OperatorAvailable && cond.Status == configv1.ConditionFalse: |
| 53 | + available = cond |
| 54 | + case cond.Type == configv1.OperatorDegraded && cond.Status == configv1.ConditionTrue: |
| 55 | + degraded = cond |
| 56 | + case cond.Type == configv1.OperatorProgressing && cond.Status == configv1.ConditionTrue: |
| 57 | + progressing = cond |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + mostCritical := progressing |
| 62 | + if degraded != nil { |
| 63 | + mostCritical = degraded |
| 64 | + } |
| 65 | + if available != nil { |
| 66 | + mostCritical = available |
| 67 | + } |
| 68 | + |
| 69 | + if mostCritical != nil { |
| 70 | + if len(mostCritical.Message) > 0 { |
| 71 | + return [][]reflect.Value{{reflect.ValueOf(mostCritical.Message)}}, nil |
| 72 | + } |
| 73 | + if len(mostCritical.Reason) > 0 { |
| 74 | + return [][]reflect.Value{{reflect.ValueOf(mostCritical.Reason)}}, nil |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return nil, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (c clusterOperatorConditionMessage) PrintResults(wr io.Writer, results []reflect.Value) error { |
| 82 | + first := true |
| 83 | + for _, r := range results { |
| 84 | + if !first { |
| 85 | + wr.Write([]byte("; ")) // should never happen as we only return one result |
| 86 | + } |
| 87 | + if _, err := wr.Write([]byte(r.String())); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + first = false |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
0 commit comments