Skip to content

Commit

Permalink
Merge branch 'main' into feature/kyma-project#196-custom-module-state…
Browse files Browse the repository at this point in the history
…-check
  • Loading branch information
LeelaChacha authored May 27, 2023
2 parents 7dc0904 + 2c81ca3 commit 0bb253b
Show file tree
Hide file tree
Showing 25 changed files with 795 additions and 291 deletions.
1 change: 0 additions & 1 deletion PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ resources:
version: v1beta2
webhooks:
conversion: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
Expand Down
49 changes: 0 additions & 49 deletions api/v1beta2/kyma_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,11 @@ limitations under the License.
package v1beta2

import (
"errors"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

var ErrDuplicateModule = errors.New("duplicate module")

func (kyma *Kyma) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(kyma).
Complete()
}

//nolint:lll
//+kubebuilder:webhook:path=/validate-operator-kyma-project-io-v1beta2-kyma,mutating=false,failurePolicy=fail,sideEffects=None,groups=operator.kyma-project.io,resources=kymas,verbs=create;update,versions=v1beta2,name=vkyma.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &Kyma{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (kyma *Kyma) ValidateCreate() error {
logf.Log.WithName("kyma-resource").
Info("validate create", "name", kyma.Name)
return ValidateKymaModule(kyma)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (kyma *Kyma) ValidateUpdate(_ runtime.Object) error {
logf.Log.WithName("kyma-resource").
Info("validate update", "name", kyma.Name)
return ValidateKymaModule(kyma)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (kyma *Kyma) ValidateDelete() error {
return nil
}

func ValidateKymaModule(kyma *Kyma) error {
moduleSet := map[string]bool{}
for _, module := range kyma.Spec.Modules {
if _, found := moduleSet[module.Name]; found {
return apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: string(KymaKind)},
kyma.Name, field.ErrorList{field.Invalid(
field.NewPath("spec").Child("modules"),
module.Name, ErrDuplicateModule.Error())})
}
moduleSet[module.Name] = true
}
return nil
}
44 changes: 0 additions & 44 deletions api/v1beta2/kyma_webhook_test.go

This file was deleted.

23 changes: 0 additions & 23 deletions api/v1beta2/webhook_kyma_crd_validation_test.go

This file was deleted.

