Skip to content

Commit

Permalink
Merge pull request #2114 from rmweir/fix-k8s-version
Browse files Browse the repository at this point in the history
Check if vcluster chart version supports k8s version
  • Loading branch information
FabianKramm authored Sep 5, 2024
2 parents c27c95e + 7f441bf commit ac9f321
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
73 changes: 66 additions & 7 deletions config/default_extra_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"regexp"
"strconv"
"strings"

"github.com/blang/semver"
)

const (
Expand Down Expand Up @@ -71,6 +73,16 @@ var K8SEtcdVersionMap = map[string]string{
"1.27": "registry.k8s.io/etcd:3.5.7-0",
}

var vclusterVersionsWithK8sVersionSupport []semver.Version

func init() {
vclusterVersionsWithK8sVersionSupport = []semver.Version{
semver.MustParse("0.20.0-beta.16"),
semver.MustParse("0.20.0-alpha.99"),
semver.MustParse("0.21.0-alpha.4"),
}
}

// ExtraValuesOptions holds the chart options
type ExtraValuesOptions struct {
Distro string
Expand All @@ -91,21 +103,21 @@ type KubernetesVersion struct {
Minor string
}

func GetExtraValues(options *ExtraValuesOptions) (string, error) {
func GetExtraValues(options *ExtraValuesOptions, chartVersion string) (string, error) {
fromConfig, err := NewDefaultConfig()
if err != nil {
return "", err
}

toConfig, err := getExtraValues(options)
toConfig, err := getExtraValues(options, chartVersion)
if err != nil {
return "", fmt.Errorf("get extra values: %w", err)
}

return Diff(fromConfig, toConfig)
}

func getExtraValues(options *ExtraValuesOptions) (*Config, error) {
func getExtraValues(options *ExtraValuesOptions, chartVersion string) (*Config, error) {
vConfig, err := NewDefaultConfig()
if err != nil {
return nil, err
Expand All @@ -124,7 +136,7 @@ func getExtraValues(options *ExtraValuesOptions) (*Config, error) {
}

// apply k8s values
err = applyK8SExtraValues(vConfig, options)
err = applyK8SExtraValues(vConfig, options, chartVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -166,7 +178,7 @@ func applyK0SExtraValues(vConfig *Config, options *ExtraValuesOptions) error {
return nil
}

func applyK8SExtraValues(vConfig *Config, options *ExtraValuesOptions) error {
func applyK8SExtraValues(vConfig *Config, options *ExtraValuesOptions, chartVersion string) error {
// get api server image
apiImage, err := getImageByVersion(options.KubernetesVersion, K8SAPIVersionMap)
if err != nil {
Expand All @@ -180,16 +192,63 @@ func applyK8SExtraValues(vConfig *Config, options *ExtraValuesOptions) error {
}

// build values
if apiImage != "" {
vConfig.ControlPlane.Distro.K8S.Version = parseImage(apiImage).Tag
k8sVersion, err := getK8sVersion(chartVersion, apiImage)
if err != nil {
return err
}
vConfig.ControlPlane.Distro.K8S.Version = k8sVersion

if etcdImage != "" {
vConfig.ControlPlane.BackingStore.Etcd.Deploy.StatefulSet.Image = parseImage(etcdImage)
}

return nil
}

func getK8sVersion(chartVersion, apiImage string) (string, error) {
if apiImage == "" {
return "", nil
}
isSupported, err := isK8sVersionSupported(chartVersion)
if err != nil {
return "", err
}

if isSupported {
return parseImage(apiImage).Tag, nil
}
// if we set a non-empty value for a vcluster chart version that does not support
// the k8s version field, vcluster will panic
return "", nil
}

func isK8sVersionSupported(chartVersion string) (bool, error) {
if chartVersion == "" {
// probably dev chart
return true, nil
}
v, err := semver.Parse(strings.TrimPrefix(chartVersion, "v"))
if err != nil {
return false, err
}
for _, supportedStartVer := range vclusterVersionsWithK8sVersionSupport {
if v.Minor != supportedStartVer.Minor {
continue
}
if len(supportedStartVer.Pre) > 0 && len(v.Pre) > 0 && supportedStartVer.Pre[0] != v.Pre[0] {
// this is a special case- its possible in our releases for an alpha to be ahead of a beta;
// we cannot compare prebuilds unless they share the same first element.
return true, nil
}
if v.GTE(supportedStartVer) {
return true, nil
}
return false, nil
}
// in the case no relevant start version was found for minor we can assume k8s version is supported
return true, nil
}

func parseImage(image string) Image {
registry, repository, tag := SplitImage(image)
return Image{
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/create_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func CreateHelm(ctx context.Context, options *CreateOptions, globalFlags *flags.
if err != nil {
return err
}
chartValues, err := config.GetExtraValues(chartOptions)
chartValues, err := config.GetExtraValues(chartOptions, options.ChartVersion)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/create_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func mergeValues(platformClient platform.Client, options *CreateOptions, log log
if err != nil {
return "", err
}
chartValues, err := vclusterconfig.GetExtraValues(chartOptions)
chartValues, err := vclusterconfig.GetExtraValues(chartOptions, options.ChartVersion)
if err != nil {
return "", err
}
Expand Down

0 comments on commit ac9f321

Please sign in to comment.