-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
109 lines (97 loc) · 3.96 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
// Copyright (c) 2018, Vít Kotačka
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package main
import (
"context"
"os"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"github.com/sw-samuraj/oci-sd/adapter"
"github.com/sw-samuraj/oci-sd/oci"
)
var (
configFile string
outputFile string
logDirectory string
authvar bool
cfg config
compartment string
sanitise bool
)
func init() {
flag.StringVarP(&configFile, "config-file", "c", "oci-sd.toml", "external config file")
flag.StringVarP(&outputFile, "output-file", "o", "oci-sd.json", "output file for file_sd compatible file")
flag.StringVarP(&logDirectory, "log-directory", "l", "/var/log/oci-sd/", "log directory")
flag.BoolVarP(&authvar, "instance-principal", "i", false, "initialise with instance principal authentication")
flag.StringVarP(&compartment, "compartment", "t", "", "compartment for discovering targets")
flag.BoolVarP(&sanitise, "sanitise", "s", false, "sanitise instance tags to fit Prometheus requirements by removing special characters (:, -)")
}
func main() {
flag.Parse()
logger := log.New()
logSetup(logDirectory, logger)
if authvar {
logger.Info("initialising with instance principal authentication")
if compartment == "" {
logger.Fatal("flag --compartment (or shorthand -t) cannot be empty if instance principal is used")
}
cfg.SDConfig.InstancePrincipal = true
cfg.SDConfig.Compartment = compartment
if err := cfg.SDConfig.ApplyDefault(); err != nil {
logger.WithField("error", err).Warn("error to apply default config values")
}
} else {
logger.Info("initialising with user authentication")
cfg = parseConfig()
}
cfg.SDConfig.Sanitise = sanitise
ctx := context.Background()
disc, err := oci.NewDiscovery(&cfg.SDConfig, logger)
if err != nil {
logger.WithFields(log.Fields{"func": "main", "err": err}).Fatal("can't create a discovery")
}
sdAdapter := adapter.NewAdapter(ctx, outputFile, "ociSD", disc, *logger)
sdAdapter.Run()
<-ctx.Done()
}
func logSetup(logPath string, logger *log.Logger) {
logger.SetReportCaller(true)
err := os.MkdirAll(logPath, 0755)
if err != nil {
logger.Warn("Can't create a log path. Err: ", err)
}
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "02-01-2006 15:04:05"
Formatter.FullTimestamp = true
logger.SetFormatter(Formatter)
logFile, err := os.OpenFile(logPath+"oci-sd.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
logger.Warn("Can't create a log path. Err: ", err)
}
logger.SetOutput(logFile)
}