Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/base64"
"fmt"
"net"
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -32,7 +33,6 @@ import (
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
Expand All @@ -42,14 +42,13 @@ import (
"k8s.io/client-go/util/flowcontrol"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/log"
karpapis "sigs.k8s.io/karpenter/pkg/apis"
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
karpv1alpha1 "sigs.k8s.io/karpenter/pkg/apis/v1alpha1"

"sigs.k8s.io/karpenter/pkg/operator"
coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"

"github.com/Azure/karpenter-provider-azure/pkg/apis/v1beta1"

"github.com/Azure/karpenter-provider-azure/pkg/auth"
azurecache "github.com/Azure/karpenter-provider-azure/pkg/cache"

Expand Down Expand Up @@ -310,16 +309,7 @@ func getVnetGUID(ctx context.Context, creds azcore.TokenCredential, cfg *auth.Co

// WaitForCRDs waits for the required CRDs to be available with a timeout
func WaitForCRDs(ctx context.Context, timeout time.Duration, config *rest.Config, log logr.Logger) error {
gvk := func(obj runtime.Object) schema.GroupVersionKind {
return lo.Must(apiutil.GVKForObject(obj, scheme.Scheme))
}
var requiredGVKs = []schema.GroupVersionKind{
gvk(&karpv1.NodePool{}),
gvk(&karpv1.NodeClaim{}),
gvk(&karpv1alpha1.NodeOverlay{}),
gvk(&v1beta1.AKSNodeClass{}),
}

requiredGVKs := getRequiredGVKs()
client, err := rest.HTTPClientFor(config)
if err != nil {
return fmt.Errorf("creating kubernetes client, %w", err)
Expand All @@ -329,7 +319,7 @@ func WaitForCRDs(ctx context.Context, timeout time.Duration, config *rest.Config
return fmt.Errorf("creating dynamic rest mapper, %w", err)
}

log.Info("waiting for required CRDs to be available", "timeout", timeout)
log.Info("waiting for required CRDs to be available", "gvks", requiredGVKs, "timeout", timeout)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

Expand Down Expand Up @@ -387,3 +377,21 @@ func getCredential(env *auth.Environment) (azcore.TokenCredential, error) {

return auth.NewTokenWrapper(cred), nil
}

func getRequiredGVKs() []schema.GroupVersionKind {
// controller-runtime internal, ignore them as we don't watch them
internalTypes := []string{"WatchEvent", "UpdateOptions", "DeleteOptions", "ListOptions", "CreateOptions", "PatchOptions", "GetOptions"}
requiredGVKs := lo.Filter(lo.Keys(scheme.Scheme.AllKnownTypes()), func(gvk schema.GroupVersionKind, _ int) bool {
if lo.Contains(internalTypes, gvk.Kind) {
return false
}

// Ignore lists as well, we don't watch these
if strings.HasSuffix(gvk.Kind, "List") {
return false
}

return gvk.Group == karpapis.Group || gvk.Group == v1beta1.Group
})
return requiredGVKs
}
64 changes: 64 additions & 0 deletions pkg/operator/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Portions Copyright (c) Microsoft Corporation.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package operator

import (
"context"
"testing"

"github.com/awslabs/operatorpkg/object"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
karpv1alpha1 "sigs.k8s.io/karpenter/pkg/apis/v1alpha1"
coretest "sigs.k8s.io/karpenter/pkg/test"
. "sigs.k8s.io/karpenter/pkg/utils/testing"

"github.com/Azure/karpenter-provider-azure/pkg/apis"
"github.com/Azure/karpenter-provider-azure/pkg/apis/v1beta1"
)

var (
ctx context.Context
env *coretest.Environment
)

func TestOperator(t *testing.T) {
ctx = TestContextWithLogger(t)

RegisterFailHandler(Fail)
RunSpecs(t, "Operator")
}

var _ = BeforeSuite(func() {
env = coretest.NewEnvironment(coretest.WithCRDs(apis.CRDs...))
})

var _ = AfterSuite(func() {
Expect(env.Stop()).To(Succeed(), "Failed to stop environment")
})

var _ = Describe("getRequiredGVKs", func() {
It("should return the GVKs of the CRDs", func() {
gvks := getRequiredGVKs()
Expect(gvks).To(HaveLen(4))
Expect(gvks).To(ContainElement(object.GVK(&karpv1.NodePool{})))
Expect(gvks).To(ContainElement(object.GVK(&karpv1.NodeClaim{})))
Expect(gvks).To(ContainElement(object.GVK(&karpv1alpha1.NodeOverlay{})))
Expect(gvks).To(ContainElement(object.GVK(&v1beta1.AKSNodeClass{})))
})
})
Loading