diff --git a/internal/broker/instance_update_test.go b/internal/broker/instance_update_test.go index 20c18862cb..2e72c4fabc 100644 --- a/internal/broker/instance_update_test.go +++ b/internal/broker/instance_update_test.go @@ -5,15 +5,19 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "testing" "time" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "github.com/google/uuid" "github.com/kyma-project/kyma-environment-broker/internal" "github.com/kyma-project/kyma-environment-broker/internal/broker/automock" "github.com/kyma-project/kyma-environment-broker/internal/k8s" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/controller-runtime/pkg/client/fake" + "k8s.io/apimachinery/pkg/runtime/schema" "github.com/stretchr/testify/mock" @@ -728,6 +732,7 @@ func TestUpdateExpiredInstance(t *testing.T) { } func TestSubaccountMovement(t *testing.T) { + registerCRD() runtimeId := createFakeCRs(t) defer cleanFakeCRs(t, runtimeId) @@ -817,7 +822,8 @@ func TestLabelChangeWhenMovingSubaccount(t *testing.T) { oldGlobalAccountId = "first-global-account-id" newGlobalAccountId = "changed-global-account-id" ) - + registerCRD() + createCRDs(t) runtimeId := createFakeCRs(t) defer cleanFakeCRs(t, runtimeId) @@ -913,6 +919,31 @@ func TestLabelChangeWhenMovingSubaccount(t *testing.T) { }) } +func registerCRD() { + var customResourceDefinition apiextensionsv1.CustomResourceDefinition + customResourceDefinition.SetGroupVersionKind(schema.GroupVersionKind{ + Group: "apiextensions.k8s.io", + Version: "v1", + Kind: "CustomResourceDefinition", + }) + fakeKcpK8sClient.Scheme().AddKnownTypeWithName(customResourceDefinition.GroupVersionKind(), &customResourceDefinition) +} + +func createCRDs(t *testing.T) { + f := func(gvkName string) { + var customResourceDefinition apiextensionsv1.CustomResourceDefinition + gvk, err := k8s.GvkByName(gvkName) + require.NoError(t, err) + crdName := fmt.Sprintf("%ss.%s", strings.ToLower(gvk.Kind), gvk.Group) + customResourceDefinition.SetName(crdName) + err = fakeKcpK8sClient.Create(context.Background(), &customResourceDefinition) + require.NoError(t, err) + } + f(k8s.KymaCr) + f(k8s.GardenerClusterCr) + f(k8s.RuntimeCr) +} + func createFakeCRs(t *testing.T) string { runtimeID := uuid.New().String() f := func(t *testing.T, runtimeID string, crName string) { diff --git a/internal/broker/labeling.go b/internal/broker/labeler.go similarity index 68% rename from internal/broker/labeling.go rename to internal/broker/labeler.go index d137feb773..b941de61de 100644 --- a/internal/broker/labeling.go +++ b/internal/broker/labeler.go @@ -4,10 +4,15 @@ import ( "context" "errors" "fmt" + "strings" "github.com/kyma-project/kyma-environment-broker/internal/k8s" "github.com/sirupsen/logrus" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + metaerrors "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -41,6 +46,15 @@ func (l *Labeler) updateCrLabel(id, crName, newGlobalAccountId string) error { var k8sObject unstructured.Unstructured k8sObject.SetGroupVersionKind(gvk) + crdExists, err := l.checkCRDExistence(gvk) + if err != nil { + return fmt.Errorf("while checking existence of CRD for %s: %s", crName, err.Error()) + } + if !crdExists { + l.log.Infof("CRD for %s does not exist, skipping", crName) + return nil + } + err = l.kcpClient.Get(context.Background(), types.NamespacedName{Namespace: KcpNamespace, Name: id}, &k8sObject) if err != nil { return fmt.Errorf("while getting k8s object of type %s from kcp cluster for instance %s, due to: %s", crName, id, err.Error()) @@ -73,3 +87,18 @@ func addOrOverrideLabel(k8sObject *unstructured.Unstructured, key, value string) return nil } + +func (l *Labeler) checkCRDExistence(gvk schema.GroupVersionKind) (bool, error) { + crdName := fmt.Sprintf("%ss.%s", strings.ToLower(gvk.Kind), gvk.Group) + crd := &apiextensionsv1.CustomResourceDefinition{} + if err := l.kcpClient.Get(context.Background(), client.ObjectKey{Name: crdName}, crd); err != nil { + if k8serrors.IsNotFound(err) || metaerrors.IsNoMatchError(err) { + l.log.Errorf("CustomResourceDefinition does not exist %s", err.Error()) + return false, nil + } else { + l.log.Errorf("while getting CRD %s: %s", crdName, err.Error()) + return false, err + } + } + return true, nil +} diff --git a/internal/globalaccounts/globalaccounts.go b/internal/globalaccounts/globalaccounts.go index ad9b661b67..7ba8ba8a5c 100644 --- a/internal/globalaccounts/globalaccounts.go +++ b/internal/globalaccounts/globalaccounts.go @@ -248,7 +248,7 @@ func showReport(logs *logrus.Logger, okCount, mismatch, getInstanceErrorCounts, logs.Infof("no. instances in KEB which failed to get from db: %d", getInstanceErrorCounts) logs.Infof("no. instances in KEB with empty SA: %d", kebInstanceMissingSACount) logs.Infof("no. instances in KEB with empty GA: %d", kebInstanceMissingGACount) - logs.Infof("no. GA missing in SVC: %d", svcGlobalAccountMissing) + logs.Infof("no. GA missing in account service: %d", svcGlobalAccountMissing) logs.Infof("no. failed requests to account service : %d", requestErrorCount) logs.Infof("no. instances with error while updating in : %d", instanceUpdateErrorCount) logs.Infof("no. CR for which update labels failed: %d", labelsUpdateErrorCount)