1- // Copyright 2017, 2018 , Oracle Corporation and/or its affiliates. All rights reserved.
1+ // Copyright 2017, 2019 , Oracle Corporation and/or its affiliates. All rights reserved.
22// Licensed under the Universal Permissive License v 1.0 as shown at
33// http://oss.oracle.com/licenses/upl.
44
55package oracle .kubernetes .operator .helpers ;
66
77import io .kubernetes .client .models .V1ObjectMeta ;
8+ import io .kubernetes .client .models .V1Pod ;
9+ import io .kubernetes .client .util .Yaml ;
10+ import java .util .function .Function ;
11+ import org .apache .commons .codec .digest .DigestUtils ;
812
913/** Annotates pods, services with details about the Domain instance and checks these annotations. */
1014public class AnnotationHelper {
15+ private static final boolean DEBUG = false ;
16+ static final String SHA256_ANNOTATION = "weblogic.sha256" ;
17+ private static final String HASHED_STRING = "hashedString" ;
18+ private static Function <V1Pod , String > HASH_FUNCTION =
19+ pod -> DigestUtils .sha256Hex (Yaml .dump (pod ));
20+
1121 /**
1222 * Marks metadata with annotations that let Prometheus know how to retrieve metrics from the
1323 * wls-exporter web-app. The specified httpPort should be the listen port of the WebLogic server
@@ -16,10 +26,34 @@ public class AnnotationHelper {
1626 * @param meta Metadata
1727 * @param httpPort HTTP listen port
1828 */
19- public static void annotateForPrometheus (V1ObjectMeta meta , int httpPort ) {
29+ static void annotateForPrometheus (V1ObjectMeta meta , int httpPort ) {
2030 meta .putAnnotationsItem (
2131 "prometheus.io/port" , "" + httpPort ); // should be the ListenPort of the server in the pod
2232 meta .putAnnotationsItem ("prometheus.io/path" , "/wls-exporter/metrics" );
2333 meta .putAnnotationsItem ("prometheus.io/scrape" , "true" );
2434 }
35+
36+ static V1Pod withSha256Hash (V1Pod pod ) {
37+ return DEBUG ? addHashAndDebug (pod ) : addHash (pod );
38+ }
39+
40+ private static V1Pod addHashAndDebug (V1Pod pod ) {
41+ String dump = Yaml .dump (pod );
42+ addHash (pod );
43+ pod .getMetadata ().putAnnotationsItem (HASHED_STRING , dump );
44+ return pod ;
45+ }
46+
47+ private static V1Pod addHash (V1Pod pod ) {
48+ pod .getMetadata ().putAnnotationsItem (SHA256_ANNOTATION , HASH_FUNCTION .apply (pod ));
49+ return pod ;
50+ }
51+
52+ static String getHash (V1Pod pod ) {
53+ return pod .getMetadata ().getAnnotations ().get (SHA256_ANNOTATION );
54+ }
55+
56+ static String getDebugString (V1Pod pod ) {
57+ return pod .getMetadata ().getAnnotations ().get (HASHED_STRING );
58+ }
2559}
0 commit comments