Skip to content

Commit

Permalink
Load env vars from file for kops-configuration service
Browse files Browse the repository at this point in the history
  • Loading branch information
hakman committed Mar 23, 2021
1 parent 3f8fdc2 commit 1b57bfb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
13 changes: 12 additions & 1 deletion nodeup/pkg/bootstrap/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions nodeup/pkg/bootstrap/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ func (i *Installation) Run() error {

return nil
}

func (i *Installation) Build(c *fi.ModelBuilderContext) {
c.AddTask(i.buildEnvFile())
c.AddTask(i.buildSystemdJob())
}

func (i *Installation) buildEnv() string {
func (i *Installation) buildEnvFile() *nodetasks.File {
var envVars = make(map[string]string)

if os.Getenv("AWS_REGION") != "" {
Expand Down Expand Up @@ -152,7 +154,13 @@ func (i *Installation) buildEnv() string {
sysconfig += key + "=" + value + "\n"
}

return sysconfig
task := &nodetasks.File{
Path: "/etc/sysconfig/kops-configuration",
Contents: fi.NewStringResource(sysconfig),
Type: nodetasks.FileType_File,
}

return task
}

func (i *Installation) buildSystemdJob() *nodetasks.Service {
Expand All @@ -161,10 +169,10 @@ func (i *Installation) buildSystemdJob() *nodetasks.Service {
serviceName := "kops-configuration.service"

manifest := &systemd.Manifest{}
manifest.Set("Unit", "Description", "Run kops bootstrap (nodeup)")
manifest.Set("Unit", "Description", "Run kOps bootstrap (nodeup)")
manifest.Set("Unit", "Documentation", "https://github.com/kubernetes/kops")

manifest.Set("Service", "Environment", i.buildEnv())
manifest.Set("Service", "EnvironmentFile", "/etc/sysconfig/kops-configuration")
manifest.Set("Service", "EnvironmentFile", "/etc/environment")
manifest.Set("Service", "ExecStart", command)
manifest.Set("Service", "Type", "oneshot")
Expand Down
49 changes: 49 additions & 0 deletions nodeup/pkg/bootstrap/install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2019 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 bootstrap

import (
"os"
"path/filepath"
"testing"

"k8s.io/kops/pkg/testutils"
"k8s.io/kops/upup/pkg/fi"
)

func TestBootstarapBuilder_Simple(t *testing.T) {
origRegion := os.Getenv("AWS_REGION")
os.Setenv("AWS_REGION", "us-test1")
defer func() {
os.Setenv("AWS_REGION", origRegion)
}()

runInstallBuilderTest(t, "tests/simple")
}

func runInstallBuilderTest(t *testing.T, basedir string) {
installation := Installation{
Command: []string{"/opt/kops/bin/nodeup", "--conf=/opt/kops/conf/kube_env.yaml", "--v=8"},
}
tasks := make(map[string]fi.Task)
buildContext := &fi.ModelBuilderContext{
Tasks: tasks,
}
installation.Build(buildContext)

testutils.ValidateTasks(t, filepath.Join(basedir, "tasks.yaml"), buildContext)
}
23 changes: 23 additions & 0 deletions nodeup/pkg/bootstrap/tests/simple/tasks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
contents: |
AWS_REGION=us-test1
path: /etc/sysconfig/kops-configuration
type: file
---
Name: kops-configuration.service
definition: |
[Unit]
Description=Run kOps bootstrap (nodeup)
Documentation=https://github.com/kubernetes/kops
[Service]
EnvironmentFile=/etc/sysconfig/kops-configuration
EnvironmentFile=/etc/environment
ExecStart=/opt/kops/bin/nodeup --conf=/opt/kops/conf/kube_env.yaml --v=8
Type=oneshot
[Install]
WantedBy=multi-user.target
enabled: true
manageState: true
running: true
smartRestart: true

0 comments on commit 1b57bfb

Please sign in to comment.