Skip to content

Commit

Permalink
install: Add a hidden --base-version flag
Browse files Browse the repository at this point in the history
The --version flag doesn't always maps to a Cilium version. For example,
Cilium CI jobs use commit SHAs as image tags. This flag allows you to
explicitly provide the "base" Cilium version being installed so that the
install command can configure Cilium accordingly.

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent authored and tklauser committed Jul 14, 2021
1 parent 01bed3a commit 8ed8abb
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ replace (
)

require (
github.com/blang/semver/v4 v4.0.0
github.com/cilium/cilium v1.10.2
github.com/cilium/hubble v0.8.0
github.com/cilium/workerpool v1.0.0
Expand Down
30 changes: 23 additions & 7 deletions install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/cilium/cilium-cli/status"
"github.com/cilium/cilium/pkg/versioncheck"

"github.com/blang/semver/v4"
"github.com/cilium/cilium/api/v1/models"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -1058,6 +1059,10 @@ type Parameters struct {
// CiliumReadyTimeout defines the wait timeout for Cilium to become ready
// after installing.
CiliumReadyTimeout time.Duration

// BaseVersion is used to explicitly specify Cilium version for generating the config map
// in case it cannot be inferred from the Version field (e.g. commit SHA tags for CI images).
BaseVersion string
}

type rollbackStep func(context.Context)
Expand Down Expand Up @@ -1159,6 +1164,22 @@ func (k *K8sInstaller) Exec(command string, args ...string) ([]byte, error) {
return utils.Exec(k, command, args...)
}

func (k *K8sInstaller) getCiliumVersion() semver.Version {
var ersion string
if k.params.BaseVersion != "" {
ersion = strings.TrimPrefix(k.params.BaseVersion, "v")
} else {
ersion = strings.TrimPrefix(k.params.Version, "v")
}
v, err := versioncheck.Version(ersion)
if err != nil {
// TODO: Don't hard code version here, get it from stable.txt.
k.Log("Unable to parse the provided version %q, assuming it's =1.10.0", k.params.Version)
v = versioncheck.MustVersion("1.10.0")
}
return v
}

func (k *K8sInstaller) generateConfigMap() (*corev1.ConfigMap, error) {
m := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -1292,12 +1313,8 @@ func (k *K8sInstaller) generateConfigMap() (*corev1.ConfigMap, error) {
m.Data["cluster-pool-ipv4-mask-size"] = "24"
}

ersion := strings.TrimPrefix(k.params.Version, "v")
v, err := versioncheck.Version(ersion)
if err != nil {
k.Log("Unable to parse the provided version %q, assuming it's >= 1.10.0", k.params.Version)
v = versioncheck.MustVersion("1.10.0")
}
v := k.getCiliumVersion()
k.Log("🚀 Creating ConfigMap for Cilium version %s...", v.String())

masqueradeOption := "enable-ipv4-masquerade"
if v.LT(versioncheck.MustVersion("1.10.0")) {
Expand Down Expand Up @@ -1622,7 +1639,6 @@ func (k *K8sInstaller) Install(ctx context.Context) error {
}
}

k.Log("🚀 Creating ConfigMap...")
configMap, err := k.generateConfigMap()
if err != nil {
return fmt.Errorf("cannot generate ConfigMap: %w", err)
Expand Down
61 changes: 61 additions & 0 deletions install/install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020-2021 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package install

import (
"io"
"reflect"
"testing"

"github.com/blang/semver/v4"
)

func TestK8sInstaller_getCiliumVersion(t *testing.T) {
type fields struct{ params Parameters }
tests := []struct {
name string
fields fields
want semver.Version
}{
{
name: "default",
fields: fields{Parameters{Writer: io.Discard}},
want: semver.Version{Major: 1, Minor: 10, Patch: 0},
},
{
name: "version",
fields: fields{Parameters{Writer: io.Discard, Version: "v1.10.3"}},
want: semver.Version{Major: 1, Minor: 10, Patch: 3},
},
{
name: "base-version",
fields: fields{Parameters{Writer: io.Discard, Version: "random-version-string", BaseVersion: "v1.9.8"}},
want: semver.Version{Major: 1, Minor: 9, Patch: 8},
},
{
name: "random-version-without-base-version",
fields: fields{Parameters{Writer: io.Discard, Version: "random-version-string"}},
want: semver.Version{Major: 1, Minor: 10, Patch: 0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &K8sInstaller{params: tt.fields.params}
if got := k.getCiliumVersion(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getCiliumVersion() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 3 additions & 0 deletions internal/cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ cilium install --context kind-cluster1 --cluster-id 1 --cluster-name cluster1
cmd.Flags().StringVar(&params.ClusterName, "cluster-name", "", "Name of the cluster")
cmd.Flags().StringSliceVar(&params.DisableChecks, "disable-check", []string{}, "Disable a particular validation check")
cmd.Flags().StringVar(&params.Version, "version", defaults.Version, "Cilium version to install")
cmd.Flags().StringVar(&params.BaseVersion, "base-version", "",
"Specify the base Cilium version for configuration purpose in case the --version flag doesn't indicate the actual Cilium version")
cmd.Flags().MarkHidden("base-version")
cmd.Flags().StringVar(&params.DatapathMode, "datapath-mode", "", "Datapath mode to use")
cmd.Flags().StringVar(&params.IPAM, "ipam", "", "IP Address Management (IPAM) mode")
cmd.Flags().StringVar(&params.NativeRoutingCIDR, "native-routing-cidr", "", "CIDR within which native routing is possible")
Expand Down
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ github.com/asaskevich/govalidator
# github.com/beorn7/perks v1.0.1
github.com/beorn7/perks/quantile
# github.com/blang/semver/v4 v4.0.0
## explicit
github.com/blang/semver/v4
# github.com/cespare/xxhash/v2 v2.1.1
github.com/cespare/xxhash/v2
Expand Down

0 comments on commit 8ed8abb

Please sign in to comment.