Skip to content

Commit

Permalink
Use new GitHub artifact names for mirrored assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciprian Hacman committed Sep 8, 2020
1 parent 766e65c commit 315ed2c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
3 changes: 3 additions & 0 deletions upup/pkg/fi/cloudup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ go_test(
"populateinstancegroup_test.go",
"subnets_test.go",
"template_functions_test.go",
"urls_test.go",
"validation_test.go",
],
data = [
Expand All @@ -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",
Expand All @@ -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",
Expand Down
20 changes: 17 additions & 3 deletions upup/pkg/fi/cloudup/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"os"
"path"
"sort"
"strings"

"k8s.io/klog/v2"
Expand All @@ -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/"},
}
Expand Down Expand Up @@ -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)
Expand Down
84 changes: 84 additions & 0 deletions upup/pkg/fi/cloudup/urls_test.go
Original file line number Diff line number Diff line change
@@ -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
}
})
}
}

0 comments on commit 315ed2c

Please sign in to comment.