From 315ed2c9b5a9937e9528e5d5aebfd4b213598941 Mon Sep 17 00:00:00 2001 From: Ciprian Hacman Date: Tue, 8 Sep 2020 08:04:02 +0300 Subject: [PATCH] Use new GitHub artifact names for mirrored assets --- upup/pkg/fi/cloudup/BUILD.bazel | 3 ++ upup/pkg/fi/cloudup/urls.go | 20 ++++++-- upup/pkg/fi/cloudup/urls_test.go | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 upup/pkg/fi/cloudup/urls_test.go diff --git a/upup/pkg/fi/cloudup/BUILD.bazel b/upup/pkg/fi/cloudup/BUILD.bazel index 2d3f5eafaabc3..ece3ac29d2c4b 100644 --- a/upup/pkg/fi/cloudup/BUILD.bazel +++ b/upup/pkg/fi/cloudup/BUILD.bazel @@ -104,6 +104,7 @@ go_test( "populateinstancegroup_test.go", "subnets_test.go", "template_functions_test.go", + "urls_test.go", "validation_test.go", ], data = [ @@ -112,6 +113,7 @@ go_test( ], embed = [":go_default_library"], deps = [ + "//:go_default_library", "//pkg/apis/kops:go_default_library", "//pkg/apis/kops/validation:go_default_library", "//pkg/assets:go_default_library", @@ -129,6 +131,7 @@ go_test( "//upup/pkg/fi/cloudup/awsup:go_default_library", "//upup/pkg/fi/fitasks:go_default_library", "//util/pkg/architectures:go_default_library", + "//util/pkg/hashing:go_default_library", "//util/pkg/vfs:go_default_library", "//vendor/github.com/ghodss/yaml:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", diff --git a/upup/pkg/fi/cloudup/urls.go b/upup/pkg/fi/cloudup/urls.go index 2cfbcfd501ccb..ab43990537113 100644 --- a/upup/pkg/fi/cloudup/urls.go +++ b/upup/pkg/fi/cloudup/urls.go @@ -21,6 +21,7 @@ import ( "net/url" "os" "path" + "sort" "strings" "k8s.io/klog/v2" @@ -46,11 +47,19 @@ type mirror struct { Replace map[string]string } +// GitHub releases have special artifact names because full paths cannot be used +// and artifact names must remain easy to read for humans. +var gitHubReplace = map[string]string{ + "/": "-", + "linux-amd64-nodeup": "nodeup-linux-amd64", + "linux-arm64-nodeup": "nodeup-linux-arm64", +} + // defaultKopsMirrors is a list of our well-known mirrors // Note that we download in order var defaultKopsMirrors = []mirror{ {URL: "https://artifacts.k8s.io/binaries/kops/%s/"}, - {URL: "https://github.com/kubernetes/kops/releases/download/v%s/", Replace: map[string]string{"/": "-"}}, + {URL: "https://github.com/kubernetes/kops/releases/download/v%s/", Replace: gitHubReplace}, // We do need to include defaultKopsMirrorBase - the list replaces the base url {URL: "https://kubeupv2.s3.amazonaws.com/kops/%s/"}, } @@ -254,9 +263,14 @@ func BuildMirroredAsset(u *url.URL, hash *hashing.Hash) *MirroredAsset { // This is under our base url - add our well-known mirrors a.Locations = []string{} for _, m := range defaultKopsMirrors { + keys := make([]string, len(m.Replace)) + for k := range m.Replace { + keys = append(keys, k) + } + sort.Strings(keys) filename := suffix - for k, v := range m.Replace { - filename = strings.Replace(filename, k, v, -1) + for _, k := range keys { + filename = strings.Replace(filename, k, m.Replace[k], -1) } base := fmt.Sprintf(m.URL, kops.Version) a.Locations = append(a.Locations, base+filename) diff --git a/upup/pkg/fi/cloudup/urls_test.go b/upup/pkg/fi/cloudup/urls_test.go new file mode 100644 index 0000000000000..94b656dde6438 --- /dev/null +++ b/upup/pkg/fi/cloudup/urls_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2020 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 cloudup + +import ( + "fmt" + "net/url" + "reflect" + "testing" + + "k8s.io/kops" + "k8s.io/kops/util/pkg/hashing" +) + +func Test_BuildMirroredAsset(t *testing.T) { + tests := []struct { + url string + hash string + expected []string + }{ + { + url: "https://kubeupv2.s3.amazonaws.com/kops/%s/images/protokube-linux-amd64", + expected: []string{ + "https://artifacts.k8s.io/binaries/kops/1.19.0-alpha.3/images/protokube-linux-amd64", + "https://github.com/kubernetes/kops/releases/download/v1.19.0-alpha.3/images-protokube-linux-amd64", + "https://kubeupv2.s3.amazonaws.com/kops/1.19.0-alpha.3/images/protokube-linux-amd64", + }, + }, + { + url: "https://kubeupv2.s3.amazonaws.com/kops/%s/images/protokube-linux-arm64", + expected: []string{ + "https://artifacts.k8s.io/binaries/kops/1.19.0-alpha.3/images/protokube-linux-arm64", + "https://github.com/kubernetes/kops/releases/download/v1.19.0-alpha.3/images-protokube-linux-arm64", + "https://kubeupv2.s3.amazonaws.com/kops/1.19.0-alpha.3/images/protokube-linux-arm64", + }, + }, + { + url: "https://kubeupv2.s3.amazonaws.com/kops/%s/linux/amd64/nodeup", + expected: []string{ + "https://artifacts.k8s.io/binaries/kops/1.19.0-alpha.3/linux/amd64/nodeup", + "https://github.com/kubernetes/kops/releases/download/v1.19.0-alpha.3/nodeup-linux-amd64", + "https://kubeupv2.s3.amazonaws.com/kops/1.19.0-alpha.3/linux/amd64/nodeup", + }, + }, + { + url: "https://kubeupv2.s3.amazonaws.com/kops/%s/linux/arm64/nodeup", + expected: []string{ + "https://artifacts.k8s.io/binaries/kops/1.19.0-alpha.3/linux/arm64/nodeup", + "https://github.com/kubernetes/kops/releases/download/v1.19.0-alpha.3/nodeup-linux-arm64", + "https://kubeupv2.s3.amazonaws.com/kops/1.19.0-alpha.3/linux/arm64/nodeup", + }, + }, + } + for _, tc := range tests { + t.Run(tc.url, func(t *testing.T) { + h, _ := hashing.FromString("0000000000000000000000000000000000000000000000000000000000000000") + u, err := url.Parse(fmt.Sprintf(tc.url, kops.Version)) + if err != nil { + t.Errorf("cannot parse URL: %s", fmt.Sprintf(tc.url, kops.Version)) + return + } + actual := BuildMirroredAsset(u, h) + + if !reflect.DeepEqual(actual.Locations, tc.expected) { + t.Errorf("Locations differ:\nActual: %+v\nExpect: %+v", actual.Locations, tc.expected) + return + } + }) + } +}