-
Notifications
You must be signed in to change notification settings - Fork 0
/
restclient.go
222 lines (187 loc) · 6.6 KB
/
restclient.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package k8s
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/kaweezle/iknite/pkg/apis/iknite/v1alpha1"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/kubectl/pkg/polymorphichelpers"
)
type RESTClientGetter struct {
clientconfig clientcmd.ClientConfig
}
func (config *Config) RESTClient() *RESTClientGetter {
return &RESTClientGetter{clientcmd.NewDefaultClientConfig(api.Config(*config), nil)}
}
func (r *RESTClientGetter) ToRESTConfig() (*rest.Config, error) {
return r.clientconfig.ClientConfig()
}
func (r *RESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
restconfig, err := r.clientconfig.ClientConfig()
if err != nil {
return nil, err
}
dc, err := discovery.NewDiscoveryClientForConfig(restconfig)
if err != nil {
return nil, err
}
return memory.NewMemCacheClient(dc), nil
}
func (r *RESTClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
dc, err := r.ToDiscoveryClient()
if err != nil {
return nil, err
}
return restmapper.NewDeferredDiscoveryRESTMapper(dc), nil
}
func (r *RESTClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
return r.clientconfig
}
var ApplicationSchemaGroupVersionKind = schema.GroupVersionKind{Group: "argoproj.io", Version: "v1alpha1", Kind: "Application"}
type SyncStatus struct {
Status string `json:"status" protobuf:"bytes,1,opt,name=status,casttype=SyncStatusCode"`
}
type HealthStatus struct {
// Status holds the status code of the application or resource
Status string `json:"status,omitempty" protobuf:"bytes,1,opt,name=status"`
// Message is a human-readable informational message describing the health status
Message string `json:"message,omitempty" protobuf:"bytes,2,opt,name=message"`
}
type ApplicationStatus struct {
Sync SyncStatus `json:"sync,omitempty" protobuf:"bytes,2,opt,name=sync"`
Health HealthStatus `json:"health,omitempty" protobuf:"bytes,3,opt,name=health"`
}
type Application struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata" protobuf:"bytes,1,opt,name=metadata"`
Status ApplicationStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}
type ApplicationStatusViewer struct{}
func StatusViewerFor(kind schema.GroupKind) (polymorphichelpers.StatusViewer, error) {
if kind == ApplicationSchemaGroupVersionKind.GroupKind() {
return &ApplicationStatusViewer{}, nil
}
return polymorphichelpers.StatusViewerFor(kind)
}
func (s *ApplicationStatusViewer) Status(obj runtime.Unstructured, revision int64) (string, bool, error) {
application := &Application{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), application)
if err != nil {
return "", false, fmt.Errorf("failed to convert %T to %T: %v", obj, application, err)
}
healthStatusString := application.Status.Health.Status
syncStatusString := application.Status.Sync.Status
msg := fmt.Sprintf("application \"%s\" sync status: %s, health status: %s", application.Name, syncStatusString, healthStatusString)
return msg, healthStatusString == "Healthy" && syncStatusString == "Synced", nil
}
func (client *RESTClientGetter) HasApplications() (has bool, err error) {
var mapper meta.RESTMapper
if mapper, err = client.ToRESTMapper(); err != nil {
return
}
_, err = mapper.RESTMapping(ApplicationSchemaGroupVersionKind.GroupKind(), ApplicationSchemaGroupVersionKind.Version)
if err != nil {
if meta.IsNoMatchError(err) {
err = nil
} else {
return
}
} else {
has = true
}
return
}
func (client *RESTClientGetter) AllWorkloadStates() (result []*v1alpha1.WorkloadState, err error) {
var _result []*v1alpha1.WorkloadState
var resourceTypes = "deployments,statefulsets,daemonsets"
var hasApplications bool
if hasApplications, err = client.HasApplications(); err != nil {
return
}
if hasApplications {
resourceTypes += ",applications"
}
r := resource.NewBuilder(client).
Unstructured().
AllNamespaces(true).
ResourceTypeOrNameArgs(true, resourceTypes).
ContinueOnError().
Flatten().
Do()
var infos []*resource.Info
if infos, err = r.Infos(); err != nil {
return
}
for _, info := range infos {
var u map[string]interface{}
if u, err = runtime.DefaultUnstructuredConverter.ToUnstructured(info.Object); err != nil {
return
}
var v polymorphichelpers.StatusViewer
if v, err = /* polymorphichelpers. */ StatusViewerFor(info.Object.GetObjectKind().GroupVersionKind().GroupKind()); err != nil {
return
}
var msg string
var ok bool
if msg, ok, err = v.Status(&unstructured.Unstructured{Object: u}, 0); err != nil {
return
}
_result = append(_result, &v1alpha1.WorkloadState{Namespace: info.Namespace, Name: info.ObjectName(), Ok: ok, Message: strings.TrimSuffix(msg, "\n")})
}
sort.SliceStable(_result, func(i, j int) bool {
return _result[i].String() < _result[j].String()
})
result = _result
return
}
type WorkloadStateCallbackFunc func(state bool, total int, ready []*v1alpha1.WorkloadState, unready []*v1alpha1.WorkloadState)
func AreWorkloadsReady(config *Config, callback WorkloadStateCallbackFunc) wait.ConditionWithContextFunc {
client := config.RESTClient()
return func(ctx context.Context) (bool, error) {
states, err := client.AllWorkloadStates()
if err != nil {
return false, err
}
var result bool = true
var ready, unready []*v1alpha1.WorkloadState
for _, state := range states {
if !state.Ok {
result = false
unready = append(unready, state)
} else {
ready = append(ready, state)
}
}
log.WithFields(log.Fields{
"total": len(states),
"ready": len(ready),
"unready": len(unready),
}).Infof("Workloads total: %d, ready: %d, unready:%d", len(states), len(ready), len(unready))
if callback != nil {
callback(result, len(states), ready, unready)
}
return result, nil
}
}
func (config *Config) WaitForWorkloads(ctx context.Context, timeout time.Duration, callback WorkloadStateCallbackFunc) error {
if timeout > 0 {
return wait.PollUntilContextTimeout(ctx, time.Second*time.Duration(2), timeout, true, AreWorkloadsReady(config, callback))
} else {
return wait.PollUntilContextCancel(ctx, time.Second*time.Duration(2), true, AreWorkloadsReady(config, callback))
}
}