aboutsummaryrefslogtreecommitdiffstats
path: root/src/k8splugin
diff options
context:
space:
mode:
Diffstat (limited to 'src/k8splugin')
-rw-r--r--src/k8splugin/internal/app/client.go19
-rw-r--r--src/k8splugin/internal/app/client_test.go2
-rw-r--r--src/k8splugin/internal/app/config_backend.go4
-rw-r--r--src/k8splugin/internal/app/instance.go8
-rw-r--r--src/k8splugin/internal/config/config.go54
-rw-r--r--src/k8splugin/internal/plugin/helpers.go58
-rw-r--r--src/k8splugin/internal/utils.go4
-rw-r--r--src/k8splugin/plugins/generic/plugin.go26
-rw-r--r--src/k8splugin/plugins/namespace/plugin.go3
-rw-r--r--src/k8splugin/plugins/namespace/plugin_test.go4
-rw-r--r--src/k8splugin/plugins/network/plugin.go22
-rw-r--r--src/k8splugin/plugins/network/plugin_test.go13
-rw-r--r--src/k8splugin/plugins/service/plugin.go12
-rw-r--r--src/k8splugin/plugins/service/plugin_test.go7
14 files changed, 179 insertions, 57 deletions
diff --git a/src/k8splugin/internal/app/client.go b/src/k8splugin/internal/app/client.go
index 8d2af297..914a8eec 100644
--- a/src/k8splugin/internal/app/client.go
+++ b/src/k8splugin/internal/app/client.go
@@ -39,6 +39,7 @@ type KubernetesClient struct {
dynamicClient dynamic.Interface
discoverClient discovery.CachedDiscoveryInterface
restMapper meta.RESTMapper
+ instanceID string
}
// getKubeConfig uses the connectivity client to get the kubeconfig based on the name
@@ -55,11 +56,17 @@ func (k *KubernetesClient) getKubeConfig(cloudregion string) (string, error) {
}
// init loads the Kubernetes configuation values stored into the local configuration file
-func (k *KubernetesClient) init(cloudregion string) error {
+func (k *KubernetesClient) init(cloudregion string, iid string) error {
if cloudregion == "" {
return pkgerrors.New("Cloudregion is empty")
}
+ if iid == "" {
+ return pkgerrors.New("Instance ID is empty")
+ }
+
+ k.instanceID = iid
+
configPath, err := k.getKubeConfig(cloudregion)
if err != nil {
return pkgerrors.Wrap(err, "Get kubeconfig file")
@@ -89,6 +96,7 @@ func (k *KubernetesClient) init(cloudregion string) error {
}
k.restMapper = restmapper.NewDeferredDiscoveryRESTMapper(k.discoverClient)
+
return nil
}
@@ -122,8 +130,6 @@ func (k *KubernetesClient) ensureNamespace(namespace string) error {
func (k *KubernetesClient) createKind(resTempl helm.KubernetesResourceTemplate,
namespace string) (helm.KubernetesResource, error) {
- log.Println("Processing Kind: " + resTempl.GVK.Kind)
-
if _, err := os.Stat(resTempl.FilePath); os.IsNotExist(err) {
return helm.KubernetesResource{}, pkgerrors.New("File " + resTempl.FilePath + "does not exists")
}
@@ -137,6 +143,7 @@ func (k *KubernetesClient) createKind(resTempl helm.KubernetesResourceTemplate,
createdResourceName, err := pluginImpl.Create(resTempl.FilePath, namespace, k)
if err != nil {
+ log.Printf("Error: %s while creating: %s", err.Error(), resTempl.GVK.Kind)
return helm.KubernetesResource{}, pkgerrors.Wrap(err, "Error in plugin "+resTempl.GVK.Kind+" plugin")
}
@@ -212,3 +219,9 @@ func (k *KubernetesClient) GetDynamicClient() dynamic.Interface {
func (k *KubernetesClient) GetStandardClient() kubernetes.Interface {
return k.clientSet
}
+
+//GetInstanceID returns the instanceID that is injected into all the
+//resources created by the plugin
+func (k *KubernetesClient) GetInstanceID() string {
+ return k.instanceID
+}
diff --git a/src/k8splugin/internal/app/client_test.go b/src/k8splugin/internal/app/client_test.go
index fd293ab0..7001d9e2 100644
--- a/src/k8splugin/internal/app/client_test.go
+++ b/src/k8splugin/internal/app/client_test.go
@@ -72,7 +72,7 @@ func TestInit(t *testing.T) {
kubeClient := KubernetesClient{}
// Refer to the connection via its name
- err = kubeClient.init("mock_connection")
+ err = kubeClient.init("mock_connection", "abcdefg")
if err != nil {
t.Fatalf("TestGetKubeClient returned an error (%s)", err)
}
diff --git a/src/k8splugin/internal/app/config_backend.go b/src/k8splugin/internal/app/config_backend.go
index b31cbac7..6bc145ee 100644
--- a/src/k8splugin/internal/app/config_backend.go
+++ b/src/k8splugin/internal/app/config_backend.go
@@ -354,7 +354,7 @@ func scheduleResources(c chan configResourceList) {
log.Printf("[scheduleResources]: POST %v %v", data.profile, data.resourceTemplates)
for _, inst := range resp {
k8sClient := KubernetesClient{}
- err = k8sClient.init(inst.Request.CloudRegion)
+ err = k8sClient.init(inst.Request.CloudRegion, inst.ID)
if err != nil {
log.Printf("Getting CloudRegion Information: %s", err.Error())
//Move onto the next cloud region
@@ -374,7 +374,7 @@ func scheduleResources(c chan configResourceList) {
log.Printf("[scheduleResources]: DELETE %v %v", data.profile, data.resourceTemplates)
for _, inst := range resp {
k8sClient := KubernetesClient{}
- err = k8sClient.init(inst.Request.CloudRegion)
+ err = k8sClient.init(inst.Request.CloudRegion, inst.ID)
if err != nil {
log.Printf("Getting CloudRegion Information: %s", err.Error())
//Move onto the next cloud region
diff --git a/src/k8splugin/internal/app/instance.go b/src/k8splugin/internal/app/instance.go
index d28fe799..5cfdaea1 100644
--- a/src/k8splugin/internal/app/instance.go
+++ b/src/k8splugin/internal/app/instance.go
@@ -127,8 +127,10 @@ func (v *InstanceClient) Create(i InstanceRequest) (InstanceResponse, error) {
return InstanceResponse{}, pkgerrors.Wrap(err, "Error resolving helm charts")
}
+ id := generateInstanceID()
+
k8sClient := KubernetesClient{}
- err = k8sClient.init(i.CloudRegion)
+ err = k8sClient.init(i.CloudRegion, id)
if err != nil {
return InstanceResponse{}, pkgerrors.Wrap(err, "Getting CloudRegion Information")
}
@@ -138,8 +140,6 @@ func (v *InstanceClient) Create(i InstanceRequest) (InstanceResponse, error) {
return InstanceResponse{}, pkgerrors.Wrap(err, "Create Kubernetes Resources")
}
- id := generateInstanceID()
-
//Compose the return response
resp := InstanceResponse{
ID: id,
@@ -292,7 +292,7 @@ func (v *InstanceClient) Delete(id string) error {
}
k8sClient := KubernetesClient{}
- err = k8sClient.init(inst.Request.CloudRegion)
+ err = k8sClient.init(inst.Request.CloudRegion, inst.ID)
if err != nil {
return pkgerrors.Wrap(err, "Getting CloudRegion Information")
}
diff --git a/src/k8splugin/internal/config/config.go b/src/k8splugin/internal/config/config.go
index ac653282..23ec401e 100644
--- a/src/k8splugin/internal/config/config.go
+++ b/src/k8splugin/internal/config/config.go
@@ -26,19 +26,20 @@ import (
// Configuration loads up all the values that are used to configure
// backend implementations
type Configuration struct {
- CAFile string `json:"ca-file"`
- ServerCert string `json:"server-cert"`
- ServerKey string `json:"server-key"`
- Password string `json:"password"`
- DatabaseAddress string `json:"database-address"`
- DatabaseType string `json:"database-type"`
- PluginDir string `json:"plugin-dir"`
- EtcdIP string `json:"etcd-ip"`
- EtcdCert string `json:"etcd-cert"`
- EtcdKey string `json:"etcd-key"`
- EtcdCAFile string `json:"etcd-ca-file"`
- OVNCentralAddress string `json:"ovn-central-address"`
- ServicePort string `json:"service-port"`
+ CAFile string `json:"ca-file"`
+ ServerCert string `json:"server-cert"`
+ ServerKey string `json:"server-key"`
+ Password string `json:"password"`
+ DatabaseAddress string `json:"database-address"`
+ DatabaseType string `json:"database-type"`
+ PluginDir string `json:"plugin-dir"`
+ EtcdIP string `json:"etcd-ip"`
+ EtcdCert string `json:"etcd-cert"`
+ EtcdKey string `json:"etcd-key"`
+ EtcdCAFile string `json:"etcd-ca-file"`
+ OVNCentralAddress string `json:"ovn-central-address"`
+ ServicePort string `json:"service-port"`
+ KubernetesLabelName string `json:"kubernetes-label-name"`
}
// Config is the structure that stores the configuration
@@ -74,19 +75,20 @@ func defaultConfiguration() *Configuration {
}
return &Configuration{
- CAFile: "ca.cert",
- ServerCert: "server.cert",
- ServerKey: "server.key",
- Password: "",
- DatabaseAddress: "127.0.0.1",
- DatabaseType: "mongo",
- PluginDir: cwd,
- EtcdIP: "127.0.0.1",
- EtcdCert: "etcd.cert",
- EtcdKey: "etcd.key",
- EtcdCAFile: "etcd-ca.cert",
- OVNCentralAddress: "127.0.0.1:6641",
- ServicePort: "9015",
+ CAFile: "ca.cert",
+ ServerCert: "server.cert",
+ ServerKey: "server.key",
+ Password: "",
+ DatabaseAddress: "127.0.0.1",
+ DatabaseType: "mongo",
+ PluginDir: cwd,
+ EtcdIP: "127.0.0.1",
+ EtcdCert: "etcd.cert",
+ EtcdKey: "etcd.key",
+ EtcdCAFile: "etcd-ca.cert",
+ OVNCentralAddress: "127.0.0.1:6641",
+ ServicePort: "9015",
+ KubernetesLabelName: "k8splugin.io/rb-instance-id",
}
}
diff --git a/src/k8splugin/internal/plugin/helpers.go b/src/k8splugin/internal/plugin/helpers.go
index 26e0f467..b5c9109c 100644
--- a/src/k8splugin/internal/plugin/helpers.go
+++ b/src/k8splugin/internal/plugin/helpers.go
@@ -17,14 +17,18 @@
package plugin
import (
+ "encoding/json"
"log"
"strings"
utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
+ "github.com/onap/multicloud-k8s/src/k8splugin/internal/config"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
pkgerrors "github.com/pkg/errors"
+ corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
@@ -45,6 +49,9 @@ type KubernetesConnector interface {
// GetStandardClient returns the standard client that can be used to handle
// standard kubernetes kinds
GetStandardClient() kubernetes.Interface
+
+ //GetInstanceID returns the InstanceID for tracking during creation
+ GetInstanceID() string
}
// Reference is the interface that is implemented
@@ -90,3 +97,54 @@ func GetPluginByKind(kind string) (Reference, error) {
return pluginImpl, nil
}
+
+// TagPodsIfPresent finds the PodTemplateSpec from any workload
+// object that contains it and changes the spec to include the tag label
+func TagPodsIfPresent(unstruct *unstructured.Unstructured, tag string) {
+
+ spec, ok := unstruct.Object["spec"].(map[string]interface{})
+ if !ok {
+ log.Println("Error converting spec to map")
+ return
+ }
+ template, ok := spec["template"].(map[string]interface{})
+ if !ok {
+ log.Println("Error converting template to map")
+ return
+ }
+
+ data, err := json.Marshal(template)
+ if err != nil {
+ log.Println("Error Marshaling Podspec")
+ return
+ }
+
+ //Attempt to convert the template to a podtemplatespec.
+ //This is to check if we have any pods being created.
+ podTemplateSpec := &corev1.PodTemplateSpec{}
+ _, err = podTemplateSpec.MarshalTo(data)
+ if err != nil {
+ log.Println("Did not find a podTemplateSpec" + err.Error())
+ return
+ }
+
+ //At this point, we know that the data contains a PodTemplateSpec
+ metadata, ok := template["metadata"].(map[string]interface{})
+ if !ok {
+ log.Println("Error converting metadata to map")
+ return
+ }
+
+ //Get the labels map
+ labels, ok := metadata["labels"].(map[string]string)
+ if !ok {
+ log.Println("Error converting labels to map")
+ return
+ }
+
+ //Check if labels exist for this object
+ if labels == nil {
+ labels = map[string]string{}
+ }
+ labels[config.GetConfiguration().KubernetesLabelName] = tag
+}
diff --git a/src/k8splugin/internal/utils.go b/src/k8splugin/internal/utils.go
index 47a236c2..174f8e79 100644
--- a/src/k8splugin/internal/utils.go
+++ b/src/k8splugin/internal/utils.go
@@ -17,8 +17,8 @@ import (
"io/ioutil"
"log"
"os"
- "path/filepath"
"path"
+ "path/filepath"
"plugin"
"strings"
@@ -52,13 +52,11 @@ func DecodeYAML(path string, into runtime.Object) (runtime.Object, error) {
}
}
- log.Println("Reading YAML file")
rawBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, pkgerrors.Wrap(err, "Read YAML file error")
}
- log.Println("Decoding deployment YAML")
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode(rawBytes, nil, into)
if err != nil {
diff --git a/src/k8splugin/plugins/generic/plugin.go b/src/k8splugin/plugins/generic/plugin.go
index b9a96ab5..0711466f 100644
--- a/src/k8splugin/plugins/generic/plugin.go
+++ b/src/k8splugin/plugins/generic/plugin.go
@@ -14,8 +14,6 @@ limitations under the License.
package main
import (
- "log"
-
pkgerrors "github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -23,10 +21,14 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
- "github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin"
+ "github.com/onap/multicloud-k8s/src/k8splugin/internal/config"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
+ "github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin"
)
+// Compile time check to see if genericPlugin implements the correct interface
+var _ plugin.Reference = genericPlugin{}
+
// ExportedVariable is what we will look for when calling the generic plugin
var ExportedVariable genericPlugin
@@ -56,6 +58,20 @@ func (g genericPlugin) Create(yamlFilePath string, namespace string, client plug
return "", pkgerrors.Wrap(err, "Mapping kind to resource error")
}
+ //Add the tracking label to all resources created here
+ labels := unstruct.GetLabels()
+ //Check if labels exist for this object
+ if labels == nil {
+ labels = map[string]string{}
+ }
+ labels[config.GetConfiguration().KubernetesLabelName] = client.GetInstanceID()
+ unstruct.SetLabels(labels)
+
+ // This checks if the resource we are creating has a podSpec in it
+ // Eg: Deployment, StatefulSet, Job etc..
+ // If a PodSpec is found, the label will be added to it too.
+ plugin.TagPodsIfPresent(unstruct, client.GetInstanceID())
+
gvr := mapping.Resource
var createdObj *unstructured.Unstructured
@@ -94,8 +110,6 @@ func (g genericPlugin) Get(resource helm.KubernetesResource,
}
gvr := mapping.Resource
- log.Printf("Using gvr: %s, %s, %s", gvr.Group, gvr.Version, gvr.Resource)
-
opts := metav1.GetOptions{}
var unstruct *unstructured.Unstructured
switch mapping.Scope.Name() {
@@ -141,8 +155,6 @@ func (g genericPlugin) Delete(resource helm.KubernetesResource, namespace string
}
gvr := mapping.Resource
- log.Printf("Using gvr: %s, %s, %s", gvr.Group, gvr.Version, gvr.Resource)
-
deletePolicy := metav1.DeletePropagationForeground
opts := &metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
diff --git a/src/k8splugin/plugins/namespace/plugin.go b/src/k8splugin/plugins/namespace/plugin.go
index d30f55b8..feb2aa61 100644
--- a/src/k8splugin/plugins/namespace/plugin.go
+++ b/src/k8splugin/plugins/namespace/plugin.go
@@ -26,6 +26,9 @@ import (
"github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin"
)
+// Compile time check to see if namespacePlugin implements the correct interface
+var _ plugin.Reference = namespacePlugin{}
+
// ExportedVariable is what we will look for when calling the plugin
var ExportedVariable namespacePlugin
diff --git a/src/k8splugin/plugins/namespace/plugin_test.go b/src/k8splugin/plugins/namespace/plugin_test.go
index 489ac096..c1944a40 100644
--- a/src/k8splugin/plugins/namespace/plugin_test.go
+++ b/src/k8splugin/plugins/namespace/plugin_test.go
@@ -46,6 +46,10 @@ func (t TestKubernetesConnector) GetStandardClient() kubernetes.Interface {
return fake.NewSimpleClientset(t.object)
}
+func (t TestKubernetesConnector) GetInstanceID() string {
+ return ""
+}
+
func TestCreateNamespace(t *testing.T) {
testCases := []struct {
label string
diff --git a/src/k8splugin/plugins/network/plugin.go b/src/k8splugin/plugins/network/plugin.go
index ca5aa959..aa0d584b 100644
--- a/src/k8splugin/plugins/network/plugin.go
+++ b/src/k8splugin/plugins/network/plugin.go
@@ -14,9 +14,10 @@ limitations under the License.
package main
import (
- v1 "github.com/onap/multicloud-k8s/src/k8splugin/plugins/network/v1"
"regexp"
+ v1 "github.com/onap/multicloud-k8s/src/k8splugin/plugins/network/v1"
+
utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin"
@@ -25,20 +26,25 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)
+// Compile time check to see if networkPlugin implements the correct interface
+var _ plugin.Reference = networkPlugin{}
+
// ExportedVariable is what we will look for when calling the plugin
var ExportedVariable networkPlugin
type networkPlugin struct {
}
-func extractData(data string) (cniType, networkName string) {
+func extractData(data string) (cniType, networkName string, err error) {
re := regexp.MustCompile("_")
split := re.Split(data, -1)
- if len(split) != 3 {
+ if len(split) != 2 {
+ err = pkgerrors.New("Couldn't split resource '" + data +
+ "' into CNI type and Network name")
return
}
- cniType = split[1]
- networkName = split[2]
+ cniType = split[0]
+ networkName = split[1]
return
}
@@ -82,7 +88,11 @@ func (p networkPlugin) List(gvk schema.GroupVersionKind, namespace string,
// Delete an existing Network
func (p networkPlugin) Delete(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) error {
- cniType, networkName := extractData(resource.Name)
+ cniType, networkName, err := extractData(resource.Name)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Error extracting CNI type from resource")
+ }
+
typePlugin, ok := utils.LoadedPlugins[cniType+"-network"]
if !ok {
return pkgerrors.New("No plugin for resource " + cniType + " found")
diff --git a/src/k8splugin/plugins/network/plugin_test.go b/src/k8splugin/plugins/network/plugin_test.go
index 586bccb8..33cae1c7 100644
--- a/src/k8splugin/plugins/network/plugin_test.go
+++ b/src/k8splugin/plugins/network/plugin_test.go
@@ -130,18 +130,23 @@ func TestDeleteNetwork(t *testing.T) {
}{
{
label: "Fail to load non-existing plugin",
- input: "test",
- expectedError: "No plugin for resource",
+ input: "non-existing-cni_test",
+ expectedError: "No plugin for resource non-existing-cni",
},
{
- label: "Fail to delete a network",
+ label: "Fail to extract cni from network name",
input: "1_ovn4nfvk8s_test",
+ expectedError: "Error extracting CNI type from resource: Couldn't split resource '1_ovn4nfvk8s_test' into CNI type and Network name",
+ },
+ {
+ label: "Fail to delete a network",
+ input: "ovn4nfvk8s_test",
mockError: "Internal error",
expectedError: "Error during the deletion for ovn4nfvk8s plugin: Internal error",
},
{
label: "Successfully delete a ovn4nfv network",
- input: "1_ovn4nfvk8s_test",
+ input: "ovn4nfvk8s_test",
},
}
diff --git a/src/k8splugin/plugins/service/plugin.go b/src/k8splugin/plugins/service/plugin.go
index 2fceffc0..4c1f37be 100644
--- a/src/k8splugin/plugins/service/plugin.go
+++ b/src/k8splugin/plugins/service/plugin.go
@@ -22,10 +22,14 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
+ "github.com/onap/multicloud-k8s/src/k8splugin/internal/config"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin"
)
+// Compile time check to see if servicePlugin implements the correct interface
+var _ plugin.Reference = servicePlugin{}
+
// ExportedVariable is what we will look for when calling the plugin
var ExportedVariable servicePlugin
@@ -49,6 +53,14 @@ func (p servicePlugin) Create(yamlFilePath string, namespace string, client plug
}
service.Namespace = namespace
+ labels := service.GetLabels()
+ //Check if labels exist for this object
+ if labels == nil {
+ labels = map[string]string{}
+ }
+ labels[config.GetConfiguration().KubernetesLabelName] = client.GetInstanceID()
+ service.SetLabels(labels)
+
result, err := client.GetStandardClient().CoreV1().Services(namespace).Create(service)
if err != nil {
return "", pkgerrors.Wrap(err, "Create Service error")
diff --git a/src/k8splugin/plugins/service/plugin_test.go b/src/k8splugin/plugins/service/plugin_test.go
index aa0bcc29..1cef5027 100644
--- a/src/k8splugin/plugins/service/plugin_test.go
+++ b/src/k8splugin/plugins/service/plugin_test.go
@@ -14,11 +14,12 @@ limitations under the License.
package main
import (
- "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
"reflect"
"strings"
"testing"
+ "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
+
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -45,6 +46,10 @@ func (t TestKubernetesConnector) GetStandardClient() kubernetes.Interface {
return fake.NewSimpleClientset(t.object)
}
+func (t TestKubernetesConnector) GetInstanceID() string {
+ return ""
+}
+
func TestCreateService(t *testing.T) {
name := "mock-service"
testCases := []struct {