-
Notifications
You must be signed in to change notification settings - Fork 621
Add reload logic to collector container start script. #4099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ package collector | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
|
@@ -16,6 +17,8 @@ import ( | |
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" | ||
) | ||
|
||
const configDirectory = "/etc/otel-collector" | ||
|
||
// AddToConfigMap populates the shared ConfigMap with fields needed to run the Collector. | ||
func AddToConfigMap( | ||
ctx context.Context, | ||
|
@@ -50,7 +53,7 @@ func AddToPod( | |
// Create volume and volume mount for otel collector config | ||
configVolumeMount := corev1.VolumeMount{ | ||
Name: "collector-config", | ||
MountPath: "/etc/otel-collector", | ||
MountPath: configDirectory, | ||
ReadOnly: true, | ||
} | ||
configVolume := corev1.Volume{Name: configVolumeMount.Name} | ||
|
@@ -144,22 +147,37 @@ func AddToPod( | |
|
||
// startCommand generates the command script used by the collector container | ||
func startCommand(includeLogrotate bool) []string { | ||
var startScript = ` | ||
/otelcol-contrib --config /etc/otel-collector/config.yaml | ||
` | ||
|
||
var logrotateCommand string | ||
if includeLogrotate { | ||
startScript = ` | ||
/otelcol-contrib --config /etc/otel-collector/config.yaml & | ||
logrotateCommand = `logrotate -s /tmp/logrotate.status /etc/logrotate.d/logrotate.conf` | ||
} | ||
|
||
var startScript = fmt.Sprintf(` | ||
OTEL_PIDFILE=/tmp/otel.pid | ||
|
||
start_otel_collector() { | ||
echo "Starting OTel Collector" | ||
/otelcol-contrib --config %s/config.yaml & | ||
echo $! > $OTEL_PIDFILE | ||
} | ||
start_otel_collector | ||
|
||
exec {fd}<> <(:||:) | ||
while read -r -t 5 -u "${fd}" ||:; do | ||
logrotate -s /tmp/logrotate.status /etc/logrotate.d/logrotate.conf | ||
%s | ||
Comment on lines
-157
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🌱 Perhaps this could always be |
||
if [[ "${directory}" -nt "/proc/self/fd/${fd}" ]] && kill -HUP $(head -1 ${OTEL_PIDFILE?}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 What happens if Bash is terminated? Is the entire container torn down, including the collector? If so, we can keep the collector pid in a variable in memory. # start
otel ... &
OTEL_PID=$!
# here
kill -HUP ${OTEL_PID}
# below
[[ ! -e /proc/${OTEL_PID} ]] |
||
then | ||
echo "OTel configuration changed..." | ||
exec {fd}>&- && exec {fd}<> <(:||:) | ||
stat --format='Loaded configuration dated %%y' "${directory}" | ||
fi | ||
if [[ ! -e /proc/$(head -1 ${OTEL_PIDFILE?}) ]] ; then | ||
start_otel_collector | ||
fi | ||
done | ||
` | ||
} | ||
`, configDirectory, logrotateCommand) | ||
|
||
wrapper := `monitor() {` + startScript + `}; export -f monitor; exec -a "$0" bash -ceu monitor` | ||
wrapper := `monitor() {` + startScript + `}; export directory="$1"; export -f monitor; exec -a "$0" bash -ceu monitor` | ||
|
||
return []string{"bash", "-ceu", "--", wrapper, "collector"} | ||
return []string{"bash", "-ceu", "--", wrapper, "collector", configDirectory} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the
otelcontrib
process have a name? Or did we want the PID to check if it's running (without recourse topgrep
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm honestly not sure of the best way to do this. I used some prior art in our codebase as a starting point