Skip to content

Commit

Permalink
feat: fetch K3s image tag from channelserver on latest/stable/+min.ma…
Browse files Browse the repository at this point in the history
…jor (#841)
  • Loading branch information
iwilltry42 authored Nov 7, 2021
1 parent a1a04b7 commit b073e76
Show file tree
Hide file tree
Showing 42 changed files with 140 additions and 4,707 deletions.
2 changes: 1 addition & 1 deletion cmd/cluster/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func NewCmdClusterCreate() *cobra.Command {

cmd.Flags().StringP("image", "i", "", "Specify k3s image that you want to use for the nodes")
_ = cfgViper.BindPFlag("image", cmd.Flags().Lookup("image"))
cfgViper.SetDefault("image", fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.GetK3sVersion(false)))
cfgViper.SetDefault("image", fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.K3sVersion))

cmd.Flags().String("network", "", "Join an existing network")
_ = cfgViper.BindPFlag("network", cmd.Flags().Lookup("network"))
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/nodeCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewCmdNodeCreate() *cobra.Command {
l.Log().Fatalln("Failed to register flag completion for '--cluster'", err)
}

cmd.Flags().StringP("image", "i", fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.GetK3sVersion(false)), "Specify k3s image used for the node(s)")
cmd.Flags().StringP("image", "i", fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.K3sVersion), "Specify k3s image used for the node(s)")
cmd.Flags().String("memory", "", "Memory limit imposed on the node [From docker]")

cmd.Flags().BoolVar(&createNodeOpts.Wait, "wait", true, "Wait for the node(s) to be ready before returning.")
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/docker/go-units v0.4.0
github.com/fvbommel/sortorder v1.0.2 // indirect
github.com/go-test/deep v1.0.7
github.com/heroku/docker-registry-client v0.0.0-20190909225348-afc9e1acc3d5
github.com/imdario/mergo v0.3.12
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
github.com/mitchellh/copystructure v1.2.0
Expand Down Expand Up @@ -50,7 +49,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
94 changes: 0 additions & 94 deletions go.sum

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions pkg/config/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,18 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim
simpleConfig.Name = k3d.DefaultClusterName
}

// fetch latest image
if simpleConfig.Image == "latest" {
simpleConfig.Image = version.GetK3sVersion(true)
/* Special cases for Image:
* - "latest" / "stable": get latest / stable channel image
* - starts with "+": get channel following the "+"
*/
if simpleConfig.Image == "latest" || simpleConfig.Image == "stable" || strings.HasPrefix(simpleConfig.Image, "+") {
searchChannel := strings.TrimPrefix(simpleConfig.Image, "+")
v, err := version.GetK3sVersion(searchChannel)
if err != nil {
return nil, err
}
l.Log().Debugf("Using fetched K3s version %s", v)
simpleConfig.Image = fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, v)
}

clusterNetwork := k3d.ClusterNetwork{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var DefaultConfig = fmt.Sprintf(
DefaultConfigTpl,
ApiVersion,
k3d.DefaultClusterName,
fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.GetK3sVersion(false)),
fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.K3sVersion),
)

type VolumeWithNodeFilters struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ image: %s
var DefaultConfig = fmt.Sprintf(
DefaultConfigTpl,
k3d.DefaultClusterName,
fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.GetK3sVersion(false)),
fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.K3sVersion),
)

type VolumeWithNodeFilters struct {
Expand Down
54 changes: 54 additions & 0 deletions pkg/types/k3s/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright © 2020-2021 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package k3s

const K3sChannelServerURL string = "https://update.k3s.io/v1-release/channels"

// Source: https://github.com/rancher/channelserver/blob/b084228bfc14d280e524ad132311d8cfcebd1271/pkg/model/config.go#L15-L20
type Channel struct {
Name string `json:"name,omitempty"`
Latest string `json:"latest,omitempty"`
LatestRegexp string `json:"latestRegexp,omitempty"`
ExcludeRegexp string `json:"excludeRegexp,omitempty"`
}

type ChannelServerResponse struct {
Channels []struct {
Channel `json:,squash`
} `json:"data"`
}

/*
{
"type": "collection",
"links": {
"self": "…/v1-release/channels"
},
"actions": { },
"resourceType": "channels",
"data": [ 13 items
{
"id": "stable",
"type": "channel",
"links": { … },
*/
Loading

0 comments on commit b073e76

Please sign in to comment.