This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.go
164 lines (133 loc) · 3.95 KB
/
server.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
package server
import (
"encoding/json"
"fmt"
"github.com/kubeflow/code-intelligence/Label_Microservice/go/cmd/automl/pkg/automl"
"github.com/kubeflow/code-intelligence/Label_Microservice/go/cmd/automl/pkg/kpt"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/http"
"time"
)
const (
kind = "NeedsSync"
apiVersion = "v1alpha1"
)
type Server struct {
Project string
Location string
Name string
KptFile string
SetterName string
RetrainInterval time.Duration
}
type NeedsSyncResponse struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
NeedsSync bool `json:"needsSync"`
Parameters map[string]string `json:"parameters"`
Errors []responseError `json:"errors,omitempty"`
}
type responseError struct {
Message string `json:"message"`
}
func (s *Server) Healthz(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
w.WriteHeader(http.StatusOK)
}
func appendError(r *NeedsSyncResponse, msg string) {
r.Errors = append(r.Errors, responseError{Message: msg})
}
func (s *Server) NeedsSync(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
response := &NeedsSyncResponse{
TypeMeta: metav1.TypeMeta{
Kind: kind,
APIVersion: apiVersion,
},
NeedsSync: false,
Parameters: map[string]string{},
Errors: []responseError{},
}
getErr := func() error {
latest, err := automl.GetLatestDeployed(s.Project, s.Location, s.Name)
if err != nil {
appendError(response, fmt.Sprintf("Error getting latest model; %v", err))
return err
}
if latest == nil {
log.Infof("No deployed models found: project: %v, location: %v, Display Name: %v", s.Project, s.Location, s.Name)
} else {
response.Parameters["name"] = latest.GetName()
}
setterPath := []string{"openAPI", "definitions", "io.k8s.cli.setters." + s.SetterName, "x-k8s-cli", "setter", s.SetterName}
current, err := kpt.GetKptSetter(s.KptFile, setterPath)
if err != nil {
appendError(response, fmt.Sprintf("Error getting current model; %v", err))
return err
}
response.NeedsSync = current != latest.GetName()
return nil
}()
if getErr != nil {
log.Errorf("Error determining if sync needed; %v", getErr)
}
buf, err := json.Marshal(response)
if err != nil {
log.Errorf("Error marshling response; %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(response.Errors) > 0 {
w.WriteHeader(http.StatusInternalServerError)
}
if _, err := w.Write(buf); err != nil {
log.Errorf("Error writing response; %v", err)
}
}
func (s *Server) NeedsTrain(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
response := &NeedsSyncResponse{
TypeMeta: metav1.TypeMeta{
Kind: kind,
APIVersion: apiVersion,
},
NeedsSync: false,
Parameters: map[string]string{},
Errors: []responseError{},
}
getErr := func() error {
latest, err := automl.GetLatestTrained(s.Project, s.Location, s.Name)
if err != nil {
appendError(response, fmt.Sprintf("Error getting latest model; %v", err))
return err
}
if latest == nil {
response.NeedsSync = true
return nil
}
// Retrain the model if it exceeds some age
now := time.Now()
age := now.Sub(latest.GetCreateTime().AsTime())
if age > s.RetrainInterval {
log.Infof("Latest model %v; is %v old", latest.GetName(), age)
response.NeedsSync = true
}
return nil
}()
if getErr != nil {
log.Errorf("Error determining if sync needed; %v", getErr)
}
buf, err := json.Marshal(response)
if err != nil {
log.Errorf("Error marshling response; %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(response.Errors) > 0 {
w.WriteHeader(http.StatusInternalServerError)
}
if _, err := w.Write(buf); err != nil {
log.Errorf("Error writing response; %v", err)
}
}