Skip to content

Commit

Permalink
Merge branch 'main' into deprecate-kyma-provision-gardener
Browse files Browse the repository at this point in the history
  • Loading branch information
kolodziejczak authored May 23, 2024
2 parents 11ac743 + 1494ad8 commit e2aa094
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 106 deletions.
42 changes: 36 additions & 6 deletions apis/gateway/v1beta2/apirule_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ import (
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

var beta1to2statusConversionMap = map[v1beta1.StatusCode]State{
v1beta1.StatusOK: Ready,
v1beta1.StatusError: Error,
v1beta1.StatusWarning: Warning,

// StatusSkipped is not supported in v1beta2, and it happens only when another component has Error or Warning status
// In this case, we map it to Warning
v1beta1.StatusSkipped: Warning,
}

func convertMap(m map[v1beta1.StatusCode]State) map[State]v1beta1.StatusCode {
inv := make(map[State]v1beta1.StatusCode)
for k, v := range m {
inv[v] = k
}
return inv
}

// The 2 => 1 map is generated automatically based on 1 => 2 map
var beta2to1statusConversionMap = convertMap(beta1to2statusConversionMap)

// Converts this ApiRule (v1beta2) to the Hub version (v1beta1)
func (apiRuleBeta2 *APIRule) ConvertTo(hub conversion.Hub) error {
apiRuleBeta1 := hub.(*v1beta1.APIRule)
Expand All @@ -24,9 +45,14 @@ func (apiRuleBeta2 *APIRule) ConvertTo(hub conversion.Hub) error {
if err != nil {
return err
}
err = convertOverJson(apiRuleBeta2.Status, &apiRuleBeta1.Status)
if err != nil {
return err

// Status
apiRuleBeta1.Status = v1beta1.APIRuleStatus{
APIRuleStatus: &v1beta1.APIRuleResourceStatus{
Code: beta2to1statusConversionMap[apiRuleBeta2.Status.State],
Description: apiRuleBeta2.Status.Description,
},
LastProcessedTime: apiRuleBeta2.Status.LastProcessedTime,
}

// Only one host is supported in v1beta1, so we use the first one from the list
Expand Down Expand Up @@ -76,9 +102,13 @@ func (apiRuleBeta2 *APIRule) ConvertFrom(hub conversion.Hub) error {
if err != nil {
return err
}
err = convertOverJson(apiRuleBeta1.Status, &apiRuleBeta2.Status)
if err != nil {
return err

if apiRuleBeta1.Status.APIRuleStatus != nil {
apiRuleBeta2.Status = APIRuleStatus{
State: beta1to2statusConversionMap[apiRuleBeta1.Status.APIRuleStatus.Code],
Description: apiRuleBeta1.Status.APIRuleStatus.Description,
LastProcessedTime: apiRuleBeta1.Status.LastProcessedTime,
}
}

apiRuleBeta2.Spec.Hosts = []*Host{new(Host)}
Expand Down
91 changes: 91 additions & 0 deletions apis/gateway/v1beta2/apirule_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,49 @@ var _ = Describe("APIRule Conversion", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("either jwt is configured or noAuth must be set to true in a rule"))
})
It("should convert v1beta2 Ready state to OK status from APIRuleStatus", func() {
// given
apiRuleBeta2 := v1beta2.APIRule{
Spec: v1beta2.APIRuleSpec{
Hosts: []*v1beta2.Host{&host1},
},
Status: v1beta2.APIRuleStatus{
State: v1beta2.Ready,
Description: "description",
},
}
apiRuleBeta1 := v1beta1.APIRule{}

// when
err := apiRuleBeta2.ConvertTo(&apiRuleBeta1)

// then
Expect(err).To(BeNil())
Expect(apiRuleBeta1.Status.APIRuleStatus.Code).To(Equal(v1beta1.StatusOK))
Expect(apiRuleBeta1.Status.APIRuleStatus.Description).To(Equal("description"))
})

It("should convert v1beta2 Error state to Error status from APIRuleStatusError status from APIRuleStatus", func() {
// given
apiRuleBeta2 := v1beta2.APIRule{
Spec: v1beta2.APIRuleSpec{
Hosts: []*v1beta2.Host{&host1},
},
Status: v1beta2.APIRuleStatus{
State: v1beta2.Error,
Description: "description",
},
}
apiRuleBeta1 := v1beta1.APIRule{}

// when
err := apiRuleBeta2.ConvertTo(&apiRuleBeta1)

// then
Expect(err).To(BeNil())
Expect(apiRuleBeta1.Status.APIRuleStatus.Code).To(Equal(v1beta1.StatusError))
Expect(apiRuleBeta1.Status.APIRuleStatus.Description).To(Equal("description"))
})
})

Describe("v1beta1 to v1beta2", func() {
Expand Down Expand Up @@ -536,5 +579,53 @@ var _ = Describe("APIRule Conversion", func() {
Expect(apiRuleBeta2.Spec.Rules[1].NoAuth).To(BeNil())
Expect(apiRuleBeta2.Spec.Rules[1].Jwt).ToNot(BeNil())
})

It("should convert OK status from APIRuleStatus to v1beta2 Ready state", func() {
// given
apiRuleBeta1 := v1beta1.APIRule{
Spec: v1beta1.APIRuleSpec{
Host: &host1string,
},
Status: v1beta1.APIRuleStatus{
APIRuleStatus: &v1beta1.APIRuleResourceStatus{
Code: v1beta1.StatusOK,
Description: "description",
},
},
}
apiRuleBeta2 := v1beta2.APIRule{}

// when
err := apiRuleBeta2.ConvertFrom(&apiRuleBeta1)

// then
Expect(err).To(BeNil())
Expect(apiRuleBeta2.Status.State).To(Equal(v1beta2.Ready))
Expect(apiRuleBeta2.Status.Description).To(Equal("description"))
})

It("should convert Error status from APIRuleStatus to v1beta2 Error state", func() {
// given
apiRuleBeta1 := v1beta1.APIRule{
Spec: v1beta1.APIRuleSpec{
Host: &host1string,
},
Status: v1beta1.APIRuleStatus{
APIRuleStatus: &v1beta1.APIRuleResourceStatus{
Code: v1beta1.StatusError,
Description: "description",
},
},
}
apiRuleBeta2 := v1beta2.APIRule{}

// when
err := apiRuleBeta2.ConvertFrom(&apiRuleBeta1)

// then
Expect(err).To(BeNil())
Expect(apiRuleBeta2.Status.State).To(Equal(v1beta2.Error))
Expect(apiRuleBeta2.Status.Description).To(Equal("description"))
})
})
})
40 changes: 15 additions & 25 deletions apis/gateway/v1beta2/apirule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)

