diff options
Diffstat (limited to 'src')
32 files changed, 870 insertions, 148 deletions
diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 00000000..38397c81 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,11 @@ +build: + $(MAKE) -C k8splugin build + +deploy: + $(MAKE) -C k8splugin deploy + +all: + $(MAKE) -C k8splugin all + +clean: + $(MAKE) -C k8splugin clean
\ No newline at end of file diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index 636b80b4..4308db4f 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -26,6 +26,7 @@ func NewRouter(defClient rb.DefinitionManager, profileClient rb.ProfileManager, instClient app.InstanceManager, configClient app.ConfigManager, + connectionClient connection.ConnectionManager, templateClient rb.ConfigTemplateManager) *mux.Router { router := mux.NewRouter() @@ -37,6 +38,7 @@ func NewRouter(defClient rb.DefinitionManager, instHandler := instanceHandler{client: instClient} instRouter := router.PathPrefix("/v1").Subrouter() instRouter.HandleFunc("/instance", instHandler.createHandler).Methods("POST") + instRouter.HandleFunc("/instance", instHandler.listHandler).Methods("GET") instRouter.HandleFunc("/instance/{instID}", instHandler.getHandler).Methods("GET") instRouter.HandleFunc("/instance/{instID}", instHandler.deleteHandler).Methods("DELETE") // (TODO): Fix update method @@ -51,11 +53,13 @@ func NewRouter(defClient rb.DefinitionManager, router.HandleFunc("/{cloud-owner}/{cloud-region}/infra_workload/{instID}", brokerHandler.deleteHandler).Methods("DELETE") //Setup the connectivity api handler here - connectionClient := connection.NewConnectionClient() - connectionHandler := connection.ConnectionHandler{Client: connectionClient} - instRouter.HandleFunc("/connectivity-info", connectionHandler.CreateHandler).Methods("POST") - instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.GetHandler).Methods("GET") - instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.DeleteHandler).Methods("DELETE") + if connectionClient == nil { + connectionClient = connection.NewConnectionClient() + } + connectionHandler := connectionHandler{client: connectionClient} + instRouter.HandleFunc("/connectivity-info", connectionHandler.createHandler).Methods("POST") + instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.getHandler).Methods("GET") + instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.deleteHandler).Methods("DELETE") //Setup resource bundle definition routes if defClient == nil { @@ -66,6 +70,7 @@ func NewRouter(defClient rb.DefinitionManager, resRouter.HandleFunc("/definition", defHandler.createHandler).Methods("POST") resRouter.HandleFunc("/definition/{rbname}/{rbversion}/content", defHandler.uploadHandler).Methods("POST") resRouter.HandleFunc("/definition/{rbname}", defHandler.listVersionsHandler).Methods("GET") + resRouter.HandleFunc("/definition", defHandler.listAllHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.getHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.deleteHandler).Methods("DELETE") @@ -75,6 +80,7 @@ func NewRouter(defClient rb.DefinitionManager, } profileHandler := rbProfileHandler{client: profileClient} resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile", profileHandler.createHandler).Methods("POST") + resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile", profileHandler.listHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}/content", profileHandler.uploadHandler).Methods("POST") resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.getHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.deleteHandler).Methods("DELETE") diff --git a/src/k8splugin/api/brokerhandler_test.go b/src/k8splugin/api/brokerhandler_test.go index f93ce5ff..319c64e7 100644 --- a/src/k8splugin/api/brokerhandler_test.go +++ b/src/k8splugin/api/brokerhandler_test.go @@ -154,7 +154,7 @@ func TestBrokerCreateHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/cloudowner/cloudregion/infra_workload", testCase.input) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { body, _ := ioutil.ReadAll(resp.Body) @@ -238,7 +238,7 @@ func TestBrokerGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/cloudowner/cloudregion/infra_workload/"+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", @@ -334,7 +334,7 @@ func TestBrokerFindHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/cloudowner/cloudregion/infra_workload?name="+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", @@ -396,7 +396,7 @@ func TestBrokerDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("DELETE", "/cloudowner/cloudregion/infra_workload/"+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode) diff --git a/src/k8splugin/internal/connection/connectionhandler.go b/src/k8splugin/api/connectionhandler.go index 8c860d31..21250b14 100644 --- a/src/k8splugin/internal/connection/connectionhandler.go +++ b/src/k8splugin/api/connectionhandler.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package connection +package api import ( "bytes" @@ -24,15 +24,17 @@ import ( "io/ioutil" "net/http" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/connection" + "github.com/gorilla/mux" ) -// ConnectionHandler is used to store backend implementations objects +// connectionHandler is used to store backend implementations objects // Also simplifies mocking for unit testing purposes -type ConnectionHandler struct { +type connectionHandler struct { // Interface that implements Connectivity operations // We will set this variable with a mock interface for testing - Client ConnectionManager + client connection.ConnectionManager } // CreateHandler handles creation of the connectivity entry in the database @@ -40,8 +42,8 @@ type ConnectionHandler struct { // curl -i -F "metadata={\"cloud-region\":\"kud\",\"cloud-owner\":\"me\"};type=application/json" \ // -F file=@/home/user/.kube/config \ // -X POST http://localhost:8081/v1/connectivity-info -func (h ConnectionHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { - var v Connection +func (h connectionHandler) createHandler(w http.ResponseWriter, r *http.Request) { + var v connection.Connection // Implemenation using multipart form // Review and enable/remove at a later date @@ -93,7 +95,7 @@ func (h ConnectionHandler) CreateHandler(w http.ResponseWriter, r *http.Request) v.Kubeconfig = base64.StdEncoding.EncodeToString(content) - ret, err := h.Client.Create(v) + ret, err := h.client.Create(v) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -110,11 +112,11 @@ func (h ConnectionHandler) CreateHandler(w http.ResponseWriter, r *http.Request) // getHandler handles GET operations on a particular name // Returns a Connectivity instance -func (h ConnectionHandler) GetHandler(w http.ResponseWriter, r *http.Request) { +func (h connectionHandler) getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) name := vars["connname"] - ret, err := h.Client.Get(name) + ret, err := h.client.Get(name) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -130,11 +132,11 @@ func (h ConnectionHandler) GetHandler(w http.ResponseWriter, r *http.Request) { } // deleteHandler handles DELETE operations on a particular record -func (h ConnectionHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) { +func (h connectionHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) name := vars["connname"] - err := h.Client.Delete(name) + err := h.client.Delete(name) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/src/k8splugin/api/defhandler.go b/src/k8splugin/api/defhandler.go index c1110a97..480d4be5 100644 --- a/src/k8splugin/api/defhandler.go +++ b/src/k8splugin/api/defhandler.go @@ -20,9 +20,10 @@ import ( "encoding/json" "io" "io/ioutil" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" "net/http" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" + "github.com/gorilla/mux" ) @@ -122,6 +123,25 @@ func (h rbDefinitionHandler) listVersionsHandler(w http.ResponseWriter, r *http. } } +// listVersionsHandler handles GET (list) operations on the endpoint +// Returns a list of rb.Definitions +func (h rbDefinitionHandler) listAllHandler(w http.ResponseWriter, r *http.Request) { + + ret, err := h.client.List("") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // getHandler handles GET operations on a particular ids // Returns a rb.Definition func (h rbDefinitionHandler) getHandler(w http.ResponseWriter, r *http.Request) { diff --git a/src/k8splugin/api/defhandler_test.go b/src/k8splugin/api/defhandler_test.go index 912f96dd..dcfea1de 100644 --- a/src/k8splugin/api/defhandler_test.go +++ b/src/k8splugin/api/defhandler_test.go @@ -20,13 +20,14 @@ import ( "bytes" "encoding/json" "io" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" "net/http" "net/http/httptest" "reflect" "sort" "testing" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" + pkgerrors "github.com/pkg/errors" ) @@ -138,7 +139,7 @@ func TestRBDefCreateHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/rb/definition", testCase.reader) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -207,7 +208,87 @@ func TestRBDefListVersionsHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/rb/definition/testresourcebundle", nil) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []rb.Definition{} + json.NewDecoder(resp.Body).Decode(&got) + + // Since the order of returned slice is not guaranteed + // Check both and return error if both don't match + sort.Slice(got, func(i, j int) bool { + return got[i].RBVersion < got[j].RBVersion + }) + // Sort both as it is not expected that testCase.expected + // is sorted + sort.Slice(testCase.expected, func(i, j int) bool { + return testCase.expected[i].RBVersion < testCase.expected[j].RBVersion + }) + + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("listHandler returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + +func TestRBDefListAllHandler(t *testing.T) { + + testCases := []struct { + label string + expected []rb.Definition + expectedCode int + rbDefClient *mockRBDefinition + }{ + { + label: "List Bundle Definitions", + expectedCode: http.StatusOK, + expected: []rb.Definition{ + { + RBName: "resourcebundle1", + RBVersion: "v1", + ChartName: "barchart", + Description: "test description for one", + }, + { + RBName: "resourcebundle2", + RBVersion: "version2", + ChartName: "foochart", + Description: "test description for two", + }, + }, + rbDefClient: &mockRBDefinition{ + // list of definitions that will be returned by the mockclient + Items: []rb.Definition{ + { + RBName: "resourcebundle1", + RBVersion: "v1", + ChartName: "barchart", + Description: "test description for one", + }, + { + RBName: "resourcebundle2", + RBVersion: "version2", + ChartName: "foochart", + Description: "test description for two", + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + request := httptest.NewRequest("GET", "/v1/rb/definition", nil) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -287,7 +368,7 @@ func TestRBDefGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.name+"/"+testCase.version, nil) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -338,7 +419,7 @@ func TestRBDefDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("DELETE", "/v1/rb/definition/"+testCase.name+"/"+testCase.version, nil) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -395,7 +476,7 @@ func TestRBDefUploadHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/rb/definition/"+testCase.name+"/"+testCase.version+"/content", testCase.body) - resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { diff --git a/src/k8splugin/api/healthcheckhandler_test.go b/src/k8splugin/api/healthcheckhandler_test.go index d96bb3cd..ab9322fd 100644 --- a/src/k8splugin/api/healthcheckhandler_test.go +++ b/src/k8splugin/api/healthcheckhandler_test.go @@ -35,7 +35,7 @@ func TestHealthCheckHandler(t *testing.T) { Err: nil, } request := httptest.NewRequest("GET", "/v1/healthcheck", nil) - resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != http.StatusOK { @@ -48,7 +48,7 @@ func TestHealthCheckHandler(t *testing.T) { Err: pkgerrors.New("Runtime Error in DB"), } request := httptest.NewRequest("GET", "/v1/healthcheck", nil) - resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != http.StatusInternalServerError { diff --git a/src/k8splugin/api/instancehandler.go b/src/k8splugin/api/instancehandler.go index 42f3b212..3ec055bc 100644 --- a/src/k8splugin/api/instancehandler.go +++ b/src/k8splugin/api/instancehandler.go @@ -106,6 +106,24 @@ func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) { } } +// getHandler retrieves information about an instance via the ID +func (i instanceHandler) listHandler(w http.ResponseWriter, r *http.Request) { + + resp, err := i.client.List() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(resp) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // deleteHandler method terminates an instance via the ID func (i instanceHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/src/k8splugin/api/instancehandler_test.go b/src/k8splugin/api/instancehandler_test.go index 3fc0202d..83fa3d2b 100644 --- a/src/k8splugin/api/instancehandler_test.go +++ b/src/k8splugin/api/instancehandler_test.go @@ -21,6 +21,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "sort" "testing" "github.com/onap/multicloud-k8s/src/k8splugin/internal/app" @@ -38,8 +39,9 @@ type mockInstanceClient struct { app.InstanceManager // Items and err will be used to customize each test // via a localized instantiation of mockInstanceClient - items []app.InstanceResponse - err error + items []app.InstanceResponse + miniitems []app.InstanceMiniResponse + err error } func (m *mockInstanceClient) Create(inp app.InstanceRequest) (app.InstanceResponse, error) { @@ -58,6 +60,14 @@ func (m *mockInstanceClient) Get(id string) (app.InstanceResponse, error) { return m.items[0], nil } +func (m *mockInstanceClient) List() ([]app.InstanceMiniResponse, error) { + if m.err != nil { + return []app.InstanceMiniResponse{}, m.err + } + + return m.miniitems, nil +} + func (m *mockInstanceClient) Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]app.InstanceResponse, error) { if m.err != nil { return nil, m.err @@ -175,7 +185,7 @@ func TestInstanceCreateHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/instance", testCase.input) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { body, _ := ioutil.ReadAll(resp.Body) @@ -276,7 +286,7 @@ func TestInstanceGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/instance/"+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", @@ -297,6 +307,110 @@ func TestInstanceGetHandler(t *testing.T) { } } +func TestInstanceListHandler(t *testing.T) { + testCases := []struct { + label string + input string + expectedCode int + expectedResponse []app.InstanceMiniResponse + instClient *mockInstanceClient + }{ + { + label: "Fail to List Instance", + input: "HaKpys8e", + expectedCode: http.StatusInternalServerError, + instClient: &mockInstanceClient{ + err: pkgerrors.New("Internal error"), + }, + }, + { + label: "Succesful List Instances", + expectedCode: http.StatusOK, + expectedResponse: []app.InstanceMiniResponse{ + { + ID: "HaKpys8e", + Request: app.InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + }, + Namespace: "testnamespace", + }, + { + ID: "HaKpys8f", + Request: app.InstanceRequest{ + RBName: "test-rbdef-two", + RBVersion: "versionsomething", + ProfileName: "profile3", + CloudRegion: "region1", + }, + Namespace: "testnamespace-two", + }, + }, + instClient: &mockInstanceClient{ + miniitems: []app.InstanceMiniResponse{ + { + ID: "HaKpys8e", + Request: app.InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + }, + Namespace: "testnamespace", + }, + { + ID: "HaKpys8f", + Request: app.InstanceRequest{ + RBName: "test-rbdef-two", + RBVersion: "versionsomething", + ProfileName: "profile3", + CloudRegion: "region1", + }, + Namespace: "testnamespace-two", + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + request := httptest.NewRequest("GET", "/v1/instance", nil) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) + + if testCase.expectedCode != resp.StatusCode { + t.Fatalf("Request method returned: %v and it was expected: %v", + resp.StatusCode, testCase.expectedCode) + } + if resp.StatusCode == http.StatusOK { + var response []app.InstanceMiniResponse + err := json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Parsing the returned response got an error (%s)", err) + } + + // Since the order of returned slice is not guaranteed + // Sort them first and then do deepequal + // Check both and return error if both don't match + sort.Slice(response, func(i, j int) bool { + return response[i].ID < response[j].ID + }) + + sort.Slice(testCase.expectedResponse, func(i, j int) bool { + return testCase.expectedResponse[i].ID < testCase.expectedResponse[j].ID + }) + + if reflect.DeepEqual(testCase.expectedResponse, response) == false { + t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v", + &response, testCase.expectedResponse) + } + } + }) + } +} + func TestDeleteHandler(t *testing.T) { testCases := []struct { label string @@ -323,7 +437,7 @@ func TestDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("DELETE", "/v1/instance/"+testCase.input, nil) - resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil)) + resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode) diff --git a/src/k8splugin/api/profilehandler.go b/src/k8splugin/api/profilehandler.go index adb9249b..68ab77a4 100644 --- a/src/k8splugin/api/profilehandler.go +++ b/src/k8splugin/api/profilehandler.go @@ -20,9 +20,10 @@ import ( "encoding/json" "io" "io/ioutil" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" "net/http" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" + "github.com/gorilla/mux" ) @@ -119,6 +120,28 @@ func (h rbProfileHandler) getHandler(w http.ResponseWriter, r *http.Request) { } } +// getHandler handles GET operations on a particular ids +// Returns a rb.Definition +func (h rbProfileHandler) listHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + rbName := vars["rbname"] + rbVersion := vars["rbversion"] + + ret, err := h.client.List(rbName, rbVersion) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // deleteHandler handles DELETE operations on a particular bundle definition id func (h rbProfileHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go index eb65827a..4dae377c 100644 --- a/src/k8splugin/api/profilehandler_test.go +++ b/src/k8splugin/api/profilehandler_test.go @@ -20,12 +20,14 @@ import ( "bytes" "encoding/json" "io" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" "net/http" "net/http/httptest" "reflect" + "sort" "testing" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" + pkgerrors "github.com/pkg/errors" ) @@ -56,6 +58,14 @@ func (m *mockRBProfile) Get(rbname, rbversion, prname string) (rb.Profile, error return m.Items[0], nil } +func (m *mockRBProfile) List(rbname, rbversion string) ([]rb.Profile, error) { + if m.Err != nil { + return []rb.Profile{}, m.Err + } + + return m.Items, nil +} + func (m *mockRBProfile) Delete(rbname, rbversion, prname string) error { return m.Err } @@ -117,7 +127,7 @@ func TestRBProfileCreateHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/rb/definition/test-rbdef/v1/profile", testCase.reader) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -188,7 +198,7 @@ func TestRBProfileGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -209,6 +219,98 @@ func TestRBProfileGetHandler(t *testing.T) { } } +func TestRBProfileListHandler(t *testing.T) { + + testCases := []struct { + def string + version string + label string + expected []rb.Profile + expectedCode int + rbProClient *mockRBProfile + }{ + { + def: "test-rbdef", + version: "v1", + label: "List Profiles", + expectedCode: http.StatusOK, + expected: []rb.Profile{ + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + ReleaseName: "testprofilereleasename", + Namespace: "ns1", + KubernetesVersion: "1.12.3", + }, + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile2", + ReleaseName: "testprofilereleasename", + Namespace: "ns2", + KubernetesVersion: "1.12.3", + }, + }, + rbProClient: &mockRBProfile{ + // list of Profiles that will be returned by the mockclient + Items: []rb.Profile{ + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + ReleaseName: "testprofilereleasename", + Namespace: "ns1", + KubernetesVersion: "1.12.3", + }, + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile2", + ReleaseName: "testprofilereleasename", + Namespace: "ns2", + KubernetesVersion: "1.12.3", + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + request := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.def+"/"+testCase.version+"/profile", nil) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []rb.Profile{} + json.NewDecoder(resp.Body).Decode(&got) + + // Since the order of returned slice is not guaranteed + // Check both and return error if both don't match + sort.Slice(got, func(i, j int) bool { + return got[i].ProfileName < got[j].ProfileName + }) + // Sort both as it is not expected that testCase.expected + // is sorted + sort.Slice(testCase.expected, func(i, j int) bool { + return testCase.expected[i].ProfileName < testCase.expected[j].ProfileName + }) + + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("listHandler returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestRBProfileDeleteHandler(t *testing.T) { testCases := []struct { @@ -236,7 +338,7 @@ func TestRBProfileDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("DELETE", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -289,7 +391,7 @@ func TestRBProfileUploadHandler(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("POST", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname+"/content", testCase.body) - resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil)) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { diff --git a/src/k8splugin/cmd/main.go b/src/k8splugin/cmd/main.go index 8c887eb1..8f9ffc56 100644 --- a/src/k8splugin/cmd/main.go +++ b/src/k8splugin/cmd/main.go @@ -39,7 +39,7 @@ func main() { rand.Seed(time.Now().UnixNano()) - httpRouter := api.NewRouter(nil, nil, nil, nil, nil) + httpRouter := api.NewRouter(nil, nil, nil, nil, nil, nil) loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter) log.Println("Starting Kubernetes Multicloud API") diff --git a/src/k8splugin/internal/app/client.go b/src/k8splugin/internal/app/client.go index 4fdce599..914a8eec 100644 --- a/src/k8splugin/internal/app/client.go +++ b/src/k8splugin/internal/app/client.go @@ -18,7 +18,6 @@ import ( "os" "time" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/config" "github.com/onap/multicloud-k8s/src/k8splugin/internal/connection" "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm" "github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin" @@ -40,13 +39,15 @@ 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 // of the cloudregion. This is written out to a file. func (k *KubernetesClient) getKubeConfig(cloudregion string) (string, error) { + conn := connection.NewConnectionClient() - kubeConfigPath, err := conn.Download(cloudregion, config.GetConfiguration().KubeConfigDir) + kubeConfigPath, err := conn.Download(cloudregion) if err != nil { return "", pkgerrors.Wrap(err, "Downloading kubeconfig") } @@ -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 5272d60f..5cfdaea1 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -19,6 +19,7 @@ package app import ( "encoding/base64" "encoding/json" + "log" "math/rand" "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" @@ -46,10 +47,20 @@ type InstanceResponse struct { Resources []helm.KubernetesResource `json:"resources"` } +// InstanceMiniResponse contains the response from instantiation +// It does NOT include the created resources. +// Use the regular GET to get the created resources for a particular instance +type InstanceMiniResponse struct { + ID string `json:"id"` + Request InstanceRequest `json:"request"` + Namespace string `json:"namespace"` +} + // InstanceManager is an interface exposes the instantiation functionality type InstanceManager interface { Create(i InstanceRequest) (InstanceResponse, error) Get(id string) (InstanceResponse, error) + List() ([]InstanceMiniResponse, error) Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) Delete(id string) error } @@ -116,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") } @@ -127,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, @@ -171,6 +182,37 @@ func (v *InstanceClient) Get(id string) (InstanceResponse, error) { return InstanceResponse{}, pkgerrors.New("Error getting Instance") } +// List returns the instance for corresponding ID +// Empty string returns all +func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { + + dbres, err := db.DBconn.ReadAll(v.storeName, v.tagInst) + if err != nil || len(dbres) == 0 { + return []InstanceMiniResponse{}, pkgerrors.Wrap(err, "Listing Instances") + } + + var results []InstanceMiniResponse + for key, value := range dbres { + //value is a byte array + if value != nil { + resp := InstanceResponse{} + err = db.DBconn.Unmarshal(value, &resp) + if err != nil { + log.Printf("[Instance] Error: %s Unmarshaling Instance: %s", err.Error(), key) + } + + miniresp := InstanceMiniResponse{ + ID: resp.ID, + Request: resp.Request, + Namespace: resp.Namespace, + } + results = append(results, miniresp) + } + } + + return results, nil +} + // Find returns the instances that match the given criteria // If version is empty, it will return all instances for a given rbName // If profile is empty, it will return all instances for a given rbName+version @@ -250,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/app/instance_test.go b/src/k8splugin/internal/app/instance_test.go index 24558a44..3cb62ee1 100644 --- a/src/k8splugin/internal/app/instance_test.go +++ b/src/k8splugin/internal/app/instance_test.go @@ -51,7 +51,7 @@ func TestInstanceCreate(t *testing.T) { Items: map[string]map[string][]byte{ rb.ProfileKey{RBName: "test-rbdef", RBVersion: "v1", ProfileName: "profile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"profile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -59,7 +59,7 @@ func TestInstanceCreate(t *testing.T) { "\"rb-version\":\"v1\"," + "\"kubernetesversion\":\"1.12.3\"}"), // base64 encoding of vagrant/tests/vnfs/testrb/helm/profile - "content": []byte("H4sICLmjT1wAA3Byb2ZpbGUudGFyAO1Y32/bNhD2s/6Kg/KyYZZsy" + + "profilecontent": []byte("H4sICLmjT1wAA3Byb2ZpbGUudGFyAO1Y32/bNhD2s/6Kg/KyYZZsy" + "78K78lLMsxY5gRxmqIYhoKWaJsYJWokZdfo+r/vSFmunCZNBtQJ1vF7sXX36e54vDN5T" + "knGFlTpcEtS3jgO2ohBr2c/EXc/29Gg1+h0e1F32Ol1B1Gj3Ymifr8B7SPFc4BCaSIBG" + "lII/SXeY/r/KIIg8NZUKiayEaw7nt7mdOQBrAkvqBqBL1ArWULflRJbJz4SYpEt2FJSJ" + @@ -84,13 +84,13 @@ func TestInstanceCreate(t *testing.T) { "yJ66WPQwcHBwcHBwcHBwcHBwcHBwcHhm8Q/mTHqWgAoAAA="), }, rb.DefinitionKey{RBName: "test-rbdef", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"test-rbdef\"," + "\"rb-version\":\"v1\"," + "\"chart-name\":\"vault-consul-dev\"," + "\"description\":\"testresourcebundle\"}"), // base64 encoding of vagrant/tests/vnfs/testrb/helm/vault-consul-dev - "content": []byte("H4sICEetS1wAA3ZhdWx0LWNvbnN1bC1kZXYudGFyAO0c7XLbNjK/+R" + + "defcontent": []byte("H4sICEetS1wAA3ZhdWx0LWNvbnN1bC1kZXYudGFyAO0c7XLbNjK/+R" + "QYujdJehatb+V4czPnOmnPk9bO2Gk7nbaTgUhIxpgiGAK0o3P9QPca92S3C5AU9GXZiax" + "c7rA/LJEAFovdxX4AK1/RIlGNSKSySBoxuzp4sn1oAgx6Pf0JsPipv7c63XZ70O61W4Mn" + "zVZ7MGg9Ib1HoGUJCqloTsiTXAh1V79N7V8oXC3K/+iC5iqY0kmytTlQwP1ud538W51Wf" + diff --git a/src/k8splugin/internal/config/config.go b/src/k8splugin/internal/config/config.go index dc3f7a11..23ec401e 100644 --- a/src/k8splugin/internal/config/config.go +++ b/src/k8splugin/internal/config/config.go @@ -26,20 +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"` - KubeConfigDir string `json:"kube-config-dir"` - 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 @@ -75,20 +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", - KubeConfigDir: cwd, - OVNCentralAddress: "127.0.0.1", - 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/connection/connection.go b/src/k8splugin/internal/connection/connection.go index d110c221..df1400b5 100644 --- a/src/k8splugin/internal/connection/connection.go +++ b/src/k8splugin/internal/connection/connection.go @@ -20,7 +20,6 @@ import ( "encoding/base64" "encoding/json" "io/ioutil" - "path/filepath" "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" @@ -32,7 +31,13 @@ type Connection struct { CloudRegion string `json:"cloud-region"` CloudOwner string `json:"cloud-owner"` Kubeconfig string `json:"kubeconfig"` - OtherConnectivityList map[string]interface{} `json:"other-connectivity-list"` + OtherConnectivityList ConnectivityRecordList `json:"other-connectivity-list"` +} + +// ConnectivityRecordList covers lists of connectivity records +// and any other data that needs to be stored +type ConnectivityRecordList struct { + ConnectivityRecords []map[string]string `json:"connectivity-records"` } // ConnectionKey is the key structure that is used in the database @@ -56,6 +61,7 @@ type ConnectionManager interface { Create(c Connection) (Connection, error) Get(name string) (Connection, error) Delete(name string) error + GetConnectivityRecordByName(connname string, name string) (map[string]string, error) } // ConnectionClient implements the ConnectionManager @@ -65,7 +71,7 @@ type ConnectionClient struct { tagMeta string } -// New ConnectionClient returns an instance of the ConnectionClient +// NewConnectionClient returns an instance of the ConnectionClient // which implements the ConnectionManager func NewConnectionClient() *ConnectionClient { return &ConnectionClient{ @@ -117,6 +123,38 @@ func (v *ConnectionClient) Get(name string) (Connection, error) { return Connection{}, pkgerrors.New("Error getting Connection") } +// GetConnectivityRecordByName returns Connection for corresponding to name +// JSON example: +// "connectivity-records" : +// [ +// { +// “connectivity-record-name” : “<name>”, // example: OVN +// “FQDN-or-ip” : “<fqdn>”, +// “ca-cert-to-verify-server” : “<contents of CA certificate to validate the OVN server>”, +// “ssl-initiator” : “<true/false”>, +// “user-name”: “<user name>”, //valid if ssl-initator is false +// “password” : “<password>”, // valid if ssl-initiator is false +// “private-key” : “<contents of private key in PEM>”, // valid if ssl-initiator is true +// “cert-to-present” : “<contents of certificate to present to server>” , //valid if ssl-initiator is true +// }, +// ] +func (v *ConnectionClient) GetConnectivityRecordByName(connectionName string, + connectivityRecordName string) (map[string]string, error) { + + conn, err := v.Get(connectionName) + if err != nil { + return nil, pkgerrors.Wrap(err, "Error getting connection") + } + + for _, value := range conn.OtherConnectivityList.ConnectivityRecords { + if connectivityRecordName == value["connectivity-record-name"] { + return value, nil + } + } + + return nil, pkgerrors.New("Connectivity record " + connectivityRecordName + " not found") +} + // Delete the Connection from database func (v *ConnectionClient) Delete(name string) error { @@ -132,7 +170,7 @@ func (v *ConnectionClient) Delete(name string) error { // Download the connection information onto a kubeconfig file // The file is named after the name of the connection and will // be placed in the provided parent directory -func (v *ConnectionClient) Download(name string, parentdir string) (string, error) { +func (v *ConnectionClient) Download(name string) (string, error) { conn, err := v.Get(name) if err != nil { @@ -145,11 +183,17 @@ func (v *ConnectionClient) Download(name string, parentdir string) (string, erro return "", pkgerrors.Wrap(err, "Converting from base64") } - target := filepath.Join(parentdir, conn.CloudRegion) - err = ioutil.WriteFile(target, kubeContent, 0644) + //Create temp file to write kubeconfig + //Assume this file will be deleted after usage by the consumer + tempF, err := ioutil.TempFile("", "kube-config-temp-") + if err != nil { + return "", pkgerrors.Wrap(err, "Creating temp file") + } + + _, err = tempF.Write(kubeContent) if err != nil { return "", pkgerrors.Wrap(err, "Writing kubeconfig to file") } - return target, nil + return tempF.Name(), nil } 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/rb/definition.go b/src/k8splugin/internal/rb/definition.go index 476e40ee..65ae8e00 100644 --- a/src/k8splugin/internal/rb/definition.go +++ b/src/k8splugin/internal/rb/definition.go @@ -79,8 +79,8 @@ type DefinitionClient struct { func NewDefinitionClient() *DefinitionClient { return &DefinitionClient{ storeName: "rbdef", - tagMeta: "metadata", - tagContent: "content", + tagMeta: "defmetadata", + tagContent: "defcontent", } } @@ -121,10 +121,13 @@ func (v *DefinitionClient) List(name string) ([]Definition, error) { log.Printf("[Definition] Error Unmarshaling value for: %s", key) continue } + //Select only the definitions that match name provided - if def.RBName == name { + //If name is empty, return all + if def.RBName == name || name == "" { results = append(results, def) } + } } diff --git a/src/k8splugin/internal/rb/definition_test.go b/src/k8splugin/internal/rb/definition_test.go index 054da2cd..0140b459 100644 --- a/src/k8splugin/internal/rb/definition_test.go +++ b/src/k8splugin/internal/rb/definition_test.go @@ -113,14 +113,14 @@ func TestListDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + "\"chart-name\":\"testchart\"}"), }, DefinitionKey{RBName: "testresourcebundle", RBVersion: "v2"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle_version2\"," + "\"rb-version\":\"v2\"," + @@ -196,7 +196,7 @@ func TestGetDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -326,7 +326,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"}"), @@ -362,7 +362,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -400,7 +400,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -438,7 +438,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -459,7 +459,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -551,12 +551,12 @@ func TestDownloadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + "\"chart-name\":\"firewall\"}"), - "content": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" + + "defcontent": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" + "QEtLE40igAUtSC+2IHt9IEVwIpYtShP/bvGFmFk/SLI08Re3IVCG077Rn" + "b75zYZ2yztVV8N7XP9vWSWmzZ6mP+yxx0lrF7pJzjkN/Sz//1u5/6ppKG" + "R/jVLrT0VUAAAAAAAAAAAAAAAAAABu8ALXoSvkAKAAA"), @@ -572,7 +572,7 @@ func TestDownloadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + diff --git a/src/k8splugin/internal/rb/profile.go b/src/k8splugin/internal/rb/profile.go index 37e9aba8..49768d4b 100644 --- a/src/k8splugin/internal/rb/profile.go +++ b/src/k8splugin/internal/rb/profile.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/base64" "encoding/json" + "log" "path/filepath" "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" @@ -44,6 +45,7 @@ type Profile struct { type ProfileManager interface { Create(def Profile) (Profile, error) Get(rbName, rbVersion, prName string) (Profile, error) + List(rbName, rbVersion string) ([]Profile, error) Delete(rbName, rbVersion, prName string) error Upload(rbName, rbVersion, prName string, inp []byte) error } @@ -78,8 +80,8 @@ type ProfileClient struct { func NewProfileClient() *ProfileClient { return &ProfileClient{ storeName: "rbdef", - tagMeta: "metadata", - tagContent: "content", + tagMeta: "profilemetadata", + tagContent: "profilecontent", manifestName: "manifest.yaml", } } @@ -148,6 +150,38 @@ func (v *ProfileClient) Get(rbName, rbVersion, prName string) (Profile, error) { return Profile{}, pkgerrors.New("Error getting Resource Bundle Profile") } +// List returns the Resource Bundle Profile for corresponding ID +func (v *ProfileClient) List(rbName, rbVersion string) ([]Profile, error) { + + //Get all profiles + dbres, err := db.DBconn.ReadAll(v.storeName, v.tagMeta) + if err != nil || len(dbres) == 0 { + return []Profile{}, pkgerrors.Wrap(err, "No Profiles Found") + } + + var results []Profile + for key, value := range dbres { + //value is a byte array + if value != nil { + pr := Profile{} + err = db.DBconn.Unmarshal(value, &pr) + if err != nil { + log.Printf("[Profile] Error: %s Unmarshaling value for: %s", err.Error(), key) + continue + } + if pr.RBName == rbName && pr.RBVersion == rbVersion { + results = append(results, pr) + } + } + } + + if len(results) == 0 { + return results, pkgerrors.New("No Profiles Found for Definition and Version") + } + + return results, nil +} + // Delete the Resource Bundle Profile from database func (v *ProfileClient) Delete(rbName, rbVersion, prName string) error { key := ProfileKey{ diff --git a/src/k8splugin/internal/rb/profile_test.go b/src/k8splugin/internal/rb/profile_test.go index 29efb506..26b0161d 100644 --- a/src/k8splugin/internal/rb/profile_test.go +++ b/src/k8splugin/internal/rb/profile_test.go @@ -18,11 +18,13 @@ package rb import ( "bytes" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" "reflect" + "sort" "strings" "testing" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" + pkgerrors "github.com/pkg/errors" ) @@ -56,7 +58,7 @@ func TestCreateProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -79,7 +81,7 @@ func TestCreateProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v2"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -145,7 +147,7 @@ func TestGetProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -187,6 +189,106 @@ func TestGetProfile(t *testing.T) { } } +func TestListProfile(t *testing.T) { + + testCases := []struct { + label string + name string + rbdef string + version string + expectedError string + mockdb *db.MockDB + expected []Profile + }{ + { + label: "List Resource Bundle Profile", + name: "testresourcebundle", + rbdef: "testresourcebundle", + version: "v1", + expected: []Profile{ + { + ProfileName: "testprofile1", + ReleaseName: "testprofilereleasename", + Namespace: "testnamespace", + KubernetesVersion: "1.12.3", + RBName: "testresourcebundle", + RBVersion: "v1", + }, + { + ProfileName: "testprofile2", + ReleaseName: "testprofilereleasename2", + Namespace: "testnamespace2", + KubernetesVersion: "1.12.3", + RBName: "testresourcebundle", + RBVersion: "v1", + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { + "profilemetadata": []byte( + "{\"profile-name\":\"testprofile1\"," + + "\"release-name\":\"testprofilereleasename\"," + + "\"namespace\":\"testnamespace\"," + + "\"rb-name\":\"testresourcebundle\"," + + "\"rb-version\":\"v1\"," + + "\"kubernetes-version\":\"1.12.3\"}"), + }, + ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile2"}.String(): { + "profilemetadata": []byte( + "{\"profile-name\":\"testprofile2\"," + + "\"release-name\":\"testprofilereleasename2\"," + + "\"namespace\":\"testnamespace2\"," + + "\"rb-name\":\"testresourcebundle\"," + + "\"rb-version\":\"v1\"," + + "\"kubernetes-version\":\"1.12.3\"}"), + }, + }, + }, + }, + { + label: "List Error", + expectedError: "DB Error", + mockdb: &db.MockDB{ + Err: pkgerrors.New("DB Error"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewProfileClient() + got, err := impl.List(testCase.rbdef, testCase.version) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("List returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("List returned an unexpected error %s", err) + } + } else { + // Since the order of returned slice is not guaranteed + // Check both and return error if both don't match + sort.Slice(got, func(i, j int) bool { + return got[i].ProfileName < got[j].ProfileName + }) + // Sort both as it is not expected that testCase.expected + // is sorted + sort.Slice(testCase.expected, func(i, j int) bool { + return testCase.expected[i].ProfileName < testCase.expected[j].ProfileName + }) + + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("List Resource Bundle returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestDeleteProfile(t *testing.T) { testCases := []struct { @@ -265,7 +367,7 @@ func TestUploadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -306,7 +408,7 @@ func TestUploadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile2"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -330,7 +432,7 @@ func TestUploadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -425,14 +527,14 @@ func TestDownloadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + "\"rb-name\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + "\"kubernetesversion\":\"1.12.3\"}"), - "content": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" + + "profilecontent": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" + "QEtLE40igAUtSC+2IHt9IEVwIpYtShP/bvGFmFk/SLI08Re3IVCG077Rn" + "b75zYZ2yztVV8N7XP9vWSWmzZ6mP+yxx0lrF7pJzjkN/Sz//1u5/6ppKG" + "R/jVLrT0VUAAAAAAAAAAAAAAAAAABu8ALXoSvkAKAAA"), @@ -449,7 +551,7 @@ func TestDownloadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile2"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -512,7 +614,7 @@ func TestResolveProfile(t *testing.T) { Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "profile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"profile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -520,7 +622,7 @@ func TestResolveProfile(t *testing.T) { "\"rb-version\":\"v1\"," + "\"kubernetesversion\":\"1.12.3\"}"), // base64 encoding of vagrant/tests/vnfs/testrb/helm/profile - "content": []byte("H4sICLmjT1wAA3Byb2ZpbGUudGFyAO1Y32/bNhD2s/6Kg/KyYZZsy" + + "profilecontent": []byte("H4sICLmjT1wAA3Byb2ZpbGUudGFyAO1Y32/bNhD2s/6Kg/KyYZZsy" + "78K78lLMsxY5gRxmqIYhoKWaJsYJWokZdfo+r/vSFmunCZNBtQJ1vF7sXX36e54vDN5T" + "knGFlTpcEtS3jgO2ohBr2c/EXc/29Gg1+h0e1F32Ol1B1Gj3Ymifr8B7SPFc4BCaSIBG" + "lII/SXeY/r/KIIg8NZUKiayEaw7nt7mdOQBrAkvqBqBL1ArWULflRJbJz4SYpEt2FJSJ" + @@ -545,13 +647,13 @@ func TestResolveProfile(t *testing.T) { "yJ66WPQwcHBwcHBwcHBwcHBwcHBwcHhm8Q/mTHqWgAoAAA="), }, DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + "\"chart-name\":\"vault-consul-dev\"," + "\"description\":\"testresourcebundle\"}"), // base64 encoding of vagrant/tests/vnfs/testrb/helm/vault-consul-dev - "content": []byte("H4sICEetS1wAA3ZhdWx0LWNvbnN1bC1kZXYudGFyAO0c7XLbNjK/+R" + + "defcontent": []byte("H4sICEetS1wAA3ZhdWx0LWNvbnN1bC1kZXYudGFyAO0c7XLbNjK/+R" + "QYujdJehatb+V4czPnOmnPk9bO2Gk7nbaTgUhIxpgiGAK0o3P9QPca92S3C5AU9GXZiax" + "c7rA/LJEAFovdxX4AK1/RIlGNSKSySBoxuzp4sn1oAgx6Pf0JsPipv7c63XZ70O61W4Mn" + "zVZ7MGg9Ib1HoGUJCqloTsiTXAh1V79N7V8oXC3K/+iC5iqY0kmytTlQwP1ud538W51Wf" + 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 e69a075b..aa0d584b 100644 --- a/src/k8splugin/plugins/network/plugin.go +++ b/src/k8splugin/plugins/network/plugin.go @@ -14,36 +14,42 @@ 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/app" "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin" pkgerrors "github.com/pkg/errors" "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 } // Create an ONAP Network object -func (p networkPlugin) Create(yamlFilePath string, namespace string, client *app.KubernetesClient) (string, error) { +func (p networkPlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) { network := &v1.OnapNetwork{} if _, err := utils.DecodeYAML(yamlFilePath, network); err != nil { return "", pkgerrors.Wrap(err, "Decode network object error") @@ -69,20 +75,24 @@ func (p networkPlugin) Create(yamlFilePath string, namespace string, client *app } // Get a Network -func (p networkPlugin) Get(resource helm.KubernetesResource, namespace string, client *app.KubernetesClient) (string, error) { +func (p networkPlugin) Get(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) (string, error) { return "", nil } // List of Networks func (p networkPlugin) List(gvk schema.GroupVersionKind, namespace string, - client *app.KubernetesClient) ([]helm.KubernetesResource, error) { + client plugin.KubernetesConnector) ([]helm.KubernetesResource, error) { return nil, nil } // Delete an existing Network -func (p networkPlugin) Delete(resource helm.KubernetesResource, namespace string, client *app.KubernetesClient) error { - cniType, networkName := extractData(resource.Name) +func (p networkPlugin) Delete(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) error { + 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 { |