-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathentrypoint.go
More file actions
180 lines (157 loc) · 5.29 KB
/
Copy pathentrypoint.go
File metadata and controls
180 lines (157 loc) · 5.29 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/GoogleCloudPlatform/run-gmp-sidecar/confgenerator"
)
// Create channel to listen for signals.
var signalChan chan (os.Signal) = make(chan os.Signal, 1)
var userConfigFile = "/etc/rungmp/config.yaml"
var otelConfigFile = "/run/rungmp/otel.yaml"
var configRefreshInterval = 20 * time.Second
var selfMetricsPort = 0
var livenessProbePort = 13133
var livenessProbePath = "/liveness"
var delayLivenessProbe = 5 * time.Second
func getRawUserConfig(userConfigFile string) (string, error) {
_, err := os.Stat(userConfigFile)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", fmt.Errorf("failed to stat file %q: %v", userConfigFile, err)
}
data, err := ioutil.ReadFile(userConfigFile)
if err != nil {
return "", fmt.Errorf("failed to read file %q: %v", userConfigFile, err)
}
return string(data), nil
}
// Generate OTel config from RunMonitoring config. Returns an error if
// generation of OTel configs failed.
func generateOtelConfig(ctx context.Context, userConfigFile string) error {
// Pick up RunMonitoring configuration from mounted volume that is tied to
// secret manager. Translate it from RunMonitoring to OTel.
c, err := confgenerator.ReadConfigFromFile(ctx, userConfigFile)
if err != nil {
log.Fatal(err)
}
if selfMetricsPort == 0 {
selfMetricsPort, err = confgenerator.GetFreePort()
if err != nil {
return err
}
}
// Create the OTel config and write it to disk
otel, err := c.GenerateOtelConfig(ctx, selfMetricsPort)
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(otelConfigFile), 0755); err != nil {
return fmt.Errorf("failed to create directory for %q: %v", otelConfigFile, err)
}
if err := ioutil.WriteFile(otelConfigFile, []byte(otel), 0644); err != nil {
return fmt.Errorf("failed to write file to %q: %v", otelConfigFile, err)
}
return nil
}
// The container is allocated CPU for the duration of the healthcheck. Delaying
// the response to this probe allows the container to complete telemetry flushes
// that may have been throttled.
//
// TODO(b/342463831): Use a more reliable way of checking if telemetry is being
// flushed instead of using a static sleep.
func healthcheckHandler(_ http.ResponseWriter, _ *http.Request) {
time.Sleep(delayLivenessProbe)
}
func main() {
// SIGINT handles Ctrl+C locally.
// SIGTERM handles Cloud Run termination signal.
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
ctx := context.Background()
lastRawConfig, err := getRawUserConfig(userConfigFile)
if err != nil {
log.Fatal(err)
}
// Generate the OTel config for the first time.
err = generateOtelConfig(ctx, userConfigFile)
if err != nil {
log.Fatal(err)
}
entrypointMux := http.NewServeMux()
entrypointMux.HandleFunc(livenessProbePath, healthcheckHandler)
go func() {
err := http.ListenAndServe(fmt.Sprintf(":%d", livenessProbePort), entrypointMux)
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
// Spin up new-subprocess that runs the OTel collector and store the PID.
// This OTel collector should use the generated config.
var procAttr os.ProcAttr
procAttr.Files = []*os.File{nil, /* stdin is not needed for the collector */
os.Stdout, os.Stderr}
collectorProcess, err := os.StartProcess("./rungmpcol", []string{"./rungmpcol", "--config", otelConfigFile}, &procAttr)
if err != nil {
log.Fatal(err)
}
log.Printf("entrypoint: started OTel successfully")
refreshTicker := time.NewTicker(configRefreshInterval)
for {
select {
case <-refreshTicker.C:
rawConfig, err := getRawUserConfig(userConfigFile)
if err != nil {
log.Fatal(err)
}
// Check if we're using the default config. Only reload if something
// has changed since the last time we checked.
if rawConfig == lastRawConfig {
continue
}
// Something changed since the last time we checked the config.
err = generateOtelConfig(ctx, userConfigFile)
if err != nil {
log.Fatal(err)
}
lastRawConfig = rawConfig
// Signal the OTel collector to reload its config
collectorProcess.Signal(syscall.SIGHUP)
log.Println("entrypoint: reloaded OTel config")
case sig := <-signalChan:
// Wait for signals from Cloud Run. Signal the sub process appropriately
// after making relevant changes to the config and/or health signals.
// TODO(b/307317433): Consider having a timeout to shutdown the subprocess
// non-gracefully.
log.Printf("entrypoint: %s signal caught", sig)
collectorProcess.Signal(sig)
processState, err := collectorProcess.Wait()
if err != nil {
log.Fatalf(processState.String(), err)
}
log.Print("entrypoint: sidecar exited")
return
}
}
}