8e959cefca91942ac666679544639b31e7ce41e1
[oom/utils.git] / external-schema-repo-generator / README.md
1 StndDefined schemas ConfigMap generator
2 =======================================
3
4 ## Description
5 StndDefined schemas Kubernetes ConfigMap generator is a Makefile with shell scripts: 'generate', 'install', 'clean' ane 'utils'. 
6 This Makefile may be used by VES Collector users to generate and install ConfigMap containing schemas
7 and mapping file for stndDefined validation in VES pod. Additionally script creates file with snippets containing 
8 auto-generated configuration of volumes finally mounted in VES deployment. Process of generation of ConfigMap spec file
9 is configurable via environment.config file.
10
11 Generator files are available in oom/utils ONAP Gerrit repository under external-schema-repo-generator folder.
12
13 ## Requirements and limitations
14
15 ### Environment
16 Target *generate* from Makefile requires stable internet connection to properly clone Git repositories.
17 Target *install* should be ran on the RKE node, so it can create ConfigMap in the same Kubernetes environment as VES Pod
18 is installed. It is possible to generate a spec in one environment and move it together with all ConfigMap generator
19 files to the RKE environment.
20
21 ### Repository limitations
22 When running the script branches from selected repository are being downloaded. Time of script execution depends mostly
23 on repository size and number of schemas. All .yaml files from selected directory in Git repository will be considered
24 as schemas and attached to ConfigMap spec.
25
26 ### Generator tool files integration
27 It is recommended to consider files of this tool as a unity and not split them when moving generator through
28 environments. Generator tool files that are required are located in `generator` directory:
29 - Makefile
30 - install.sh
31 - generate.sh
32 - utils.sh
33 - clean.sh
34 - environment.config
35
36 ## Instruction
37
38 ### Parameters description
39 Before running any target from Makefile, configuration in *environment.config* must be properly prepared. Description of
40 the configurable properties is below.
41
42 - **GENERATION_DIRECTORY** - Directory in which generated files will be located.
43 - **EXTERNAL_REPO_CONFIGMAP_FILENAME_PREFIX** - Prefix for file name of ConfigMap spec, that will be generated.
44 - **EXTERNAL_REPO_CONFIGMAP_DIRECTORY** - Directory inside *GENERATION_DIRECTORY*, in which generated specs will be located.
45 - **EXTERNAL_REPO_CONFIGMAP_NAME_PREFIX** - Prefix for Kubernetes name of ConfigMap, that will be generated and installed.
46 - **SNIPPET_FILENAME** - Filename of snippet with autogenerated content that should be added to VES deployment.
47
48 - **REPOSITORY_URL_HTTPS** - URL to remote Git repository which lets cloning repository via HTTPS.
49 - **REPOSITORY_BRANCH** - Valid branch from selected Git repository which contains schemas desired to mount. Script
50 accepts an array of branches from which schemas will be collected. To pass an array split branch names with space and
51 cover list in quotation marks.
52 - **SCHEMAS_LOCATION** - Path to schemas directory on selected Git repo. All YAML files from this repository will be
53 considered as schema and added to ConfigMap.
54
55 - **VENDOR_NAME** - Name of organisation delivering schemas, used only for naming of schemas destination directory in
56 VES.
57
58 ### Running commands
59
60
61 #### All
62
63 To generate and install ConfigMaps run:
64
65 ```
66 make all
67 ```
68
69 #### Generation
70
71 To run ConfigMaps spec generation as well as snippet file used for mounting ConfigMap to specific Deployment use:
72
73 ```
74 make generate
75 ```
76
77 #### Installation
78
79 To run ConfigMaps installation in Kubernetes use:
80
81 **NOTE**: Remember about running this command on RKE node.
82
83 ```
84 make install
85 ```
86
87 **NOTE**: It is possible that ConfigMap with selected K8S_CONFIGMAP_NAME already exists in Kubernetes. 
88 In such situation existing ConfigMap will be replaced.
89
90 **NOTE**: If ConfigMap is bigger than one megabyte it won't be installed
91 and warning will be displayed during installation.
92
93 #### Cleaning 
94
95 To remove generated files use:
96
97 ```
98 make clean
99 ```
100
101 ## ConfigMap validation
102 After running the script ConfigMap spec file is generated in selected (be default `genereted`) directory.
103 Spec files can be manually validated via any text editor. The last generated file includes in spec is schema-map.json file with
104 mappings of external URLs to prepared local URLs.
105
106 To check whether it has been created use command:
107
108 ```
109 kubectl -n onap get configmap | grep <CONFIGMAP_NAME>
110 ```
111
112 A ConfigMaps with configured name should be visible on the list.
113
114 ## Mounting ConfigMap into VES Collector
115
116 To mount created ConfigMaps in VES, its deployment must be edited. It can be done with:
117 ```
118 kubectl -n onap edit deployment dep-dcae-ves-collector
119 ```
120
121 Snippets with content that should be added to VES deployment are generated in file with name setup in configuration file
122 under SNIPPET_FILENAME property. No extra configuration in VES is needed when using them.
123
124 **NOTE**: When using Vi text editor for deployment edition, correct input mode must be set to keep proper indentation
125 when pasting snippets. Use ``:set paste `` to turn paste mode on. To close paste mode use ``:set nopaste`` 
126
127 1. Add volumeMounts element
128
129     In spec.template.spec.containers[0].volumeMounts add new list element:
130
131     **NOTE**: spec.template.spec.containers[0] should be a container with the image:
132         *nexus3.onap.org:10001/onap/org.onap.dcaegen2.collectors.ves.vescollector::x.x.x*.
133         It should be the first container, but make sure that the correct container is being edited.
134
135     ```
136     volumeMounts:
137         - mountPath: /opt/app/VESCollector/etc/externalRepo
138           name: custom-3gpp-schemas
139         - mountPath: /opt/app/VESCollector/etc/externalRepo/3gpp/rep/sa5/MnS/tree/branch/OpenAPI
140           name: branch-custom-3gpp-schemas
141         - ...
142     ```
143
144     - mountPath - Directory context for schemas. **NOTE**: must be the same as configuration of VES Collector property
145     *collector.externalSchema.schemasLocation* in *collector.properties*. This property might be modified via Consul UI,
146     later after changes in deployment.
147
148    - name - Name of ConfigMap volume. For schema map must be the same as set in the 2. step in *name* field.
149
150 2. Add volumes element
151
152     In spec.template.spec.volumes add a new list element with all desired to mount schemas from ConfigMap in
153     *items* list. *key* are file names from generated previously spec and *path* is relative path from directory set up
154     in step 1 as *mountPath*.
155
156     Sample spec.template.spec.volumes content:
157
158     ```
159     volumes:
160       - configMap:
161           defaultMode: 420
162           name: schema-map-stnd-defined-configmap
163         name: custom-3gpp-schemas
164       - configMap:
165           defaultMode: 420
166           name: branch-stnd-defined-configmap
167         name: branch-custom-3gpp-schemas
168       - ...
169     ```
170    Fields description:
171    - name - Name of ConfigMap volume. Consist of branch name and *name* field. Must be same as name in 1. step 
172    - configMap.name - name of installed Kubernetes ConfigMap consist of schema-map or branch name 
173      and K8S_CONFIGMAP_NAME from configuration setting
174
175 3. Save and close an editor, K8S will automatically detect changes, terminate old VES Pod and deploy new one with
176 mounted ConfigMap. Correctness of new VES Pod initialization and mounting ConfigMap can be tracked using
177 `kubectl -n onap describe pod <VES_POD_NAME>`.
178
179 To check if mounted schemas and VES properties configuration are correctly aligned see logs of VES. Each schema which is
180 present in mapping file, but is not detected by VES will be logged on application startup e.g.:
181 ```
182 2020-10-01 11:46:55.872  WARN 24 [       Thread-5] o.o.d.s.s.s.e.s.m.s.UrlMapperFactory     : Local schema resource missing. Schema file with path /opt/app/VESCollector/etc/externalRepoCustom/3gpp/rep/sa5/MnS/tree/master/OpenAPI/5gcNrm.yaml has not been found.
183 2020-10-01 11:46:55.872  WARN 24 [       Thread-5] o.o.d.s.s.s.e.s.m.s.UrlMapperFactory     : Mapping for publicURL ("https://forge.3gpp.org/rep/sa5/MnS/tree/master/OpenAPI/5gcNrm.yaml") will not be added to validator.
184 ```
185
186 To see logs of VES use:
187 ```
188 kubectl -n onap logs <VES_POD_NAME>
189 ```