diff options
Diffstat (limited to 'src/k8splugin/internal')
-rw-r--r-- | src/k8splugin/internal/app/config_backend.go | 6 | ||||
-rw-r--r-- | src/k8splugin/internal/app/instance.go | 81 | ||||
-rw-r--r-- | src/k8splugin/internal/app/instance_test.go | 194 |
3 files changed, 169 insertions, 112 deletions
diff --git a/src/k8splugin/internal/app/config_backend.go b/src/k8splugin/internal/app/config_backend.go index 763aed0d..9894b6ac 100644 --- a/src/k8splugin/internal/app/config_backend.go +++ b/src/k8splugin/internal/app/config_backend.go @@ -343,7 +343,7 @@ func scheduleResources(c chan configResourceList) { data := <-c //TODO: ADD Check to see if Application running ic := NewInstanceClient() - resp, err := ic.Find(data.profile.RBName, data.profile.RBVersion, data.profile.ProfileName) + resp, err := ic.Find(data.profile.RBName, data.profile.RBVersion, data.profile.ProfileName, nil) if err != nil || len(resp) == 0 { log.Println("Error finding a running instance. Retrying later...") time.Sleep(time.Second * 10) @@ -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.CloudRegion) + err = k8sClient.init(inst.Request.CloudRegion) 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.CloudRegion) + err = k8sClient.init(inst.Request.CloudRegion) 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 41eca211..19841c01 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -40,20 +40,17 @@ type InstanceRequest struct { // InstanceResponse contains the response from instantiation type InstanceResponse struct { - ID string `json:"id"` - RBName string `json:"rb-name"` - RBVersion string `json:"rb-version"` - ProfileName string `json:"profile-name"` - CloudRegion string `json:"cloud-region"` - Namespace string `json:"namespace"` - Resources []helm.KubernetesResource `json:"resources"` + ID string `json:"id"` + Request InstanceRequest `json:"request"` + Namespace string `json:"namespace"` + Resources []helm.KubernetesResource `json:"resources"` } // InstanceManager is an interface exposes the instantiation functionality type InstanceManager interface { Create(i InstanceRequest) (InstanceResponse, error) Get(id string) (InstanceResponse, error) - Find(rbName string, ver string, profile string) ([]InstanceResponse, error) + Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) Delete(id string) error } @@ -134,13 +131,10 @@ func (v *InstanceClient) Create(i InstanceRequest) (InstanceResponse, error) { //Compose the return response resp := InstanceResponse{ - ID: id, - RBName: i.RBName, - RBVersion: i.RBVersion, - ProfileName: i.ProfileName, - CloudRegion: i.CloudRegion, - Namespace: profile.Namespace, - Resources: createdResources, + ID: id, + Request: i, + Namespace: profile.Namespace, + Resources: createdResources, } key := InstanceKey{ @@ -180,9 +174,11 @@ func (v *InstanceClient) Get(id string) (InstanceResponse, error) { // 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 -func (v *InstanceClient) Find(rbName string, version string, profile string) ([]InstanceResponse, error) { - if rbName == "" { - return []InstanceResponse{}, pkgerrors.New("rbName is required and cannot be empty") +// If labelKeys are provided, the results are filtered based on that. +// It is an AND operation for labelkeys. +func (v *InstanceClient) Find(rbName string, version string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) { + if rbName == "" && len(labelKeys) == 0 { + return []InstanceResponse{}, pkgerrors.New("rbName or labelkeys is required and cannot be empty") } values, err := db.DBconn.ReadAll(v.storeName, v.tagInst) @@ -192,6 +188,7 @@ func (v *InstanceClient) Find(rbName string, version string, profile string) ([] response := []InstanceResponse{} //values is a map[string][]byte +InstanceResponseLoop: for _, value := range values { resp := InstanceResponse{} db.DBconn.Unmarshal(value, &resp) @@ -199,19 +196,45 @@ func (v *InstanceClient) Find(rbName string, version string, profile string) ([] return []InstanceResponse{}, pkgerrors.Wrap(err, "Unmarshaling Instance Value") } - if resp.RBName == rbName { + // Filter by labels provided + if len(labelKeys) != 0 { + for lkey, lvalue := range labelKeys { + //Check if label key exists and get its value + if val, ok := resp.Request.Labels[lkey]; ok { + if lvalue != val { + continue InstanceResponseLoop + } + } else { + continue InstanceResponseLoop + } + } + } + + if rbName != "" { + if resp.Request.RBName == rbName { - //Check if a version is provided and if it matches - if version != "" { - if resp.RBVersion == version { - //Check if a profilename matches or if it is not provided - if profile == "" || resp.ProfileName == profile { - response = append(response, resp) + //Check if a version is provided and if it matches + if version != "" { + if resp.Request.RBVersion == version { + //Check if a profilename matches or if it is not provided + if profile == "" || resp.Request.ProfileName == profile { + response = append(response, resp) + } } + } else { + //Append all versions as version is not provided + response = append(response, resp) } - } else { - //Append all versions as version is not provided - response = append(response, resp) + } + } else { + response = append(response, resp) + } + } + + //filter the list by labelKeys now + for _, value := range response { + for _, label := range labelKeys { + if _, ok := value.Request.Labels[label]; ok { } } } @@ -227,7 +250,7 @@ func (v *InstanceClient) Delete(id string) error { } k8sClient := KubernetesClient{} - err = k8sClient.init(inst.CloudRegion) + err = k8sClient.init(inst.Request.CloudRegion) 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 2fa2115b..ea377482 100644 --- a/src/k8splugin/internal/app/instance_test.go +++ b/src/k8splugin/internal/app/instance_test.go @@ -203,12 +203,14 @@ func TestInstanceGet(t *testing.T) { InstanceKey{ID: "HaKpys8e"}.String(): { "instance": []byte( `{ - "profile-name":"profile1", - "id":"HaKpys8e", + "id":"HaKpys8e", + "request": { + "profile-name":"profile1", + "rb-name":"test-rbdef", + "rb-version":"v1", + "cloud-region":"region1" + }, "namespace":"testnamespace", - "rb-name":"test-rbdef", - "rb-version":"v1", - "cloud-region":"region1", "resources": [ { "GVK": { @@ -233,13 +235,14 @@ func TestInstanceGet(t *testing.T) { } expected := InstanceResponse{ - ID: "HaKpys8e", - RBName: "test-rbdef", - RBVersion: "v1", - ProfileName: "profile1", - CloudRegion: "region1", - Namespace: "testnamespace", - + ID: "HaKpys8e", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + }, + Namespace: "testnamespace", Resources: []helm.KubernetesResource{ { GVK: schema.GroupVersionKind{ @@ -275,12 +278,14 @@ func TestInstanceGet(t *testing.T) { InstanceKey{ID: "HaKpys8e"}.String(): { "instance": []byte( `{ - "profile-name":"profile1", - "id":"HaKpys8e", + "id":"HaKpys8e", + "request": { + "profile-name":"profile1", + "rb-name":"test-rbdef", + "rb-version":"v1", + "cloud-region":"region1" + }, "namespace":"testnamespace", - "rb-name":"test-rbdef", - "rb-version":"v1", - "cloud-region":"region1", "resources": [ { "GVK": { @@ -329,12 +334,17 @@ func TestInstanceFind(t *testing.T) { InstanceKey{ID: "HaKpys8e"}.String(): { "instance": []byte( `{ - "profile-name":"profile1", - "id":"HaKpys8e", + "id":"HaKpys8e", + "request": { + "profile-name":"profile1", + "rb-name":"test-rbdef", + "rb-version":"v1", + "cloud-region":"region1", + "labels":{ + "vf_module_id": "test-vf-module-id" + } + }, "namespace":"testnamespace", - "rb-name":"test-rbdef", - "rb-version":"v1", - "cloud-region":"region1", "resources": [ { "GVK": { @@ -358,12 +368,14 @@ func TestInstanceFind(t *testing.T) { InstanceKey{ID: "HaKpys8f"}.String(): { "instance": []byte( `{ - "profile-name":"profile2", - "id":"HaKpys8f", + "id":"HaKpys8f", + "request": { + "profile-name":"profile2", + "rb-name":"test-rbdef", + "rb-version":"v1", + "cloud-region":"region1" + }, "namespace":"testnamespace", - "rb-name":"test-rbdef", - "rb-version":"v1", - "cloud-region":"region1", "resources": [ { "GVK": { @@ -387,12 +399,14 @@ func TestInstanceFind(t *testing.T) { InstanceKey{ID: "HaKpys8g"}.String(): { "instance": []byte( `{ - "profile-name":"profile1", - "id":"HaKpys8g", + "id":"HaKpys8g", + "request": { + "profile-name":"profile1", + "rb-name":"test-rbdef", + "rb-version":"v2", + "cloud-region":"region1" + }, "namespace":"testnamespace", - "rb-name":"test-rbdef", - "rb-version":"v2", - "cloud-region":"region1", "resources": [ { "GVK": { @@ -422,13 +436,17 @@ func TestInstanceFind(t *testing.T) { expected := []InstanceResponse{ { - ID: "HaKpys8e", - RBName: "test-rbdef", - RBVersion: "v1", - ProfileName: "profile1", - CloudRegion: "region1", - Namespace: "testnamespace", - + ID: "HaKpys8e", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + Labels: map[string]string{ + "vf_module_id": "test-vf-module-id", + }, + }, + Namespace: "testnamespace", Resources: []helm.KubernetesResource{ { GVK: schema.GroupVersionKind{ @@ -447,13 +465,14 @@ func TestInstanceFind(t *testing.T) { }, }, { - ID: "HaKpys8f", - RBName: "test-rbdef", - RBVersion: "v1", - ProfileName: "profile2", - CloudRegion: "region1", - Namespace: "testnamespace", - + ID: "HaKpys8f", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile2", + CloudRegion: "region1", + }, + Namespace: "testnamespace", Resources: []helm.KubernetesResource{ { GVK: schema.GroupVersionKind{ @@ -472,13 +491,14 @@ func TestInstanceFind(t *testing.T) { }, }, { - ID: "HaKpys8g", - RBName: "test-rbdef", - RBVersion: "v2", - ProfileName: "profile1", - CloudRegion: "region1", - Namespace: "testnamespace", - + ID: "HaKpys8g", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v2", + ProfileName: "profile1", + CloudRegion: "region1", + }, + Namespace: "testnamespace", Resources: []helm.KubernetesResource{ { GVK: schema.GroupVersionKind{ @@ -499,7 +519,7 @@ func TestInstanceFind(t *testing.T) { } ic := NewInstanceClient() name := "test-rbdef" - data, err := ic.Find(name, "", "") + data, err := ic.Find(name, "", "", nil) if err != nil { t.Fatalf("TestInstanceFind returned an error (%s)", err) } @@ -528,12 +548,17 @@ func TestInstanceFind(t *testing.T) { expected := []InstanceResponse{ { - ID: "HaKpys8e", - RBName: "test-rbdef", - RBVersion: "v1", - ProfileName: "profile1", - CloudRegion: "region1", - Namespace: "testnamespace", + ID: "HaKpys8e", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + Labels: map[string]string{ + "vf_module_id": "test-vf-module-id", + }, + }, + Namespace: "testnamespace", Resources: []helm.KubernetesResource{ { @@ -553,12 +578,14 @@ func TestInstanceFind(t *testing.T) { }, }, { - ID: "HaKpys8f", - RBName: "test-rbdef", - RBVersion: "v1", - ProfileName: "profile2", - CloudRegion: "region1", - Namespace: "testnamespace", + ID: "HaKpys8f", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile2", + CloudRegion: "region1", + }, + Namespace: "testnamespace", Resources: []helm.KubernetesResource{ { @@ -580,7 +607,7 @@ func TestInstanceFind(t *testing.T) { } ic := NewInstanceClient() name := "test-rbdef" - data, err := ic.Find(name, "v1", "") + data, err := ic.Find(name, "v1", "", nil) if err != nil { t.Fatalf("TestInstanceFind returned an error (%s)", err) } @@ -609,12 +636,17 @@ func TestInstanceFind(t *testing.T) { expected := []InstanceResponse{ { - ID: "HaKpys8e", - RBName: "test-rbdef", - RBVersion: "v1", - ProfileName: "profile1", - CloudRegion: "region1", - Namespace: "testnamespace", + ID: "HaKpys8e", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + Labels: map[string]string{ + "vf_module_id": "test-vf-module-id", + }, + }, + Namespace: "testnamespace", Resources: []helm.KubernetesResource{ { @@ -636,7 +668,7 @@ func TestInstanceFind(t *testing.T) { } ic := NewInstanceClient() name := "test-rbdef" - data, err := ic.Find(name, "v1", "profile1") + data, err := ic.Find(name, "v1", "profile1", nil) if err != nil { t.Fatalf("TestInstanceFind returned an error (%s)", err) } @@ -695,7 +727,7 @@ func TestInstanceFind(t *testing.T) { ic := NewInstanceClient() name := "non-existing" - resp, _ := ic.Find(name, "", "") + resp, _ := ic.Find(name, "", "", nil) if len(resp) != 0 { t.Fatalf("Expected 0 responses, but got %d", len(resp)) } @@ -726,12 +758,14 @@ func TestInstanceDelete(t *testing.T) { InstanceKey{ID: "HaKpys8e"}.String(): { "instance": []byte( `{ - "profile-name":"profile1", - "id":"HaKpys8e", + "id":"HaKpys8e", + "request": { + "profile-name":"profile1", + "rb-name":"test-rbdef", + "rb-version":"v1", + "cloud-region":"mock_connection" + }, "namespace":"testnamespace", - "rb-name":"test-rbdef", - "rb-version":"v1", - "cloud-region":"mock_connection", "resources": [ { "GVK": { |