forked from fabric8io/kubernetes-client
/
ProjectOperationsImpl.java
176 lines (159 loc) · 6.84 KB
/
ProjectOperationsImpl.java
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
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright (C) 2015 Red Hat, 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.
*/
package io.fabric8.openshift.client.dsl.internal;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.rbac.RoleBinding;
import io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl;
import io.fabric8.openshift.api.model.ProjectBuilder;
import io.fabric8.openshift.client.dsl.ProjectOperation;
import okhttp3.OkHttpClient;
import io.fabric8.openshift.api.model.DoneableProject;
import io.fabric8.openshift.api.model.Project;
import io.fabric8.openshift.api.model.ProjectList;
import io.fabric8.openshift.client.OpenShiftConfig;
import java.util.ArrayList;
import java.util.List;
import static io.fabric8.openshift.client.OpenShiftAPIGroups.PROJECT;
public class ProjectOperationsImpl extends OpenShiftOperation<Project, ProjectList, DoneableProject,
Resource<Project, DoneableProject>> implements ProjectOperation {
public static final String OPENSHIFT_IO_DESCRIPTION_ANNOTATION = "openshift.io/description";
public static final String OPENSHIFT_IO_DISPLAY_NAME_ANNOTATION = "openshift.io/display-name";
public static final String OPENSHIFT_IO_REQUESTER_ANNOTATION = "openshift.io/requester";
public static final String RBAC_AUTHORIZATION_APIGROUP = "rbac.authorization.k8s.io";
public static final String CLUSTER_ROLE = "ClusterRole";
public ProjectOperationsImpl(OkHttpClient client, OpenShiftConfig config) {
this(new OperationContext().withOkhttpClient(client).withConfig(config));
}
public ProjectOperationsImpl(OperationContext context) {
super(context.withApiGroupName(PROJECT)
.withPlural("projects"));
this.type = Project.class;
this.listType = ProjectList.class;
this.doneableType = DoneableProject.class;
}
@Override
public ProjectOperationsImpl newInstance(OperationContext context) {
return new ProjectOperationsImpl(context);
}
@Override
public boolean isResourceNamespaced() {
return false;
}
@Override
public List<HasMetadata> createProjectAndRoleBindings(String name, String description, String displayName, String adminUser, String requestingUser) {
List<HasMetadata> result = new ArrayList<>();
Project project = initProject(name, description, displayName, requestingUser);
List<HasMetadata> projectRoleBindings = initRoleBindings(name, adminUser);
// Create Project
result.add(create(project));
// Create Role Bindings
NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl listOp = new NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(client, config, getNamespace(), null, false, false, new ArrayList<>(), projectRoleBindings, null, DeletionPropagation.BACKGROUND, true) {};
result.addAll(listOp.createOrReplace());
return result;
}
private Project initProject(String name, String description, String displayName, String requestingUser) {
return new ProjectBuilder()
.withNewMetadata()
.addToAnnotations(OPENSHIFT_IO_DESCRIPTION_ANNOTATION, description)
.addToAnnotations(OPENSHIFT_IO_DISPLAY_NAME_ANNOTATION, displayName)
.addToAnnotations(OPENSHIFT_IO_REQUESTER_ANNOTATION, requestingUser)
.withName(name)
.endMetadata()
.build();
}
private List<HasMetadata> initRoleBindings(String name, String adminUser) {
RoleBinding roleBindingPuller = new RoleBindingBuilder()
.withNewMetadata()
.addToAnnotations(OPENSHIFT_IO_DESCRIPTION_ANNOTATION, "Allows all pods in this namespace to pull images from this namespace. It is auto-managed by a controller; remove subjects to disable.")
.withName("system:image-pullers")
.withNamespace(name)
.endMetadata()
.withNewRoleRef()
.withApiGroup(RBAC_AUTHORIZATION_APIGROUP)
.withKind(CLUSTER_ROLE)
.withName("system:image-puller")
.endRoleRef()
.addNewSubject()
.withApiGroup(RBAC_AUTHORIZATION_APIGROUP)
.withKind("Group")
.withName("system:serviceaccounts:" + name)
.endSubject()
.build();
RoleBinding roleBindingBuilder = new RoleBindingBuilder()
.withNewMetadata()
.addToAnnotations(OPENSHIFT_IO_DESCRIPTION_ANNOTATION, "Allows builds in this namespace to push images to" +
"this namespace. It is auto-managed by a controller; remove subjects to disable.")
.withName("system:image-builders")
.withNamespace(name)
.endMetadata()
.withNewRoleRef()
.withApiGroup(RBAC_AUTHORIZATION_APIGROUP)
.withKind(CLUSTER_ROLE)
.withName("system:image-builder")
.endRoleRef()
.addNewSubject()
.withKind("ServiceAccount")
.withName("builder")
.withNamespace(name)
.endSubject()
.build();
RoleBinding roleBindingDeployer = new RoleBindingBuilder()
.withNewMetadata()
.addToAnnotations(OPENSHIFT_IO_DESCRIPTION_ANNOTATION, " Allows deploymentconfigs in this namespace to rollout" +
" pods in this namespace. It is auto-managed by a controller; remove subjects" +
" to disable.")
.withName("system:deployers")
.withNamespace(name)
.endMetadata()
.withNewRoleRef()
.withApiGroup(RBAC_AUTHORIZATION_APIGROUP)
.withKind(CLUSTER_ROLE)
.withName("system:deployer")
.endRoleRef()
.addNewSubject()
.withKind("ServiceAccount")
.withName("deployer")
.withNamespace(name)
.endSubject()
.build();
RoleBinding roleBindingAdmin = new RoleBindingBuilder()
.withNewMetadata()
.withName("admin")
.withNamespace(name)
.endMetadata()
.withNewRoleRef()
.withApiGroup(RBAC_AUTHORIZATION_APIGROUP)
.withKind(CLUSTER_ROLE)
.withName("admin")
.endRoleRef()
.addNewSubject()
.withApiGroup(RBAC_AUTHORIZATION_APIGROUP)
.withKind("User")
.withName(adminUser)
.endSubject()
.build();
List<HasMetadata> resources = new ArrayList<>();
resources.add(roleBindingPuller);
resources.add(roleBindingBuilder);
resources.add(roleBindingDeployer);
resources.add(roleBindingAdmin);
return resources;
}
}