Skip to content

Commit

Permalink
feat: Add prefer_name into ApisixClusterConfig (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatpa authored Mar 3, 2023
1 parent de1928e commit 3f76ae4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
2 changes: 2 additions & 0 deletions pkg/kube/apisix/apis/config/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ type ApisixClusterMonitoringConfig struct {
type ApisixClusterPrometheusConfig struct {
// Enable means whether enable Prometheus or not.
Enable bool `json:"enable" yaml:"enable"`
// PreferName means whether prints Route/Service name or ID in Prometheus metric
PreferName bool `json:"prefer_name" yaml:"prefer_name"`
}

// ApisixClusterSkywalkingConfig is the config for using Skywalking in APISIX Cluster.
Expand Down
8 changes: 6 additions & 2 deletions pkg/providers/apisix/translation/apisix_cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
)

type prometheusPluginConfig struct{}
type prometheusPluginConfig struct {
PreferName bool `json:"prefer_name,omitempty"`
}

type skywalkingPluginConfig struct {
SampleRatio float64 `json:"sample_ratio,omitempty"`
Expand Down Expand Up @@ -55,7 +57,9 @@ func (t *translator) TranslateClusterConfigV2(acc *configv2.ApisixClusterConfig)

if acc.Spec.Monitoring != nil {
if acc.Spec.Monitoring.Prometheus.Enable {
globalRule.Plugins["prometheus"] = &prometheusPluginConfig{}
globalRule.Plugins["prometheus"] = &prometheusPluginConfig{
PreferName: acc.Spec.Monitoring.Prometheus.PreferName,
}
}
if acc.Spec.Monitoring.Skywalking.Enable {
globalRule.Plugins["skywalking"] = &skywalkingPluginConfig{
Expand Down
30 changes: 30 additions & 0 deletions pkg/providers/apisix/translation/apisix_cluster_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/apache/apisix-ingress-controller/pkg/id"
configv2 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2"
configv2beta3 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2beta3"
)

Expand Down Expand Up @@ -51,3 +52,32 @@ func TestTranslateClusterConfig(t *testing.T) {
assert.Equal(t, gr.Plugins["prometheus"], &prometheusPluginConfig{})
assert.Equal(t, gr.Plugins["skywalking"], &skywalkingPluginConfig{SampleRatio: 0.5})
}

func TestTranslateClusterConfigV2(t *testing.T) {
tr := &translator{}

acc := &configv2.ApisixClusterConfig{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "qa-apisix",
},
Spec: configv2.ApisixClusterConfigSpec{
Monitoring: &configv2.ApisixClusterMonitoringConfig{
Prometheus: configv2.ApisixClusterPrometheusConfig{
Enable: true,
PreferName: true,
},
Skywalking: configv2.ApisixClusterSkywalkingConfig{
Enable: true,
SampleRatio: 0.5,
},
},
},
}
gr, err := tr.TranslateClusterConfigV2(acc)
assert.Nil(t, err, "translating ApisixClusterConfigV2")
assert.Equal(t, gr.ID, id.GenID("qa-apisix"), "checking global_rule id")
assert.Len(t, gr.Plugins, 2)
assert.Equal(t, gr.Plugins["prometheus"], &prometheusPluginConfig{PreferName: true})
assert.Equal(t, gr.Plugins["skywalking"], &skywalkingPluginConfig{SampleRatio: 0.5})
}
2 changes: 2 additions & 0 deletions samples/deploy/crd/v1/ApisixClusterConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ spec:
properties:
enable:
type: boolean
prefer_name:
type: boolean
skywalking:
type: object
properties:
Expand Down
30 changes: 26 additions & 4 deletions test/e2e/scaffold/cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,25 @@ import (
"fmt"
"time"

"github.com/apache/apisix-ingress-controller/pkg/config"
"github.com/gruntwork-io/terratest/modules/k8s"
)

var (
_apisixClusterConfigTemplate = `
apiVersion: %s
kind: ApisixClusterConfig
metadata:
name: %s
spec:
monitoring:
prometheus:
enable: %v
prefer_name: %v
`
_apisixClusterConfigV2beta3Template = `
apiVersion: %s
kind: ApisixClusterConfig
metadata:
name: %s
spec:
Expand All @@ -37,8 +49,13 @@ spec:
)

// NewApisixClusterConfig creates an ApisixClusterConfig CRD
func (s *Scaffold) NewApisixClusterConfig(name string, enable bool) error {
cc := fmt.Sprintf(_apisixClusterConfigTemplate, s.opts.ApisixResourceVersion, name, enable)
func (s *Scaffold) NewApisixClusterConfig(name string, enable bool, enablePreferName bool) error {
var cc string
if s.opts.ApisixResourceVersion == config.ApisixV2beta3 {
cc = fmt.Sprintf(_apisixClusterConfigV2beta3Template, s.opts.ApisixResourceVersion, name, enable)
} else {
cc = fmt.Sprintf(_apisixClusterConfigTemplate, s.opts.ApisixResourceVersion, name, enable, enablePreferName)
}
if err := s.CreateResourceFromString(cc); err != nil {
return err
}
Expand All @@ -47,8 +64,13 @@ func (s *Scaffold) NewApisixClusterConfig(name string, enable bool) error {
}

// DeleteApisixClusterConfig removes an ApisixClusterConfig CRD
func (s *Scaffold) DeleteApisixClusterConfig(name string, enable bool) error {
cc := fmt.Sprintf(_apisixClusterConfigTemplate, s.opts.ApisixResourceVersion, name, enable)
func (s *Scaffold) DeleteApisixClusterConfig(name string, enable bool, enablePreferName bool) error {
var cc string
if s.opts.ApisixResourceVersion == config.ApisixV2beta3 {
cc = fmt.Sprintf(_apisixClusterConfigV2beta3Template, s.opts.ApisixResourceVersion, name, enable)
} else {
cc = fmt.Sprintf(_apisixClusterConfigTemplate, s.opts.ApisixResourceVersion, name, enable, enablePreferName)
}
if err := k8s.KubectlDeleteFromStringE(s.t, s.kubectlOptions, cc); err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions test/e2e/suite-features/global_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"
"time"

"github.com/apache/apisix-ingress-controller/pkg/config"
"github.com/apache/apisix-ingress-controller/pkg/id"
ginkgo "github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/assert"
Expand All @@ -32,10 +33,10 @@ var _ = ginkgo.Describe("suite-features: ApisixClusterConfig", func() {

ginkgo.It("enable prometheus", func() {
adminSvc, adminPort := s.ApisixAdminServiceAndPort()
assert.Nil(ginkgo.GinkgoT(), s.NewApisixClusterConfig("default", true), "creating ApisixClusterConfig")
assert.Nil(ginkgo.GinkgoT(), s.NewApisixClusterConfig("default", true, true), "creating ApisixClusterConfig")

defer func() {
assert.Nil(ginkgo.GinkgoT(), s.DeleteApisixClusterConfig("default", true))
assert.Nil(ginkgo.GinkgoT(), s.DeleteApisixClusterConfig("default", true, true))
}()

// Wait until the ApisixClusterConfig create event was delivered.
Expand Down Expand Up @@ -78,6 +79,14 @@ spec:
resp.Body().Contains("# HELP apisix_etcd_modify_indexes Etcd modify index for APISIX keys")
resp.Body().Contains("# HELP apisix_etcd_reachable Config server etcd reachable from APISIX, 0 is unreachable")
resp.Body().Contains("# HELP apisix_node_info Info of APISIX node")

time.Sleep(3 * time.Second)

if s.ApisixResourceVersion() != config.ApisixV2beta3 {
resp1 := s.NewAPISIXClient().GET("/apisix/prometheus/metrics").Expect()
resp1.Status(http.StatusOK)
resp1.Body().Contains("public-api")
}
})
}

Expand Down

0 comments on commit 3f76ae4

Please sign in to comment.