Skip to content

Commit

Permalink
chore: Log Incompatible version of the API version (#4635)
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam authored Sep 15, 2023
1 parent c73bb90 commit abfbf5f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
5 changes: 0 additions & 5 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 5 additions & 15 deletions hack/docs/version_compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
22 changes: 0 additions & 22 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions pkg/providers/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit abfbf5f

Please sign in to comment.