From d6dca5e887df4ba428c06dd6e13d1f7737a3bc46 Mon Sep 17 00:00:00 2001 From: Kyrylo Yefimenko Date: Fri, 15 Dec 2023 15:17:44 +0000 Subject: [PATCH] Process DataDog ExtendedDaemonSetReplicaSet CRDs --- go.mod | 1 + go.sum | 3 ++ internal/services/controller/controller.go | 9 +++++ .../services/controller/controller_test.go | 33 +++++++++++++++++-- .../services/controller/scheme/register.go | 2 ++ 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index a12e799f..54f40732 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( cloud.google.com/go/compute/metadata v0.2.3 + github.com/DataDog/extendeddaemonset v0.9.0-rc.3 github.com/aws/aws-sdk-go v1.44.294 github.com/aws/karpenter v0.29.2 github.com/aws/karpenter-core v0.29.2 diff --git a/go.sum b/go.sum index d863738f..4c94884a 100644 --- a/go.sum +++ b/go.sum @@ -42,6 +42,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/DataDog/extendeddaemonset v0.9.0-rc.3 h1:jlf40dnVG069YsjU4SfV3UUvgFdFhMfyyyFzsTyd/fk= +github.com/DataDog/extendeddaemonset v0.9.0-rc.3/go.mod h1:ZuADIM2ye3GSLokHyPi/BGhyhvHSCjvDHU3BPXHr8Ow= github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg= github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y= github.com/aws/aws-sdk-go v1.44.294 h1:3x7GaEth+pDU9HwFcAU0awZlEix5CEdyIZvV08SlHa8= @@ -217,6 +219,7 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU= github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM= github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc= diff --git a/internal/services/controller/controller.go b/internal/services/controller/controller.go index ff5b6a06..c8e5a00f 100644 --- a/internal/services/controller/controller.go +++ b/internal/services/controller/controller.go @@ -11,6 +11,7 @@ import ( "sync" "time" + datadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1" karpenterCore "github.com/aws/karpenter-core/pkg/apis/v1alpha5" karpenter "github.com/aws/karpenter/pkg/apis/v1alpha1" "github.com/samber/lo" @@ -505,6 +506,14 @@ func getConditionalInformers(f informers.SharedInformerFactory, df dynamicinform return df.ForResource(karpenter.SchemeGroupVersion.WithResource("awsnodetemplates")).Informer() }, }, + { + resource: datadoghqv1alpha1.GroupVersion.WithResource("extendeddaemonsetreplicasets"), + apiType: reflect.TypeOf(&datadoghqv1alpha1.ExtendedDaemonSetReplicaSet{}), + permissionVerbs: []string{"get", "list", "watch"}, + informerFactory: func() cache.SharedIndexInformer { + return df.ForResource(datadoghqv1alpha1.GroupVersion.WithResource("extendeddaemonsetreplicasets")).Informer() + }, + }, { resource: v1beta1.SchemeGroupVersion.WithResource("pods"), apiType: reflect.TypeOf(&v1beta1.PodMetrics{}), diff --git a/internal/services/controller/controller_test.go b/internal/services/controller/controller_test.go index c5a7d6e6..ebee5639 100644 --- a/internal/services/controller/controller_test.go +++ b/internal/services/controller/controller_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + datadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1" karpenterCore "github.com/aws/karpenter-core/pkg/apis/v1alpha5" karpenter "github.com/aws/karpenter/pkg/apis/v1alpha1" "github.com/golang/mock/gomock" @@ -64,6 +65,7 @@ func TestController_HappyPath(t *testing.T) { scheme := runtime.NewScheme() utilruntime.Must(karpenterCore.SchemeBuilder.AddToScheme(scheme)) utilruntime.Must(karpenter.SchemeBuilder.AddToScheme(scheme)) + utilruntime.Must(datadoghqv1alpha1.SchemeBuilder.AddToScheme(scheme)) mockctrl := gomock.NewController(t) castaiclient := mock_castai.NewMockClient(mockctrl) @@ -76,6 +78,7 @@ func TestController_HappyPath(t *testing.T) { provisionersGvr := karpenterCore.SchemeGroupVersion.WithResource("provisioners") machinesGvr := karpenterCore.SchemeGroupVersion.WithResource("machines") awsNodeTemplatesGvr := karpenter.SchemeGroupVersion.WithResource("awsnodetemplates") + datadogExtendedDSReplicaSetsGvr := datadoghqv1alpha1.GroupVersion.WithResource("extendeddaemonsetreplicasets") node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{}}} expectedNode := node.DeepCopy() @@ -163,6 +166,20 @@ func TestController_HappyPath(t *testing.T) { awsNodeTemplatesData, err := delta.Encode(awsNodeTemplates) require.NoError(t, err) + datadogExtendedDSReplicaSet := &datadoghqv1alpha1.ExtendedDaemonSetReplicaSet{ + TypeMeta: metav1.TypeMeta{ + Kind: "ExtendedDaemonSetReplicaSet", + APIVersion: datadogExtendedDSReplicaSetsGvr.GroupVersion().String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Name: datadogExtendedDSReplicaSetsGvr.Resource, + Namespace: v1.NamespaceDefault, + }, + } + + datadogExtendedDSReplicaSetData, err := delta.Encode(datadogExtendedDSReplicaSet) + require.NoError(t, err) + fakeSelfSubjectAccessReviewsClient := &authfakev1.FakeSelfSubjectAccessReviews{ Fake: &authfakev1.FakeAuthorizationV1{ Fake: &k8stesting.Fake{}, @@ -180,7 +197,7 @@ func TestController_HappyPath(t *testing.T) { clientset := fake.NewSimpleClientset(node, pod, cfgMap, pdb, hpa, csi) metricsClient := metrics_fake.NewSimpleClientset() - dynamicClient := dynamic_fake.NewSimpleDynamicClient(scheme, provisioners, machines, awsNodeTemplates) + dynamicClient := dynamic_fake.NewSimpleDynamicClient(scheme, provisioners, machines, awsNodeTemplates, datadogExtendedDSReplicaSet) clientset.Fake.Resources = []*metav1.APIResourceList{ { @@ -260,6 +277,17 @@ func TestController_HappyPath(t *testing.T) { }, }, }, + { + GroupVersion: datadogExtendedDSReplicaSetsGvr.GroupVersion().String(), + APIResources: []metav1.APIResource{ + { + Group: datadogExtendedDSReplicaSetsGvr.Group, + Name: datadogExtendedDSReplicaSetsGvr.Resource, + Version: datadogExtendedDSReplicaSetsGvr.Version, + Verbs: []string{"get", "list", "watch"}, + }, + }, + }, } version.EXPECT().Full().Return("1.21+").MaxTimes(3) @@ -276,7 +304,7 @@ func TestController_HappyPath(t *testing.T) { require.Equal(t, clusterID, d.ClusterID) require.Equal(t, "1.21+", d.ClusterVersion) require.True(t, d.FullSnapshot) - require.Len(t, d.Items, 9) + require.Len(t, d.Items, 10) var actualValues []string for _, item := range d.Items { @@ -292,6 +320,7 @@ func TestController_HappyPath(t *testing.T) { require.Contains(t, actualValues, fmt.Sprintf("%s-%s-%v", castai.EventAdd, "Provisioner", provisionersData)) require.Contains(t, actualValues, fmt.Sprintf("%s-%s-%v", castai.EventAdd, "Machine", machinesData)) require.Contains(t, actualValues, fmt.Sprintf("%s-%s-%v", castai.EventAdd, "AWSNodeTemplate", awsNodeTemplatesData)) + require.Contains(t, actualValues, fmt.Sprintf("%s-%s-%v", castai.EventAdd, "ExtendedDaemonSetReplicaSet", datadogExtendedDSReplicaSetData)) return nil }) diff --git a/internal/services/controller/scheme/register.go b/internal/services/controller/scheme/register.go index a14efbb6..f5548e85 100644 --- a/internal/services/controller/scheme/register.go +++ b/internal/services/controller/scheme/register.go @@ -1,6 +1,7 @@ package scheme import ( + datadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1" karpenterCore "github.com/aws/karpenter-core/pkg/apis/v1alpha5" karpenter "github.com/aws/karpenter/pkg/apis/v1alpha1" appsv1 "k8s.io/api/apps/v1" @@ -25,6 +26,7 @@ var builder = runtime.SchemeBuilder{ policyv1.AddToScheme, karpenterCore.SchemeBuilder.AddToScheme, karpenter.SchemeBuilder.AddToScheme, + datadoghqv1alpha1.SchemeBuilder.AddToScheme, } func init() {