Skip to content

Commit

Permalink
Chore: Separate validation for update (#7575)
Browse files Browse the repository at this point in the history
  • Loading branch information
edibble21 authored Jan 10, 2025
1 parent a3201dc commit b631d9e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewControllers(
ssminvalidation.NewController(ssmCache, amiProvider),
status.NewController[*v1.EC2NodeClass](kubeClient, mgr.GetEventRecorderFor("karpenter"), status.EmitDeprecatedMetrics),
opevents.NewController[*corev1.Node](kubeClient, clk),
controllersversion.NewController(versionProvider),
controllersversion.NewController(versionProvider, versionProvider.UpdateVersionWithValidation),
}
if options.FromContext(ctx).InterruptionQueue != "" {
sqsapi := servicesqs.NewFromConfig(cfg)
Expand Down
8 changes: 6 additions & 2 deletions pkg/controllers/providers/version/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ import (
"github.com/aws/karpenter-provider-aws/pkg/providers/version"
)

type UpdateVersion func(context.Context) error

type Controller struct {
versionProvider *version.DefaultProvider
updateVersion UpdateVersion
}

func NewController(versionProvider *version.DefaultProvider) *Controller {
func NewController(versionProvider *version.DefaultProvider, updateVersion UpdateVersion) *Controller {
return &Controller{
versionProvider: versionProvider,
updateVersion: updateVersion,
}
}

func (c *Controller) Reconcile(ctx context.Context) (reconcile.Result, error) {
ctx = injection.WithControllerName(ctx, "providers.version")

if err := c.versionProvider.UpdateVersion(ctx); err != nil {
if err := c.updateVersion(ctx); err != nil {
return reconcile.Result{}, fmt.Errorf("updating version, %w", err)
}
return reconcile.Result{RequeueAfter: 5 * time.Minute}, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/providers/version/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var _ = BeforeSuite(func() {
ctx = options.ToContext(ctx, test.Options())
ctx, stop = context.WithCancel(ctx)
awsEnv = test.NewEnvironment(ctx, env)
controller = controllersversion.NewController(awsEnv.VersionProvider)
controller = controllersversion.NewController(awsEnv.VersionProvider, awsEnv.VersionProvider.UpdateVersionWithValidation)
})

var _ = AfterSuite(func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/version/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = BeforeSuite(func() {
ctx, stop = context.WithCancel(ctx)
awsEnv = test.NewEnvironment(ctx, env)
testEnv = &environmentaws.Environment{Environment: &common.Environment{KubeClient: env.KubernetesInterface}}
versionController = controllersversion.NewController(awsEnv.VersionProvider)
versionController = controllersversion.NewController(awsEnv.VersionProvider, awsEnv.VersionProvider.UpdateVersionWithValidation)
})

var _ = AfterSuite(func() {
Expand Down
12 changes: 10 additions & 2 deletions pkg/providers/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (p *DefaultProvider) Get(ctx context.Context) string {
}

func (p *DefaultProvider) UpdateVersion(ctx context.Context) error {
var version, versionSource string
var version string
var err error

if options.FromContext(ctx).EKSControlPlane {
Expand All @@ -84,7 +84,15 @@ func (p *DefaultProvider) UpdateVersion(ctx context.Context) error {
}
}
p.version.Store(&version)
if p.cm.HasChanged("kubernetes-version", version) || p.cm.HasChanged("version-source", versionSource) {
return nil
}
func (p *DefaultProvider) UpdateVersionWithValidation(ctx context.Context) error {
err := p.UpdateVersion(ctx)
if err != nil {
return err
}
version := p.Get(ctx)
if p.cm.HasChanged("kubernetes-version", version) {
log.FromContext(ctx).WithValues("version", version).V(1).Info("discovered kubernetes version")
if err := validateK8sVersion(version); err != nil {
return fmt.Errorf("validating kubernetes version, %w", err)
Expand Down

0 comments on commit b631d9e

Please sign in to comment.