Files
odo/pkg/binding/remove.go
Armel Soro eeda644cc9 Add support for OpenShift Devfile components (#6548)
* Add integration test case

Co-authored-by: Anand Kumar Singh <anandrkskd@gmail.com>
Co-authored-by: Philippe Martin <phmartin@redhat.com>

* Add ApplyOpenShift method to handler

* Test openhift component with odo dev

* Rename GetKubernetesComponentsToPush to GetK8sAndOcComponentsToPush and modify if to obtain both k8s and oc components

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Fix unit test failures with delete_test

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* update ListClusterResourcesToDeleteFromDevfile to fetch openshift component,Add ListOpenShiftComponents

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* fix testcase 'should have deleted the old resource and created the new resource' and add helper function ReplaceStrings

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* fix debug test to check openshift component

Signed-off-by: anandrkskd <anandrkskd@gmail.com>

* Update GetBindingsFromDevfile to include openshift components

* Update offline tests

* Add openshift component to devfiles

* Update tests

* Fix binding tests

* Fix RemoveBinding unit tests

* Handle OpenShift components when removing binding

* odo describe component displaysOpenShift components

* Remove unused function

---------

Signed-off-by: Parthvi Vala <pvala@redhat.com>
Signed-off-by: anandrkskd <anandrkskd@gmail.com>
Co-authored-by: Anand Kumar Singh <anandrkskd@gmail.com>
Co-authored-by: Philippe Martin <phmartin@redhat.com>
Co-authored-by: Parthvi Vala <pvala@redhat.com>
2023-02-03 14:30:24 +01:00

75 lines
2.8 KiB
Go

package binding
import (
"fmt"
"path/filepath"
"strings"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser"
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
devfilefs "github.com/devfile/library/v2/pkg/testingutil/filesystem"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
backendpkg "github.com/redhat-developer/odo/pkg/binding/backend"
"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/libdevfile"
)
// ValidateRemoveBinding validates if the command has adequate arguments/flags
func (o *BindingClient) ValidateRemoveBinding(flags map[string]string) error {
if flags[backendpkg.FLAG_NAME] == "" {
return fmt.Errorf("you must specify the service binding name with --%s flag", backendpkg.FLAG_NAME)
}
return nil
}
// RemoveBinding removes the binding from devfile
func (o *BindingClient) RemoveBinding(servicebindingName string, obj parser.DevfileObj) (parser.DevfileObj, error) {
// Get all the K8s type devfile components
k8sComponents, err := obj.Data.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.KubernetesComponentType},
})
if err != nil {
return obj, err
}
// Get all the OpenShift type devfile components
ocpComponents, err := obj.Data.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.OpenshiftComponentType},
})
if err != nil {
return obj, err
}
allComponents := make([]v1alpha2.Component, 0, len(k8sComponents)+len(ocpComponents))
allComponents = append(allComponents, k8sComponents...)
allComponents = append(allComponents, ocpComponents...)
var componentName string
var options []string
for _, component := range allComponents {
var unstructuredObjs []unstructured.Unstructured
// Parse the K8s manifest
unstructuredObjs, err = libdevfile.GetK8sComponentAsUnstructuredList(obj, component.Name, filepath.Dir(obj.Ctx.GetAbsPath()), devfilefs.DefaultFs{})
if err != nil || len(unstructuredObjs) == 0 {
continue
}
// We default to the first object in the list because as far as ServiceBinding is concerned,
// we assume that only one resource will be defined for the Devfile K8s component; which is true
unstructuredObj := unstructuredObjs[0]
if unstructuredObj.GetKind() == kclient.ServiceBindingKind {
options = append(options, unstructuredObj.GetName())
if unstructuredObj.GetName() == servicebindingName {
componentName = component.Name
break
}
}
}
if componentName == "" {
return obj, fmt.Errorf("Service Binding %q not found in the devfile. Available Service Bindings: %s", servicebindingName, strings.Join(options, ", "))
}
err = obj.Data.DeleteComponent(componentName)
return obj, err
}