diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-05-30 14:43:06 -0700 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-06-06 17:32:41 -0700 |
commit | d780f1b30c98a27d269e3e05423e9e54e0e022f6 (patch) | |
tree | 3d01a1fab1a846206ae714f94838de2f0a659e13 /src/k8splugin/internal/plugin | |
parent | f006c55c0793a0cacac5aa45ba7f13fd5c6ef5f4 (diff) |
Plugin code refactoring
The plugin code has been refactored to
implement a common interface.
This will allow us to do plugin validation
at loadtime of the plugin instead of at runtime.
This also makes the code calling the plugins cleaner
and easier to read.
Issue-ID: MULTICLOUD-557
Change-Id: Ice2bcc9b850d7c0e1707dcc42132c63dd77472a7
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src/k8splugin/internal/plugin')
-rw-r--r-- | src/k8splugin/internal/plugin/helpers.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/k8splugin/internal/plugin/helpers.go b/src/k8splugin/internal/plugin/helpers.go new file mode 100644 index 00000000..efcd18c1 --- /dev/null +++ b/src/k8splugin/internal/plugin/helpers.go @@ -0,0 +1,92 @@ +/* + * Copyright 2019 Intel Corporation, 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. + */ + +package plugin + +import ( + "log" + "strings" + + utils "k8splugin/internal" + "k8splugin/internal/helm" + + pkgerrors "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" + "k8s.io/client-go/kubernetes" +) + +// KubernetesConnector is an interface that is expected to be implemented +// by any code that calls the plugin framework functions. +// It implements methods that are needed by the plugins to get Kubernetes +// clients and other information needed to interface with Kubernetes +type KubernetesConnector interface { + //GetMapper returns the RESTMapper that was created for this client + GetMapper() meta.RESTMapper + + //GetDynamicClient returns the dynamic client that is needed for + //unstructured REST calls to the apiserver + GetDynamicClient() dynamic.Interface + + // GetStandardClient returns the standard client that can be used to handle + // standard kubernetes kinds + GetStandardClient() kubernetes.Interface +} + +// Reference is the interface that is implemented +type Reference interface { + //Create a kubernetes resource described by the yaml in yamlFilePath + Create(yamlFilePath string, namespace string, client KubernetesConnector) (string, error) + + //Get a kubernetes resource based on the groupVersionKind and resourceName provided in resource + Get(resource helm.KubernetesResource, namespace string, client KubernetesConnector) (string, error) + + //List all resources of the specified GroupVersionKind in the given namespace + //If gvk is empty, the plugin will return all supported objects in the namespace + List(gvk schema.GroupVersionKind, namespace string, client KubernetesConnector) ([]helm.KubernetesResource, error) + + //Delete a kubernetes resource described in the provided namespace + Delete(resource helm.KubernetesResource, namespace string, client KubernetesConnector) error +} + +// GetPluginByKind returns a plugin by the kind name +// If plugin does not exist, it will return the generic plugin +// TODO: Change this once we have a plugin registration mechanism +func GetPluginByKind(kind string) (Reference, error) { + + typePlugin, ok := utils.LoadedPlugins[strings.ToLower(kind)] + if !ok { + log.Println("No plugin for kind " + kind + " found. Using generic Plugin") + typePlugin, ok = utils.LoadedPlugins["generic"] + if !ok { + return nil, pkgerrors.New("No generic plugin found") + } + } + + symbol, err := typePlugin.Lookup("ExportedVariable") + if err != nil { + return nil, pkgerrors.Wrap(err, "No ExportedVariable symbol found") + } + + //Assert if it implements the PluginReference interface + pluginImpl, ok := symbol.(Reference) + if !ok { + return nil, pkgerrors.New("ExportedVariable does not implement plugins.Reference interface type") + } + + return pluginImpl, nil +} |