-
Notifications
You must be signed in to change notification settings - Fork 7
/
start.go
126 lines (112 loc) · 3.94 KB
/
start.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
package command
import (
"context"
"os"
"os/signal"
"syscall"
"time"
"github.com/lunarway/release-manager/cmd/daemon/kubernetes"
httpinternal "github.com/lunarway/release-manager/internal/http"
"github.com/lunarway/release-manager/internal/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// Release Daemon wathces for the following changes
// 1. Successful release of a new deployment
// 2. Detects CrashLoopBackOff, fetches the specific pods log
// 3. Detects CreateContainerConfigError, and fetches the message about the wrong config.
func StartDaemon() *cobra.Command {
var environment, kubeConfigPath string
var moduloCrashReportNotif float64
var logConfiguration *log.Configuration
client := httpinternal.Client{}
var command = &cobra.Command{
Use: "start",
Short: "start the release-daemon",
RunE: func(c *cobra.Command, args []string) error {
done := make(chan error, 1)
logConfiguration.ParseFromEnvironmnet()
log.Init(logConfiguration)
kubectl, err := kubernetes.NewClient(kubeConfigPath, moduloCrashReportNotif, &kubernetes.ReleaseManagerExporter{
Log: log.With("type", "k8s-exporter"),
Client: client,
Environment: environment,
})
if err != nil {
return err
}
log.Info("Deamon started")
go func() {
for {
err = kubectl.HandleNewDeployments(context.Background())
if err != nil && err != kubernetes.ErrWatcherClosed {
done <- errors.WithMessage(err, "kubectl handle new deployments: watcher closed")
return
}
}
}()
go func() {
for {
err = kubectl.HandleNewDaemonSets(context.Background())
if err != nil && err != kubernetes.ErrWatcherClosed {
done <- errors.WithMessage(err, "kubectl handle new daemonsets: watcher closed")
return
}
}
}()
go func() {
for {
err = kubectl.HandleNewStatefulSets(context.Background())
if err != nil && err != kubernetes.ErrWatcherClosed {
done <- errors.WithMessage(err, "kubectl handle new statefulsets: watcher closed")
return
}
}
}()
go func() {
for {
err = kubectl.HandlePodErrors(context.Background())
if err != nil && err != kubernetes.ErrWatcherClosed {
done <- errors.WithMessage(err, "kubectl handle pod errors: watcher closed")
return
}
}
}()
go func() {
for {
err = kubectl.HandleJobErrors(context.Background())
if err != nil && err != kubernetes.ErrWatcherClosed {
done <- errors.WithMessage(err, "kubectl handle job errors: watcher closed")
return
}
}
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Infof("received os signal '%s'", sig)
done <- nil
}()
err = <-done
if err != nil {
log.Errorf("Exited unknown error: %v", err)
os.Exit(1)
}
log.Infof("Program ended")
return nil
},
}
command.Flags().StringVar(&client.BaseURL, "release-manager-url", os.Getenv("RELEASE_MANAGER_ADDRESS"), "address of the release-manager, e.g. http://release-manager")
command.Flags().StringVar(&client.Metadata.AuthToken, "auth-token", os.Getenv("DAEMON_AUTH_TOKEN"), "token to be used to communicate with the release-manager")
command.Flags().DurationVar(&client.Timeout, "http-timeout", 20*time.Second, "HTTP request timeout")
command.Flags().StringVar(&environment, "environment", "", "environment where release-daemon is running")
command.Flags().StringVar(&kubeConfigPath, "kubeconfig", "", "path to kubeconfig file. If not specified, then daemon is expected to run inside kubernetes")
command.Flags().Float64Var(&moduloCrashReportNotif, "modulo-crash-report-notif", 5, "modulo for how often to report CrashLoopBackOff events")
// errors are skipped here as the only case they can occour are if thee flag
// does not exist on the command.
//nolint:errcheck
command.MarkFlagRequired("environment")
logConfiguration = log.RegisterFlags(command)
return command
}