Skip to content

Commit

Permalink
Remove unused ComponentExist, PushedComponent function; Move ListFrom…
Browse files Browse the repository at this point in the history
…Cluster to List

Signed-off-by: Parthvi Vala <[email protected]>
  • Loading branch information
valaparthvi committed Jul 13, 2022
1 parent c7f3f3b commit 59262ba
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 663 deletions.
10 changes: 0 additions & 10 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ func GetOnePod(client kclient.ClientInterface, componentName string, appName str
return client.GetOnePodFromSelector(odolabels.GetSelector(componentName, appName, odolabels.ComponentDevMode))
}

// ComponentExists checks whether a deployment by the given name exists in the given app
func ComponentExists(client kclient.ClientInterface, name string, app string) (bool, error) {
deployment, err := client.GetOneDeployment(name, app)
if _, ok := err.(*kclient.DeploymentNotFoundError); ok {
klog.V(2).Infof("Deployment %s not found for belonging to the %s app ", name, app)
return false, nil
}
return deployment != nil, err
}

// Log returns log from component
func Log(client kclient.ClientInterface, componentName string, appName string, follow bool, command v1alpha2.Command) (io.ReadCloser, error) {

Expand Down
64 changes: 9 additions & 55 deletions pkg/component/pushed_component.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package component

import (
"errors"
"fmt"

"github.com/redhat-developer/odo/pkg/kclient"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"

odolabels "github.com/redhat-developer/odo/pkg/labels"
"github.com/redhat-developer/odo/pkg/storage"
v1 "k8s.io/api/apps/v1"
v12 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
)

type provider interface {
GetLabels() map[string]string
GetAnnotations() map[string]string
GetName() string
GetEnvVars() []v12.EnvVar
GetEnvVars() []v1.EnvVar
GetLinkedSecrets() []SecretMount
}

Expand All @@ -40,7 +38,6 @@ type defaultPushedComponent struct {
application string
storage []storage.Storage
provider provider
client kclient.ClientInterface
storageClient storage.Client
}

Expand All @@ -63,7 +60,7 @@ func (d defaultPushedComponent) GetType() (string, error) {
return getType(d.provider)
}

func (d defaultPushedComponent) GetEnvVars() []v12.EnvVar {
func (d defaultPushedComponent) GetEnvVars() []v1.EnvVar {
return d.provider.GetEnvVars()
}

Expand All @@ -75,7 +72,7 @@ func (d defaultPushedComponent) GetLinkedSecrets() []SecretMount {
func (d defaultPushedComponent) GetStorage() ([]storage.Storage, error) {
if d.storage == nil {
if _, ok := d.provider.(*devfileComponent); ok {
storageList, err := d.storageClient.ListFromCluster()
storageList, err := d.storageClient.List()
if err != nil {
return nil, err
}
Expand All @@ -90,7 +87,7 @@ func (d defaultPushedComponent) GetApplication() string {
}

type devfileComponent struct {
d v1.Deployment
d appsv1.Deployment
}

var _ provider = (*devfileComponent)(nil)
Expand Down Expand Up @@ -129,8 +126,8 @@ func (d devfileComponent) GetLinkedSecrets() (secretMounts []SecretMount) {
return secretMounts
}

func (d devfileComponent) GetEnvVars() []v12.EnvVar {
var envs []v12.EnvVar
func (d devfileComponent) GetEnvVars() []v1.EnvVar {
var envs []v1.EnvVar
for _, container := range d.d.Spec.Template.Spec.Containers {
envs = append(envs, container.Env...)
}
Expand All @@ -155,46 +152,3 @@ func getType(component provider) (string, error) {
}
return res, nil
}

func newPushedComponent(applicationName string, p provider, c kclient.ClientInterface, storageClient storage.Client) PushedComponent {
return &defaultPushedComponent{
application: applicationName,
provider: p,
client: c,
storageClient: storageClient,
}
}

// GetPushedComponent returns an abstraction over the cluster representation of the component
func GetPushedComponent(c kclient.ClientInterface, componentName, applicationName string) (PushedComponent, error) {
d, err := c.GetOneDeployment(componentName, applicationName)
if err != nil {
if isIgnorableError(err) {
return nil, nil
}
return nil, err
}
storageClient := storage.NewClient(storage.ClientOptions{
Client: c,
Deployment: d,
})

return newPushedComponent(applicationName, &devfileComponent{d: *d}, c, storageClient), nil
}

func isIgnorableError(err error) bool {
for {
e := errors.Unwrap(err)
if e != nil {
err = e
} else {
break
}
}

if _, ok := err.(*kclient.DeploymentNotFoundError); ok {
return true
}
return kerrors.IsNotFound(err) || kerrors.IsForbidden(err) || kerrors.IsUnauthorized(err)

}
134 changes: 0 additions & 134 deletions pkg/devfile/adapters/kubernetes/component/adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package component

import (
"encoding/json"
"errors"
"reflect"
"testing"
"time"
Expand All @@ -12,7 +10,6 @@ import (

"github.com/devfile/library/pkg/devfile/generator"

"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/envinfo"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/preference"
Expand Down Expand Up @@ -250,137 +247,6 @@ func TestGetFirstContainerWithSourceVolume(t *testing.T) {
}
}

func TestDoesComponentExist(t *testing.T) {

tests := []struct {
name string
componentName string
appName string
getComponentName string
envInfo envinfo.EnvSpecificInfo
want bool
wantErr bool
}{
{
name: "Case 1: Valid component name",
componentName: "test-name",
appName: "app",
getComponentName: "test-name",
envInfo: envinfo.EnvSpecificInfo{},
want: true,
wantErr: false,
},
{
name: "Case 2: Non-existent component name",
componentName: "test-name",
appName: "app",
getComponentName: "fake-component",
envInfo: envinfo.EnvSpecificInfo{},
want: false,
wantErr: false,
},
{
name: "Case 3: Error condition",
componentName: "test-name",
appName: "app",
getComponentName: "test-name",
envInfo: envinfo.EnvSpecificInfo{},
want: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: func() data.DevfileData {
devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
if err != nil {
t.Error(err)
}
err = devfileData.AddComponents([]devfilev1.Component{testingutil.GetFakeContainerComponent("component")})
if err != nil {
t.Error(err)
}
err = devfileData.AddCommands([]devfilev1.Command{getExecCommand("run", devfilev1.RunCommandGroupKind)})
if err != nil {
t.Error(err)
}
return devfileData
}(),
}

adapterCtx := AdapterContext{
ComponentName: tt.componentName,
AppName: tt.appName,
Devfile: devObj,
}

fkclient, fkclientset := kclient.FakeNew()
fkWatch := watch.NewFake()

tt.envInfo.EnvInfo = *envinfo.GetFakeEnvInfo(envinfo.ComponentSettings{
Name: tt.componentName,
AppName: tt.appName,
})

fkclientset.Kubernetes.PrependReactor("patch", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) {
if patchAction, is := action.(ktesting.PatchAction); is {
patch := patchAction.GetPatch()
var deployment v1.Deployment
err := json.Unmarshal(patch, &deployment)
if err != nil {
t.Errorf("unable to parse deployment %q\n", err)
return false, nil, err
}
return true, &deployment, nil
}
return false, nil, nil
})

fkclientset.Kubernetes.PrependWatchReactor("pods", func(action ktesting.Action) (handled bool, ret watch.Interface, err error) {
return true, fkWatch, nil
})

tt.envInfo.EnvInfo = *envinfo.GetFakeEnvInfo(envinfo.ComponentSettings{
Name: tt.componentName,
AppName: tt.appName,
})

// DoesComponentExist requires an already started component, so start it.
componentAdapter := NewKubernetesAdapter(fkclient, nil, nil, adapterCtx, "")
_, err := componentAdapter.createOrUpdateComponent(false, tt.envInfo, false, libdevfile.DevfileCommands{}, 0, nil)

// Checks for unexpected error cases
if err != nil {
t.Errorf("component adapter start unexpected error %v", err)
}

fkclientset.Kubernetes.PrependReactor("list", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) {
emptyDeployment := odoTestingUtil.CreateFakeDeployment("")
deployment := odoTestingUtil.CreateFakeDeployment(tt.getComponentName)

if tt.wantErr {
return true, &v1.DeploymentList{Items: []v1.Deployment{*emptyDeployment}}, errors.New("deployment get error")
} else if tt.getComponentName == tt.componentName {
return true, &v1.DeploymentList{Items: []v1.Deployment{*deployment}}, nil
}

return true, &v1.DeploymentList{Items: []v1.Deployment{}}, nil
})

// Verify that a component with the specified name exists
componentExists, err := component.ComponentExists(fkclient, tt.getComponentName, tt.appName)
if !tt.wantErr && err != nil {
t.Errorf("unexpected error: %v", err)
} else if !tt.wantErr && componentExists != tt.want {
t.Errorf("expected %v, actual %v", tt.want, componentExists)
}

})
}

}

func TestWaitAndGetComponentPod(t *testing.T) {

testComponentName := "test"
Expand Down
Loading

0 comments on commit 59262ba

Please sign in to comment.