Add support for parsing profile yaml files
[multicloud/k8s.git] / src / k8splugin / internal / utils.go
1 /*
2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package utils
15
16 import (
17         "io/ioutil"
18         "k8splugin/internal/db"
19         "log"
20         "os"
21         "path/filepath"
22         "plugin"
23         "strings"
24
25         pkgerrors "github.com/pkg/errors"
26         "k8s.io/apimachinery/pkg/runtime"
27         "k8s.io/client-go/kubernetes/scheme"
28 )
29
30 // LoadedPlugins stores references to the stored plugins
31 var LoadedPlugins = map[string]*plugin.Plugin{}
32
33 const ResourcesListLimit = 10
34
35 // ResourceData stores all supported Kubernetes plugin types
36 type ResourceData struct {
37         YamlFilePath string
38         Namespace    string
39         VnfId        string
40 }
41
42 // DecodeYAML reads a YAMl file to extract the Kubernetes object definition
43 var DecodeYAML = func(path string, into runtime.Object) (runtime.Object, error) {
44         if _, err := os.Stat(path); err != nil {
45                 if os.IsNotExist(err) {
46                         return nil, pkgerrors.New("File " + path + " not found")
47                 } else {
48                         return nil, pkgerrors.Wrap(err, "Stat file error")
49                 }
50         }
51
52         log.Println("Reading YAML file")
53         rawBytes, err := ioutil.ReadFile(path)
54         if err != nil {
55                 return nil, pkgerrors.Wrap(err, "Read YAML file error")
56         }
57
58         log.Println("Decoding deployment YAML")
59         decode := scheme.Codecs.UniversalDeserializer().Decode
60         obj, _, err := decode(rawBytes, nil, into)
61         if err != nil {
62                 return nil, pkgerrors.Wrap(err, "Deserialize YAML error")
63         }
64
65         return obj, nil
66 }
67
68 // CheckEnvVariables checks for required Environment variables
69 func CheckEnvVariables() error {
70         envList := []string{"CSAR_DIR", "KUBE_CONFIG_DIR", "PLUGINS_DIR",
71                 "DATABASE_TYPE", "DATABASE_IP", "OVN_CENTRAL_ADDRESS"}
72         for _, env := range envList {
73                 if _, ok := os.LookupEnv(env); !ok {
74                         return pkgerrors.New("environment variable " + env + " not set")
75                 }
76         }
77
78         return nil
79 }
80
81 // CheckDatabaseConnection checks if the database is up and running and
82 // plugin can talk to it
83 func CheckDatabaseConnection() error {
84         err := db.CreateDBClient(os.Getenv("DATABASE_TYPE"))
85         if err != nil {
86                 return pkgerrors.Cause(err)
87         }
88
89         err = db.DBconn.HealthCheck()
90         if err != nil {
91                 return pkgerrors.Cause(err)
92         }
93         return nil
94 }
95
96 // LoadPlugins loads all the compiled .so plugins
97 func LoadPlugins() error {
98         pluginsDir := os.Getenv("PLUGINS_DIR")
99         err := filepath.Walk(pluginsDir,
100                 func(path string, info os.FileInfo, err error) error {
101                         if strings.Contains(path, ".so") {
102                                 p, err := plugin.Open(path)
103                                 if err != nil {
104                                         return pkgerrors.Cause(err)
105                                 }
106                                 LoadedPlugins[info.Name()[:len(info.Name())-3]] = p
107                         }
108                         return err
109                 })
110         if err != nil {
111                 return err
112         }
113
114         return nil
115 }
116
117 // CheckInitialSettings is used to check initial settings required to start api
118 func CheckInitialSettings() error {
119         err := CheckEnvVariables()
120         if err != nil {
121                 return pkgerrors.Cause(err)
122         }
123
124         err = CheckDatabaseConnection()
125         if err != nil {
126                 return pkgerrors.Cause(err)
127         }
128
129         err = LoadPlugins()
130         if err != nil {
131                 return pkgerrors.Cause(err)
132         }
133
134         return nil
135 }