|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "os/exec" |
| 7 | + |
| 8 | + internaltesting "github.com/app-sre/deployment-validation-operator/internal/testing" |
| 9 | + io_prometheus_client "github.com/prometheus/client_model/go" |
| 10 | + |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + . "github.com/onsi/gomega/gexec" |
| 14 | + appsv1 "k8s.io/api/apps/v1" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | +) |
| 18 | + |
| 19 | +var ErrNilMetric = errors.New("nil metric") |
| 20 | + |
| 21 | +var _ = Describe("deployments", func() { |
| 22 | + var ( |
| 23 | + ctx context.Context |
| 24 | + cancel context.CancelFunc |
| 25 | + namespace string |
| 26 | + namespaceGen = nameGenerator("deployment-test-namespace") |
| 27 | + ) |
| 28 | + |
| 29 | + prom := internaltesting.NewPromClient() |
| 30 | + |
| 31 | + BeforeEach(func() { |
| 32 | + ctx, cancel = context.WithCancel(context.Background()) |
| 33 | + |
| 34 | + namespace = namespaceGen() |
| 35 | + |
| 36 | + By("Starting manager") |
| 37 | + |
| 38 | + manager := exec.Command(_binPath, |
| 39 | + "--kubeconfig", _kubeConfigPath, |
| 40 | + ) |
| 41 | + |
| 42 | + manager.Env = []string{ |
| 43 | + "WATCH_NAMESPACE=" + namespace, |
| 44 | + } |
| 45 | + |
| 46 | + session, err := Start(manager, GinkgoWriter, GinkgoWriter) |
| 47 | + Expect(err).ToNot(HaveOccurred()) |
| 48 | + |
| 49 | + By("Creating the watch namespace") |
| 50 | + |
| 51 | + ns := newNamespace(namespace) |
| 52 | + |
| 53 | + _client.Create(ctx, &ns) |
| 54 | + |
| 55 | + rbac, err := getRBAC(namespace, managerGroup) |
| 56 | + Expect(err).ToNot(HaveOccurred()) |
| 57 | + |
| 58 | + for _, obj := range rbac { |
| 59 | + _client.Create(ctx, obj) |
| 60 | + } |
| 61 | + |
| 62 | + DeferCleanup(func() { |
| 63 | + cancel() |
| 64 | + |
| 65 | + By("Stopping the managers") |
| 66 | + |
| 67 | + session.Interrupt() |
| 68 | + |
| 69 | + if usingExistingCluster() { |
| 70 | + By("Deleting watch namspace") |
| 71 | + |
| 72 | + _client.Delete(ctx, &ns) |
| 73 | + } |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + When("created with missing resource requirements", func() { |
| 78 | + It("should generate metrics", func() { |
| 79 | + _client.Create(ctx, newDeployment("test", namespace)) |
| 80 | + |
| 81 | + var ( |
| 82 | + deploymentMemReqMetric *io_prometheus_client.Metric |
| 83 | + deploymentCPUReqMetric *io_prometheus_client.Metric |
| 84 | + ) |
| 85 | + |
| 86 | + Eventually(func() error { |
| 87 | + metrics, err := prom.GetDVOMetrics(ctx, "http://localhost:8383/metrics") |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + memReqMetrics := metrics["unset_memory_requirements"] |
| 93 | + deploymentMemReqMetric = searchableMetrics( |
| 94 | + memReqMetrics, |
| 95 | + ).FindDVOMetric("Deployment", "test", namespace) |
| 96 | + if deploymentMemReqMetric == nil { |
| 97 | + return ErrNilMetric |
| 98 | + } |
| 99 | + |
| 100 | + cpuReqMetrics := metrics["unset_cpu_requirements"] |
| 101 | + deploymentCPUReqMetric = searchableMetrics( |
| 102 | + cpuReqMetrics, |
| 103 | + ).FindDVOMetric("Deployment", "test", namespace) |
| 104 | + if deploymentCPUReqMetric == nil { |
| 105 | + return ErrNilMetric |
| 106 | + } |
| 107 | + |
| 108 | + return nil |
| 109 | + }, "10s").Should(Succeed()) |
| 110 | + |
| 111 | + Expect(deploymentMemReqMetric.Gauge.GetValue()).To(Equal(float64(1))) |
| 112 | + Expect(deploymentCPUReqMetric.Gauge.GetValue()).To(Equal(float64(1))) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | +}) |
| 117 | + |
| 118 | +func newNamespace(name string) corev1.Namespace { |
| 119 | + return corev1.Namespace{ |
| 120 | + ObjectMeta: metav1.ObjectMeta{ |
| 121 | + Name: name, |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func newDeployment(name, namespace string) *appsv1.Deployment { |
| 127 | + return &appsv1.Deployment{ |
| 128 | + ObjectMeta: metav1.ObjectMeta{ |
| 129 | + Name: name, |
| 130 | + Namespace: namespace, |
| 131 | + Labels: map[string]string{ |
| 132 | + "app": "test", |
| 133 | + }, |
| 134 | + }, |
| 135 | + Spec: appsv1.DeploymentSpec{ |
| 136 | + Replicas: int32Ptr(1), |
| 137 | + Selector: &metav1.LabelSelector{ |
| 138 | + MatchLabels: map[string]string{ |
| 139 | + "app": "test", |
| 140 | + }, |
| 141 | + }, |
| 142 | + Template: corev1.PodTemplateSpec{ |
| 143 | + ObjectMeta: metav1.ObjectMeta{ |
| 144 | + Labels: map[string]string{ |
| 145 | + "app": "test", |
| 146 | + }, |
| 147 | + }, |
| 148 | + Spec: corev1.PodSpec{ |
| 149 | + Containers: []corev1.Container{ |
| 150 | + { |
| 151 | + Name: "test", |
| 152 | + Image: "test", |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func int32Ptr(i int32) *int32 { return &i } |
| 162 | + |
| 163 | +type searchableMetrics []*io_prometheus_client.Metric |
| 164 | + |
| 165 | +func (ms searchableMetrics) FindDVOMetric(kind, name, namespace string) *io_prometheus_client.Metric { |
| 166 | + for _, m := range ms { |
| 167 | + labels := searchableLables(m.GetLabel()) |
| 168 | + |
| 169 | + if labels.Contains("kind", kind) && |
| 170 | + labels.Contains("name", name) && |
| 171 | + labels.Contains("namespace", namespace) { |
| 172 | + return m |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +type searchableLables []*io_prometheus_client.LabelPair |
| 180 | + |
| 181 | +func (ls searchableLables) Contains(name, val string) bool { |
| 182 | + for _, l := range ls { |
| 183 | + if l.GetName() != name { |
| 184 | + continue |
| 185 | + } |
| 186 | + |
| 187 | + if l.GetValue() == val { |
| 188 | + return true |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return false |
| 193 | +} |
0 commit comments