...
1 package storage
2
3 import (
4 "fmt"
5
6 v1 "k8s.io/api/apps/v1"
7
8 "github.com/devfile/library/v2/pkg/devfile/parser"
9 "github.com/redhat-developer/odo/pkg/kclient"
10 "github.com/redhat-developer/odo/pkg/log"
11 )
12
13 const (
14
15 OdoSourceVolume = "odo-projects"
16
17
18 SharedDataVolumeName = "odo-shared-data"
19
20
21 SharedDataMountPath = "/opt/odo/"
22
23
24 OdoSourceVolumeSize = "2Gi"
25 )
26
27
28 type generic struct {
29 appName string
30 componentName string
31 runtime string
32 }
33
34 type ClientOptions struct {
35 Client kclient.ClientInterface
36 Deployment *v1.Deployment
37 Runtime string
38 }
39
40 type Client interface {
41 Create(Storage) error
42 Delete(string) error
43 List() (StorageList, error)
44 }
45
46
47 func NewClient(componentName string, appName string, options ClientOptions) Client {
48 var genericInfo generic
49
50 genericInfo.componentName = componentName
51 genericInfo.appName = appName
52 genericInfo.runtime = options.Runtime
53
54 return kubernetesClient{
55 generic: genericInfo,
56 client: options.Client,
57 deployment: options.Deployment,
58 }
59 }
60
61
62
63 func Push(client Client, devfileObj parser.DevfileObj) (ephemerals map[string]Storage, _ error) {
64
65 storageClusterList, err := client.List()
66 if err != nil {
67 return nil, err
68 }
69 storageClusterNames := make(map[string]Storage)
70 for _, storage := range storageClusterList.Items {
71 storageClusterNames[storage.Name] = storage
72 }
73
74
75 persistentConfigNames := make(map[string]Storage)
76
77 ephemeralConfigNames := make(map[string]Storage)
78
79 localStorage, err := ListStorage(devfileObj)
80 if err != nil {
81 return nil, err
82 }
83 for _, storage := range ConvertListLocalToMachine(localStorage).Items {
84 if storage.Spec.Ephemeral == nil || storage.Spec.Ephemeral != nil && !*storage.Spec.Ephemeral {
85 persistentConfigNames[storage.Name] = storage
86 } else {
87 ephemeralConfigNames[storage.Name] = storage
88 }
89 }
90
91
92 for storageName, storage := range storageClusterNames {
93 val, ok := persistentConfigNames[storageName]
94 if !ok {
95
96 err = client.Delete(storage.Name)
97 if err != nil {
98 return nil, err
99 }
100 log.Successf("Deleted storage %v from component", storage.Name)
101 continue
102 } else if storage.Name == val.Name {
103 if val.Spec.Size != storage.Spec.Size {
104 return nil, fmt.Errorf("config mismatch for storage with the same name %s", storage.Name)
105 }
106 }
107 }
108
109
110 for storageName, storage := range persistentConfigNames {
111 _, ok := storageClusterNames[storageName]
112 if ok {
113 continue
114 }
115 if e := client.Create(storage); e != nil {
116 return nil, e
117 }
118 log.Successf("Added storage %v to component", storage.Name)
119 }
120
121 return ephemeralConfigNames, nil
122 }
123
View as plain text