Skip to content

Commit

Permalink
storage-controller: register only in control plane (#87)
Browse files Browse the repository at this point in the history
We don't need to register storage-controllers in console anymore.
  • Loading branch information
fcdm authored Jul 2, 2024
1 parent 4e68735 commit 1083c47
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 77 deletions.
2 changes: 1 addition & 1 deletion charts/neon-storage-controller/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: neon-storage-controller
description: Neon storage controller
type: application
version: 1.0.12
version: 1.1.0
appVersion: "0.1.0"
kubeVersion: "^1.18.x-x"
home: https://neon.tech
Expand Down
2 changes: 1 addition & 1 deletion charts/neon-storage-controller/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# neon-storage-controller

![Version: 1.0.12](https://img.shields.io/badge/Version-1.0.12-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) [![Lint and Test Charts](https://github.com/neondatabase/helm-charts/actions/workflows/lint-test.yaml/badge.svg)](https://github.com/neondatabase/helm-charts/actions/workflows/lint-test.yaml)
![Version: 1.1.0](https://img.shields.io/badge/Version-1.1.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) [![Lint and Test Charts](https://github.com/neondatabase/helm-charts/actions/workflows/lint-test.yaml/badge.svg)](https://github.com/neondatabase/helm-charts/actions/workflows/lint-test.yaml)

Neon storage controller

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
HOST = os.environ["HOST"]
PORT = os.getenv("PORT", 50051)

GLOBAL_CPLANE_JWT_TOKEN = os.environ["CONTROL_PLANE_JWT_TOKEN"]
LOCAL_CPLANE_JWT_TOKEN = os.environ["CONTROL_PLANE_JWT_TOKEN"]
CPLANE_JWT_TOKEN = os.environ["CONTROL_PLANE_JWT_TOKEN"]
CONSOLE_API_KEY = os.environ["CONSOLE_API_KEY"]

# To register new pageservers
URL_PATH = "management/api/v2/pageservers"
# To get pageservers
ADMIN_URL_PATH = "api/v1/admin/pageservers"
ADMIN_URL_PATH = f"regions/{REGION}/api/v1/admin/pageservers"

GLOBAL_CPLANE_URL = f"{os.environ['GLOBAL_CPLANE_URL'].strip('/')}/{URL_PATH}"
LOCAL_CPLANE_URL = f"{os.environ['LOCAL_CPLANE_URL'].strip('/')}/{URL_PATH}"
CPLANE_MANAGEMENT_URL = f"{os.environ['CPLANE_URL'].strip('/')}/{URL_PATH}"
CONSOLE_URL = f"{os.environ['CONSOLE_URL']}/{ADMIN_URL_PATH}"

PAYLOAD = dict(
Expand All @@ -43,7 +41,7 @@
)


def get_data(url, token, host=None):
def get_data(url, token, host=None, method="GET", data=None, raise_on_error=True):
if host is not None:
url = f"{url}/{host}"
headers = {
Expand All @@ -52,7 +50,7 @@ def get_data(url, token, host=None):
"Content-Type": "application/json",
}
# Check if the server is already registered
req = urllib.request.Request(url=url, headers=headers, method="GET")
req = urllib.request.Request(url=url, headers=headers, method=method, data=data)
try:
with urllib.request.urlopen(req) as response:
code = response.getcode()
Expand All @@ -64,11 +62,14 @@ def get_data(url, token, host=None):
if code == 200:
return json.loads(response_body)

raise Exception(f'GET {url} returned unexpected response: {code} {response_body}')
if raise_on_error:
raise Exception(f'{method} {url} returned unexpected response: {code} {response_body}')

return {}


def get_pageserver_id(url, token):
data = get_data(url, token, HOST)
data = get_data(url, token, HOST, raise_on_error=False)
if "node_id" in data:
return int(data["node_id"])

Expand All @@ -85,21 +86,8 @@ def get_pageserver_version():


def register(url, token, payload):
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": "Python script 1.0",
}
data = str(json.dumps(payload)).encode()
req = urllib.request.Request(
url=url,
data=data,
headers=headers,
method="POST",
)
with urllib.request.urlopen(req) as resp:
response = json.loads(resp.read())
response = get_data(url, token, data=data, method="POST")
log.info(response)
if "node_id" in response:
return int(response["node_id"])
Expand All @@ -117,8 +105,7 @@ def register(url, token, payload):
log.info(
json.dumps(
dict(
GLOBAL_CPLANE_URL=GLOBAL_CPLANE_URL,
LOCAL_CPLANE_URL=LOCAL_CPLANE_URL,
CPLANE_MANAGEMENT_URL=CPLANE_MANAGEMENT_URL,
CONSOLE_URL=CONSOLE_URL,
**PAYLOAD,
),
Expand All @@ -136,43 +123,20 @@ def register(url, token, payload):
log.info(f"found latest version={version} for region={REGION}")
PAYLOAD.update(dict(version=version))

log.info("check if pageserver already registered or not in console")
node_id_in_console = get_pageserver_id(
GLOBAL_CPLANE_URL, GLOBAL_CPLANE_JWT_TOKEN
)

if node_id_in_console is None:
log.info("Registering storage controller in console")
node_id_in_console = register(
GLOBAL_CPLANE_URL, GLOBAL_CPLANE_JWT_TOKEN, PAYLOAD
)
log.info(
f"Storage controller registered in console with node_id \
{node_id_in_console}"
)
else:
log.info(
f"Storage controller already registered in console with node_id \
{node_id_in_console}"
)

log.info("check if pageserver already registered or not in cplane")
node_id_in_cplane = get_pageserver_id(
LOCAL_CPLANE_URL, LOCAL_CPLANE_JWT_TOKEN
log.info("check if pageserver already registered")
node_id = get_pageserver_id(
CPLANE_MANAGEMENT_URL, CPLANE_JWT_TOKEN
)

if node_id_in_cplane is None and node_id_in_console is not None:
PAYLOAD.update(dict(node_id=node_id_in_console))
log.info("Registering storage controller in cplane")
node_id_in_cplane = register(
LOCAL_CPLANE_URL, LOCAL_CPLANE_JWT_TOKEN, PAYLOAD
if node_id is None:
log.info("Registering storage controller")
node_id = register(
CPLANE_MANAGEMENT_URL, CPLANE_JWT_TOKEN, PAYLOAD
)
log.info(
f"Storage controller registered in cplane with node_id \
{node_id_in_cplane}"
f"Storage controller registered with node_id {node_id}"
)
else:
log.info(
f"Storage controller already registered in cplane with node_id \
{node_id_in_cplane}"
f"Storage controller already registered with node_id {node_id}"
)
13 changes: 4 additions & 9 deletions charts/neon-storage-controller/templates/post-install-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
{{ if (empty .Values.registerControlPlane.region_id ) }}
{{- fail (printf "Value for .Values.registerControlPlane.region_id is empty") }}
{{- end }}
{{ if (empty .Values.registerControlPlane.global_cplane_url) }}
{{- fail (printf "Value for .Values.registerControlPlane.global_cplane_url is empty") }}
{{- end }}
{{ if (empty .Values.registerControlPlane.local_cplane_url) }}
{{- fail (printf "Value for .Values.registerControlPlane.local_cplane_url is empty") }}
{{ if (empty .Values.registerControlPlane.cplane_url) }}
{{- fail (printf "Value for .Values.registerControlPlane.cplane_url is empty") }}
{{- end }}
apiVersion: batch/v1
kind: Job
Expand Down Expand Up @@ -61,10 +58,8 @@ spec:
value: {{ index .Values.service.annotations "external-dns.alpha.kubernetes.io/hostname" | quote }}
- name: PORT
value: {{ .Values.service.port | quote }}
- name: GLOBAL_CPLANE_URL
value: {{ .Values.registerControlPlane.global_cplane_url | quote }}
- name: LOCAL_CPLANE_URL
value: {{ .Values.registerControlPlane.local_cplane_url | quote }}
- name: CPLANE_URL
value: {{ .Values.registerControlPlane.cplane_url | quote }}
- name: CONSOLE_URL
value: {{ .Values.registerControlPlane.console_url | quote }}
- name: REGION_ID
Expand Down
11 changes: 2 additions & 9 deletions charts/neon-storage-controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,11 @@ settings:
# This will run postinstall job
registerControlPlane:
enable: false
# region_id: "aws-us-east-2"
# generate using
# curl https://console.stage.neon.tech/regions/console/api/v1/admin/issue_token \
# -H "Accept: application/json" \
# -H "Content-Type: application/json" \
# -H "Authorization: Bearer $NEON_API_KEY" \
# -X POST -d '{"ttl_seconds": 31536000, "scope": "infra"}'
# region_id: "<region-id>"
controlPlaneJwtToken: ""
# Console API key to list existing pageserver to get version
apiKey: ""
# global_cplane_url: "http://neon-internal-api.aws.neon.build"
# local_cplane_url: "https://control-plane.zeta.us-east-2.internal.aws.neon.build"
# cplane_url: "<control-plane-url>"
# console_url: ""
resources:
limits:
Expand Down

0 comments on commit 1083c47

Please sign in to comment.