-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathnamespaceimpl.go
163 lines (129 loc) · 4.66 KB
/
namespaceimpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package namespaceservice
/*
Copyright 2019 - 2022 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"github.com/crunchydata/postgres-operator/internal/apiserver"
"github.com/crunchydata/postgres-operator/internal/ns"
msgs "github.com/crunchydata/postgres-operator/pkg/apiservermsgs"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func ShowNamespace(clientset kubernetes.Interface, username string, request *msgs.ShowNamespaceRequest) msgs.ShowNamespaceResponse {
log.Debug("ShowNamespace called")
resp := msgs.ShowNamespaceResponse{}
resp.Status = msgs.Status{Code: msgs.Ok, Msg: ""}
resp.Username = username
resp.Results = make([]msgs.NamespaceResult, 0)
//namespaceList := util.GetNamespaces()
nsList := make([]string, 0)
if request.AllFlag {
namespaceList, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
for _, v := range namespaceList.Items {
nsList = append(nsList, v.Name)
}
} else {
if len(request.Args) == 0 {
resp.Status.Code = msgs.Error
resp.Status.Msg = "namespace names or --all flag is required for this command"
return resp
}
for i := 0; i < len(request.Args); i++ {
_, err := clientset.CoreV1().Namespaces().Get(request.Args[i], metav1.GetOptions{})
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = "namespace " + request.Args[i] + " not found"
return resp
} else {
nsList = append(nsList, request.Args[i])
}
}
}
for i := 0; i < len(nsList); i++ {
iaccess, uaccess, err := apiserver.UserIsPermittedInNamespace(username, nsList[i])
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = fmt.Sprintf("Error when determining whether user [%s] is allowed "+
"access to namespace [%s]: %s", username, nsList[i], err.Error())
return resp
}
r := msgs.NamespaceResult{
Namespace: nsList[i],
InstallationAccess: iaccess,
UserAccess: uaccess,
}
resp.Results = append(resp.Results, r)
}
return resp
}
// CreateNamespace ...
func CreateNamespace(clientset kubernetes.Interface, createdBy string, request *msgs.CreateNamespaceRequest) msgs.CreateNamespaceResponse {
log.Debugf("CreateNamespace %v", request)
resp := msgs.CreateNamespaceResponse{}
resp.Status.Code = msgs.Ok
resp.Status.Msg = ""
resp.Results = make([]string, 0)
//iterate thru all the args (namespace names)
for _, namespace := range request.Args {
if err := ns.CreateNamespace(clientset, apiserver.InstallationName,
apiserver.PgoNamespace, createdBy, namespace); err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
resp.Results = append(resp.Results, "created namespace "+namespace)
}
return resp
}
// DeleteNamespace ...
func DeleteNamespace(clientset kubernetes.Interface, deletedBy string, request *msgs.DeleteNamespaceRequest) msgs.DeleteNamespaceResponse {
resp := msgs.DeleteNamespaceResponse{}
resp.Status.Code = msgs.Ok
resp.Status.Msg = ""
resp.Results = make([]string, 0)
for _, namespace := range request.Args {
err := ns.DeleteNamespace(clientset, apiserver.InstallationName, apiserver.PgoNamespace, deletedBy, namespace)
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
resp.Results = append(resp.Results, "deleted namespace "+namespace)
}
return resp
}
// UpdateNamespace ...
func UpdateNamespace(clientset kubernetes.Interface, updatedBy string, request *msgs.UpdateNamespaceRequest) msgs.UpdateNamespaceResponse {
log.Debugf("UpdateNamespace %v", request)
resp := msgs.UpdateNamespaceResponse{}
resp.Status.Code = msgs.Ok
resp.Status.Msg = ""
resp.Results = make([]string, 0)
//iterate thru all the args (namespace names)
for _, namespace := range request.Args {
if err := ns.UpdateNamespace(clientset, apiserver.InstallationName,
apiserver.PgoNamespace, updatedBy, namespace); err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
return resp
}
resp.Results = append(resp.Results, "updated namespace "+namespace)
}
return resp
}