Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds storage Push() for devfile components. #4407

Merged
merged 6 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func CreateComponent(client *occlient.Client, componentConfig config.LocalConfig
if err != nil {
return err
}
storageToBeMounted, _, err := storage.Push(client, storageList, componentConfig.GetName(), componentConfig.GetApplication(), false)
storageToBeMounted, _, err := storage.S2iPush(client, storageList, componentConfig.GetName(), componentConfig.GetApplication(), false)
if err != nil {
return err
}
Expand Down Expand Up @@ -1231,7 +1231,7 @@ func Update(client *occlient.Client, componentConfig config.LocalConfigInfo, new
if err != nil {
return err
}
storageToMount, storageToUnMount, err := storage.Push(client, storageList, componentConfig.GetName(), componentConfig.GetApplication(), true)
storageToMount, storageToUnMount, err := storage.S2iPush(client, storageList, componentConfig.GetName(), componentConfig.GetApplication(), true)
if err != nil {
return errors.Wrapf(err, "unable to get storage to mount and unmount")
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/devfile/adapters/common/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@ type ComponentAdapter interface {
Log(follow, debug bool) (io.ReadCloser, error)
Exec(command []string) error
}

// StorageAdapter defines the storage functions that platform-specific adapters must implement
type StorageAdapter interface {
Create([]Storage) error
}
11 changes: 0 additions & 11 deletions pkg/devfile/adapters/docker/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ func (a Adapter) createComponent() (err error) {
return fmt.Errorf("no valid components found in the devfile")
}

// Get the storage adapter and create the volumes if it does not exist
stoAdapter := storage.New(a.AdapterContext, a.Client)
err = stoAdapter.Create(a.uniqueStorage)
if err != nil {
return errors.Wrapf(err, "unable to create Docker storage adapter for component %s", componentName)
}

// Loop over each container component and start a container for it
for _, comp := range containerComponents {
var dockerVolumeMounts []mount.Mount
Expand Down Expand Up @@ -73,10 +66,6 @@ func (a Adapter) updateComponent() (componentExists bool, err error) {
componentExists = true
componentName := a.ComponentName

// Get the storage adapter and create the volumes if it does not exist
stoAdapter := storage.New(a.AdapterContext, a.Client)
err = stoAdapter.Create(a.uniqueStorage)

containerComponents, err := a.Devfile.Data.GetDevfileContainerComponents(parsercommon.DevfileOptions{})
if err != nil {
return false, err
Expand Down
8 changes: 0 additions & 8 deletions pkg/devfile/adapters/docker/storage/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import (
"github.com/openshift/odo/pkg/lclient"
)

// New instantiates a storage adapter
func New(adapterContext common.AdapterContext, client lclient.Client) common.StorageAdapter {
return &Adapter{
Client: client,
AdapterContext: adapterContext,
}
}

// Adapter is a storage adapter implementation for Kubernetes
type Adapter struct {
Client lclient.Client
Expand Down
100 changes: 0 additions & 100 deletions pkg/devfile/adapters/docker/storage/adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,101 +1 @@
package storage

import (
"testing"

devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
devfileParser "github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/testingutil"
"github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/lclient"
)

func TestCreate(t *testing.T) {

testComponentName := "test"
fakeClient := lclient.FakeNew()
fakeErrorClient := lclient.FakeErrorNew()

volNames := [...]string{"vol1", "vol2"}
volSize := "5Gi"

tests := []struct {
name string
storage []common.Storage
client *lclient.Client
wantErr bool
}{
{
name: "Case 1: No volumes",
storage: nil,
client: fakeClient,
wantErr: false,
},
{
name: "Case 2: Multiple volumes",
storage: []common.Storage{
{
Name: "vol1",
Volume: common.DevfileVolume{
Name: volNames[0],
Size: volSize,
},
},
{
Name: "vol2",
Volume: common.DevfileVolume{
Name: volNames[1],
Size: volSize,
},
},
},
client: fakeClient,
wantErr: false,
},
{
name: "Case 3: Docker client error",
storage: []common.Storage{
{
Name: "vol1",
Volume: common.DevfileVolume{
Name: volNames[0],
Size: volSize,
},
},
{
Name: "vol2",
Volume: common.DevfileVolume{
Name: volNames[1],
Size: volSize,
},
},
},
client: fakeErrorClient,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: &testingutil.TestDevfileData{
Components: []devfilev1.Component{},
},
}

adapterCtx := common.AdapterContext{
ComponentName: testComponentName,
Devfile: devObj,
}

storageAdapter := New(adapterCtx, *tt.client)
// ToDo: Add more meaningful unit tests once Push actually does something with its parameters
err := storageAdapter.Create(tt.storage)

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

}
130 changes: 55 additions & 75 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ import (
devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
parsercommon "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/preference"

"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/devfile/adapters/kubernetes/storage"
"github.com/openshift/odo/pkg/devfile/adapters/kubernetes/utils"
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/occlient"
odoutil "github.com/openshift/odo/pkg/odo/util"
storagepkg "github.com/openshift/odo/pkg/storage"
storagelabels "github.com/openshift/odo/pkg/storage/labels"
"github.com/openshift/odo/pkg/sync"
kerrors "k8s.io/apimachinery/pkg/api/errors"
)
Expand Down Expand Up @@ -166,7 +167,7 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
return errors.Wrap(err, "unable to create or update component")
}

_, err = a.Client.WaitForDeploymentRollout(a.ComponentName)
deployment, err := a.Client.WaitForDeploymentRollout(a.ComponentName)
if err != nil {
return errors.Wrap(err, "error while waiting for deployment rollout")
}
Expand All @@ -177,6 +178,23 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
return errors.Wrapf(err, "unable to get pod for component %s", a.ComponentName)
}

// list the latest state of the PVCs
pvcs, err := a.Client.ListPVCs(fmt.Sprintf("%v=%v", "component", a.ComponentName))
if err != nil {
return err
}

// update the owner reference of the PVCs with the deployment
for i := range pvcs {
if pvcs[i].OwnerReferences != nil || pvcs[i].DeletionTimestamp != nil {
continue
}
err = a.Client.UpdateStorageOwnerReference(&pvcs[i], generator.GetOwnerReference(deployment))
if err != nil {
return err
}
}

parameters.EnvSpecificInfo.SetDevfileObj(a.Devfile)
err = component.ApplyConfig(nil, &a.Client, config.LocalConfigInfo{}, parameters.EnvSpecificInfo, color.Output, componentExists, false)
if err != nil {
Expand Down Expand Up @@ -267,6 +285,29 @@ func (a Adapter) DoesComponentExist(cmpName string) (bool, error) {
}

func (a Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSpecificInfo) (err error) {
ei.SetDevfileObj(a.Devfile)
ocClient, err := occlient.New()
if err != nil {
return err
}
ocClient.SetKubeClient(&a.Client)

storageClient := storagepkg.NewClient(storagepkg.ClientOptions{
OCClient: *ocClient,
LocalConfigProvider: &ei,
})

// handle the ephemeral storage
err = storage.HandleEphemeralStorage(a.Client, storageClient, a.ComponentName)
if err != nil {
return err
}

err = storagepkg.Push(storageClient, &ei)
if err != nil {
return err
}

componentName := a.ComponentName

componentType := strings.TrimSuffix(a.AdapterContext.Devfile.Data.GetMetadata().Name, "-")
Expand Down Expand Up @@ -311,77 +352,23 @@ func (a Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSpe
return err
}

pref, err := preference.New()
if err != nil {
return err
}

if !pref.GetEphemeralSourceVolume() {

// If ephemeral volume is false, then we need to add to source volume in the map to create pvc.
containerNameToVolumes["odosource"] = []common.DevfileVolume{
{
Name: utils.OdoSourceVolume,
Size: utils.OdoSourceVolumeSize,
},
}
}

var odoSourcePVCName string

var uniqueStorages []common.Storage
volumeNameToPVCName := make(map[string]string)
processedVolumes := make(map[string]bool)

// Get a list of all the unique volume names and generate their PVC names
// we do not use the volume components which are unique here because
// not all volume components maybe referenced by a container component.
// We only want to create PVCs which are going to be used by a container
for _, volumes := range containerNameToVolumes {
for _, vol := range volumes {
if _, ok := processedVolumes[vol.Name]; !ok {
processedVolumes[vol.Name] = true

// Generate the PVC Names
klog.V(2).Infof("Generating PVC name for %v", vol.Name)
generatedPVCName, err := storage.GeneratePVCNameFromDevfileVol(vol.Name, componentName)
if err != nil {
return err
}

// Check if we have an existing PVC with the labels, overwrite the generated name with the existing name if present
existingPVCName, err := storage.GetExistingPVC(&a.Client, vol.Name, componentName)
if err != nil {
return err
}
if len(existingPVCName) > 0 {
klog.V(2).Infof("Found an existing PVC for %v, PVC %v will be re-used", vol.Name, existingPVCName)
generatedPVCName = existingPVCName
}

if vol.Name == utils.OdoSourceVolume {
odoSourcePVCName = generatedPVCName
}

pvc := common.Storage{
Name: generatedPVCName,
Volume: vol,
}
uniqueStorages = append(uniqueStorages, pvc)
volumeNameToPVCName[vol.Name] = generatedPVCName
}
}
}

err = storage.DeleteOldPVCs(&a.Client, componentName, processedVolumes)
// list all the pvcs for the component
pvcs, err := a.Client.ListPVCs(fmt.Sprintf("%v=%v", "component", a.ComponentName))
if err != nil {
return err
}

// remove odo source volume from these maps as we do not want to pass source volume in GetPVCAndVolumeMount
// we are mounting odo source volume seperately
delete(volumeNameToPVCName, utils.OdoSourceVolume)
delete(containerNameToVolumes, "odosource")
for _, pvc := range pvcs {
// check if the pvc is in the terminating state
if pvc.DeletionTimestamp != nil {
continue
}
volumeNameToPVCName[pvc.Labels[storagelabels.StorageLabel]] = pvc.Name
}

// Get PVC volumes and Volume Mounts
containers, pvcVolumes, err := storage.GetPVCAndVolumeMount(containers, volumeNameToPVCName, containerNameToVolumes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so i was looking to extract this logic out, so that the library can return the updated containers with volume mounts and the pvc volumes after reading the devfile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only modified the network calls in this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, after this merged, I can look to consolidate this in devfile/library call; so anyone calling devfile/library generator func like generator.GetVolume() will return the pvcVolumes and updated container volumeMounts 👍

Expand Down Expand Up @@ -420,7 +407,7 @@ func (a Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSpe
if componentExists {
// If the component already exists, get the resource version of the deploy before updating
klog.V(2).Info("The component already exists, attempting to update it")
deployment, err := a.Client.UpdateDeployment(*deployment)
deployment, err = a.Client.UpdateDeployment(*deployment)
if err != nil {
return err
}
Expand Down Expand Up @@ -454,7 +441,7 @@ func (a Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSpe
}
}
} else {
deployment, err := a.Client.CreateDeployment(*deployment)
deployment, err = a.Client.CreateDeployment(*deployment)
if err != nil {
return err
}
Expand All @@ -471,13 +458,6 @@ func (a Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSpe

}

// Get the storage adapter and create the volumes if it does not exist
stoAdapter := storage.New(a.AdapterContext, a.Client)
err = stoAdapter.Create(uniqueStorages)
if err != nil {
return err
}
maysunfaisal marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

Expand Down
Loading