generated from giantswarm/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e08b44
commit 945e6dd
Showing
12 changed files
with
511 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package organization | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
organizationLabel string = "giantswarm.io/organization" | ||
) | ||
|
||
type OrganizationRepository interface { | ||
Read(ctx context.Context, cluster *clusterv1.Cluster) (string, error) | ||
} | ||
|
||
type NamespaceOrganizationRepository struct { | ||
client.Client | ||
} | ||
|
||
func NewNamespaceRepository(client client.Client) OrganizationRepository { | ||
return NamespaceOrganizationRepository{client} | ||
} | ||
|
||
func (r NamespaceOrganizationRepository) Read(ctx context.Context, cluster *clusterv1.Cluster) (string, error) { | ||
namespace := &corev1.Namespace{} | ||
err := r.Client.Get(ctx, client.ObjectKey{Name: cluster.GetNamespace()}, namespace) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if organization, ok := namespace.Labels[organizationLabel]; ok { | ||
return organization, nil | ||
} | ||
return "", errors.New("cluster namespace missing organization label") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package querier | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/api" | ||
v1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
// headerAdder is an http.RoundTripper that adds additional headers to the request | ||
type headerAdder struct { | ||
headers map[string][]string | ||
|
||
rt http.RoundTripper | ||
} | ||
|
||
func (h *headerAdder) RoundTrip(req *http.Request) (*http.Response, error) { | ||
for k, vv := range h.headers { | ||
for _, v := range vv { | ||
req.Header.Add(k, v) | ||
} | ||
} | ||
return h.rt.RoundTrip(req) | ||
} | ||
|
||
// QueryTSDBHeadSeries performs an instant query against Mimir. | ||
func QueryTSDBHeadSeries(ctx context.Context, clusterName string) (float64, error) { | ||
headerAdder := &headerAdder{ | ||
headers: map[string][]string{ | ||
"X-Org-Id": {"anonynous"}, | ||
}, | ||
rt: http.DefaultTransport, | ||
} | ||
config := api.Config{ | ||
Address: "http://mimir-gateway.mimir.svc/prometheus", | ||
RoundTripper: headerAdder, | ||
} | ||
|
||
// Create new client. | ||
c, err := api.NewClient(config) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
// Run query against client. | ||
api := v1.NewAPI(c) | ||
|
||
queryContext, cancel := context.WithTimeout(ctx, 2*time.Minute) | ||
val, _, err := api.Query(queryContext, fmt.Sprintf("max_over_time(count({cluster_id=\"%s\"})[6h])", clusterName), time.Now()) | ||
cancel() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
switch val.Type() { | ||
case model.ValVector: | ||
vector := val.(model.Vector) | ||
return float64(vector[0].Value), nil | ||
default: | ||
return 0, errors.New("failed to get current number of time series") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package remotewrite | ||
|
||
import ( | ||
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
) | ||
|
||
type RemoteWriteConfig struct { | ||
PrometheusAgentConfig PrometheusAgentConfig `yaml:"prometheus-agent,omitempty" json:"prometheus-agent,omitempty"` | ||
} | ||
|
||
type PrometheusAgentConfig struct { | ||
ExternalLabels map[string]string `yaml:"externalLabels,omitempty" json:"externalLabels,omitempty"` | ||
Image PrometheusAgentImage `yaml:"image,omitempty" json:"image,omitempty"` | ||
RemoteWrite []RemoteWrite `yaml:"remoteWrite,omitempty" json:"remoteWrite,omitempty"` | ||
Shards int `yaml:"shards,omitempty" json:"shards,omitempty"` | ||
Version string `yaml:"version,omitempty" json:"version,omitempty"` | ||
} | ||
|
||
type PrometheusAgentImage struct { | ||
Tag string `yaml:"tag" json:"tag"` | ||
} | ||
|
||
type RemoteWrite struct { | ||
promv1.RemoteWriteSpec `yaml:",inline" json:",inline"` | ||
Password string `yaml:"password" json:"password"` | ||
Username string `yaml:"username" json:"username"` | ||
} |
Oops, something went wrong.