custom_metrics_deployments.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package monitoring
  14. import (
  15. "fmt"
  16. "os/exec"
  17. "strings"
  18. gcm "google.golang.org/api/monitoring/v3"
  19. appsv1 "k8s.io/api/apps/v1"
  20. corev1 "k8s.io/api/core/v1"
  21. rbac "k8s.io/api/rbac/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  25. )
  26. var (
  27. // CustomMetricName is the metrics name used in test cases.
  28. CustomMetricName = "foo"
  29. // UnusedMetricName is the unused metrics name used in test cases.
  30. UnusedMetricName = "unused"
  31. // CustomMetricValue is the value for CustomMetricName.
  32. CustomMetricValue = int64(448)
  33. // UnusedMetricValue is the value for UnusedMetricName.
  34. UnusedMetricValue = int64(446)
  35. // StackdriverExporter is exporter name.
  36. StackdriverExporter = "stackdriver-exporter"
  37. // HPAPermissions is a ClusterRoleBinding that grants unauthenticated user permissions granted for
  38. // HPA for testing purposes, i.e. it should grant permission to read custom metrics.
  39. HPAPermissions = &rbac.ClusterRoleBinding{
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: "custom-metrics-reader",
  42. },
  43. RoleRef: rbac.RoleRef{
  44. APIGroup: "rbac.authorization.k8s.io",
  45. Kind: "ClusterRole",
  46. Name: "system:controller:horizontal-pod-autoscaler",
  47. },
  48. Subjects: []rbac.Subject{
  49. {
  50. APIGroup: "rbac.authorization.k8s.io",
  51. Kind: "Group",
  52. Name: "system:unauthenticated",
  53. },
  54. },
  55. }
  56. // StagingDeploymentsLocation is the location where the adapter deployment files are stored.
  57. StagingDeploymentsLocation = "https://raw.githubusercontent.com/GoogleCloudPlatform/k8s-stackdriver/master/custom-metrics-stackdriver-adapter/deploy/staging/"
  58. // AdapterForOldResourceModel is file name for the old resource model.
  59. AdapterForOldResourceModel = "adapter_old_resource_model.yaml"
  60. // AdapterForNewResourceModel is file name for the new resource model.
  61. AdapterForNewResourceModel = "adapter_new_resource_model.yaml"
  62. // AdapterDefault is the default model.
  63. AdapterDefault = AdapterForOldResourceModel
  64. // ClusterAdminBinding is the cluster rolebinding name for test cases.
  65. ClusterAdminBinding = "e2e-test-cluster-admin-binding"
  66. )
  67. // CustomMetricContainerSpec allows to specify a config for StackdriverExporterDeployment
  68. // with multiple containers exporting different metrics.
  69. type CustomMetricContainerSpec struct {
  70. Name string
  71. MetricName string
  72. MetricValue int64
  73. }
  74. // SimpleStackdriverExporterDeployment is a Deployment of simple application that exports a metric of
  75. // fixed value to Stackdriver in a loop.
  76. func SimpleStackdriverExporterDeployment(name, namespace string, replicas int32, metricValue int64) *appsv1.Deployment {
  77. return StackdriverExporterDeployment(name, namespace, replicas,
  78. []CustomMetricContainerSpec{
  79. {
  80. Name: StackdriverExporter,
  81. MetricName: CustomMetricName,
  82. MetricValue: metricValue,
  83. },
  84. })
  85. }
  86. // StackdriverExporterDeployment is a Deployment of an application that can expose
  87. // an arbitrary amount of metrics of fixed value to Stackdriver in a loop. Each metric
  88. // is exposed by a different container in one pod.
  89. // The metric names and values are configured via the containers parameter.
  90. func StackdriverExporterDeployment(name, namespace string, replicas int32, containers []CustomMetricContainerSpec) *appsv1.Deployment {
  91. podSpec := corev1.PodSpec{Containers: []corev1.Container{}}
  92. for _, containerSpec := range containers {
  93. podSpec.Containers = append(podSpec.Containers, stackdriverExporterContainerSpec(containerSpec.Name, namespace, containerSpec.MetricName, containerSpec.MetricValue))
  94. }
  95. return &appsv1.Deployment{
  96. ObjectMeta: metav1.ObjectMeta{
  97. Name: name,
  98. Namespace: namespace,
  99. },
  100. Spec: appsv1.DeploymentSpec{
  101. Selector: &metav1.LabelSelector{
  102. MatchLabels: map[string]string{"name": name},
  103. },
  104. Template: corev1.PodTemplateSpec{
  105. ObjectMeta: metav1.ObjectMeta{
  106. Labels: map[string]string{
  107. "name": name,
  108. },
  109. },
  110. Spec: podSpec,
  111. },
  112. Replicas: &replicas,
  113. },
  114. }
  115. }
  116. // StackdriverExporterPod is a Pod of simple application that exports a metric of fixed value to
  117. // Stackdriver in a loop.
  118. func StackdriverExporterPod(podName, namespace, podLabel, metricName string, metricValue int64) *corev1.Pod {
  119. return &corev1.Pod{
  120. ObjectMeta: metav1.ObjectMeta{
  121. Name: podName,
  122. Namespace: namespace,
  123. Labels: map[string]string{
  124. "name": podLabel,
  125. },
  126. },
  127. Spec: corev1.PodSpec{
  128. Containers: []corev1.Container{stackdriverExporterContainerSpec(StackdriverExporter, namespace, metricName, metricValue)},
  129. },
  130. }
  131. }
  132. func stackdriverExporterContainerSpec(name string, namespace string, metricName string, metricValue int64) corev1.Container {
  133. return corev1.Container{
  134. Name: name,
  135. Image: "k8s.gcr.io/sd-dummy-exporter:v0.2.0",
  136. ImagePullPolicy: corev1.PullPolicy("Always"),
  137. Command: []string{
  138. "/bin/sh",
  139. "-c",
  140. strings.Join([]string{
  141. "./sd_dummy_exporter",
  142. "--pod-id=$(POD_ID)",
  143. "--pod-name=$(POD_NAME)",
  144. "--namespace=" + namespace,
  145. "--metric-name=" + metricName,
  146. fmt.Sprintf("--metric-value=%v", metricValue),
  147. "--use-old-resource-model",
  148. "--use-new-resource-model",
  149. }, " "),
  150. },
  151. Env: []corev1.EnvVar{
  152. {
  153. Name: "POD_ID",
  154. ValueFrom: &corev1.EnvVarSource{
  155. FieldRef: &corev1.ObjectFieldSelector{
  156. FieldPath: "metadata.uid",
  157. },
  158. },
  159. },
  160. {
  161. Name: "POD_NAME",
  162. ValueFrom: &corev1.EnvVarSource{
  163. FieldRef: &corev1.ObjectFieldSelector{
  164. FieldPath: "metadata.name",
  165. },
  166. },
  167. },
  168. },
  169. Ports: []corev1.ContainerPort{{ContainerPort: 80}},
  170. }
  171. }
  172. // PrometheusExporterDeployment is a Deployment of simple application with two containers
  173. // one exposing a metric in prometheus format and second a prometheus-to-sd container
  174. // that scrapes the metric and pushes it to stackdriver.
  175. func PrometheusExporterDeployment(name, namespace string, replicas int32, metricValue int64) *appsv1.Deployment {
  176. return &appsv1.Deployment{
  177. ObjectMeta: metav1.ObjectMeta{
  178. Name: name,
  179. Namespace: namespace,
  180. },
  181. Spec: appsv1.DeploymentSpec{
  182. Selector: &metav1.LabelSelector{
  183. MatchLabels: map[string]string{"name": name},
  184. },
  185. Template: corev1.PodTemplateSpec{
  186. ObjectMeta: metav1.ObjectMeta{
  187. Labels: map[string]string{
  188. "name": name,
  189. },
  190. },
  191. Spec: prometheusExporterPodSpec(CustomMetricName, metricValue, 8080),
  192. },
  193. Replicas: &replicas,
  194. },
  195. }
  196. }
  197. func prometheusExporterPodSpec(metricName string, metricValue int64, port int32) corev1.PodSpec {
  198. return corev1.PodSpec{
  199. Containers: []corev1.Container{
  200. {
  201. Name: "prometheus-exporter",
  202. Image: "k8s.gcr.io/prometheus-dummy-exporter:v0.1.0",
  203. ImagePullPolicy: corev1.PullPolicy("Always"),
  204. Command: []string{"/prometheus_dummy_exporter", "--metric-name=" + metricName,
  205. fmt.Sprintf("--metric-value=%v", metricValue), fmt.Sprintf("=--port=%d", port)},
  206. Ports: []corev1.ContainerPort{{ContainerPort: port}},
  207. },
  208. {
  209. Name: "prometheus-to-sd",
  210. Image: "k8s.gcr.io/prometheus-to-sd:v0.5.0",
  211. ImagePullPolicy: corev1.PullPolicy("Always"),
  212. Command: []string{"/monitor", fmt.Sprintf("--source=:http://localhost:%d", port),
  213. "--stackdriver-prefix=custom.googleapis.com", "--pod-id=$(POD_ID)", "--namespace-id=$(POD_NAMESPACE)"},
  214. Env: []corev1.EnvVar{
  215. {
  216. Name: "POD_ID",
  217. ValueFrom: &corev1.EnvVarSource{
  218. FieldRef: &corev1.ObjectFieldSelector{
  219. FieldPath: "metadata.uid",
  220. },
  221. },
  222. },
  223. {
  224. Name: "POD_NAMESPACE",
  225. ValueFrom: &corev1.EnvVarSource{
  226. FieldRef: &corev1.ObjectFieldSelector{
  227. FieldPath: "metadata.namespace",
  228. },
  229. },
  230. },
  231. },
  232. },
  233. },
  234. }
  235. }
  236. // CreateAdapter creates Custom Metrics - Stackdriver adapter
  237. // adapterDeploymentFile should be a filename for adapter deployment located in StagingDeploymentLocation
  238. func CreateAdapter(adapterDeploymentFile string) error {
  239. // A workaround to make the work on GKE. GKE doesn't normally allow to create cluster roles,
  240. // which the adapter deployment does. The solution is to create cluster role binding for
  241. // cluster-admin role and currently used service account.
  242. err := createClusterAdminBinding()
  243. if err != nil {
  244. return err
  245. }
  246. adapterURL := StagingDeploymentsLocation + adapterDeploymentFile
  247. err = exec.Command("wget", adapterURL).Run()
  248. if err != nil {
  249. return err
  250. }
  251. stat, err := framework.RunKubectl("create", "-f", adapterURL)
  252. e2elog.Logf(stat)
  253. return err
  254. }
  255. func createClusterAdminBinding() error {
  256. stdout, stderr, err := framework.RunCmd("gcloud", "config", "get-value", "core/account")
  257. if err != nil {
  258. e2elog.Logf(stderr)
  259. return err
  260. }
  261. serviceAccount := strings.TrimSpace(stdout)
  262. e2elog.Logf("current service account: %q", serviceAccount)
  263. stat, err := framework.RunKubectl("create", "clusterrolebinding", ClusterAdminBinding, "--clusterrole=cluster-admin", "--user="+serviceAccount)
  264. e2elog.Logf(stat)
  265. return err
  266. }
  267. // CreateDescriptors creates descriptors for metrics: CustomMetricName and UnusedMetricName.
  268. func CreateDescriptors(service *gcm.Service, projectID string) error {
  269. _, err := service.Projects.MetricDescriptors.Create(fmt.Sprintf("projects/%s", projectID), &gcm.MetricDescriptor{
  270. Name: CustomMetricName,
  271. ValueType: "INT64",
  272. Type: "custom.googleapis.com/" + CustomMetricName,
  273. MetricKind: "GAUGE",
  274. }).Do()
  275. if err != nil {
  276. return err
  277. }
  278. _, err = service.Projects.MetricDescriptors.Create(fmt.Sprintf("projects/%s", projectID), &gcm.MetricDescriptor{
  279. Name: UnusedMetricName,
  280. ValueType: "INT64",
  281. Type: "custom.googleapis.com/" + UnusedMetricName,
  282. MetricKind: "GAUGE",
  283. }).Do()
  284. return err
  285. }
  286. // CleanupDescriptors deletes descriptors for metrics: CustomMetricName and UnusedMetricName.
  287. // TODO: Cleanup time series as well
  288. func CleanupDescriptors(service *gcm.Service, projectID string) {
  289. _, err := service.Projects.MetricDescriptors.Delete(fmt.Sprintf("projects/%s/metricDescriptors/custom.googleapis.com/%s", projectID, CustomMetricName)).Do()
  290. if err != nil {
  291. e2elog.Logf("Failed to delete descriptor for metric '%s': %v", CustomMetricName, err)
  292. }
  293. _, err = service.Projects.MetricDescriptors.Delete(fmt.Sprintf("projects/%s/metricDescriptors/custom.googleapis.com/%s", projectID, UnusedMetricName)).Do()
  294. if err != nil {
  295. e2elog.Logf("Failed to delete descriptor for metric '%s': %v", CustomMetricName, err)
  296. }
  297. }
  298. // CleanupAdapter deletes Custom Metrics - Stackdriver adapter deployments.
  299. func CleanupAdapter(adapterDeploymentFile string) {
  300. stat, err := framework.RunKubectl("delete", "-f", adapterDeploymentFile)
  301. e2elog.Logf(stat)
  302. if err != nil {
  303. e2elog.Logf("Failed to delete adapter deployments: %s", err)
  304. }
  305. err = exec.Command("rm", adapterDeploymentFile).Run()
  306. if err != nil {
  307. e2elog.Logf("Failed to delete adapter deployment file: %s", err)
  308. }
  309. cleanupClusterAdminBinding()
  310. }
  311. func cleanupClusterAdminBinding() {
  312. stat, err := framework.RunKubectl("delete", "clusterrolebinding", ClusterAdminBinding)
  313. e2elog.Logf(stat)
  314. if err != nil {
  315. e2elog.Logf("Failed to delete cluster admin binding: %s", err)
  316. }
  317. }