Skip to content

Commit

Permalink
Fix various nits when changing Protokube to run as service
Browse files Browse the repository at this point in the history
  • Loading branch information
hakman committed Mar 8, 2021
1 parent 1d721c3 commit 77b72ef
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 91 deletions.
2 changes: 1 addition & 1 deletion docs/contributing/adding_a_feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:


Expand Down
48 changes: 25 additions & 23 deletions nodeup/pkg/model/protokube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions upup/pkg/fi/cloudup/apply_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
94 changes: 31 additions & 63 deletions upup/pkg/fi/cloudup/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/url"
"os"
"path"
"strings"

"k8s.io/klog/v2"
"k8s.io/kops"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 77b72ef

Please sign in to comment.