1 change: 0 additions & 1 deletion api/v1beta2/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func SetupWebhook() {
Expect((&v1beta2.Kyma{}).SetupWebhookWithManager(mgr)).NotTo(HaveOccurred())
Expect((&v1beta2.Manifest{}).SetupWebhookWithManager(mgr)).NotTo(HaveOccurred())
Expect((&v1beta2.Watcher{}).SetupWebhookWithManager(mgr)).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
Expand Down
16 changes: 14 additions & 2 deletions config/control-plane/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,27 @@ patches:
- op: add
path: /spec/template/spec/containers/0/args/-
value: --in-kcp-mode
- op: add
path: /spec/template/spec/containers/0/args/-
value: --rate-limiter-burst=2000
- op: add
path: /spec/template/spec/containers/0/args/-
value: --rate-limiter-frequency=1000
- op: add
path: /spec/template/spec/containers/0/args/-
value: --cache-sync-timeout=60m
- op: add
path: /spec/template/spec/containers/0/args/-
value: --max-concurrent-manifest-reconciles=10
- op: add
path: /spec/template/spec/containers/0/args/-
value: --max-concurrent-kyma-reconciles=5
value: --max-concurrent-kyma-reconciles=50
- op: add
path: /spec/template/spec/containers/0/args/-
value: --failure-max-delay=30s
- op: add
path: /spec/template/spec/containers/0/args/-
value: --failure-max-delay=5m
value: --is-kyma-managed
target:
kind: Deployment
Expand Down
6 changes: 6 additions & 0 deletions config/watcher_local_test/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ patches:
- op: add
path: /spec/template/spec/containers/0/args/-
value: --enable-watcher-local-testing
- op: add
path: /spec/template/spec/containers/0/args/-
value: --in-kcp-mode=true
- op: add
path: /spec/template/spec/containers/0/args/-
value: --in-kcp-mode=true
- op: add
path: /spec/template/spec/containers/0/args/-
value: --listener-http-local-mapping=9443
Expand Down
20 changes: 0 additions & 20 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,6 @@ metadata:
creationTimestamp: null
name: validating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-operator-kyma-project-io-v1beta2-kyma
failurePolicy: Fail
name: vkyma.kb.io
rules:
- apiGroups:
- operator.kyma-project.io
apiVersions:
- v1beta2
operations:
- CREATE
- UPDATE
resources:
- kymas
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
108 changes: 105 additions & 3 deletions controllers/control-plane/kcp_controller_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (

"github.com/kyma-project/lifecycle-manager/api/v1beta2"
. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1extensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
Expand Down Expand Up @@ -118,3 +119,104 @@ func updateModuleTemplateSpec(clnt client.Client,
moduleTemplate.Spec.Data.Object["spec"] = map[string]any{"initKey": newValue}
return clnt.Update(ctx, moduleTemplate)
}

func kymaHasCondition(conditionType v1beta2.KymaConditionType, reason string, status metav1.ConditionStatus) func(
clnt client.Client, kymaName, kymaNamespace string) error {
return func(clnt client.Client, kymaName, kymaNamespace string) error {
kyma, err := GetKyma(ctx, clnt, kymaName, kymaNamespace)
if err != nil {
return err
}

for _, cnd := range kyma.Status.Conditions {
if cnd.Type == string(conditionType) && cnd.Reason == reason && cnd.Status == status {
return nil
}
}

return ErrNotContainsExpectedCondition
}
}

func containsModuleTemplateCondition(clnt client.Client, kymaName, kymaNamespace string) error {
kyma, err := GetKyma(ctx, clnt, kymaName, kymaNamespace)
if err != nil {
return err
}
if !kyma.ContainsCondition(v1beta2.ConditionTypeModuleCatalog) {
return ErrNotContainsExpectedCondition
}
return nil
}

func containsNoModulesInSpec(clnt client.Client, kymaName, kymaNamespace string) error {
kyma, err := GetKyma(ctx, clnt, kymaName, kymaNamespace)
if err != nil {
return err
}
if len(kyma.Spec.Modules) != 0 {
return ErrContainsUnexpectedModules
}
return nil
}

func addModuleToKyma(clnt client.Client, kymaName, kymaNamespace string, module v1beta2.Module) error {
kyma, err := GetKyma(ctx, clnt, kymaName, kymaNamespace)
if err != nil {
return err
}

kyma.Spec.Modules = append(
kyma.Spec.Modules, module)
return clnt.Update(ctx, kyma)
}

func updateKymaCRD(clnt client.Client) (*v1extensions.CustomResourceDefinition, error) {
crd, err := fetchCrd(clnt, v1beta2.KymaKind)
if err != nil {
return nil, err
}

crd.SetManagedFields(nil)
crdSpecVersions := crd.Spec.Versions
channelProperty := getCrdSpec(crd).Properties["channel"]
channelProperty.Description = "test change"
getCrdSpec(crd).Properties["channel"] = channelProperty
crd.Spec = v1extensions.CustomResourceDefinitionSpec{
Versions: crdSpecVersions,
Names: crd.Spec.Names,
Group: crd.Spec.Group,
Conversion: crd.Spec.Conversion,
Scope: crd.Spec.Scope,
PreserveUnknownFields: crd.Spec.PreserveUnknownFields,
}
if err := clnt.Patch(ctx, crd,
client.Apply,
client.ForceOwnership,
client.FieldOwner(v1beta2.OperatorName)); err != nil {
return nil, err
}

crd, err = fetchCrd(clnt, v1beta2.KymaKind)
if err != nil {
return nil, err
}
return crd, nil
}

func getCrdSpec(crd *v1extensions.CustomResourceDefinition) v1extensions.JSONSchemaProps {
return crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["spec"]
}

func fetchCrd(clnt client.Client, crdKind v1beta2.Kind) (*v1extensions.CustomResourceDefinition, error) {
crd := &v1extensions.CustomResourceDefinition{}
if err := clnt.Get(
ctx, client.ObjectKey{
Name: fmt.Sprintf("%s.%s", crdKind.Plural(), v1beta2.GroupVersion.Group),
}, crd,
); err != nil {
return nil, err
}

return crd, nil
}
Loading

0 comments on commit 0bb253b

Please sign in to comment.