-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
112 lines (87 loc) · 3.04 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
package main
import (
"fmt"
"os"
"strings"
"github.com/mattn/go-colorable"
"github.com/rancher/system-agent/pkg/applyinator"
"github.com/rancher/system-agent/pkg/config"
"github.com/rancher/system-agent/pkg/image"
"github.com/rancher/system-agent/pkg/k8splan"
"github.com/rancher/system-agent/pkg/localplan"
"github.com/rancher/system-agent/pkg/version"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
const (
cattleLogLevelEnv = "CATTLE_LOGLEVEL"
cattleAgentConfigEnv = "CATTLE_AGENT_CONFIG"
cattleAgentStrictVerifyEnv = "CATTLE_AGENT_STRICT_VERIFY"
defaultConfigFile = "/etc/rancher/agent/config.yaml"
)
func main() {
logrus.SetOutput(colorable.NewColorableStdout())
if os.Getuid() != 0 {
logrus.Fatalf("Must be run as root.")
}
rawLevel := os.Getenv(cattleLogLevelEnv)
if rawLevel != "" {
if lvl, err := logrus.ParseLevel(os.Getenv(cattleLogLevelEnv)); err != nil {
logrus.Fatal(err)
} else {
logrus.SetLevel(lvl)
}
}
app := &cli.App{
Name: "rancher-system-agent",
Usage: "Rancher System Agent runs a sentinel that reconciles desired plans with the node it is being run on",
Version: version.FriendlyVersion(),
Commands: []*cli.Command{
{
Name: "sentinel",
Usage: "run the rancher-system-agent sentinel to watch plans",
Action: run,
},
}}
if err := app.Run(os.Args); err != nil {
logrus.Fatalf("Fatal error running: %v", err)
}
}
func run(_ *cli.Context) error {
topContext := signals.SetupSignalContext()
logrus.Infof("Rancher System Agent version %s is starting", version.FriendlyVersion())
configFile := os.Getenv(cattleAgentConfigEnv)
if configFile == "" {
configFile = defaultConfigFile
}
var cf config.AgentConfig
err := config.Parse(configFile, &cf)
if err != nil {
return fmt.Errorf("unable to parse config file: %w", err)
}
if !cf.LocalEnabled && !cf.RemoteEnabled {
return fmt.Errorf("local and/or remote watching must be enabled")
}
logrus.Infof("Using directory %s for work", cf.WorkDir)
imageUtil := image.NewUtility(cf.ImagesDir, cf.ImageCredentialProviderConfig, cf.ImageCredentialProviderBinDir, cf.AgentRegistriesFile)
applyinator := applyinator.NewApplyinator(cf.WorkDir, cf.PreserveWorkDir, cf.AppliedPlanDir, cf.InterlockDir, imageUtil)
if cf.RemoteEnabled {
logrus.Infof("Starting remote watch of plans")
var connInfo config.ConnectionInfo
if err := config.Parse(cf.ConnectionInfoFile, &connInfo); err != nil {
return fmt.Errorf("unable to parse connection info file: %w", err)
}
var strictVerify bool // When strictVerify is set to true, the kubeconfig validator will not discard CA data if it is invalid
if strings.ToLower(os.Getenv(cattleAgentStrictVerifyEnv)) == "true" {
strictVerify = true
}
k8splan.Watch(topContext, *applyinator, connInfo, strictVerify)
}
if cf.LocalEnabled {
logrus.Infof("Starting local watch of plans in %s", cf.LocalPlanDir)
localplan.WatchFiles(topContext, *applyinator, cf.LocalPlanDir)
}
<-topContext.Done()
return nil
}