version

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2025 License: Apache-2.0 Imports: 10 Imported by: 11

README

version

FOSSA Status

A go-language package for parsing, comparing, sorting and constraint-checking k0s version numbers. The API is modeled after hashicorp/go-version.

K0s versioning follows semver v2.0 with the exception that there is a special metadata field for the k0s build version like v1.23.4+k0s.1 which affects precedence while sorting or comparing version numbers.

The library should work fine for performing the same operations on non-k0s version numbers as long as there are maximum of 3 numeric segments (1.2.3), but this is not a priority. There are no dependencies.

Usage

Basic comparison
import (
	"fmt"

	"github.com/k0sproject/version"
)

func main() {
	a := version.MustParse("1.23.3+k0s.1")
	b := version.MustParse("1.23.3+k0s.2")
	fmt.Printf("a is greater than b: %t\n", a.GreaterThan(b))
	fmt.Printf("a is less than b: %t\n", a.LessThan(b))
	fmt.Printf("a is equal to b: %t\n", a.Equal(b))
}

Outputs:

a is greater than b: false
a is less than b: true
a is equal to b: false
Constraints
import (
	"fmt"

	"github.com/k0sproject/version"
)

func main() {
	v := version.MustParse("1.23.3+k0s.1")
	c := version.MustConstraint("> 1.23")
    fmt.Printf("constraint %s satisfied by %s: %t\n", c, v, c.Check(v))
}

Outputs:

constraint > 1.2.3 satisfied by v1.23.3+k0s.1: true
Sorting
import (
	"fmt"
    "sort"

	"github.com/k0sproject/version"
)

func main() {
    versions := []*version.Version{
	    version.MustParse("v1.23.3+k0s.2"),
        version.MustParse("1.23.2+k0s.3"),
        version.MustParse("1.23.3+k0s.1"),
    }

    fmt.Println("Before:")
    for _, v range versions {
        fmt.Println(v)
    }
    sort.Sort(versions)
    fmt.Println("After:")
    for _, v range versions {
        fmt.Println(v)
    }
}

Outputs:

Before:
v1.23.3+k0s.2
v1.23.2+k0s.3
v1.23.3+k0s.1
After:
v1.23.2+k0s.3
v1.23.3+k0s.1
v1.23.3+k0s.2
Check online for latest version
import (
	"fmt"
	"github.com/k0sproject/version"
)

func main() {
	latest, err := version.Latest()
	if err != nil {
		panic(err)
	}
	fmt.Printf("Latest k0s version is: %s\n", latest)
}
k0s_sort executable

A command-line interface to the package. Can be used to sort lists of versions or to obtain the latest version number.

Usage: k0s_sort [options] [filename ...]
  -l	only print the latest version
  -o	print the latest version from online
  -s	omit prerelease versions
  -v	print k0s_sort version

License

FOSSA Status

Documentation

Overview

package version implements a k0s version type and functions to parse and compare versions

Index

Examples

Constants

View Source
const (
	BaseUrl = "https://github.com/k0sproject/k0s/"
)

Variables

View Source
var Timeout = time.Second * 10

Functions

This section is empty.

Types

type Collection

type Collection []*Version

Collection is a type that implements the sort.Interface interface so that versions can be sorted.

func NewCollection

func NewCollection(versions ...string) (Collection, error)

func (Collection) Len

func (c Collection) Len() int

func (Collection) Less

func (c Collection) Less(i, j int) bool

func (Collection) Swap

func (c Collection) Swap(i, j int)

type Constraint added in v0.8.0

type Constraint struct {
	// contains filtered or unexported fields
}

func MustConstraint added in v0.4.0

func MustConstraint(cs string) Constraint

MustConstraint is like NewConstraint but panics if the constraint is invalid.

func NewConstraint added in v0.4.0

func NewConstraint(cs string) (Constraint, error)

NewConstraint parses a string and returns a Contraint and an error if the parsing fails.

func (Constraint) Check added in v0.8.0

func (cs Constraint) Check(v *Version) bool

Check returns true if the given version satisfies all of the constraints.

func (Constraint) CheckString added in v0.8.0

func (cs Constraint) CheckString(v string) bool

CheckString is like Check but takes a string version. If the version is invalid, it returns false.

func (Constraint) String added in v0.8.0

func (cs Constraint) String() string

String returns the constraint as a string.

type Delta added in v0.8.0

type Delta struct {
	MajorUpgrade        bool
	MinorUpgrade        bool
	PatchUpgrade        bool
	K0sUpgrade          bool
	Equal               bool
	Downgrade           bool
	PrereleaseOnly      bool
	BuildMetadataChange bool
	Consecutive         bool
	// contains filtered or unexported fields
}

Delta represents the differences between two versions.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/version"
)

func main() {
	a, _ := version.NewVersion("v1.0.0")
	b, _ := version.NewVersion("v1.2.1")
	delta := version.NewDelta(a, b)
	fmt.Printf("patch upgrade: %t\n", delta.PatchUpgrade)
	fmt.Println(delta.String())
}
Output:

patch upgrade: false
a non-consecutive minor upgrade from v1.0 to v1.2

func NewDelta added in v0.8.0

func NewDelta(a, b *Version) Delta

NewDelta analyzes the differences between two versions and returns a Delta.

func (Delta) String added in v0.8.0

func (d Delta) String() string

String returns a human-readable representation of the Delta.

type Version

type Version struct {
	// contains filtered or unexported fields
}

Version is a k0s version

func Latest added in v0.3.0

func Latest() (*Version, error)

LatestVersion returns the semantically sorted latest version even if it is a prerelease from the online repository

func LatestByPrerelease added in v0.3.0

func LatestByPrerelease(allowpre bool) (*Version, error)

