diff options
Diffstat (limited to 'src/k8splugin/internal/app/instance.go')
-rw-r--r-- | src/k8splugin/internal/app/instance.go | 81 |
1 files changed, 52 insertions, 29 deletions
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") } |