Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use k8s.gcr.io alias only for 1.10 #4494

Merged
merged 5 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ func RunCreateCluster(f *util.Factory, out io.Writer, c *CreateClusterOptions) e
return err
}

assetBuilder := assets.NewAssetBuilder(cluster.Spec.Assets, "")
assetBuilder := assets.NewAssetBuilder(cluster, "")
fullCluster, err := cloudup.PopulateClusterSpec(clientset, cluster, assetBuilder)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/edit_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func RunEditCluster(f *util.Factory, cmd *cobra.Command, args []string, out io.W
return preservedFile(fmt.Errorf("error populating configuration: %v", err), file, out)
}

assetBuilder := assets.NewAssetBuilder(newCluster.Spec.Assets, "")
assetBuilder := assets.NewAssetBuilder(newCluster, "")
fullCluster, err := cloudup.PopulateClusterSpec(clientset, newCluster, assetBuilder)
if err != nil {
results = editResults{
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/edit_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func RunEditInstanceGroup(f *util.Factory, cmd *cobra.Command, args []string, ou
return fmt.Errorf("error populating configuration: %v", err)
}

assetBuilder := assets.NewAssetBuilder(cluster.Spec.Assets, "")
assetBuilder := assets.NewAssetBuilder(cluster, "")
fullCluster, err := cloudup.PopulateClusterSpec(clientset, cluster, assetBuilder)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/upgrade_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (c *UpgradeClusterCmd) Run(args []string) error {
return fmt.Errorf("error populating configuration: %v", err)
}

assetBuilder := assets.NewAssetBuilder(cluster.Spec.Assets, "")
assetBuilder := assets.NewAssetBuilder(cluster, "")
fullCluster, err := cloudup.PopulateClusterSpec(clientset, cluster, assetBuilder)
if err != nil {
return err
Expand Down
Binary file modified docs/img/k8s-aws.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions hack/.packages
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ k8s.io/kops/pkg/formatter
k8s.io/kops/pkg/instancegroups
k8s.io/kops/pkg/jsonutils
k8s.io/kops/pkg/k8scodecs
k8s.io/kops/pkg/k8sversion
k8s.io/kops/pkg/kopscodecs
k8s.io/kops/pkg/kubeconfig
k8s.io/kops/pkg/kubemanifest
Expand Down
2 changes: 1 addition & 1 deletion nodeup/pkg/model/protokube.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (t *ProtokubeBuilder) ProtokubeFlags(k8sVersion semver.Version) (*Protokube
if etcdContainerImage != "" {
image = etcdContainerImage
}
assets := assets.NewAssetBuilder(t.Cluster.Spec.Assets, "")
assets := assets.NewAssetBuilder(t.Cluster, "")
remapped, err := assets.RemapImage(image)
if err != nil {
return nil, fmt.Errorf("unable to remap container %q: %v", image, err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/assets/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/kops:go_default_library",
"//pkg/apis/kops/util:go_default_library",
"//pkg/featureflag:go_default_library",
"//pkg/kubemanifest:go_default_library",
"//pkg/values:go_default_library",
"//util/pkg/hashing:go_default_library",
"//util/pkg/vfs:go_default_library",
"//vendor/github.com/blang/semver:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
],
)
32 changes: 28 additions & 4 deletions pkg/assets/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"path"
"strings"

"github.com/blang/semver"
"github.com/golang/glog"

"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/util"
"k8s.io/kops/pkg/featureflag"
"k8s.io/kops/pkg/kubemanifest"
"k8s.io/kops/pkg/values"
Expand All @@ -45,6 +47,9 @@ type AssetBuilder struct {
AssetsLocation *kops.Assets
// TODO we'd like to use cloudup.Phase here, but that introduces a go cyclic dependency
Phase string

// KubernetesVersion is the version of kubernetes we are installing
KubernetesVersion semver.Version
}

// ContainerAsset models a container's location.
Expand All @@ -67,11 +72,20 @@ type FileAsset struct {
}

// NewAssetBuilder creates a new AssetBuilder.
func NewAssetBuilder(assets *kops.Assets, phase string) *AssetBuilder {
return &AssetBuilder{
AssetsLocation: assets,
func NewAssetBuilder(cluster *kops.Cluster, phase string) *AssetBuilder {
a := &AssetBuilder{
AssetsLocation: cluster.Spec.Assets,
Phase: phase,
}

version, err := util.ParseKubernetesVersion(cluster.Spec.KubernetesVersion)
if err != nil {
// This should have already been validated
glog.Fatalf("unexpected error from ParseKubernetesVersion %s: %v", cluster.Spec.KubernetesVersion, err)
}
a.KubernetesVersion = *version

return a
}

// RemapManifest transforms a kubernetes manifest.
Expand Down Expand Up @@ -112,6 +126,15 @@ func (a *AssetBuilder) RemapImage(image string) (string, error) {

asset.DockerImage = image

// The k8s.gcr.io prefix is an alias, but for CI builds we run from a docker load,
// and we only double-tag from 1.10 onwards.
// For versions prior to 1.10, remap k8s.gcr.io to the old name.
// This also means that we won't start using the aliased names on existing clusters,
// which could otherwise be surprising to users.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this comment, thanks for the explanation.

if !util.IsKubernetesGTE("1.10", a.KubernetesVersion) && strings.HasPrefix(image, "k8s.gcr.io/") {
image = "gcr.io/google_containers/" + strings.TrimPrefix(image, "k8s.gcr.io/")
}

if strings.HasPrefix(image, "kope/dns-controller:") {
// To use user-defined DNS Controller:
// 1. DOCKER_REGISTRY=[your docker hub repo] make dns-controller-push
Expand Down Expand Up @@ -145,7 +168,8 @@ func (a *AssetBuilder) RemapImage(image string) (string, error) {
return image, nil
}

// RemapFile sets a new url location for the file, if a AssetsLocation is defined.
// RemapFileAndSHA returns a remapped url for the file, if AssetsLocation is defined.
// It also returns the SHA hash of the file.
func (a *AssetBuilder) RemapFileAndSHA(fileURL *url.URL) (*url.URL, *hashing.Hash, error) {
if fileURL == nil {
return nil, nil, fmt.Errorf("unable to remap an nil URL")
Expand Down
2 changes: 1 addition & 1 deletion pkg/bundle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (b *Builder) Build(cluster *kops.Cluster, ig *kops.InstanceGroup) (*Data, e

{
phase := cloudup.PhaseCluster
assetBuilder := assets.NewAssetBuilder(cluster.Spec.Assets, string(phase))
assetBuilder := assets.NewAssetBuilder(cluster, string(phase))

applyCmd := &cloudup.ApplyClusterCmd{
Cluster: cluster,
Expand Down
19 changes: 19 additions & 0 deletions pkg/k8sversion/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["version.go"],
importpath = "k8s.io/kops/pkg/k8sversion",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/kops/util:go_default_library",
"//vendor/github.com/blang/semver:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["version_test.go"],
embed = [":go_default_library"],
importpath = "k8s.io/kops/pkg/k8sversion",
)
50 changes: 50 additions & 0 deletions pkg/k8sversion/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2018 The Kubernetes Authors.

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 k8sversion

import (
"github.com/blang/semver"

"k8s.io/kops/pkg/apis/kops/util"
)

// KubernetesVersion holds a semver-version of kubernetes
type KubernetesVersion struct {
semver semver.Version
}

// Parse parses the string to determine the KubernetesVersion.
// The version may be a semver version, or it may be a URL with the kubernetes version in the path
func Parse(version string) (*KubernetesVersion, error) {
sv, err := util.ParseKubernetesVersion(version)
if err != nil {
return nil, err
}

return &KubernetesVersion{semver: *sv}, nil
}

// IsGTE checks if the version is greater than or equal to the passed version. Pre and Build fields are ignored.
// Panic if version is not valid, so version should only be used with static strings like "1.10"
func (k *KubernetesVersion) IsGTE(version string) bool {
return util.IsKubernetesGTE(version, k.semver)
}

// String returns a string representation of the semver, like 1.10.1. It does not include a leading 'v'
func (k *KubernetesVersion) String() string {
return k.semver.String()
}
51 changes: 51 additions & 0 deletions pkg/k8sversion/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2018 The Kubernetes Authors.

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 k8sversion

import "testing"

func TestParse(t *testing.T) {
grid := []struct {
Input string
Expected string
}{
{Input: "1.1.0", Expected: "1.1.0"},
{Input: "1.2.0", Expected: "1.2.0"},
{Input: "1.3.0", Expected: "1.3.0"},
{Input: "1.4.0", Expected: "1.4.0"},
{Input: "1.5.0", Expected: "1.5.0"},
{Input: "1.6.0", Expected: "1.6.0"},
{Input: "1.7.0", Expected: "1.7.0"},
{Input: "1.8.0", Expected: "1.8.0"},
{Input: "1.9.0", Expected: "1.9.0"},
{Input: "1.10.0", Expected: "1.10.0"},
{Input: "v1.1.0-alpha1", Expected: "1.1.0-alpha1"},
{Input: "https://example.com/v1.8.0-downloads", Expected: "1.8.0"},
}

for _, g := range grid {
actual, err := Parse(g.Input)
if err != nil {
t.Errorf("error parsing %q: %v", g.Input, err)
continue
}
if actual.String() != g.Expected {
t.Errorf("unexpected result parsing %q: actual=%q expected=%q", g.Input, actual.String(), g.Expected)
continue
}
}
}
1 change: 1 addition & 0 deletions pkg/model/components/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ go_library(
"//pkg/apis/kops:go_default_library",
"//pkg/apis/kops/util:go_default_library",
"//pkg/assets:go_default_library",
"//pkg/k8sversion:go_default_library",
"//upup/pkg/fi:go_default_library",
"//upup/pkg/fi/cloudup/gce:go_default_library",
"//upup/pkg/fi/loader:go_default_library",
Expand Down
23 changes: 20 additions & 3 deletions pkg/model/components/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/util"
"k8s.io/kops/pkg/assets"
"k8s.io/kops/pkg/k8sversion"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/util/pkg/vfs"

Expand Down Expand Up @@ -120,7 +121,7 @@ func WellKnownServiceIP(clusterSpec *kops.ClusterSpec, id int) (net.IP, error) {
}

func IsBaseURL(kubernetesVersion string) bool {
return strings.HasPrefix(kubernetesVersion, "http:") || strings.HasPrefix(kubernetesVersion, "https:")
return strings.HasPrefix(kubernetesVersion, "http:") || strings.HasPrefix(kubernetesVersion, "https:") || strings.HasPrefix(kubernetesVersion, "memfs:")
}

// Image returns the docker image name for the specified component
Expand All @@ -134,8 +135,13 @@ func Image(component string, clusterSpec *kops.ClusterSpec, assetsBuilder *asset
return "k8s.gcr.io/kubedns-amd64:1.3", nil
}

kubernetesVersion, err := k8sversion.Parse(clusterSpec.KubernetesVersion)
if err != nil {
return "", err
}

if !IsBaseURL(clusterSpec.KubernetesVersion) {
image := "k8s.gcr.io/" + component + ":" + "v" + clusterSpec.KubernetesVersion
image := "k8s.gcr.io/" + component + ":" + "v" + kubernetesVersion.String()

image, err := assetsBuilder.RemapImage(image)
if err != nil {
Expand All @@ -158,7 +164,18 @@ func Image(component string, clusterSpec *kops.ClusterSpec, assetsBuilder *asset
tag := strings.TrimSpace(string(b))
glog.V(2).Infof("Found tag %q for %q", tag, component)

return "k8s.gcr.io/" + component + ":" + tag, nil
image := "k8s.gcr.io/" + component + ":" + tag

// When we're using a docker load-ed image, we are likely a CI build.
// But the k8s.gcr.io prefix is an alias, and we only double-tagged from 1.10 onwards.
// For versions prior to 1.10, remap k8s.gcr.io to the old name.
// This also means that we won't start using the aliased names on existing clusters,
// which could otherwise be surprising to users.
if !kubernetesVersion.IsGTE("1.10") {
image = "gcr.io/google_containers/" + strings.TrimPrefix(image, "k8s.gcr.io/")
}

return image, nil
}

func GCETagForRole(clusterName string, role kops.InstanceGroupRole) string {
Expand Down
Loading