Skip to content

Commit

Permalink
feat: add type to policy CRD (ratify-project#1079)
Browse files Browse the repository at this point in the history
Signed-off-by: Binbin Li <[email protected]>
  • Loading branch information
binbin-li authored Sep 22, 2023
1 parent 3f3174e commit 4ef2061
Show file tree
Hide file tree
Showing 21 changed files with 844 additions and 101 deletions.
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
}
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 {
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
}
// 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

0 comments on commit 4ef2061

Please sign in to comment.