Skip to content

Commit

Permalink
Moved api pkg from eventing-controller (#260)
Browse files Browse the repository at this point in the history
* Moved api pkg from eventing-controller

* updated samples

* Fix failing tests

* Delete external CRD folder

* Revert 02-configuration.md doc

---------

Co-authored-by: Mansur Uralov <[email protected]>
  • Loading branch information
2 people authored and friedrichwilken committed Dec 14, 2023
1 parent 8ac92c6 commit 738bd7b
Show file tree
Hide file tree
Showing 141 changed files with 5,154 additions and 891 deletions.
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,9 @@ ifndef ignore-not-found
endif

.PHONY: install
install: download-external-crds manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -

.PHONY: download-external-crds
download-external-crds:
curl -s -L -o config/crd/external/subscriptions.eventing.kyma-project.io.crd.yaml https://raw.githubusercontent.com/kyma-project/kyma/main/installation/resources/crds/eventing/subscriptions.eventing.kyma-project.io.crd.yaml

.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
Expand Down
27 changes: 26 additions & 1 deletion PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ resources:
domain: kyma-project.io
group: operator
kind: Eventing
path: github.com/kyma-project/eventing-manager/api/batch/v1alpha1
path: github.com/kyma-project/eventing-manager/api/operator/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: kyma-project.io
group: eventing
kind: Subscription
path: github.com/kyma-project/eventing-manager/api/eventing/v1alpha1
version: v1alpha1
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: kyma-project.io
group: eventing
kind: Subscription
path: github.com/kyma-project/eventing-manager/api/eventing/v1alpha2
version: v1alpha2
webhooks:
defaulting: true
validation: true
conversion: true
webhookVersion: v1
version: "3"
260 changes: 260 additions & 0 deletions api/eventing/v1alpha1/condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
package v1alpha1

import (
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ConditionType string

const (
ConditionSubscribed ConditionType = "Subscribed"
ConditionSubscriptionActive ConditionType = "Subscription active"
ConditionAPIRuleStatus ConditionType = "APIRule status"
ConditionWebhookCallStatus ConditionType = "Webhook call status"

ConditionPublisherProxyReady ConditionType = "Publisher Proxy Ready"
ConditionControllerReady ConditionType = "Subscription Controller Ready"
)

var allSubscriptionConditions = MakeSubscriptionConditions()

type Condition struct {
// Short description of the condition.
Type ConditionType `json:"type,omitempty"`
// Status of the condition. The value is either `True`, `False`, or `Unknown`.
Status corev1.ConditionStatus `json:"status"`
// Defines the date of the last condition status change.
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
// Defines the reason for the condition status change.
Reason ConditionReason `json:"reason,omitempty"`
// Provides more details about the condition status change.
Message string `json:"message,omitempty"`
}

type ConditionReason string

const (
// BEB Conditions.
ConditionReasonSubscriptionActive ConditionReason = "BEB Subscription active"
ConditionReasonSubscriptionNotActive ConditionReason = "BEB Subscription not active"
ConditionReasonSubscriptionDeleted ConditionReason = "BEB Subscription deleted"
ConditionReasonAPIRuleStatusReady ConditionReason = "APIRule status ready"
ConditionReasonAPIRuleStatusNotReady ConditionReason = "APIRule status not ready"

// Common backend Conditions.
ConditionReasonSubscriptionControllerReady ConditionReason = "Subscription controller started"
ConditionReasonSubscriptionControllerNotReady ConditionReason = "Subscription controller not ready"
ConditionReasonPublisherDeploymentReady ConditionReason = "Publisher proxy deployment ready"
ConditionReasonPublisherDeploymentNotReady ConditionReason = "Publisher proxy deployment not ready"
)

// initializeConditions sets unset conditions to Unknown.
func initializeConditions(initialConditions, currentConditions []Condition) []Condition {
givenConditions := make(map[ConditionType]Condition)

// create map of Condition per ConditionType
for _, condition := range currentConditions {
givenConditions[condition.Type] = condition
}

finalConditions := currentConditions
// check if every Condition is present in the current Conditions
for _, expectedCondition := range initialConditions {
if _, ok := givenConditions[expectedCondition.Type]; !ok {
// and add it if it is missing
finalConditions = append(finalConditions, expectedCondition)
}
}
return finalConditions
}

// InitializeConditions sets unset Subscription conditions to Unknown.
func (s *SubscriptionStatus) InitializeConditions() {
initialConditions := MakeSubscriptionConditions()
s.Conditions = initializeConditions(initialConditions, s.Conditions)
}

func (s SubscriptionStatus) IsReady() bool {
if !ContainSameConditionTypes(allSubscriptionConditions, s.Conditions) {
return false
}

// the subscription is ready if all its conditions are evaluated to true
for _, c := range s.Conditions {
if c.Status != corev1.ConditionTrue {
return false
}
}
return true
}

func (s SubscriptionStatus) FindCondition(conditionType ConditionType) *Condition {
for _, condition := range s.Conditions {
if conditionType == condition.Type {
return &condition
}
}
return nil
}

// ShouldUpdateReadyStatus checks if there is a mismatch between the
// subscription Ready Status and the Ready status of all the conditions.
func (s SubscriptionStatus) ShouldUpdateReadyStatus() bool {
if !s.Ready && s.IsReady() || s.Ready && !s.IsReady() {
return true
}
return false
}

// MakeSubscriptionConditions creates a map of all conditions which the Subscription should have.
func MakeSubscriptionConditions() []Condition {
conditions := []Condition{
{
Type: ConditionAPIRuleStatus,
LastTransitionTime: metav1.Now(),
Status: corev1.ConditionUnknown,
},
{
Type: ConditionSubscribed,
LastTransitionTime: metav1.Now(),
Status: corev1.ConditionUnknown,
},
{
Type: ConditionSubscriptionActive,
LastTransitionTime: metav1.Now(),
Status: corev1.ConditionUnknown,
},
{
Type: ConditionWebhookCallStatus,
LastTransitionTime: metav1.Now(),
Status: corev1.ConditionUnknown,
},
}
return conditions
}

func ContainSameConditionTypes(conditions1, conditions2 []Condition) bool {
if len(conditions1) != len(conditions2) {
return false
}

for _, condition := range conditions1 {
if !containConditionType(conditions2, condition.Type) {
return false
}
}

return true
}

func containConditionType(conditions []Condition, conditionType ConditionType) bool {
for _, condition := range conditions {
if condition.Type == conditionType {
return true
}
}

return false
}

func MakeCondition(conditionType ConditionType, reason ConditionReason, status corev1.ConditionStatus, message string) Condition {
return Condition{
Type: conditionType,
Status: status,
LastTransitionTime: metav1.Now(),
Reason: reason,
// TODO: https://github.com/kyma-project/kyma/issues/9770
Message: message,
}
}

func (s *SubscriptionStatus) IsConditionSubscribed() bool {
for _, condition := range s.Conditions {
if condition.Type == ConditionSubscribed && condition.Status == corev1.ConditionTrue {
return true
}
}
return false
}

func (s *SubscriptionStatus) IsConditionWebhookCall() bool {
for _, condition := range s.Conditions {
if condition.Type == ConditionWebhookCallStatus &&
(condition.Status == corev1.ConditionTrue || condition.Status == corev1.ConditionUnknown) {
return true
}
}
return false
}

func (s *SubscriptionStatus) GetConditionAPIRuleStatus() corev1.ConditionStatus {
for _, condition := range s.Conditions {
if condition.Type == ConditionAPIRuleStatus {
return condition.Status
}
}
return corev1.ConditionUnknown
}

func (s *SubscriptionStatus) SetConditionAPIRuleStatus(err error) {
reason := ConditionReasonAPIRuleStatusReady
status := corev1.ConditionTrue
message := ""
if err != nil {
reason = ConditionReasonAPIRuleStatusNotReady
status = corev1.ConditionFalse
message = err.Error()
}

newConditions := []Condition{MakeCondition(ConditionAPIRuleStatus, reason, status, message)}
for _, condition := range s.Conditions {
if condition.Type == ConditionAPIRuleStatus {
continue
}
newConditions = append(newConditions, condition)
}
s.Conditions = newConditions
}

func CreateMessageForConditionReasonSubscriptionCreated(bebName string) string {
return fmt.Sprintf("BEB-subscription-name=%s", bebName)
}

// ConditionsEquals checks if two list of conditions are equal.
func ConditionsEquals(existing, expected []Condition) bool {
// not equal if length is different
if len(existing) != len(expected) {
return false
}

// compile map of Conditions per ConditionType
existingMap := make(map[ConditionType]Condition, len(existing))
for _, value := range existing {
existingMap[value.Type] = value
}

for _, value := range expected {
if !ConditionEquals(existingMap[value.Type], value) {
return false
}
}

return true
}

// ConditionEquals checks if two conditions are equal.
func ConditionEquals(existing, expected Condition) bool {
isTypeEqual := existing.Type == expected.Type
isStatusEqual := existing.Status == expected.Status
isReasonEqual := existing.Reason == expected.Reason
isMessageEqual := existing.Message == expected.Message

if !isStatusEqual || !isReasonEqual || !isMessageEqual || !isTypeEqual {
return false
}

return true
}
Loading

0 comments on commit 738bd7b

Please sign in to comment.