From 1b57bfbb8f1ef7b0c90d5ef1842d448badcab347 Mon Sep 17 00:00:00 2001 From: Ciprian Hacman Date: Mon, 22 Mar 2021 14:06:25 +0200 Subject: [PATCH] Load env vars from file for kops-configuration service --- nodeup/pkg/bootstrap/BUILD.bazel | 13 +++++- nodeup/pkg/bootstrap/install.go | 16 +++++-- nodeup/pkg/bootstrap/install_test.go | 49 ++++++++++++++++++++ nodeup/pkg/bootstrap/tests/simple/tasks.yaml | 23 +++++++++ 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 nodeup/pkg/bootstrap/install_test.go create mode 100644 nodeup/pkg/bootstrap/tests/simple/tasks.yaml diff --git a/nodeup/pkg/bootstrap/BUILD.bazel b/nodeup/pkg/bootstrap/BUILD.bazel index 0dc4784413..b68ff500e3 100644 --- a/nodeup/pkg/bootstrap/BUILD.bazel +++ b/nodeup/pkg/bootstrap/BUILD.bazel @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", @@ -15,3 +15,14 @@ go_library( "//vendor/k8s.io/klog/v2:go_default_library", ], ) + +go_test( + name = "go_default_test", + srcs = ["install_test.go"], + data = glob(["tests/**"]), #keep + embed = [":go_default_library"], + deps = [ + "//pkg/testutils:go_default_library", + "//upup/pkg/fi:go_default_library", + ], +) diff --git a/nodeup/pkg/bootstrap/install.go b/nodeup/pkg/bootstrap/install.go index c5b5deaeff..90d94af076 100644 --- a/nodeup/pkg/bootstrap/install.go +++ b/nodeup/pkg/bootstrap/install.go @@ -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") != "" { @@ -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 { @@ -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") diff --git a/nodeup/pkg/bootstrap/install_test.go b/nodeup/pkg/bootstrap/install_test.go new file mode 100644 index 0000000000..0440eeb161 --- /dev/null +++ b/nodeup/pkg/bootstrap/install_test.go @@ -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) +} diff --git a/nodeup/pkg/bootstrap/tests/simple/tasks.yaml b/nodeup/pkg/bootstrap/tests/simple/tasks.yaml new file mode 100644 index 0000000000..e7e5a6e76a --- /dev/null +++ b/nodeup/pkg/bootstrap/tests/simple/tasks.yaml @@ -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