Skip to content

Commit

Permalink
chore: Fix configmap waiter for karpenter-global-settings (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Nov 29, 2023
1 parent 023c796 commit 838b4c7
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/operator/injection/injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func WithSettingsOrDie(ctx context.Context, kubernetesInterface kubernetes.Inter
factory.Start(cancelCtx.Done())

for _, setting := range settings {
cm, err := WaitForConfigMap(ctx, setting.ConfigMap(), informer)
cm, err := WaitForConfigMap(cancelCtx, setting.ConfigMap(), informer)
if client.IgnoreNotFound(err) != nil {
panic(fmt.Errorf("failed to get configmap %s, %w", setting.ConfigMap(), err))
}
Expand Down
24 changes: 17 additions & 7 deletions pkg/operator/injection/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package injection_test

import (
"context"
"sync/atomic"
"testing"
"time"

Expand All @@ -37,6 +38,7 @@ import (
)

var ctx context.Context
var cancel context.CancelFunc
var env *test.Environment
var defaultConfigMap *v1.ConfigMap

Expand All @@ -48,13 +50,6 @@ func TestAPIs(t *testing.T) {

var _ = BeforeSuite(func() {
env = test.NewEnvironment(scheme.Scheme)
defaultConfigMap = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "karpenter-global-settings",
Namespace: system.Namespace(),
},
}
ExpectApplied(ctx, env.Client, defaultConfigMap)
})

var _ = AfterSuite(func() {
Expand All @@ -68,11 +63,13 @@ var _ = BeforeEach(func() {
Namespace: system.Namespace(),
},
}
ctx, cancel = context.WithCancel(context.Background())
ExpectApplied(ctx, env.Client, defaultConfigMap)
})

var _ = AfterEach(func() {
ExpectDeleted(ctx, env.Client, defaultConfigMap.DeepCopy())
cancel()
})

var _ = Describe("Settings", func() {
Expand Down Expand Up @@ -106,6 +103,19 @@ var _ = Describe("Settings", func() {
g.Expect(s.BatchMaxDuration).To(Equal(15 * time.Second))
}).Should(Succeed())
})
It("should timeout if the karpenter-global-settings doesn't exist", func() {
ExpectDeleted(ctx, env.Client, defaultConfigMap)

finished := atomic.Bool{}
go func() {
// This won't succeed to exit in time if the waiter isn't using the correct context
injection.WithSettingsOrDie(ctx, env.KubernetesInterface, &settings.Settings{})
finished.Store(true)
}()
Eventually(func() bool {
return finished.Load()
}, time.Second*10, time.Second).Should(BeTrue())
})
})
Context("Multiple Settings", func() {
It("should get operator settings and features from same configMap", func() {
Expand Down
89 changes: 89 additions & 0 deletions pkg/operator/logging/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
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 logging_test

import (
"context"
"sync/atomic"
"testing"
"time"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/system"

"github.com/aws/karpenter-core/pkg/operator/logging"
"github.com/aws/karpenter-core/pkg/operator/options"
"github.com/aws/karpenter-core/pkg/operator/scheme"
"github.com/aws/karpenter-core/pkg/test"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "knative.dev/pkg/logging/testing"

. "github.com/aws/karpenter-core/pkg/test/expectations"
)

var ctx context.Context
var cancel context.CancelFunc
var env *test.Environment
var defaultConfigMap *v1.ConfigMap

func TestAPIs(t *testing.T) {
ctx = TestContextWithLogger(t)
RegisterFailHandler(Fail)
RunSpecs(t, "Logging")
}

var _ = BeforeSuite(func() {
env = test.NewEnvironment(scheme.Scheme)
})

var _ = AfterSuite(func() {
Expect(env.Stop()).To(Succeed())
})

var _ = BeforeEach(func() {
defaultConfigMap = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-logging",
Namespace: system.Namespace(),
},
}
ctx, cancel = context.WithCancel(context.Background())
ctx = options.ToContext(ctx, test.Options())
ExpectApplied(ctx, env.Client, defaultConfigMap)
})

var _ = AfterEach(func() {
ExpectDeleted(ctx, env.Client, defaultConfigMap.DeepCopy())
cancel()
})

var _ = Describe("Logging", func() {
It("should timeout if the config-logging doesn't exist", func() {
ExpectDeleted(ctx, env.Client, defaultConfigMap)

finished := atomic.Bool{}
go func() {
// This won't succeed to exit in time if the waiter isn't using the correct context
logging.NewLogger(ctx, "controller", env.KubernetesInterface)
finished.Store(true)
}()
Eventually(func() bool {
return finished.Load()
}, time.Second*10, time.Second).Should(BeTrue())
})
})

0 comments on commit 838b4c7

Please sign in to comment.