Skip to content

Commit 56a2ca3

Browse files
committed
fix: don't hardcode CRDs, discover them from scheme
1 parent 8e3e97e commit 56a2ca3

2 files changed

Lines changed: 86 additions & 14 deletions

File tree

pkg/operator/operator.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/base64"
2222
"fmt"
2323
"net"
24+
"strings"
2425
"time"
2526

2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -32,7 +33,6 @@ import (
3233
"github.com/samber/lo"
3334
corev1 "k8s.io/api/core/v1"
3435
"k8s.io/apimachinery/pkg/api/meta"
35-
"k8s.io/apimachinery/pkg/runtime"
3636
"k8s.io/apimachinery/pkg/runtime/schema"
3737
"k8s.io/apimachinery/pkg/util/wait"
3838
"k8s.io/client-go/kubernetes"
@@ -42,14 +42,13 @@ import (
4242
"k8s.io/client-go/util/flowcontrol"
4343
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
4444
"sigs.k8s.io/controller-runtime/pkg/log"
45+
karpapis "sigs.k8s.io/karpenter/pkg/apis"
4546
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
46-
karpv1alpha1 "sigs.k8s.io/karpenter/pkg/apis/v1alpha1"
4747

4848
"sigs.k8s.io/karpenter/pkg/operator"
4949
coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"
5050

5151
"github.com/Azure/karpenter-provider-azure/pkg/apis/v1beta1"
52-
5352
"github.com/Azure/karpenter-provider-azure/pkg/auth"
5453
azurecache "github.com/Azure/karpenter-provider-azure/pkg/cache"
5554

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

311310
// WaitForCRDs waits for the required CRDs to be available with a timeout
312311
func WaitForCRDs(ctx context.Context, timeout time.Duration, config *rest.Config, log logr.Logger) error {
313-
gvk := func(obj runtime.Object) schema.GroupVersionKind {
314-
return lo.Must(apiutil.GVKForObject(obj, scheme.Scheme))
315-
}
316-
var requiredGVKs = []schema.GroupVersionKind{
317-
gvk(&karpv1.NodePool{}),
318-
gvk(&karpv1.NodeClaim{}),
319-
gvk(&karpv1alpha1.NodeOverlay{}),
320-
gvk(&v1beta1.AKSNodeClass{}),
321-
}
322-
312+
requiredGVKs := getRequiredGVKs()
323313
client, err := rest.HTTPClientFor(config)
324314
if err != nil {
325315
return fmt.Errorf("creating kubernetes client, %w", err)
@@ -329,7 +319,7 @@ func WaitForCRDs(ctx context.Context, timeout time.Duration, config *rest.Config
329319
return fmt.Errorf("creating dynamic rest mapper, %w", err)
330320
}
331321

332-
log.Info("waiting for required CRDs to be available", "timeout", timeout)
322+
log.Info("waiting for required CRDs to be available", "gvks", requiredGVKs, "timeout", timeout)
333323
ctx, cancel := context.WithTimeout(ctx, timeout)
334324
defer cancel()
335325

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

388378
return auth.NewTokenWrapper(cred), nil
389379
}
380+
381+
func getRequiredGVKs() []schema.GroupVersionKind {
382+
// controller-runtime internal, ignore them as we don't watch them
383+
internalTypes := []string{"WatchEvent", "UpdateOptions", "DeleteOptions", "ListOptions", "CreateOptions", "PatchOptions", "GetOptions"}
384+
requiredGVKs := lo.Filter(lo.Keys(scheme.Scheme.AllKnownTypes()), func(gvk schema.GroupVersionKind, _ int) bool {
385+
if lo.Contains(internalTypes, gvk.Kind) {
386+
return false
387+
}
388+
389+
// Ignore lists as well, we don't watch these
390+
if strings.HasSuffix(gvk.Kind, "List") {
391+
return false
392+
}
393+
394+
return gvk.Group == karpapis.Group || gvk.Group == v1beta1.Group
395+
})
396+
return requiredGVKs
397+
}

pkg/operator/suite_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Portions Copyright (c) Microsoft Corporation.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package operator
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/awslabs/operatorpkg/object"
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
27+
karpv1alpha1 "sigs.k8s.io/karpenter/pkg/apis/v1alpha1"
28+
coretest "sigs.k8s.io/karpenter/pkg/test"
29+
. "sigs.k8s.io/karpenter/pkg/utils/testing"
30+
31+
"github.com/Azure/karpenter-provider-azure/pkg/apis"
32+
"github.com/Azure/karpenter-provider-azure/pkg/apis/v1beta1"
33+
)
34+
35+
var (
36+
ctx context.Context
37+
env *coretest.Environment
38+
)
39+
40+
func TestOperator(t *testing.T) {
41+
ctx = TestContextWithLogger(t)
42+
43+
RegisterFailHandler(Fail)
44+
RunSpecs(t, "Operator")
45+
}
46+
47+
var _ = BeforeSuite(func() {
48+
env = coretest.NewEnvironment(coretest.WithCRDs(apis.CRDs...))
49+
})
50+
51+
var _ = AfterSuite(func() {
52+
Expect(env.Stop()).To(Succeed(), "Failed to stop environment")
53+
})
54+
55+
var _ = Describe("getRequiredGVKs", func() {
56+
It("should return the GVKs of the CRDs", func() {
57+
gvks := getRequiredGVKs()
58+
Expect(gvks).To(HaveLen(4))
59+
Expect(gvks).To(ContainElement(object.GVK(&karpv1.NodePool{})))
60+
Expect(gvks).To(ContainElement(object.GVK(&karpv1.NodeClaim{})))
61+
Expect(gvks).To(ContainElement(object.GVK(&karpv1alpha1.NodeOverlay{})))
62+
Expect(gvks).To(ContainElement(object.GVK(&v1beta1.AKSNodeClass{})))
63+
})
64+
})

0 commit comments

Comments
 (0)