From cb9a72de554108746c4ed35f8ad9e0a5550f626d Mon Sep 17 00:00:00 2001 From: Bin Yang Date: Fri, 19 Nov 2021 10:56:09 +0800 Subject: [PATCH] Add practice of building o2ims images inside a container Add helm chart for k8s deployment Signed-off-by: Bin Yang Change-Id: Iae4501c7a489be9266771d304f6a787c5992169b --- README-o2imsbuilder.md | 60 +++++++++++ charts/.helmignore | 22 ++++ charts/Chart.yaml | 19 ++++ charts/resources/scripts/init/o2api_start.sh | 34 ++++++ charts/resources/scripts/init/o2pubsub_start.sh | 24 +++++ charts/resources/scripts/init/o2watcher_start.sh | 24 +++++ charts/resources/scripts/init/postgres_start.sh | 24 +++++ charts/templates/.helmignore | 22 ++++ charts/templates/_helpers.tpl | 49 +++++++++ charts/templates/configmap.yaml | 26 +++++ charts/templates/deployment.yaml | 131 +++++++++++++++++++++++ charts/templates/service.yaml | 30 ++++++ charts/values.yaml | 37 +++++++ docker-compose.yml | 9 +- o2ims/bootstrap.py | 15 ++- requirements.txt | 4 +- 16 files changed, 522 insertions(+), 8 deletions(-) create mode 100644 README-o2imsbuilder.md create mode 100644 charts/.helmignore create mode 100644 charts/Chart.yaml create mode 100644 charts/resources/scripts/init/o2api_start.sh create mode 100644 charts/resources/scripts/init/o2pubsub_start.sh create mode 100644 charts/resources/scripts/init/o2watcher_start.sh create mode 100644 charts/resources/scripts/init/postgres_start.sh create mode 100644 charts/templates/.helmignore create mode 100644 charts/templates/_helpers.tpl create mode 100644 charts/templates/configmap.yaml create mode 100644 charts/templates/deployment.yaml create mode 100644 charts/templates/service.yaml create mode 100644 charts/values.yaml diff --git a/README-o2imsbuilder.md b/README-o2imsbuilder.md new file mode 100644 index 0000000..e19b759 --- /dev/null +++ b/README-o2imsbuilder.md @@ -0,0 +1,60 @@ + +## build o2ims from a container over INF + + +## bring up container + +## Important: make sure container and host shares the same filepath to overcome local dir mounting issue + +mkdir -p /home/sysadmin/share +sudo docker run -dt --privileged -v /home/sysadmin/share/:/home/sysadmin/share/ -v /var/run:/var/run --name o2imsbuilder2 centos:7 + +## build inside container +sudo docker exec -it o2imsbuilder2 bash + +curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose +chmod +x /usr/local/bin/docker-compose +docker-compose -v + +yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo +yum makecache fast +yum install -y docker-ce +docker ps + +yum install -y git + +cd /home/sysadmin/share/ +git clone "https://gerrit.o-ran-sc.org/r/pti/o2" +cd o2 + +mkdir -p temp +cd temp +git clone https://opendev.org/starlingx/config.git +git clone https://opendev.org/starlingx/distcloud-client.git +cd - + +docker-compose build + +## test over inf host +export NAMESPACE=orano2 +kubectl create ns ${NAMESPACE} + +source /etc/platform/openrc +sudo docker login registry.local:9001 -u ${OS_PROJECT_NAME} -p ${OS_PASSWORD} + + +kubectl -n ${NAMESPACE} create secret docker-registry ${OS_PROJECT_NAME}-${NAMESPACE}-registry-secret \ +--docker-server=registry.local:9001 --docker-username=${OS_PROJECT_NAME} \ +--docker-password=${OS_PASSWORD} --docker-email=noreply@windriver.com + +==> secret/admin-orano2-registry-secret created + +sudo docker tag o2imsdms:latest registry.local:9001/admin/o2imsdms:0.1.1 +sudo docker image push registry.local:9001/admin/o2imsdms:0.1.1 + +cd /home/sysadmin/share/o2 +helm install o2imstest charts +kubectl -n ${NAMESPACE} get pods + + +## issues: diff --git a/charts/.helmignore b/charts/.helmignore new file mode 100644 index 0000000..50af031 --- /dev/null +++ b/charts/.helmignore @@ -0,0 +1,22 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/charts/Chart.yaml b/charts/Chart.yaml new file mode 100644 index 0000000..31f4f32 --- /dev/null +++ b/charts/Chart.yaml @@ -0,0 +1,19 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. + +apiVersion: v1 +appVersion: "1.0" +description: A Helm chart to deploy O2 Services +name: orano2 +version: 0.1.0 diff --git a/charts/resources/scripts/init/o2api_start.sh b/charts/resources/scripts/init/o2api_start.sh new file mode 100644 index 0000000..46ea5f5 --- /dev/null +++ b/charts/resources/scripts/init/o2api_start.sh @@ -0,0 +1,34 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. + +#!/bin/bash + +# pull latest code to debug +cd /root/ +git clone "https://gerrit.o-ran-sc.org/r/pti/o2" +# cd o2 +# git pull https://gerrit.o-ran-sc.org/r/pti/o2 refs/changes/85/7085/5 +# pip install retry + +pip install -e /root/o2 + +cat <>/etc/hosts +127.0.0.1 api +127.0.0.1 postgres +127.0.0.1 redis +EOF + +flask run --host=0.0.0.0 --port=80 + +sleep infinity diff --git a/charts/resources/scripts/init/o2pubsub_start.sh b/charts/resources/scripts/init/o2pubsub_start.sh new file mode 100644 index 0000000..e39329b --- /dev/null +++ b/charts/resources/scripts/init/o2pubsub_start.sh @@ -0,0 +1,24 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. + +#!/bin/bash + +# pull latest code to debug +cd /root/ +git clone "https://gerrit.o-ran-sc.org/r/pti/o2" +pip install -e /root/o2 + +python /root/o2/o2ims/entrypoints/redis_eventconsumer.py + +sleep infinity diff --git a/charts/resources/scripts/init/o2watcher_start.sh b/charts/resources/scripts/init/o2watcher_start.sh new file mode 100644 index 0000000..53fd670 --- /dev/null +++ b/charts/resources/scripts/init/o2watcher_start.sh @@ -0,0 +1,24 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. + +#!/bin/bash + +# pull latest code to debug +cd /root/ +git clone "https://gerrit.o-ran-sc.org/r/pti/o2" +pip install -e /root/o2 + +python /root/o2/o2ims/entrypoints/resource_watcher.py + +sleep infinity diff --git a/charts/resources/scripts/init/postgres_start.sh b/charts/resources/scripts/init/postgres_start.sh new file mode 100644 index 0000000..975c0ac --- /dev/null +++ b/charts/resources/scripts/init/postgres_start.sh @@ -0,0 +1,24 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. + +#!/bin/bash + +# sed -i 's/huge_page=try/huge_page=off/' /usr/share/postgresql/postgresql.conf.sample +cat <> /usr/share/postgresql/postgresql.conf.sample +huge_pages = off +EOF + +/docker-entrypoint.sh postgres + +sleep infinity diff --git a/charts/templates/.helmignore b/charts/templates/.helmignore new file mode 100644 index 0000000..50af031 --- /dev/null +++ b/charts/templates/.helmignore @@ -0,0 +1,22 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/charts/templates/_helpers.tpl b/charts/templates/_helpers.tpl new file mode 100644 index 0000000..596f567 --- /dev/null +++ b/charts/templates/_helpers.tpl @@ -0,0 +1,49 @@ +{{/* +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. +# +*/}} + +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "orano2.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "orano2.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "orano2.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} diff --git a/charts/templates/configmap.yaml b/charts/templates/configmap.yaml new file mode 100644 index 0000000..8ff60d8 --- /dev/null +++ b/charts/templates/configmap.yaml @@ -0,0 +1,26 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. + +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Chart.Name }}-scripts-configmap + namespace: {{ .Values.global.namespace }} + labels: + release: {{ .Release.Name }} + app: {{ include "orano2.name" . }} + chart: {{ .Chart.Name }} +data: +{{ tpl (.Files.Glob "resources/scripts/init/*").AsConfig . | indent 2 }} diff --git a/charts/templates/deployment.yaml b/charts/templates/deployment.yaml new file mode 100644 index 0000000..2e8ebf1 --- /dev/null +++ b/charts/templates/deployment.yaml @@ -0,0 +1,131 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: o2api + namespace: {{ .Values.global.namespace }} + labels: + app: o2api +spec: + replicas: 1 + selector: + matchLabels: + app: o2api + template: + metadata: + labels: + app: o2api + spec: + imagePullSecrets: + - name: {{ .Values.o2ims.imagePullSecrets }} +{{- if .Values.o2ims.affinity }} + affinity: +{{ toYaml .Values.o2ims.affinity | indent 8 }} +{{- end }} + containers: + - name: postgres + image: postgres:9.6 + ports: + - containerPort: 5432 + env: + - name: POSTGRES_PASSWORD + value: o2ims123 + - name: POSTGRES_USER + value: o2ims + command: ["/bin/bash", "/opt/postgres_start.sh"] + volumeMounts: + - name: scripts + mountPath: /opt + - name: o2api + image: "{{ .Values.o2ims.image.repository }}:{{ .Values.o2ims.image.tag }}" + ports: + - containerPort: 80 + env: + - name: API_HOST + value: api + - name: DB_HOST + value: postgres + - name: DB_PASSWORD + value: o2ims123 + - name: FLASK_APP + value: /root/o2/o2ims/entrypoints/flask_application.py + - name: FLASK_DEBUG + value: "1" + - name: LOGGING_CONFIG_LEVEL + value: DEBUG + - name: OS_AUTH_URL + - name: OS_PASSWORD + - name: OS_USERNAME + - name: PYTHONDONTWRITEBYTECODE + value: "1" + - name: PYTHONUNBUFFERED + value: "1" + - name: REDIS_HOST + value: redis + command: ["/bin/bash", "/opt/o2api_start.sh"] + volumeMounts: + - name: scripts + mountPath: /opt + - name: redis + image: redis:alpine + ports: + - containerPort: 6379 + - name: watcher + image: "{{ .Values.o2ims.image.repository }}:{{ .Values.o2ims.image.tag }}" + command: ["/bin/bash", "/opt/o2watcher_start.sh"] + env: + - name: DB_HOST + value: postgres + - name: DB_PASSWORD + value: o2ims123 + - name: LOGGING_CONFIG_LEVEL + value: DEBUG + - name: OS_AUTH_URL + - name: OS_PASSWORD + - name: OS_USERNAME + - name: PYTHONDONTWRITEBYTECODE + value: "1" + - name: REDIS_HOST + value: redis + volumeMounts: + - name: scripts + mountPath: /opt + - name: o2pubsub + image: "{{ .Values.o2ims.image.repository }}:{{ .Values.o2ims.image.tag }}" + command: ["/bin/bash", "/opt/o2pubsub_start.sh"] + env: + - name: DB_HOST + value: postgres + - name: DB_PASSWORD + value: o2ims123 + - name: LOGGING_CONFIG_LEVEL + value: DEBUG + - name: OS_AUTH_URL + - name: OS_PASSWORD + - name: OS_USERNAME + - name: PYTHONDONTWRITEBYTECODE + value: "1" + - name: REDIS_HOST + value: redis + volumeMounts: + - name: scripts + mountPath: /opt + volumes: + - name: scripts + configMap: + name: {{ .Chart.Name }}-scripts-configmap +--- diff --git a/charts/templates/service.yaml b/charts/templates/service.yaml new file mode 100644 index 0000000..7352918 --- /dev/null +++ b/charts/templates/service.yaml @@ -0,0 +1,30 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. +# +--- +apiVersion: v1 +kind: Service +metadata: + name: o2api + namespace: {{ .Values.global.namespace }} +spec: + #clusterIP: None + ports: + - name: o2api + port: 5005 + targetPort: 5005 + protocol: TCP + selector: + app: o2api +--- diff --git a/charts/values.yaml b/charts/values.yaml new file mode 100644 index 0000000..2772908 --- /dev/null +++ b/charts/values.yaml @@ -0,0 +1,37 @@ +# Copyright (C) 2021 Wind River Systems, Inc. +# +# 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. + + +# Default values for O2 services. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +nameOverride: "" +fullnameOverride: "" + +resources: + cpu: 1 + memory: 2Gi + +global: + namespace: orano2 + +o2ims: + imagePullSecrets: admin-orano2-registry-secret + image: + repository: registry.local:9001/admin/o2imsdms + tag: 0.1.1 + pullPolicy: IfNotPresent diff --git a/docker-compose.yml b/docker-compose.yml index 02190b2..64334b7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,7 @@ services: build: context: . dockerfile: Dockerfile.localtest - image: o2imsdms-image + image: o2imsdms depends_on: - postgres - redis @@ -30,7 +30,7 @@ services: - /tests/o2ims-redis-entry.sh api: - image: o2imsdms-image + image: o2imsdms depends_on: - redis_pubsub environment: @@ -64,10 +64,9 @@ services: build: context: . dockerfile: Dockerfile.localtest - image: o2imsdms-image + image: o2imsdms depends_on: - - postgres - - redis + - redis_pubsub environment: - DB_HOST=postgres - DB_PASSWORD=o2ims123 diff --git a/o2ims/bootstrap.py b/o2ims/bootstrap.py index 10bdc26..595fd91 100644 --- a/o2ims/bootstrap.py +++ b/o2ims/bootstrap.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from retry import retry import inspect from typing import Callable from o2ims.adapter import orm, redis_eventpublisher @@ -21,6 +22,16 @@ from o2ims.adapter.notifications import AbstractNotifications,\ from o2ims.service import handlers, messagebus, unit_of_work from o2ims.adapter.unit_of_work import SqlAlchemyUnitOfWork from o2ims.adapter.clients import orm_stx +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +@retry(tries=100, delay=2, backoff=1) +def wait_for_db_ready(engine): + # wait for db up + logger.info("Wait for DB ready ...") + engine.connect() + logger.info("DB is ready") def bootstrap( @@ -34,12 +45,14 @@ def bootstrap( notifications = SmoO2Notifications() if start_orm: - orm_stx.start_o2ims_stx_mappers(uow) with uow: # get default engine if uow is by default engine = uow.session.get_bind() + wait_for_db_ready(engine) orm.start_o2ims_mappers(engine) + orm_stx.start_o2ims_stx_mappers(uow) + dependencies = {"uow": uow, "notifications": notifications, "publish": publish} injected_event_handlers = { diff --git a/requirements.txt b/requirements.txt index e173bd4..fae9d70 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,5 +12,5 @@ Cython>=3.0a1 httplib2 babel PrettyTable<0.8,>=0.7.2 -# -e git+https://opendev.org/starlingx/distcloud-client.git@master#egg=distributedcloud-client&subdirectory=distributedcloud-client -# -e git+https://opendev.org/starlingx/config.git@master#egg=cgtsclient&subdirectory=sysinv/cgts-client/cgts-client# + +retry -- 2.16.6