forked from kptdev/kpt
/
status.go
149 lines (130 loc) · 4.41 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2022 The kpt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rootsyncset
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
)
// checkSyncStatus fetches the RootSync using the provided client and computes the sync status. The rules
// for computing status here mirrors the one used in the status command in the nomos cli.
func checkSyncStatus(ctx context.Context, client dynamic.Interface, rssName string) (string, error) {
// TODO: Change this to use the RootSync type instead of Unstructured.
rs, err := client.Resource(rootSyncGVR).Namespace(rootSyncNamespace).Get(ctx, rssName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get RootSync: %w", err)
}
generation, _, err := unstructured.NestedInt64(rs.Object, "metadata", "generation")
if err != nil {
return "", fmt.Errorf("failed to read generation from RootSync: %w", err)
}
observedGeneration, _, err := unstructured.NestedInt64(rs.Object, "status", "observedGeneration")
if err != nil {
return "", fmt.Errorf("failed to read observedGeneration from RootSync: %w", err)
}
if generation != observedGeneration {
return "Pending", nil
}
conditions, _, err := unstructured.NestedSlice(rs.Object, "status", "conditions")
if err != nil {
return "", fmt.Errorf("failed to extract conditions from RootSync: %w", err)
}
val, found, err := getConditionStatus(conditions, "Stalled")
if err != nil {
return "", fmt.Errorf("error fetching condition 'Stalled' from conditions slice: %w", err)
}
if found && val == "True" {
return "Stalled", nil
}
val, found, err = getConditionStatus(conditions, "Reconciling")
if err != nil {
return "", fmt.Errorf("error fetching condition 'Reconciling' from conditions slice: %w", err)
}
if found && val == "True" {
return "Reconciling", nil
}
cond, found, err := getCondition(conditions, "Syncing")
if err != nil {
return "", fmt.Errorf("error fetching condition 'Syncing' from conditions slice: %w", err)
}
if !found {
return "Reconciling", nil
}
errCount, err := extractErrorCount(cond)
if err != nil {
return "", fmt.Errorf("error extracting error count from 'Syncing' condition: %w", err)
}
if errCount > 0 {
return "Error", nil
}
val, err = extractStringField(cond, "status")
if err != nil {
return "", fmt.Errorf("error extracting status of 'Syncing' condition: %w", err)
}
if val == "True" {
return "Pending", nil
}
return "Synced", nil
}
func getConditionStatus(conditions []interface{}, condType string) (string, bool, error) {
cond, found, err := getCondition(conditions, condType)
if err != nil {
return "", false, err
}
if !found {
return "", false, nil
}
s, err := extractStringField(cond, "status")
if err != nil {
return "", false, err
}
return s, true, nil
}
func getCondition(conditions []interface{}, condType string) (map[string]interface{}, bool, error) {
for i := range conditions {
cond, ok := conditions[i].(map[string]interface{})
if !ok {
return map[string]interface{}{}, false, fmt.Errorf("failed to extract condition %d from slice", i)
}
t, err := extractStringField(cond, "type")
if err != nil {
return map[string]interface{}{}, false, err
}
if t != condType {
continue
}
return cond, true, nil
}
return map[string]interface{}{}, false, nil
}
func extractStringField(condition map[string]interface{}, field string) (string, error) {
t, ok := condition[field]
if !ok {
return "", fmt.Errorf("condition does not have a type field")
}
condVal, ok := t.(string)
if !ok {
return "", fmt.Errorf("value of '%s' condition is not of type 'string'", field)
}
return condVal, nil
}
func extractErrorCount(cond map[string]interface{}) (int64, error) {
count, found, err := unstructured.NestedInt64(cond, "errorSummary", "totalCount")
if err != nil || !found {
return 0, err
}
return count, nil
}