This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
159 lines (138 loc) · 3.31 KB
/
main.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
package main
import (
"bytes"
"context"
"encoding/json"
//"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)
var (
debug = pflag.Bool("debug", true, "sets log to debug level")
server *Server
ctx context.Context
)
// LogHook is used to setup custom hooks
type LogHook struct {
Writer io.Writer
Loglevels []log.Level
}
func main() {
//pflag.Parse()
var err error
setupLogger()
ctx = context.Background()
server, err = NewServer()
if err != nil {
log.Fatalf("[error] : %v", err)
}
http.HandleFunc("/process", handle)
http.ListenAndServe(":8090", nil)
os.Exit(0)
}
func handle(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
image := req.URL.Query().Get("image") // e.g. : oss/kubernetes/aks/etcd-operator
if image == "" {
log.Info("Failed to provide image to query")
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(nil)
}
// registry := "upstream.azurecr.io"
// repo := "oss/kubernetes/ingress/nginx-ingress-controller"
// tag := "0.16.2"
registry := strings.Split(image, "/")[0]
repo := strings.Replace(image, registry + "/", "", 1)
tag := "latest"
if strings.Contains(repo, ":") {
tag = strings.Split(repo, ":")[1]
repo = strings.Replace(repo, ":" + tag, "", 1)
}
getImageShaBinary := "getimagesha.sh"
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
cmd := exec.Command(
"sh",
getImageShaBinary,
registry,
repo,
tag,
)
log.Infof("cmd: %v", cmd)
cmd.Dir = dir
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stderr, cmd.Stdout = stderr, stdout
err = cmd.Run()
output := stdout.String()
log.Infof("output: %s",output)
if err != nil {
log.Errorf("error invoking cmd, err: %v, output: %v", err, stderr.String())
}
if output == "null\n" {
log.Infof("[error] : could not find valid digest %s", output)
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(nil)
} else {
digest := strings.TrimSuffix(output, "\n")
log.Infof("digest: %s",digest)
data, err := server.Process(ctx, digest)
if err != nil {
log.Infof("[error] : %s", err)
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(data)
} else {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(data)
}
}
}
// setupLogger sets up hooks to redirect stdout and stderr
func setupLogger() {
log.SetOutput(ioutil.Discard)
// set log level
log.SetLevel(log.InfoLevel)
if *debug {
log.SetLevel(log.DebugLevel)
}
// add hook to send info, debug, warn level logs to stdout
log.AddHook(&LogHook{
Writer: os.Stdout,
Loglevels: []log.Level{
log.InfoLevel,
log.DebugLevel,
log.WarnLevel,
},
})
// add hook to send panic, fatal, error logs to stderr
log.AddHook(&LogHook{
Writer: os.Stderr,
Loglevels: []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
},
})
}
// Fire is called when logging function with current hook is called
// write to appropriate writer based on log level
func (hook *LogHook) Fire(entry *log.Entry) error {
line, err := entry.String()
if err != nil {
return err
}
_, err = hook.Writer.Write([]byte(line))
return err
}
// Levels defines log levels at which hook is triggered
func (hook *LogHook) Levels() []log.Level {
return hook.Loglevels
}