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.
18 "k8splugin/internal/db"
25 pkgerrors "github.com/pkg/errors"
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/client-go/kubernetes/scheme"
30 // LoadedPlugins stores references to the stored plugins
31 var LoadedPlugins = map[string]*plugin.Plugin{}
33 const ResourcesListLimit = 10
35 // ResourceData stores all supported Kubernetes plugin types
36 type ResourceData struct {
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")
48 return nil, pkgerrors.Wrap(err, "Stat file error")
52 log.Println("Reading YAML file")
53 rawBytes, err := ioutil.ReadFile(path)
55 return nil, pkgerrors.Wrap(err, "Read YAML file error")
58 log.Println("Decoding deployment YAML")
59 decode := scheme.Codecs.UniversalDeserializer().Decode
60 obj, _, err := decode(rawBytes, nil, into)
62 return nil, pkgerrors.Wrap(err, "Deserialize YAML error")
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")
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"))
86 return pkgerrors.Cause(err)
89 err = db.DBconn.HealthCheck()
91 return pkgerrors.Cause(err)
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)
104 return pkgerrors.Cause(err)
106 LoadedPlugins[info.Name()[:len(info.Name())-3]] = p
117 // CheckInitialSettings is used to check initial settings required to start api
118 func CheckInitialSettings() error {
119 err := CheckEnvVariables()
121 return pkgerrors.Cause(err)
124 err = CheckDatabaseConnection()
126 return pkgerrors.Cause(err)
131 return pkgerrors.Cause(err)