From 77b72efe1df06684dc5e58d458c2a5000b75eff8 Mon Sep 17 00:00:00 2001 From: Ciprian Hacman Date: Sat, 6 Mar 2021 12:40:29 +0200 Subject: [PATCH] Fix various nits when changing Protokube to run as service --- docs/contributing/adding_a_feature.md | 2 +- nodeup/pkg/model/protokube.go | 48 +++++++------- upup/pkg/fi/cloudup/apply_cluster.go | 10 +-- upup/pkg/fi/cloudup/urls.go | 94 +++++++++------------------ 4 files changed, 63 insertions(+), 91 deletions(-) diff --git a/docs/contributing/adding_a_feature.md b/docs/contributing/adding_a_feature.md index 4b4620cce9f99..92ca8d0cb55f8 100644 --- a/docs/contributing/adding_a_feature.md +++ b/docs/contributing/adding_a_feature.md @@ -232,7 +232,7 @@ run it over SSH with the output viewable locally: For more complete testing though, you will likely want to do a private build of nodeup and launch a cluster from scratch. -To do this, you can repoint the nodeup source url by setting the `NODEUP_URL` env var, +To do this, you can repoint the nodeup source url by setting the `KOPS_BASE_URL` env var, and then push nodeup using: diff --git a/nodeup/pkg/model/protokube.go b/nodeup/pkg/model/protokube.go index 87da17cd57986..f8eb3ebd36537 100644 --- a/nodeup/pkg/model/protokube.go +++ b/nodeup/pkg/model/protokube.go @@ -57,31 +57,33 @@ func (t *ProtokubeBuilder) Build(c *fi.ModelBuilderContext) error { return nil } - protokubeBinName, protokubeBinRes, err := t.Assets.FindMatch(regexp.MustCompile("protokube$")) - if err != nil { - return err - } + { + name, res, err := t.Assets.FindMatch(regexp.MustCompile("protokube$")) + if err != nil { + return err + } - fileTaskProtokubeBin := &nodetasks.File{ - Path: filepath.Join("/opt/kops/bin", protokubeBinName), - Contents: protokubeBinRes, - Type: nodetasks.FileType_File, - Mode: fi.String("0755"), + c.AddTask(&nodetasks.File{ + Path: filepath.Join("/opt/kops/bin", name), + Contents: res, + Type: nodetasks.FileType_File, + Mode: fi.String("0755"), + }) } - c.AddTask(fileTaskProtokubeBin) - channelBinName, channelBinRes, err := t.Assets.FindMatch(regexp.MustCompile("channels$")) - if err != nil { - return err - } + { + name, res, err := t.Assets.FindMatch(regexp.MustCompile("channels$")) + if err != nil { + return err + } - fileTaskChannel := &nodetasks.File{ - Path: filepath.Join("/opt/kops/bin", channelBinName), - Contents: channelBinRes, - Type: nodetasks.FileType_File, - Mode: fi.String("0755"), + c.AddTask(&nodetasks.File{ + Path: filepath.Join("/opt/kops/bin", name), + Contents: res, + Type: nodetasks.FileType_File, + Mode: fi.String("0755"), + }) } - c.AddTask(fileTaskChannel) if t.IsMaster { name := nodetasks.PKIXName{ @@ -112,11 +114,11 @@ func (t *ProtokubeBuilder) Build(c *fi.ModelBuilderContext) error { } } - envFileTask, err := t.buildProtokubeEnvironmentVariables() + envFile, err := t.buildEnvFile() if err != nil { return err } - c.AddTask(envFileTask) + c.AddTask(envFile) service, err := t.buildSystemdService() if err != nil { @@ -400,7 +402,7 @@ func (t *ProtokubeBuilder) ProtokubeFlags(k8sVersion semver.Version) (*Protokube return f, nil } -func (t *ProtokubeBuilder) buildProtokubeEnvironmentVariables() (*nodetasks.File, error) { +func (t *ProtokubeBuilder) buildEnvFile() (*nodetasks.File, error) { var envVars = make(map[string]string) envVars["KUBECONFIG"] = "/var/lib/kops/kubeconfig" diff --git a/upup/pkg/fi/cloudup/apply_cluster.go b/upup/pkg/fi/cloudup/apply_cluster.go index 86b342fba3ff9..aa2d377955fb6 100644 --- a/upup/pkg/fi/cloudup/apply_cluster.go +++ b/upup/pkg/fi/cloudup/apply_cluster.go @@ -1227,17 +1227,19 @@ func newNodeUpConfigBuilder(cluster *kops.Cluster, assetBuilder *assets.AssetBui channelsAsset := map[architectures.Architecture][]*mirrors.MirroredAsset{} for _, arch := range architectures.GetSupported() { - protokubeBinAsset, err := ProtokubeBinaryAsset(assetBuilder, arch) + asset, err := ProtokubeAsset(assetBuilder, arch) if err != nil { return nil, err } - protokubeAsset[arch] = append(protokubeAsset[arch], protokubeBinAsset) + protokubeAsset[arch] = append(protokubeAsset[arch], asset) + } - channelsBinAsset, err := ChannelsBinaryAsset(assetBuilder, arch) + for _, arch := range architectures.GetSupported() { + asset, err := ChannelsAsset(assetBuilder, arch) if err != nil { return nil, err } - channelsAsset[arch] = append(channelsAsset[arch], channelsBinAsset) + channelsAsset[arch] = append(channelsAsset[arch], asset) } for _, role := range kops.AllInstanceGroupRoles { diff --git a/upup/pkg/fi/cloudup/urls.go b/upup/pkg/fi/cloudup/urls.go index 079935f48f64d..a1069620c7814 100644 --- a/upup/pkg/fi/cloudup/urls.go +++ b/upup/pkg/fi/cloudup/urls.go @@ -21,7 +21,6 @@ import ( "net/url" "os" "path" - "strings" "k8s.io/klog/v2" "k8s.io/kops" @@ -37,14 +36,14 @@ const ( var kopsBaseURL *url.URL -// nodeUpAsset caches the nodeup download urls/hash +// nodeUpAsset caches the nodeup binary download url/hash var nodeUpAsset map[architectures.Architecture]*mirrors.MirroredAsset -// protokubeBinAsset caches the protokube binary download urls/hash -var protokubeBinAsset map[architectures.Architecture]*mirrors.MirroredAsset +// protokubeAsset caches the protokube binary download url/hash +var protokubeAsset map[architectures.Architecture]*mirrors.MirroredAsset -// channelsBinAsset caches the channels binary download urls/hash -var channelsBinAsset map[architectures.Architecture]*mirrors.MirroredAsset +// channelsAsset caches the channels binary download url/hash +var channelsAsset map[architectures.Architecture]*mirrors.MirroredAsset // BaseURL returns the base url for the distribution of kops - in particular for nodeup & docker images func BaseURL() (*url.URL, error) { @@ -108,86 +107,55 @@ func NodeUpAsset(assetsBuilder *assets.AssetBuilder, arch architectures.Architec klog.V(8).Infof("Using cached nodeup location for %s: %v", arch, nodeUpAsset[arch].Locations) return nodeUpAsset[arch], nil } - // Use multi-arch env var, but fall back to well known env var - env := os.Getenv(fmt.Sprintf("NODEUP_URL_%s", strings.ToUpper(string(arch)))) - if env == "" { - env = os.Getenv("NODEUP_URL") - } - var err error - var u *url.URL - var hash *hashing.Hash - if env == "" { - u, hash, err = KopsFileURL(fmt.Sprintf("linux/%s/nodeup", arch), assetsBuilder) - if err != nil { - return nil, err - } - klog.V(8).Infof("Using default nodeup location for %s: %q", arch, u.String()) - } else { - u, err = url.Parse(env) - if err != nil { - return nil, fmt.Errorf("unable to parse env var NODEUP_URL(_%s) %q as a url: %v", strings.ToUpper(string(arch)), env, err) - } - u, hash, err = assetsBuilder.RemapFileAndSHA(u) - if err != nil { - return nil, err - } - klog.Warningf("Using nodeup location from NODEUP_URL(_%s) env var: %q", strings.ToUpper(string(arch)), u.String()) + u, hash, err := KopsFileURL(fmt.Sprintf("linux/%s/nodeup", arch), assetsBuilder) + if err != nil { + return nil, err } + nodeUpAsset[arch] = mirrors.BuildMirroredAsset(u, hash) + klog.V(8).Infof("Using default nodeup location for %s: %q", arch, u.String()) - asset := mirrors.BuildMirroredAsset(u, hash) - - nodeUpAsset[arch] = asset - - return asset, nil + return nodeUpAsset[arch], nil } -// ProtokubeBinaryAsset returns the url and hash of the protokube binary. This is useful for running protokube as a -// systemd process rather than a container. -func ProtokubeBinaryAsset(assetsBuilder *assets.AssetBuilder, arch architectures.Architecture) (*mirrors.MirroredAsset, error) { - if protokubeBinAsset == nil { - protokubeBinAsset = make(map[architectures.Architecture]*mirrors.MirroredAsset) +// ProtokubeAsset returns the url and hash of the protokube binary +func ProtokubeAsset(assetsBuilder *assets.AssetBuilder, arch architectures.Architecture) (*mirrors.MirroredAsset, error) { + if protokubeAsset == nil { + protokubeAsset = make(map[architectures.Architecture]*mirrors.MirroredAsset) } - - if protokubeBinAsset[arch] != nil { - klog.V(8).Infof("Using cached protokube binary location for %s: %v", arch, protokubeBinAsset[arch].Locations) - return protokubeBinAsset[arch], nil + if protokubeAsset[arch] != nil { + klog.V(8).Infof("Using cached protokube binary location for %s: %v", arch, protokubeAsset[arch].Locations) + return protokubeAsset[arch], nil } - // TODO: (bharath-123) should we allow the user to specify the binary url through an environment variable like how we do for NODEUP_URL? u, hash, err := KopsFileURL(fmt.Sprintf("linux/%s/protokube", arch), assetsBuilder) if err != nil { return nil, err } + protokubeAsset[arch] = mirrors.BuildMirroredAsset(u, hash) + klog.V(8).Infof("Using default protokube location for %s: %q", arch, u.String()) - asset := mirrors.BuildMirroredAsset(u, hash) - - protokubeBinAsset[arch] = asset - - return asset, nil + return protokubeAsset[arch], nil } -// ChannelsBinaryAsset returns the url and hash of the channels binary. Protokube requires this to run as a systemd process. -func ChannelsBinaryAsset(assetsBuilder *assets.AssetBuilder, arch architectures.Architecture) (*mirrors.MirroredAsset, error) { - if channelsBinAsset == nil { - channelsBinAsset = make(map[architectures.Architecture]*mirrors.MirroredAsset) +// ChannelsAsset returns the url and hash of the channels binary +func ChannelsAsset(assetsBuilder *assets.AssetBuilder, arch architectures.Architecture) (*mirrors.MirroredAsset, error) { + if channelsAsset == nil { + channelsAsset = make(map[architectures.Architecture]*mirrors.MirroredAsset) } - - if channelsBinAsset[arch] != nil { - klog.V(8).Infof("Using cached channels binary location for %s: %v", arch, channelsBinAsset[arch].Locations) - return channelsBinAsset[arch], nil + if channelsAsset[arch] != nil { + klog.V(8).Infof("Using cached channels binary location for %s: %v", arch, channelsAsset[arch].Locations) + return channelsAsset[arch], nil } u, hash, err := KopsFileURL(fmt.Sprintf("linux/%s/channels", arch), assetsBuilder) if err != nil { return nil, err } + channelsAsset[arch] = mirrors.BuildMirroredAsset(u, hash) + klog.V(8).Infof("Using default channels location for %s: %q", arch, u.String()) - asset := mirrors.BuildMirroredAsset(u, hash) - - channelsBinAsset[arch] = asset - - return asset, nil + return channelsAsset[arch], nil } // KopsFileURL returns the base url for the distribution of kops - in particular for nodeup & docker images