LatestByPrerelease returns the latest released k0s version, if preok is true, prereleases are also accepted.

func LatestStable added in v0.3.0

func LatestStable() (*Version, error)

LatestStable returns the semantically sorted latest non-prerelease version from the online repository

func MustParse added in v0.4.0

func MustParse(v string) *Version

MustParse is like NewVersion but panics if the version cannot be parsed. It simplifies safe initialization of global variables.

func NewVersion

func NewVersion(v string) (*Version, error)

NewVersion returns a new Version object from a string representation of a k0s version

func (*Version) AirgapDownloadURL added in v0.3.0

func (v *Version) AirgapDownloadURL(arch string) string

AirgapDownloadURL returns the k0s airgap bundle download URL for the k0s version

func (*Version) Base added in v0.7.0

func (v *Version) Base() string

Base returns the version as a string without the k0s or metadata part (eg v1.2.3+k0s.4 -> v1.2.3)

func (*Version) Clone added in v0.7.0

func (v *Version) Clone() *Version

Clone returns a copy of the k0s version

func (*Version) ComparableFields added in v0.7.0

func (v *Version) ComparableFields() comparableFields

ComparableFields returns the comparable fields of the k0s version

func (*Version) Compare

func (v *Version) Compare(b *Version) int

Compare returns 0 if the k0s version is equal to the supplied version, 1 if it's greater and -1 if it's lower

func (*Version) Delta added in v0.8.0

func (v *Version) Delta(b *Version) Delta

Delta returns a comparison to the given version

func (*Version) DocsURL added in v0.3.0

func (v *Version) DocsURL() string

DocsURL returns the documentation URL for the k0s version

func (*Version) DownloadURL added in v0.3.0

func (v *Version) DownloadURL(os, arch string) string

DownloadURL returns the k0s binary download URL for the k0s version

func (*Version) Equal

func (v *Version) Equal(b *Version) bool

Equal returns true if the k0s version is equal to the supplied version

func (*Version) GreaterThan

func (v *Version) GreaterThan(b *Version) bool

GreaterThan returns true if the version is greater than the supplied version

func (*Version) GreaterThanOrEqual

func (v *Version) GreaterThanOrEqual(b *Version) bool

GreaterThanOrEqual returns true if the version is greater than the supplied version or equal

func (*Version) IsK0s added in v0.7.0

func (v *Version) IsK0s() bool

IsK0s returns true if the version is a k0s version

func (*Version) IsPrerelease added in v0.7.0

func (v *Version) IsPrerelease() bool

IsPrerelease returns true if the k0s version is a prerelease version

func (*Version) IsZero added in v0.4.2

func (v *Version) IsZero() bool

IsZero returns true if the version is nil or empty

func (*Version) K0s added in v0.7.0

func (v *Version) K0s() (int, bool)

K0s returns the k0s version (eg 4 from v1.2.3-k0s.4) and true if the version is a k0s version. Otherwise it returns 0 and false.

func (*Version) LessThan

func (v *Version) LessThan(b *Version) bool

LessThan returns true if the version is lower than the supplied version

func (*Version) LessThanOrEqual

func (v *Version) LessThanOrEqual(b *Version) bool

LessThanOrEqual returns true if the version is lower than the supplied version or equal

func (*Version) Major added in v0.8.0

func (v *Version) Major() string

Major returns a string like "v2" from a version like 2.0.0

func (*Version) MarshalText added in v0.7.0

func (v *Version) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface (used as fallback by encoding/json and yaml.v3).

func (*Version) MarshalYAML added in v0.4.0

func (v *Version) MarshalYAML() (interface{}, error)

MarshalYAML implements the yaml.v2 Marshaler interface.

func (*Version) Metadata added in v0.7.0

func (v *Version) Metadata() string

Metadata returns the metadata part of the k0s version (eg 123abc from v1.2.3+k0s.1.123abc)

func (*Version) Minor added in v0.8.0

func (v *Version) Minor() string

Minor returns a string like "v2.3" from a version like 2.3.0

func (*Version) Patch added in v0.8.0

func (v *Version) Patch() string

Patch returns a string like "v2.3.4" from a version like 2.3.4-rc.1

func (*Version) Prerelease added in v0.7.0

func (v *Version) Prerelease() string

Prerelease returns the prerelease part of the k0s version (eg rc1 from v1.2.3-rc1).

func (*Version) Satisfies added in v0.4.0

func (v *Version) Satisfies(constraint Constraint) bool

Satisfies returns true if the version satisfies the supplied constraint

func (*Version) Segments added in v0.7.0

func (v *Version) Segments() []int

Segments returns the numerical segments of the version. The returned slice is always maxSegments long. Missing segments are zeroes. Eg 1,1,0 from v1.1

func (*Version) Segments64 added in v0.7.0

func (v *Version) Segments64() []int64

Segments64 returns the numerical segments of the k0s version as int64 (eg 1,2,3 from v1.2.3). Missing segments are zeroes.

func (*Version) String added in v0.3.0

func (v *Version) String() string

String returns a v-prefixed string representation of the k0s version

func (*Version) URL added in v0.3.0

func (v *Version) URL() string

URL returns an URL to the release information page for the k0s version

func (*Version) UnmarshalText added in v0.7.0

func (v *Version) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface (used as fallback by encoding/json and yaml.v3).

func (*Version) UnmarshalYAML added in v0.4.0

func (v *Version) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements the yaml.v2 Unmarshaler interface.

func (*Version) WithK0s added in v0.7.0

func (v *Version) WithK0s(n int) *Version

WithK0s returns a copy of the k0s version with the k0s part set to the supplied value

Directories

Path Synopsis
cmd
k0s_sort command
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL