Load env vars from file for kops-configuration service

This commit is contained in:
Ciprian Hacman 2021-03-22 14:06:25 +02:00
parent 3f8fdc2e7c
commit 1b57bfbb8f
4 changed files with 96 additions and 5 deletions

View File

@ -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",
],
)

View File

@ -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")

View File

@ -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)
}

View File

@ -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