Skip to content

Commit

Permalink
Check CRD existence in Labeler (#1373)
Browse files Browse the repository at this point in the history
* globalaccounts fixes

* wip

* globalaccounts fixes

* wip

* wip

* wip

* wip
  • Loading branch information
ukff authored Oct 23, 2024
1 parent cb4efd8 commit b16ce67
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
35 changes: 33 additions & 2 deletions internal/broker/instance_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -728,6 +732,7 @@ func TestUpdateExpiredInstance(t *testing.T) {
}

func TestSubaccountMovement(t *testing.T) {
registerCRD()
runtimeId := createFakeCRs(t)
defer cleanFakeCRs(t, runtimeId)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions internal/broker/labeling.go → internal/broker/labeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion internal/globalaccounts/globalaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b16ce67

Please sign in to comment.