// StatusCode describing APIRule.
type StatusCode string
type State string

const (
//StatusOK is set when the reconciliation finished successfully
StatusOK StatusCode = "OK"
//StatusSkipped is set when reconciliation of the APIRule component was skipped
StatusSkipped StatusCode = "SKIPPED"
//StatusError is set when an error happened during reconciliation of the APIRule
StatusError StatusCode = "ERROR"
//StatusWarning is set if a user action is required
StatusWarning StatusCode = "WARNING"
Ready State = "Ready"
Processing State = "Processing"
Error State = "Error"
Deleting State = "Deleting"
Warning State = "Warning"
)

// APIRuleSpec defines the desired state of ApiRule.
Expand Down Expand Up @@ -65,20 +61,20 @@ type Host string

// APIRuleStatus describes the observed state of ApiRule.
type APIRuleStatus struct {
LastProcessedTime *metav1.Time `json:"lastProcessedTime,omitempty"`
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
APIRuleStatus *APIRuleResourceStatus `json:"APIRuleStatus,omitempty"`
VirtualServiceStatus *APIRuleResourceStatus `json:"virtualServiceStatus,omitempty"`
// +optional
RequestAuthenticationStatus *APIRuleResourceStatus `json:"requestAuthenticationStatus,omitempty"`
// +optional
AuthorizationPolicyStatus *APIRuleResourceStatus `json:"authorizationPolicyStatus,omitempty"`
LastProcessedTime *metav1.Time `json:"lastProcessedTime,omitempty"`
// State signifies current state of APIRule.
// Value can be one of ("Ready", "Processing", "Error", "Deleting", "Warning").
// +kubebuilder:validation:Required
// +kubebuilder:validation:Enum=Processing;Deleting;Ready;Error;Warning
State State `json:"state"`
// Description of APIRule status
Description string `json:"description,omitempty"`
}

// APIRule is the Schema for ApiRule APIs.
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.APIRuleStatus.code"
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.state"
// +kubebuilder:printcolumn:name="Hosts",type="string",JSONPath=".spec.hosts"
type APIRule struct {
metav1.TypeMeta `json:",inline"`
Expand Down Expand Up @@ -140,12 +136,6 @@ type Rule struct {
// +kubebuilder:validation:Enum=GET;HEAD;POST;PUT;DELETE;CONNECT;OPTIONS;TRACE;PATCH
type HttpMethod string

// APIRuleResourceStatus describes the status of APIRule.
type APIRuleResourceStatus struct {
Code StatusCode `json:"code,omitempty"`
Description string `json:"desc,omitempty"`
}

func init() {
SchemeBuilder.Register(&APIRule{}, &APIRuleList{})
}
Expand Down
35 changes: 0 additions & 35 deletions apis/gateway/v1beta2/zz_generated.deepcopy.go

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

57 changes: 17 additions & 40 deletions config/crd/bases/gateway.kyma-project.io_apirules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ spec:
subresources:
status: {}
- additionalPrinterColumns:
- jsonPath: .status.APIRuleStatus.code
- jsonPath: .status.state
name: Status
type: string
- jsonPath: .spec.hosts
Expand Down Expand Up @@ -538,48 +538,25 @@ spec:
status:
description: APIRuleStatus describes the observed state of ApiRule.
properties:
APIRuleStatus:
description: APIRuleResourceStatus describes the status of APIRule.
properties:
code:
description: StatusCode describing APIRule.
type: string
desc:
type: string
type: object
authorizationPolicyStatus:
description: APIRuleResourceStatus describes the status of APIRule.
properties:
code:
description: StatusCode describing APIRule.
type: string
desc:
type: string
type: object
description:
description: Description of APIRule status
type: string
lastProcessedTime:
format: date-time
type: string
observedGeneration:
format: int64
type: integer
requestAuthenticationStatus:
description: APIRuleResourceStatus describes the status of APIRule.
properties:
code:
description: StatusCode describing APIRule.
type: string
desc:
type: string
type: object
virtualServiceStatus:
description: APIRuleResourceStatus describes the status of APIRule.
properties:
code:
description: StatusCode describing APIRule.
type: string
desc:
type: string
type: object
state:
description: |-
State signifies current state of APIRule.
Value can be one of ("Ready", "Processing", "Error", "Deleting", "Warning").
enum:
- Processing
- Deleting
- Ready
- Error
- Warning
type: string
required:
- state
type: object
type: object
served: true
Expand Down

0 comments on commit e2aa094

Please sign in to comment.