29
29
import java .util .Arrays ;
30
30
import java .util .Collections ;
31
31
import java .util .List ;
32
+ import java .util .Locale ;
32
33
import java .util .Optional ;
33
34
import java .util .function .Predicate ;
34
35
import java .util .logging .Level ;
35
36
import java .util .logging .Logger ;
37
+ import java .util .regex .Pattern ;
36
38
import java .util .stream .Collectors ;
37
39
import jenkins .model .Jenkins ;
40
+ import org .apache .commons .lang .RandomStringUtils ;
38
41
import org .apache .commons .lang .StringUtils ;
39
42
import org .csanchez .jenkins .plugins .kubernetes .pipeline .PodTemplateStepExecution ;
40
43
@@ -43,6 +46,9 @@ private PodUtils() {}
43
46
44
47
private static final Logger LOGGER = Logger .getLogger (PodUtils .class .getName ());
45
48
49
+ private static final Pattern NAME_PATTERN =
50
+ Pattern .compile ("[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\ .[a-z0-9]([-a-z0-9]*[a-z0-9])?)*" );
51
+
46
52
public static final Predicate <ContainerStatus > CONTAINER_IS_TERMINATED =
47
53
cs -> cs .getState ().getTerminated () != null ;
48
54
public static final Predicate <ContainerStatus > CONTAINER_IS_WAITING =
@@ -57,15 +63,27 @@ public static List<ContainerStatus> getWaitingContainers(Pod pod) {
57
63
return getContainers (pod , CONTAINER_IS_WAITING );
58
64
}
59
65
60
- public static List <ContainerStatus > getContainerStatus (Pod pod ) {
66
+ /**
67
+ * Get all container statuses (does not include ephemeral or init containers).
68
+ * @param pod pod to get container statuses for
69
+ * @return list of statuses, possibly empty, never null
70
+ */
71
+ @ NonNull
72
+ public static List <ContainerStatus > getContainerStatus (@ NonNull Pod pod ) {
61
73
PodStatus podStatus = pod .getStatus ();
62
74
if (podStatus == null ) {
63
75
return Collections .emptyList ();
64
76
}
65
77
return podStatus .getContainerStatuses ();
66
78
}
67
79
68
- public static List <ContainerStatus > getContainers (Pod pod , Predicate <ContainerStatus > predicate ) {
80
+ /**
81
+ * Get pod container statuses (does not include ephemeral or init containers) that match the given filter.
82
+ * @param pod pod to get container statuses for
83
+ * @param predicate container status predicate
84
+ * @return list of statuses, possibly empty, never null
85
+ */
86
+ public static List <ContainerStatus > getContainers (@ NonNull Pod pod , @ NonNull Predicate <ContainerStatus > predicate ) {
69
87
return getContainerStatus (pod ).stream ().filter (predicate ).collect (Collectors .toList ());
70
88
}
71
89
@@ -182,4 +200,39 @@ public static String logLastLines(@NonNull Pod pod, @NonNull KubernetesClient cl
182
200
}
183
201
return Util .fixEmpty (sb .toString ());
184
202
}
203
+
204
+ /**
205
+ * Generate a random string to be used as the suffix for dynamic resource names.
206
+ * @return random string suitable for kubernetes resources
207
+ */
208
+ @ NonNull
209
+ public static String generateRandomSuffix () {
210
+ return RandomStringUtils .random (5 , "bcdfghjklmnpqrstvwxz0123456789" );
211
+ }
212
+
213
+ /**
214
+ * Create kubernetes resource name with a random suffix appended to the given base name. This method
215
+ * performs some basic transforms to make the base name compliant (i.e. spaces and underscores). The
216
+ * returned string will also be truncated to a max of 63 characters.
217
+ * @param name base name to append to
218
+ * @return resource name with random suffix and maximum length of 63 characters
219
+ */
220
+ @ NonNull
221
+ public static String createNameWithRandomSuffix (@ NonNull String name ) {
222
+ String suffix = generateRandomSuffix ();
223
+ // no spaces
224
+ name = name .replaceAll ("[ _]" , "-" ).toLowerCase (Locale .getDefault ());
225
+ // keep it under 63 chars (62 is used to account for the '-')
226
+ name = name .substring (0 , Math .min (name .length (), 62 - suffix .length ()));
227
+ return String .join ("-" , name , suffix );
228
+ }
229
+
230
+ /**
231
+ * Check if the given name is a valid pod resource name. Does not validate string length.
232
+ * @param name name to check
233
+ * @return true if the given string contains valid pod resource name characters
234
+ */
235
+ public static boolean isValidName (@ NonNull String name ) {
236
+ return PodUtils .NAME_PATTERN .matcher (name ).matches ();
237
+ }
185
238
}
0 commit comments