Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
PLATFORMINFRA-193: rework byoh-agent flags
Browse files Browse the repository at this point in the history
  • Loading branch information
vaspahomov committed Apr 3, 2024
1 parent 5f72e50 commit 48323ce
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions agent/.goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ nfpms:
- src: debian/systemd
dst: /etc/systemd/system/
type: tree
- src: debian/scripts
dst: /opt/byoh-agent/
type: tree

changelog:
sort: asc
Expand Down
44 changes: 44 additions & 0 deletions agent/debian/scripts/byoh-agent-flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/python3
import json
import os

METADATA_FILE_PATH = '/etc/infra-k8s/host-metadata.json'
FLAGS_FILE_DIR = '/etc/byoh-agent'
FLAGS_FILE_PATH = f'{FLAGS_FILE_DIR}/byoh-agent-flags.env'

def load_metadata():
with open(METADATA_FILE_PATH) as f:
return json.load(f)

def create_config_dir():
os.makedirs(FLAGS_FILE_DIR, exist_ok=True)

def construct_labels(metadata):
common_labels = [
"--label", f"byoh.infrastructure.cluster.x-k8s.io/type={metadata.get('node_role')}",
"--label", f"byoh.infrastructure.cluster.x-k8s.io/cluster={metadata.get('cluster_name')}",
"--label", f"byoh.infrastructure.cluster.x-k8s.io/hostname={os.getenv('HOSTNAME')}",
]

extra_labels = []
for k, v in metadata.get('extra_labels', {}).items():
extra_labels += ["--label", f"{k}={v}"]

return common_labels + extra_labels

def construct_args(metadata):
return construct_labels(metadata) + [
"--bootstrap-kubeconfig", "/etc/infra-k8s/kubeconfig",
"--skip-installation",
"--namespace", metadata.get('cluster_name'),
"--metricsbindaddress", ":8081",
]

def write_env_file(metadata):
# Writing structured form of flags as string into '/etc/byoh-agent/byoh-agent-flags.env'
with open(FLAGS_FILE_PATH, 'w') as f:
f.write(f"BYOH_AGENT_FLAGS=\"{' '.join(construct_args(metadata))}\"")

metadata = load_metadata()
create_config_dir()
write_env_file(metadata)
5 changes: 5 additions & 0 deletions agent/debian/systemd/byoh-agent-flag.path
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Path]
Unit=byoh-agent-flag.service
PathModified=/etc/infra-k8s/host-metadata.json
[Install]
WantedBy=multi-user.target
16 changes: 16 additions & 0 deletions agent/debian/systemd/byoh-agent-flag.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Prepares env with flags for byoh-agent service
After=network.target
StartLimitIntervalSec=60
StartLimitBurst=5
PartOf=byoh-agent.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/opt/byoh-agent/byoh-agent-flags.py
Restart=on-failure
RestartSec=5

[Install]
WantedBy=byoh-agent.service
1 change: 1 addition & 0 deletions agent/debian/systemd/byoh-agent-watcher.path
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[Path]
Unit=byoh-agent-watcher.service
PathModified=/root/.byoh/config
PathModified=/etc/byoh-agent/byoh-agent-flags.env
[Install]
WantedBy=multi-user.target

0 comments on commit 48323ce

Please sign in to comment.