Skip to content

Commit

Permalink
fix place where metrics service is scaffolded by moving from config/r…
Browse files Browse the repository at this point in the history
…bac to config/default
  • Loading branch information
camilamacedo86 committed May 23, 2024
1 parent b7ae59f commit 7b5f3a0
Show file tree
Hide file tree
Showing 42 changed files with 76 additions and 169 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test-sample-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:
run: |
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '39s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '44s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '48,144s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '40s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '45s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '49,145s/^#//' $KUSTOMIZATION_FILE_PATH
- name: Test
run: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func main() {
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0' in order to disable the metrics server")
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ patches:
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
#- path: metrics_service.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ resources:
- role_binding.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
- metrics_service.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/getting-started/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0' in order to disable the metrics server")
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ patches:
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
#- path: metrics_service.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ resources:
- role_binding.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
- metrics_service.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
Expand Down
1 change: 1 addition & 0 deletions docs/book/src/reference/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ in the file `config/default/kustomization.yaml`, see:
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
#- path: metrics_service.yaml
```

Note that projects are scaffolded by default passing the flag `--metrics-bind-address=0`
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/common/kustomize/v2/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *initScaffolder) Scaffold() error {

templates := []machinery.Builder{
&rbac.Kustomization{},
&rbac.MetricsService{},
&kdefault.MetricsService{},
&rbac.RoleBinding{},
// We need to create a Role because if the project
// has not CRD define the controller-gen will not generate this file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ patches:
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
#- path: metrics_service.yaml
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package rbac
package kdefault

import (
"path/filepath"
Expand All @@ -33,7 +33,7 @@ type MetricsService struct {
// SetTemplateDefaults implements file.Template
func (f *MetricsService) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "rbac", "metrics_service.yaml")
f.Path = filepath.Join("config", "default", "metrics_service.yaml")
}

f.TemplateBody = metricsServiceTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ const kustomizeRBACTemplate = `resources:
- role_binding.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
- metrics_service.yaml
`
2 changes: 1 addition & 1 deletion pkg/plugins/golang/v4/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func main() {
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. " +
"Use the port :8080. If not set, it will be '0' in order to disable the metrics server")
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. " +
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/v4/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ func initingTheProject(kbc *utils.TestContext) {

const metricsTarget = `#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment`
# kind: Deployment
#- path: metrics_service.yaml`

//nolint:lll
const certManagerTarget = `#replacements:
Expand Down
107 changes: 51 additions & 56 deletions test/e2e/v4/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,66 +278,47 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)

// curlMetrics curl's the /metrics endpoint, returning all logs once a 200 status is returned.
func curlMetrics(kbc *utils.TestContext, hasMetrics bool) string {
By("validating that the controller-manager service is available")
_, err := kbc.Kubectl.Get(
true,
"service", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Controller-manager service should exist")

By("validating that the controller-manager deployment is ready")
verifyDeploymentReady := func() error {
output, err := kbc.Kubectl.Get(
var metricsOutput string
if hasMetrics {
By("validating that the controller-manager service is available")
_, err := kbc.Kubectl.Get(
true,
"deployment", fmt.Sprintf("e2e-%s-controller-manager", kbc.TestSuffix),
"-o", "jsonpath={.status.readyReplicas}",
"service", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
)
if err != nil {
return err
}
readyReplicas, _ := strconv.Atoi(output)
if readyReplicas < 1 {
return fmt.Errorf("expected at least 1 ready replica, got %d", readyReplicas)
}
return nil
}
EventuallyWithOffset(2, verifyDeploymentReady, 240*time.Second, time.Second).Should(Succeed(),
"Deployment is not ready")
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Controller-manager service should exist")

By("ensuring the service endpoint is ready")
eventuallyCheckServiceEndpoint := func() error {
output, err := kbc.Kubectl.Get(
true,
"endpoints", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
"-o", "jsonpath={.subsets[*].addresses[*].ip}",
)
if err != nil {
return err
By("ensuring the service endpoint is ready")
eventuallyCheckServiceEndpoint := func() error {
output, err := kbc.Kubectl.Get(
true,
"endpoints", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
"-o", "jsonpath={.subsets[*].addresses[*].ip}",
)
if err != nil {
return err
}
if output == "" {
return fmt.Errorf("no endpoints found")
}
return nil
}
if output == "" {
return fmt.Errorf("no endpoints found")
EventuallyWithOffset(2, eventuallyCheckServiceEndpoint, 2*time.Minute, time.Second).Should(Succeed(),
"Service endpoint should be ready")

By("creating a curl pod to access the metrics endpoint")
// nolint:lll
cmdOpts := []string{
"run", "curl",
"--restart=Never",
"--namespace", kbc.Kubectl.Namespace,
"--image=curlimages/curl:7.78.0",
"--",
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
kbc.TestSuffix, kbc.Kubectl.Namespace),
}
return nil
}
EventuallyWithOffset(2, eventuallyCheckServiceEndpoint, 2*time.Minute, time.Second).Should(Succeed(),
"Service endpoint should be ready")

By("creating a curl pod to access the metrics endpoint")
// nolint:lll
cmdOpts := []string{
"run", "curl",
"--restart=Never",
"--namespace", kbc.Kubectl.Namespace,
"--image=curlimages/curl:7.78.0",
"--",
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
kbc.TestSuffix, kbc.Kubectl.Namespace),
}
_, err = kbc.Kubectl.CommandInNamespace(cmdOpts...)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
_, err = kbc.Kubectl.CommandInNamespace(cmdOpts...)
ExpectWithOffset(2, err).NotTo(HaveOccurred())

var metricsOutput string
if hasMetrics {
By("validating that the curl pod is running as expected")
verifyCurlUp := func() error {
status, err := kbc.Kubectl.Get(
Expand All @@ -359,6 +340,20 @@ func curlMetrics(kbc *utils.TestContext, hasMetrics bool) string {
}
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("< HTTP/1.1 200 OK"))
} else {
By("creating a curl pod to access the metrics endpoint")
// nolint:lll
cmdOpts := []string{
"run", "curl",
"--restart=Never",
"--namespace", kbc.Kubectl.Namespace,
"--image=curlimages/curl:7.78.0",
"--",
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
kbc.TestSuffix, kbc.Kubectl.Namespace),
}
_, err := kbc.Kubectl.CommandInNamespace(cmdOpts...)
ExpectWithOffset(2, err).NotTo(HaveOccurred())

By("validating that the curl pod fail as expected")
verifyCurlUp := func() error {
status, err := kbc.Kubectl.Get(
Expand All @@ -375,14 +370,14 @@ func curlMetrics(kbc *utils.TestContext, hasMetrics bool) string {

By("validating that the metrics endpoint is not working as expected")
getCurlLogs := func() string {
metricsOutput, err = kbc.Kubectl.Logs("curl")
metricsOutput, err := kbc.Kubectl.Logs("curl")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
return metricsOutput
}
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("Connection refused"))
}
By("cleaning up the curl pod")
_, err = kbc.Kubectl.Delete(true, "pods/curl")
_, err := kbc.Kubectl.Delete(true, "pods/curl")
ExpectWithOffset(3, err).NotTo(HaveOccurred())

return metricsOutput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func main() {
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0' in order to disable the metrics server")
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ patches:
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
#- path: metrics_service.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ resources:
- role_binding.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
- metrics_service.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
Expand Down
18 changes: 0 additions & 18 deletions testdata/project-v4-multigroup-with-deploy-image/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1474,24 +1474,6 @@ subjects:
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: project-v4-multigroup-with-deploy-image
control-plane: controller-manager
name: project-v4-multigroup-with-deploy-image-controller-manager-metrics-service
namespace: project-v4-multigroup-with-deploy-image-system
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
control-plane: controller-manager
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-multigroup/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func main() {
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0' in order to disable the metrics server")
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ patches:
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
#- path: metrics_service.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ resources:
- role_binding.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
- metrics_service.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
Expand Down
18 changes: 0 additions & 18 deletions testdata/project-v4-multigroup/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1474,24 +1474,6 @@ subjects:
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: project-v4-multigroup
control-plane: controller-manager
name: project-v4-multigroup-controller-manager-metrics-service
namespace: project-v4-multigroup-system
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
control-plane: controller-manager
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-with-deploy-image/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0' in order to disable the metrics server")
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ patches:
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
#- path: metrics_service.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ resources:
- role_binding.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
- metrics_service.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
Expand Down
Loading

0 comments on commit 7b5f3a0

Please sign in to comment.