Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add type to policy CRD #1079

Merged
merged 6 commits into from
Sep 22, 2023
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 @@ -118,7 +118,7 @@ delete-demo-constraints:

.PHONY: deploy-rego-policy
deploy-rego-policy:
kubectl apply -f ./config/samples/policy/config_v1alpha1_policy_rego.yaml
kubectl apply -f ./config/samples/policy/config_v1beta1_policy_rego.yaml

.PHONY: deploy-gatekeeper
deploy-gatekeeper:
Expand Down
11 changes: 11 additions & 0 deletions api/unversioned/policy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
type PolicySpec struct {
// Important: Run "make" to regenerate code after modifying this file

// Type of the policy
Type string `json:"type,omitempty"`
// Parameters for this policy
Parameters runtime.RawExtension `json:"parameters,omitempty"`
}
Expand All @@ -33,6 +35,15 @@ type PolicySpec struct {
type PolicyStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Is successful while applying the policy.
IsSuccess bool `json:"issuccess"`
// Error message if policy is not successfully applied.
// +optional
Error string `json:"error,omitempty"`
// Truncated error message if the message is too long
// +optional
BriefError string `json:"brieferror,omitempty"`
}

// Policy is the Schema for the policies API
Expand Down
66 changes: 66 additions & 0 deletions api/v1alpha1/policy_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright The Ratify Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
unversioned "github.com/deislabs/ratify/api/unversioned"
"github.com/deislabs/ratify/internal/constants"
conversion "k8s.io/apimachinery/pkg/conversion"
)

// Convert unversioned PolicySpec to PolicySpec of v1alpha1.
//
//nolint:revive // ignore linter for autogenerated code
func Convert_unversioned_PolicySpec_To_v1alpha1_PolicySpec(in *unversioned.PolicySpec, out *PolicySpec, _ conversion.Scope) error {
out.Parameters = in.Parameters
return nil
}

// Convert unversioned PolicyStatus to PolicyStatus of v1alpha1.
//
//nolint:revive // ignore linter for autogenerated code
func Convert_unversioned_PolicyStatus_To_v1alpha1_PolicyStatus(in *unversioned.PolicyStatus, out *PolicyStatus, _ conversion.Scope) error {
return nil
}

// Convert unversioned Policy to Policy of v1alpha1.
//
//nolint:revive // ignore linter for autogenerated code
func Convert_unversioned_Policy_To_v1alpha1_Policy(in *unversioned.Policy, out *Policy, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
// metadata.name in v1alpha1 is same as spec.type in unversioned.
out.ObjectMeta.Name = in.Spec.Type
if err := Convert_unversioned_PolicySpec_To_v1alpha1_PolicySpec(&in.Spec, &out.Spec, s); err != nil {
return err
}

Check warning on line 49 in api/v1alpha1/policy_conversion.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/policy_conversion.go#L48-L49

Added lines #L48 - L49 were not covered by tests
return Convert_unversioned_PolicyStatus_To_v1alpha1_PolicyStatus(&in.Status, &out.Status, s)
}

// Convert Policy of v1alpha1 to unversioned Policy.
//
//nolint:revive // ignore linter for autogenerated code
func Convert_v1alpha1_Policy_To_unversioned_Policy(in *Policy, out *unversioned.Policy, s conversion.Scope) error {
binbin-li marked this conversation as resolved.
Show resolved Hide resolved
out.ObjectMeta = in.ObjectMeta
// metadata.name MUST be `ratify-policy` in unversioned.
out.ObjectMeta.Name = constants.RatifyPolicy
if err := Convert_v1alpha1_PolicySpec_To_unversioned_PolicySpec(&in.Spec, &out.Spec, s); err != nil {
return err
}

Check warning on line 62 in api/v1alpha1/policy_conversion.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/policy_conversion.go#L61-L62

Added lines #L61 - L62 were not covered by tests
// spec.type in unversioned is same as metadata.name in v1alpha1.
out.Spec.Type = in.ObjectMeta.Name
return Convert_v1alpha1_PolicyStatus_To_unversioned_PolicyStatus(&in.Status, &out.Status, s)
}
83 changes: 83 additions & 0 deletions api/v1alpha1/policy_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright The Ratify Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"reflect"
"testing"

unversioned "github.com/deislabs/ratify/api/unversioned"
"github.com/deislabs/ratify/internal/constants"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
)

var params = runtime.RawExtension{}

const testPolicyType = "testPolicyType"

func TestConvert_unversioned_PolicySpec_To_v1alpha1_PolicySpec(t *testing.T) {
in := &unversioned.PolicySpec{
Parameters: params,
}
out := &PolicySpec{}
if err := Convert_unversioned_PolicySpec_To_v1alpha1_PolicySpec(in, out, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(out.Parameters, in.Parameters) {
t.Fatalf("expect parameters to be equal, but got different values")
}
}

func TestConvert_unversioned_PolicyStatus_To_v1alpha1_PolicyStatus(t *testing.T) {
if err := Convert_unversioned_PolicyStatus_To_v1alpha1_PolicyStatus(nil, nil, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

func TestConvert_unversioned_Policy_To_v1alpha1_Policy(t *testing.T) {
in := &unversioned.Policy{
Spec: unversioned.PolicySpec{
Type: testPolicyType,
},
}
out := &Policy{}
if err := Convert_unversioned_Policy_To_v1alpha1_Policy(in, out, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.ObjectMeta.Name != in.Spec.Type {
t.Fatalf("expect metadata.name to be %s, but got %s", in.Spec.Type, out.ObjectMeta.Name)
}
}

func TestConvert_v1alpha1_Policy_To_unversioned_Policy(t *testing.T) {
in := &Policy{
ObjectMeta: metav1.ObjectMeta{
Name: testPolicyType,
},
}
out := &unversioned.Policy{}
if err := Convert_v1alpha1_Policy_To_unversioned_Policy(in, out, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.ObjectMeta.Name != constants.RatifyPolicy {
t.Fatalf("expect metadata.name to be %s, but got %s", constants.RatifyPolicy, out.ObjectMeta.Name)
}
if out.Spec.Type != in.ObjectMeta.Name {
t.Fatalf("expect spec.type to be %s, but got %s", in.ObjectMeta.Name, out.Spec.Type)
}
}
2 changes: 1 addition & 1 deletion api/v1alpha1/policy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type PolicyStatus struct {

// +kubebuilder:object:root=true
// +kubebuilder:resource:scope="Cluster"
// +kubebuilder:deprecatedversion:warning="v1alpha1 of the Policy API has been deprecated. Please migrate to v1beta1."
// Policy is the Schema for the policies API
type Policy struct {
metav1.TypeMeta `json:",inline"`
Expand All @@ -51,7 +52,6 @@ type Policy struct {
}

// +kubebuilder:object:root=true
// +kubebuilder:storageversion
// PolicyList contains a list of Policy
type PolicyList struct {
metav1.TypeMeta `json:",inline"`
Expand Down
78 changes: 41 additions & 37 deletions api/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading