diff --git a/cmd/controller/main.go b/cmd/controller/main.go index 80ce6bad9dcc..0146572b9f2c 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -16,7 +16,6 @@ package main import ( "github.com/samber/lo" - "knative.dev/pkg/logging" "github.com/aws/karpenter/pkg/cloudprovider" "github.com/aws/karpenter/pkg/controllers" @@ -44,10 +43,6 @@ func main() { lo.Must0(op.AddHealthzCheck("cloud-provider", awsCloudProvider.LivenessProbe)) cloudProvider := metrics.Decorate(awsCloudProvider) - if err := op.ValidateK8sVersion(ctx); err != nil { - logging.FromContext(ctx).Error(err) - } - op. WithControllers(ctx, corecontrollers.NewControllers( ctx, diff --git a/hack/docs/version_compatibility.go b/hack/docs/version_compatibility.go index c3f989d45628..8a08295ac039 100644 --- a/hack/docs/version_compatibility.go +++ b/hack/docs/version_compatibility.go @@ -18,10 +18,9 @@ import ( "fmt" "log" "os" - "sort" "strings" - "github.com/aws/karpenter/tools/kompat/pkg/kompat" + "github.com/aws/karpenter/pkg/providers/version" ) func main() { @@ -33,24 +32,15 @@ func main() { os.Exit(0) } - chart, err := kompat.Parse(os.Args[1]) - if err != nil { - log.Fatalf("unable to generate compatibility matrix") - } - - sort.Slice(chart[0].Compatibility, func(i int, j int) bool { - return chart[0].Compatibility[i].AppVersion < chart[0].Compatibility[j].AppVersion - }) - - version := strings.TrimPrefix(os.Args[2], "v") + v := strings.TrimPrefix(os.Args[2], "v") appendVersion := fmt.Sprintf( ` - appVersion: %s minK8sVersion: %s maxK8sVersion: %s`, - version, - chart[0].Compatibility[len(chart[0].Compatibility)-1].MinK8sVersion, - chart[0].Compatibility[len(chart[0].Compatibility)-1].MaxK8sVersion) + v, + version.MinK8sVersion, + version.MaxK8sVersion) yamlFile, err := os.ReadFile(os.Args[1]) if err != nil { diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index d532d9497096..65b355eae75b 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -37,8 +37,6 @@ import ( "github.com/aws/aws-sdk-go/service/ssm" "github.com/patrickmn/go-cache" - utilversion "k8s.io/apimachinery/pkg/util/version" - "github.com/samber/lo" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -61,12 +59,6 @@ import ( "github.com/aws/karpenter/pkg/utils/project" ) -// Karpenter's supported version of Kubernetes -// If a user runs a karpenter image on a k8s version outside the min and max, -// One error message will be fired to notify -const minK8sVersion = "1.23" -const maxK8sVersion = "1.27" - // Operator is injected into the AWS CloudProvider's factories type Operator struct { *operator.Operator @@ -187,20 +179,6 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont } } -func (op *Operator) ValidateK8sVersion(ctx context.Context) error { - v := lo.Must(op.VersionProvider.Get(ctx)) - k8sVersion := utilversion.MustParseGeneric(v) - - // We will only error if the user is running karpenter on a k8s version, - // that is out of the range of the minK8sVersion and maxK8sVersion - if k8sVersion.LessThan(utilversion.MustParseGeneric(minK8sVersion)) || - utilversion.MustParseGeneric(maxK8sVersion).LessThan(k8sVersion) { - return fmt.Errorf("karpenter version is not compatible with K8s version %s", k8sVersion) - } - - return nil -} - // withUserAgent adds a karpenter specific user-agent string to AWS session func withUserAgent(sess *session.Session) *session.Session { userAgent := fmt.Sprintf("karpenter.sh-%s", project.Version) diff --git a/pkg/providers/version/version.go b/pkg/providers/version/version.go index 7f057734240b..28191fae383b 100644 --- a/pkg/providers/version/version.go +++ b/pkg/providers/version/version.go @@ -20,6 +20,7 @@ import ( "strings" "github.com/patrickmn/go-cache" + "k8s.io/apimachinery/pkg/util/version" "k8s.io/client-go/kubernetes" "knative.dev/pkg/logging" @@ -28,6 +29,11 @@ import ( const ( kubernetesVersionCacheKey = "kubernetesVersion" + // Karpenter's supported version of Kubernetes + // If a user runs a karpenter image on a k8s version outside the min and max, + // One error message will be fired to notify + MinK8sVersion = "1.23" + MaxK8sVersion = "1.27" ) // Provider get the APIServer version. This will be initialized at start up and allows karpenter to have an understanding of the cluster version @@ -59,6 +65,22 @@ func (p *Provider) Get(ctx context.Context) (string, error) { p.cache.SetDefault(kubernetesVersionCacheKey, version) if p.cm.HasChanged("kubernetes-version", version) { logging.FromContext(ctx).With("version", version).Debugf("discovered kubernetes version") + if err := validateK8sVersion(version); err != nil { + logging.FromContext(ctx).Error(err) + } } return version, nil } + +func validateK8sVersion(v string) error { + k8sVersion := version.MustParseGeneric(v) + + // We will only error if the user is running karpenter on a k8s version, + // that is out of the range of the minK8sVersion and maxK8sVersion + if k8sVersion.LessThan(version.MustParseGeneric(MinK8sVersion)) || + version.MustParseGeneric(MaxK8sVersion).LessThan(k8sVersion) { + return fmt.Errorf("karpenter version is not compatible with K8s version %s", k8sVersion) + } + + return nil +}