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.
25 "github.com/onap/multicloud-k8s/src/k8splugin/internal/config"
26 "github.com/onap/multicloud-k8s/src/k8splugin/internal/db"
28 pkgerrors "github.com/pkg/errors"
29 "k8s.io/apimachinery/pkg/runtime"
30 "k8s.io/client-go/kubernetes/scheme"
33 // LoadedPlugins stores references to the stored plugins
34 var LoadedPlugins = map[string]*plugin.Plugin{}
36 const ResourcesListLimit = 10
38 // ResourceData stores all supported Kubernetes plugin types
39 type ResourceData struct {
45 // DecodeYAML reads a YAMl file to extract the Kubernetes object definition
46 func DecodeYAML(path string, into runtime.Object) (runtime.Object, error) {
47 if _, err := os.Stat(path); err != nil {
48 if os.IsNotExist(err) {
49 return nil, pkgerrors.New("File " + path + " not found")
51 return nil, pkgerrors.Wrap(err, "Stat file error")
55 rawBytes, err := ioutil.ReadFile(path)
57 return nil, pkgerrors.Wrap(err, "Read YAML file error")
60 decode := scheme.Codecs.UniversalDeserializer().Decode
61 obj, _, err := decode(rawBytes, nil, into)
63 return nil, pkgerrors.Wrap(err, "Deserialize YAML error")
69 // CheckDatabaseConnection checks if the database is up and running and
70 // plugin can talk to it
71 func CheckDatabaseConnection() error {
72 err := db.CreateDBClient(config.GetConfiguration().DatabaseType)
74 return pkgerrors.Cause(err)
77 err = db.DBconn.HealthCheck()
79 return pkgerrors.Cause(err)
81 // TODO Convert these to configuration files instead of environment variables.
83 Endpoint: config.GetConfiguration().EtcdIP,
84 CertFile: config.GetConfiguration().EtcdCert,
85 KeyFile: config.GetConfiguration().EtcdKey,
86 CAFile: config.GetConfiguration().EtcdCAFile,
88 err = db.NewEtcdClient(nil, c)
90 log.Printf("Etcd Client Initialization failed with error: %s", err.Error())
95 // LoadPlugins loads all the compiled .so plugins
96 func LoadPlugins() error {
97 pluginsDir := config.GetConfiguration().PluginDir
98 err := filepath.Walk(pluginsDir,
99 func(path string, info os.FileInfo, err error) error {
100 if strings.Contains(path, ".so") {
101 p, err := plugin.Open(path)
103 return pkgerrors.Cause(err)
105 LoadedPlugins[info.Name()[:len(info.Name())-3]] = p
116 // CheckInitialSettings is used to check initial settings required to start api
117 func CheckInitialSettings() error {
118 err := CheckDatabaseConnection()
120 return pkgerrors.Cause(err)
125 return pkgerrors.Cause(err)
131 //EnsureDirectory makes sure that the directories specified in the path exist
132 //If not, it will create them, if possible.
133 func EnsureDirectory(f string) error {
135 _, err := os.Stat(base)
136 if err != nil && !os.IsNotExist(err) {
139 return os.MkdirAll(base, 0755)