...
1 package project
2
3 import (
4 "errors"
5 "fmt"
6
7 "github.com/redhat-developer/odo/pkg/kclient"
8 )
9
10 type kubernetesClient struct {
11 client kclient.ClientInterface
12 }
13
14 var _ Client = (*kubernetesClient)(nil)
15
16 func NewClient(client kclient.ClientInterface) Client {
17 return kubernetesClient{
18 client: client,
19 }
20 }
21
22
23 func (o kubernetesClient) SetCurrent(projectName string) error {
24 err := o.client.SetCurrentNamespace(projectName)
25 if err != nil {
26 return fmt.Errorf("unable to set current project to %s: %w", projectName, err)
27 }
28 return nil
29 }
30
31
32
33
34
35
36 func (o kubernetesClient) Create(projectName string, wait bool) error {
37 if projectName == "" {
38 return errors.New("no project name given")
39 }
40
41 projectSupport, err := o.client.IsProjectSupported()
42 if err != nil {
43 return fmt.Errorf("unable to detect project support: %w", err)
44 }
45
46 if projectSupport {
47 err = o.client.CreateNewProject(projectName, wait)
48
49 } else {
50 _, err = o.client.CreateNamespace(projectName)
51 }
52 if err != nil {
53 return fmt.Errorf("unable to create new project: %w", err)
54 }
55
56 if wait {
57 err = o.client.WaitForServiceAccountInNamespace(projectName, "default")
58 if err != nil {
59 return fmt.Errorf("unable to wait for service account: %w", err)
60 }
61 }
62 return nil
63 }
64
65
66
67 func (o kubernetesClient) Delete(projectName string, wait bool) error {
68 if projectName == "" {
69 return errors.New("no project name given")
70 }
71
72 projectSupport, err := o.client.IsProjectSupported()
73 if err != nil {
74 return fmt.Errorf("unable to detect project support: %w", err)
75 }
76
77 if projectSupport {
78 err = o.client.DeleteProject(projectName, wait)
79 } else {
80 err = o.client.DeleteNamespace(projectName, wait)
81 }
82 if err != nil {
83 return fmt.Errorf("unable to delete project %q: %w", projectName, err)
84 }
85 return nil
86 }
87
88
89 func (o kubernetesClient) List() (ProjectList, error) {
90 currentProject := o.client.GetCurrentNamespace()
91
92 projectSupport, err := o.client.IsProjectSupported()
93 if err != nil {
94 return ProjectList{}, fmt.Errorf("unable to detect project support: %w", err)
95 }
96
97 var allProjects []string
98 if projectSupport {
99 allProjects, err = o.client.ListProjectNames()
100 } else {
101 allProjects, err = o.client.GetNamespaces()
102 }
103 if err != nil {
104 return ProjectList{}, fmt.Errorf("cannot get all the projects: %w", err)
105 }
106
107 projects := make([]Project, len(allProjects))
108 for i, project := range allProjects {
109 isActive := project == currentProject
110 projects[i] = NewProject(project, isActive)
111 }
112
113 return NewProjectList(projects), nil
114 }
115
116
117 func (o kubernetesClient) Exists(projectName string) (bool, error) {
118 projectSupport, err := o.client.IsProjectSupported()
119 if err != nil {
120 return false, fmt.Errorf("unable to detect project support: %w", err)
121 }
122
123 if projectSupport {
124 project, err := o.client.GetProject(projectName)
125 if err != nil || project == nil {
126 return false, err
127 }
128 } else {
129 namespace, err := o.client.GetNamespace(projectName)
130 if err != nil || namespace == nil {
131 return false, err
132 }
133 }
134
135 return true, nil
136 }
137
View as plain text