Skip to content

Commit

Permalink
Merge pull request #374 from doitintl/feature/cover
Browse files Browse the repository at this point in the history
feat: Cover deprecated autoscaling/v2beta1 and autoscaling/v2beta2
  • Loading branch information
stepanstipl authored Sep 30, 2022
2 parents b6e3d71 + 3f69b06 commit 4582f53
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 3 deletions.
30 changes: 30 additions & 0 deletions fixtures/autoscaler-v2beta1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
status:
observedGeneration: 1
lastScaleTime: 12
currentReplicas: 1
desiredReplicas: 1
currentMetrics:
- type: Resource
resource:
name: cpu
current:
averageUtilization: 0
averageValue: 0
30 changes: 30 additions & 0 deletions fixtures/autoscaler-v2beta2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
status:
observedGeneration: 1
lastScaleTime: 12
currentReplicas: 1
desiredReplicas: 1
currentMetrics:
- type: Resource
resource:
name: cpu
current:
averageUtilization: 0
averageValue: 0
1 change: 1 addition & 0 deletions pkg/collector/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (c *ClusterCollector) Get() ([]map[string]interface{}, error) {
schema.GroupVersionResource{Group: "policy", Version: "v1beta1", Resource: "podsecuritypolicies"},
schema.GroupVersionResource{Group: "discovery.k8s.io", Version: "v1", Resource: "endpointslices"},
schema.GroupVersionResource{Group: "batch", Version: "v1", Resource: "cronjobs"},
schema.GroupVersionResource{Group: "autoscaling", Version: "v2", Resource: "horizontalpodautoscalers"},
schema.GroupVersionResource{Group: "snapshot.storage.k8s.io", Version: "v1", Resource: "volumesnapshots"},
schema.GroupVersionResource{Group: "snapshot.storage.k8s.io", Version: "v1", Resource: "volumesnapshotclasses"},
schema.GroupVersionResource{Group: "snapshot.storage.k8s.io", Version: "v1", Resource: "volumesnapshotcontents"},
Expand Down
42 changes: 42 additions & 0 deletions pkg/rules/rego/deprecated-1-26.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package deprecated126

main[return] {
resource := input[_]
api := deprecated_resource(resource)
return := {
"Name": get_default(resource.metadata, "name", "<undefined>"),
# Namespace does not have to be defined in case of local manifests
"Namespace": get_default(resource.metadata, "namespace", "<undefined>"),
"Kind": resource.kind,
"ApiVersion": api.old,
"ReplaceWith": api.new,
"RuleSet": "Deprecated APIs removed in 1.26",
"Since": api.since,
}
}

deprecated_resource(r) = api {
api := deprecated_api(r.kind, r.apiVersion)
}

deprecated_api(kind, api_version) = api {
deprecated_apis = {"HorizontalPodAutoscaler": {
"old": ["autoscaling/v2beta1", "autoscaling/v2beta2"],
"new": "autoscaling/v2",
"since": "1.23",
}}

deprecated_apis[kind].old[_] == api_version

api := {
"old": api_version,
"new": deprecated_apis[kind].new,
"since": deprecated_apis[kind].since,
}
}

get_default(val, key, _) = val[key]

get_default(val, key, fallback) = fallback {
not val[key]
}
6 changes: 3 additions & 3 deletions test/rules_125_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

func TestRego125(t *testing.T) {
testCases := []resourceFixtureTestCase{
{"RuntimeClass", []string{"../fixtures/runtimeclass-v1beta1.yaml"}, []string{"RuntimeClass"}},
{"CronJob", []string{"../fixtures/cronjob-v1beta1.yaml"}, []string{"CronJob"}},
{"EndpointSlice", []string{"../fixtures/endpointslice-v1beta1.yaml"}, []string{"EndpointSlice"}},
{"PodDisruptionBudget", []string{"../fixtures/poddisruptionbudget-v1beta1.yaml"}, []string{"PodDisruptionBudget"}},
{"PodSecurityPolicy", []string{"../fixtures/podsecuritypolicy-v1beta1.yaml"}, []string{"PodSecurityPolicy"}},
{"EndpointSlice", []string{"../fixtures/endpointslice-v1beta1.yaml"}, []string{"EndpointSlice"}},
{"CronJob", []string{"../fixtures/cronjob-v1beta1.yaml"}, []string{"CronJob"}},
{"RuntimeClass", []string{"../fixtures/runtimeclass-v1beta1.yaml"}, []string{"RuntimeClass"}},
}

testResourcesUsingFixtures(t, testCases)
Expand Down
14 changes: 14 additions & 0 deletions test/rules_126_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test

import (
"testing"
)

func TestRego126(t *testing.T) {
testCases := []resourceFixtureTestCase{
{"AutoScaler-v2beta1", []string{"../fixtures/autoscaler-v2beta1.yaml"}, []string{"HorizontalPodAutoscaler"}},
{"AutoScaler-v2beta2", []string{"../fixtures/autoscaler-v2beta2.yaml"}, []string{"HorizontalPodAutoscaler"}},
}

testResourcesUsingFixtures(t, testCases)
}

0 comments on commit 4582f53

Please sign in to comment.