diff --git a/go.mod b/go.mod index d353ca1b87..03d258a7f5 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/install/install.go b/install/install.go index 05f51720fc..5f7e08be54 100644 --- a/install/install.go +++ b/install/install.go @@ -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" @@ -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) @@ -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{ @@ -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")) { @@ -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) diff --git a/install/install_test.go b/install/install_test.go new file mode 100644 index 0000000000..43c9484429 --- /dev/null +++ b/install/install_test.go @@ -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) + } + }) + } +} diff --git a/internal/cli/cmd/install.go b/internal/cli/cmd/install.go index 1a44cf4646..b6e2d745c5 100644 --- a/internal/cli/cmd/install.go +++ b/internal/cli/cmd/install.go @@ -61,6 +61,9 @@ cilium install --context kind-cluster1 --cluster-id 1 --cluster-name cluster1 cmd.Flags().StringVar(¶ms.ClusterName, "cluster-name", "", "Name of the cluster") cmd.Flags().StringSliceVar(¶ms.DisableChecks, "disable-check", []string{}, "Disable a particular validation check") cmd.Flags().StringVar(¶ms.Version, "version", defaults.Version, "Cilium version to install") + cmd.Flags().StringVar(¶ms.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(¶ms.DatapathMode, "datapath-mode", "", "Datapath mode to use") cmd.Flags().StringVar(¶ms.IPAM, "ipam", "", "IP Address Management (IPAM) mode") cmd.Flags().StringVar(¶ms.NativeRoutingCIDR, "native-routing-cidr", "", "CIDR within which native routing is possible") diff --git a/vendor/modules.txt b/vendor/modules.txt index 3df39a85e9..421e5fe72b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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