Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Remove OpenAPI validations #71

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ARCH ?= amd64
OS ?= $(shell uname -s | tr A-Z a-z)
K8S_LATEST_VER ?= $(shell curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)
export CONTROLLER_IMG ?= $(REGISTRY)/$(IMAGE_NAME)
TAG ?= main
TAG ?= dev

.PHONY: all
all: build
Expand Down
2 changes: 1 addition & 1 deletion config/default/manager_image_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ spec:
spec:
containers:
# Change the value of image field below to your controller image URL
- image: projectsveltos/addon-compliance-controller-amd64:main
- image: projectsveltos/addon-compliance-controller-amd64:dev
name: manager
56 changes: 0 additions & 56 deletions controllers/addoncompliance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

sourcev1 "github.com/fluxcd/source-controller/api/v1"
sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -212,14 +211,6 @@ func (r *AddonComplianceReconciler) reconcileNormal(
r.updateMaps(addonConstraintScope, logger)

var validations map[string][]byte
validations, err = r.collectOpenapiValidations(ctx, addonConstraintScope, logger)
if err != nil {
failureMsg := err.Error()
addonConstraintScope.SetFailureMessage(&failureMsg)
return reconcile.Result{Requeue: true, RequeueAfter: normalRequeueAfter}, nil
}
addonConstraintScope.AddonCompliance.Status.OpenapiValidations = validations

validations, err = r.collectLuaValidations(ctx, addonConstraintScope, logger)
if err != nil {
failureMsg := err.Error()
Expand Down Expand Up @@ -495,18 +486,6 @@ func (r *AddonComplianceReconciler) getMatchingClusters(ctx context.Context,
func (r *AddonComplianceReconciler) getCurrentReferences(addonConstraintScope *scope.AddonComplianceScope) *libsveltosset.Set {
currentReferences := &libsveltosset.Set{}

for i := range addonConstraintScope.AddonCompliance.Spec.OpenAPIValidationRefs {
referencedNamespace := addonConstraintScope.AddonCompliance.Spec.OpenAPIValidationRefs[i].Namespace
referencedName := addonConstraintScope.AddonCompliance.Spec.OpenAPIValidationRefs[i].Name

apiVersion := getReferenceAPIVersion(addonConstraintScope.AddonCompliance.Spec.OpenAPIValidationRefs[i].Kind)
currentReferences.Insert(&corev1.ObjectReference{
APIVersion: apiVersion,
Kind: addonConstraintScope.AddonCompliance.Spec.OpenAPIValidationRefs[i].Kind,
Namespace: referencedNamespace,
Name: referencedName,
})
}
for i := range addonConstraintScope.AddonCompliance.Spec.LuaValidationRefs {
referencedNamespace := addonConstraintScope.AddonCompliance.Spec.LuaValidationRefs[i].Namespace
referencedName := addonConstraintScope.AddonCompliance.Spec.LuaValidationRefs[i].Name
Expand All @@ -523,41 +502,6 @@ func (r *AddonComplianceReconciler) getCurrentReferences(addonConstraintScope *s
return currentReferences
}

func (r *AddonComplianceReconciler) collectOpenapiValidations(ctx context.Context, addonConstraintScope *scope.AddonComplianceScope,
logger logr.Logger) (map[string][]byte, error) {

logger.V(logs.LogDebug).Info("collect openapi validations")
validations := make(map[string][]byte)
for i := range addonConstraintScope.AddonCompliance.Spec.OpenAPIValidationRefs {
ref := &addonConstraintScope.AddonCompliance.Spec.OpenAPIValidationRefs[i]
currentValidation, err := r.collectValidations(ctx, ref.Kind, ref.Namespace, ref.Name, ref.Path, logger)
if err != nil {
return nil, err
}
for k := range currentValidation {
loader := &openapi3.Loader{Context: ctx, IsExternalRefsAllowed: true}

// Load the OpenAPI specification from the content
doc, err := loader.LoadFromData([]byte(currentValidation[k]))
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to loadFromData: %v", err))
return nil, err
}

err = doc.Validate(ctx)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to validate: %v", err))
return nil, err
}

validations[k] = currentValidation[k]
}

}

return validations, nil
}

func (r *AddonComplianceReconciler) collectLuaValidations(ctx context.Context,
addonConstraintScope *scope.AddonComplianceScope, logger logr.Logger) (map[string][]byte, error) {

Expand Down
36 changes: 6 additions & 30 deletions controllers/addoncompliance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,22 @@ var _ = Describe("AddonCompliance Controller", func() {
}
})

It("getCurrentReferences collects all OpenAPIValidationRef referenced objects", func() {
addonConstraint.Spec.OpenAPIValidationRefs = []libsveltosv1alpha1.OpenAPIValidationRef{
It("getCurrentReferences collects all LuaValidationRefs referenced objects", func() {
addonConstraint.Spec.LuaValidationRefs = []libsveltosv1alpha1.LuaValidationRef{
{
Namespace: namespace,
Name: randomString(),
Kind: string(libsveltosv1alpha1.ConfigMapReferencedResourceKind),
Kind: string(libsveltosv1alpha1.SecretReferencedResourceKind),
},
{
Namespace: namespace,
Name: randomString(),
Kind: sourcev1.GitRepositoryKind,
Kind: string(libsveltosv1alpha1.ConfigMapReferencedResourceKind),
},
}

addonConstraint.Spec.LuaValidationRefs = []libsveltosv1alpha1.LuaValidationRef{
{
Namespace: namespace,
Name: randomString(),
Kind: string(libsveltosv1alpha1.SecretReferencedResourceKind),
Kind: sourcev1.GitRepositoryKind,
},
}

Expand Down Expand Up @@ -279,7 +276,7 @@ var _ = Describe("AddonCompliance Controller", func() {
APIVersion: libsveltosv1alpha1.GroupVersion.String(),
}

addonConstraint.Spec.OpenAPIValidationRefs = []libsveltosv1alpha1.OpenAPIValidationRef{
addonConstraint.Spec.LuaValidationRefs = []libsveltosv1alpha1.LuaValidationRef{
{
Namespace: configMap.Namespace,
Name: configMap.Name,
Expand Down Expand Up @@ -471,27 +468,6 @@ var _ = Describe("AddonCompliance Controller", func() {
Expect(len(u)).To(Equal(2))
})

It("collectOpenapiValidations updates AddonCompliance status", func() {
configMap := createConfigMapWithPolicy(randomString(), randomString(), []string{nameSpec, deplReplicaSpec}...)

addonConstraint.Spec.OpenAPIValidationRefs = []libsveltosv1alpha1.OpenAPIValidationRef{
{Namespace: configMap.Namespace, Name: configMap.Name,
Kind: string(libsveltosv1alpha1.ConfigMapReferencedResourceKind)},
}

initObjects := []client.Object{configMap, addonConstraint}

c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()
addonConstraintScope := getAddonComplianceScope(c, klogr.New(), addonConstraint)
reconciler := getAddonComplianceReconciler(c)

result, err := controllers.CollectOpenapiValidations(reconciler, context.TODO(),
addonConstraintScope, klogr.New())
Expect(err).To(BeNil())
Expect(result).ToNot(BeNil())
Expect(len(result)).To(Equal(2))
})

It("collectLuaValidations updates AddonCompliance status", func() {
configMap := createConfigMapWithPolicy(randomString(), randomString(), []string{deploymentReplicaCheck}...)

Expand Down
8 changes: 4 additions & 4 deletions controllers/addoncompliance_transformations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = Describe("AddonComplianceTransformation map functions", func() {
Namespace: randomString(),
},
Spec: libsveltosv1alpha1.AddonComplianceSpec{
OpenAPIValidationRefs: []libsveltosv1alpha1.OpenAPIValidationRef{
LuaValidationRefs: []libsveltosv1alpha1.LuaValidationRef{
{
Namespace: configMap.Namespace,
Name: configMap.Name,
Expand All @@ -68,7 +68,7 @@ var _ = Describe("AddonComplianceTransformation map functions", func() {
Namespace: randomString(),
},
Spec: libsveltosv1alpha1.AddonComplianceSpec{
OpenAPIValidationRefs: []libsveltosv1alpha1.OpenAPIValidationRef{
LuaValidationRefs: []libsveltosv1alpha1.LuaValidationRef{
{
Namespace: randomString(),
Name: configMap.Name,
Expand Down Expand Up @@ -131,7 +131,7 @@ var _ = Describe("AddonComplianceTransformation map functions", func() {
Namespace: randomString(),
},
Spec: libsveltosv1alpha1.AddonComplianceSpec{
OpenAPIValidationRefs: []libsveltosv1alpha1.OpenAPIValidationRef{
LuaValidationRefs: []libsveltosv1alpha1.LuaValidationRef{
{
Namespace: gitRepo.Namespace,
Name: gitRepo.Name,
Expand All @@ -147,7 +147,7 @@ var _ = Describe("AddonComplianceTransformation map functions", func() {
Namespace: randomString(),
},
Spec: libsveltosv1alpha1.AddonComplianceSpec{
OpenAPIValidationRefs: []libsveltosv1alpha1.OpenAPIValidationRef{
LuaValidationRefs: []libsveltosv1alpha1.LuaValidationRef{
{
Namespace: gitRepo.Namespace,
Name: randomString(),
Expand Down
1 change: 0 additions & 1 deletion controllers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var (
CleanMaps = (*AddonComplianceReconciler).cleanMaps
CollectContentOfConfigMap = (*AddonComplianceReconciler).collectContentOfConfigMap
CollectContentOfSecret = (*AddonComplianceReconciler).collectContentOfSecret
CollectOpenapiValidations = (*AddonComplianceReconciler).collectOpenapiValidations
CollectLuaValidations = (*AddonComplianceReconciler).collectLuaValidations
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/onsi/ginkgo/v2 v2.13.1
github.com/onsi/gomega v1.30.0
github.com/pkg/errors v0.9.1
github.com/projectsveltos/libsveltos v0.20.1-0.20231129081648-ef5be475b0c0
github.com/projectsveltos/libsveltos v0.20.1-0.20231221080505-f3a4eea4967b
github.com/spf13/pflag v1.0.5
golang.org/x/text v0.14.0
k8s.io/api v0.28.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/projectsveltos/libsveltos v0.20.1-0.20231129081648-ef5be475b0c0 h1:X2sl3kxINpYeOrQDkfDR2f772KH23iVf+17IFmghn9c=
github.com/projectsveltos/libsveltos v0.20.1-0.20231129081648-ef5be475b0c0/go.mod h1:lDfsnHGVIvb9Xs30gevyGkCEJXwo5B22HUo0Ya/+gHA=
github.com/projectsveltos/libsveltos v0.20.1-0.20231221080505-f3a4eea4967b h1:JnzdkwqFg+M0UC2GYDQjMdaBEnpCP2egymHxxwyu0hA=
github.com/projectsveltos/libsveltos v0.20.1-0.20231221080505-f3a4eea4967b/go.mod h1:lDfsnHGVIvb9Xs30gevyGkCEJXwo5B22HUo0Ya/+gHA=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM=
Expand Down
2 changes: 1 addition & 1 deletion manifest/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ spec:
- --v=5
command:
- /manager
image: projectsveltos/addon-compliance-controller-amd64:main
image: projectsveltos/addon-compliance-controller-amd64:dev
livenessProbe:
httpGet:
path: /healthz
Expand Down
Loading