Skip to content

Commit

Permalink
Merge pull request #10 from chaosnative/sync-upstream
Browse files Browse the repository at this point in the history
Sync the upstream changes for auth and tls
  • Loading branch information
Jonsy13 authored Sep 5, 2023
2 parents 96471e7 + d8f6b60 commit 7252fa6
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 65 deletions.
31 changes: 31 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "PromQL CLI Dev",
"dockerComposeFile": "../docker-compose.dev.yml",
"service": "promql",
"workspaceFolder": "/promql-cli/",
"remoteUser": "promql",
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"configureZshAsDefaultShell": true,
"username": "promql"
},
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/eitsupi/devcontainer-features/jq-likes:1": {},
"ghcr.io/devcontainers/features/go:1": {},
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
},
"customizations": {
"vscode": {
"extensions": [
"golang.go",
"ms-azuretools.vscode-docker",
"Gruntfuggly.todo-tree"
],
"settings": {
"go.toolsManagement.autoUpdate": true,
"go.useLanguageServer": true,
"go.gopath": "/go"
}
}
}
}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
go-version:
- "1.20"
os:
- ubuntu-20.04
- ubuntu-latest
- macos-latest
runs-on: ${{ matrix.os }}
steps:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*.swp
build/*
.idea/
.devcontainer/
26 changes: 6 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
FROM golang:1.18-buster AS build

ADD go.mod /promql-cli/go.mod
ADD go.sum /promql-cli/go.sum
COPY go.mod /promql-cli/go.mod
COPY go.sum /promql-cli/go.sum

WORKDIR /promql-cli/
RUN go mod download

RUN apt-get update && apt-get install -y make

ADD ./ /promql-cli/
COPY ./ /promql-cli/
ARG TARGETARCH
RUN OS=linux ARCH=${TARGETARCH} INSTALL_PATH=/promql-cli/build/bin/ make install

# TODO explore other base images here.
# Requirements:
# - small
# - correct perms available for mounting and using a config file (config > cmdline flags), right now we just run as root... (see below)
# I'm generally not a fan of alpine/busybox for a cmdline env but maybe minideb or similar?
# Stock deb slim is pretty rad already :)
FROM debian:buster-slim AS promql-cli
COPY --from=build /promql-cli/build/bin/promql /bin/promql

RUN apt-get update \
&& apt-get install -y ca-certificates \
&& rm -rf /var/lib/apt/lists/*

# TODO don't run as root...
ENTRYPOINT [ "/bin/promql" ]
FROM gcr.io/distroless/base-debian11:nonroot AS promql-cli
COPY --from=build /promql-cli/build/bin/promql /promql
ENTRYPOINT [ "/promql" ]
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ go_memstats_frees_total
```

A query can be provided to narrow the list of metrics returned, for example to return all metrics that have the string `gc` in their name you can run:

```
➜ ~ promql metrics '{__name__=~".+gc.+"}'
METRICS
go_gc_duration_seconds
go_gc_duration_seconds_count
go_gc_duration_seconds_sum
go_memstats_gc_sys_bytes
go_memstats_last_gc_time_seconds
go_memstats_next_gc_bytes
prometheus_tsdb_head_gc_duration_seconds_count
prometheus_tsdb_head_gc_duration_seconds_sum
```

You can also view the metadata information for a metric (or all metrics) with the `promql meta` command.

```
Expand Down
18 changes: 12 additions & 6 deletions cmd/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -22,12 +22,18 @@ import (

// metricsCmd represents the metrics command
var metricsCmd = &cobra.Command{
Use: "metrics",
Short: "Get a list of all prometheus metric names",
Long: `Get a list of all prometheus metric names`,
Use: "metrics [query_string]",
Short: "Get a list of prometheus metric names matching the provided query",
Long: `Get a list of prometheus metric names matching the provided query. If no query is provided, all metric names will be returned.`,
Run: func(cmd *cobra.Command, args []string) {
var r writer.MetaResult
result, err := pql.MetaQuery("")
var r writer.SeriesResult
if query == "" {
query = `{job=~".+"}`
}
result, warnings, err := pql.SeriesQuery(query)
if len(warnings) > 0 {
errlog.Printf("Warnings: %v\n", warnings)
}
if err != nil {
errlog.Fatalln(err)
}
Expand Down
29 changes: 16 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ var rootCmd = &cobra.Command{
pql.Auth.Credentials = config.Secret(viper.GetString("auth-credentials"))
pql.Auth.CredentialsFile = viper.GetString("auth-credentials-file")
pql.TLSConfig = config.TLSConfig{
CAFile: viper.GetString("tls_config.ca_cert_file"),
CertFile: viper.GetString("tls_config.cert_file"),
KeyFile: viper.GetString("tls_config.key_file"),
ServerName: viper.GetString("tls_config.servername"),
CAFile: viper.GetString("tls_config.ca_cert_file"),
CertFile: viper.GetString("tls_config.cert_file"),
KeyFile: viper.GetString("tls_config.key_file"),
ServerName: viper.GetString("tls_config.servername"),
InsecureSkipVerify: viper.GetBool("tls_config.insecure_skip_verify"),
}

Expand Down Expand Up @@ -167,23 +167,26 @@ func init() {
if err := viper.BindPFlag("auth-credentials-file", rootCmd.PersistentFlags().Lookup("auth-credentials-file")); err != nil {
errlog.Fatalln(err)
}
rootCmd.PersistentFlags().String("tls_config.ca_cert_file","","CA cert Path for TLS config")
if err := viper.BindPFlag("tls_config.ca_cert_file",rootCmd.PersistentFlags().Lookup("tls_config.ca_cert_file")); err != nil {
rootCmd.PersistentFlags().String("tls_config.ca_cert_file", "", "CA cert Path for TLS config")
if err := viper.BindPFlag("tls_config.ca_cert_file", rootCmd.PersistentFlags().Lookup("tls_config.ca_cert_file")); err != nil {
errlog.Fatalln(err)
}
rootCmd.PersistentFlags().String("tls_config.cert_file","","client cert Path for TLS config")
if err := viper.BindPFlag("tls_config.cert_file",rootCmd.PersistentFlags().Lookup("tls_config.cert_file")); err != nil {
rootCmd.PersistentFlags().String("tls_config.cert_file", "", "client cert Path for TLS config")
if err := viper.BindPFlag("tls_config.cert_file", rootCmd.PersistentFlags().Lookup("tls_config.cert_file")); err != nil {
errlog.Fatalln(err)
}
rootCmd.PersistentFlags().String("tls_config.key_file","","client key for TLS config")
if err := viper.BindPFlag("tls_config.key_file",rootCmd.PersistentFlags().Lookup("tls_config.key_file")); err != nil {
rootCmd.PersistentFlags().String("tls_config.key_file", "", "client key for TLS config")
if err := viper.BindPFlag("tls_config.key_file", rootCmd.PersistentFlags().Lookup("tls_config.key_file")); err != nil {
errlog.Fatalln(err)
}
rootCmd.PersistentFlags().String("tls_config.servername","","server name for TLS config")
if err := viper.BindPFlag("tls_config.servername",rootCmd.PersistentFlags().Lookup("tls_config.servername")); err != nil {
rootCmd.PersistentFlags().String("tls_config.servername", "", "server name for TLS config")
if err := viper.BindPFlag("tls_config.servername", rootCmd.PersistentFlags().Lookup("tls_config.servername")); err != nil {
errlog.Fatalln(err)
}
rootCmd.PersistentFlags().Bool("tls_config.insecure_skip_verify", false, "disable the TLS verification of server certificates.")
if err := viper.BindPFlag("tls_config.insecure_skip_verify", rootCmd.PersistentFlags().Lookup("tls_config.insecure_skip_verify")); err != nil {
errlog.Fatalln(err)
}
rootCmd.PersistentFlags().Bool("tls_config.insecure_skip_verify",false,"disable the TLS verification of server certificates.")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.8'
services:
promql:
build:
context: .
dockerfile: Dockerfile
target: build
volumes:
- "./:/promql-cli/"
command: sleep infinity
network_mode: service:prometheus
prometheus:
image: prom/prometheus:latest
Loading

0 comments on commit 7252fa6

Please sign in to comment.