Skip to content

Commit

Permalink
api: adds HTTPS and HTTP to scheme field
Browse files Browse the repository at this point in the history
For unknown reason prometheus-operator uses mix of lower and uppercase values for different CRDs.

 This commit allows to use case-insensitive values and fixes prometheus-operator `ScrapeConfig` conversion.

 Related issue:
#1148

Signed-off-by: f41gh7 <[email protected]>
  • Loading branch information
f41gh7 committed Nov 7, 2024
1 parent 1424ba8 commit cbe3688
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/operator/v1beta1/common_scrapeparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ type EndpointScrapeParams struct {
Path string `json:"path,omitempty"`
// HTTP scheme to use for scraping.
// +optional
// +kubebuilder:validation:Enum=http;https
// +kubebuilder:validation:Enum=http;https;HTTPS;HTTP
Scheme string `json:"scheme,omitempty"`
// Optional HTTP URL parameters
// +optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,9 @@ func addCommonScrapeParamsTo(cfg yaml.MapSlice, cs vmv1beta1.EndpointScrapeParam
cfg = append(cfg, yaml.MapItem{Key: "params", Value: params})
}
if cs.Scheme != "" {
cfg = append(cfg, yaml.MapItem{Key: "scheme", Value: cs.Scheme})
// scheme may have uppercase format to be compatible with prometheus-operator objects
// vmagent expects lower case format only
cfg = append(cfg, yaml.MapItem{Key: "scheme", Value: strings.ToLower(cs.Scheme)})
}
if cs.MaxScrapeSize != "" {
cfg = append(cfg, yaml.MapItem{Key: "max_scrape_size", Value: cs.MaxScrapeSize})
Expand Down
35 changes: 35 additions & 0 deletions test/e2e/prometheus_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -27,6 +28,40 @@ type testCase struct {
var (
namespace = "default"
testCases = []testCase{
{
name: "PrometheusScrapeConfig-https",
source: &promv1alpha1.ScrapeConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: "e2e-test-scrapeconfig",
},
Spec: promv1alpha1.ScrapeConfigSpec{
Scheme: ptr.To("HTTPS"),
StaticConfigs: []promv1alpha1.StaticConfig{
{

Targets: []promv1alpha1.Target{
"localhost:9100",
},
},
},
},
},
targetTpl: &vmv1beta1.VMScrapeConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: "e2e-test-scrapeconfig",
},
},
targetValidator: func(obj client.Object) error {
ss := obj.(*vmv1beta1.VMScrapeConfig)
if len(ss.Spec.StaticConfigs) != 1 {
return fmt.Errorf("unexpected len of static config: %d, want 1", len(ss.Spec.StaticConfigs))
}
return nil
},
},

{
name: "PrometheusScrapeConfig",
source: &promv1alpha1.ScrapeConfig{
Expand Down

0 comments on commit cbe3688

Please sign in to comment.