From b3fd80ac119d519af1eec1d4c9c1116753cc10da Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Sun, 15 Jan 2017 17:52:56 -0500 Subject: [PATCH] Split out systemd package Part of splitting up the CoreOS PR --- hack/.packages | 1 + nodeup/pkg/model/docker.go | 3 +- pkg/systemd/manifest.go | 90 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 pkg/systemd/manifest.go diff --git a/hack/.packages b/hack/.packages index 49922f6199..713003852f 100644 --- a/hack/.packages +++ b/hack/.packages @@ -31,6 +31,7 @@ k8s.io/kops/pkg/model k8s.io/kops/pkg/model/components k8s.io/kops/pkg/model/iam k8s.io/kops/pkg/model/resources +k8s.io/kops/pkg/systemd k8s.io/kops/pkg/validation k8s.io/kops/protokube/cmd/protokube k8s.io/kops/protokube/pkg/protokube diff --git a/nodeup/pkg/model/docker.go b/nodeup/pkg/model/docker.go index 099ce1ebf4..8f30badffa 100644 --- a/nodeup/pkg/model/docker.go +++ b/nodeup/pkg/model/docker.go @@ -21,6 +21,7 @@ import ( "github.com/blang/semver" "github.com/golang/glog" "k8s.io/kops/nodeup/pkg/model/resources" + "k8s.io/kops/pkg/systemd" "k8s.io/kops/upup/pkg/fi" "k8s.io/kops/upup/pkg/fi/nodeup/nodetasks" ) @@ -296,7 +297,7 @@ func (b *DockerBuilder) buildSystemdService(dockerVersion semver.Version) *nodet hasDockerBabysitter = true } - manifest := &ServiceManifest{} + manifest := &systemd.Manifest{} manifest.Set("Unit", "Description", "Docker Application Container Engine") manifest.Set("Unit", "Documentation", "https://docs.docker.com") diff --git a/pkg/systemd/manifest.go b/pkg/systemd/manifest.go new file mode 100644 index 0000000000..9d869da225 --- /dev/null +++ b/pkg/systemd/manifest.go @@ -0,0 +1,90 @@ +/* +Copyright 2016 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 systemd + +import "bytes" + +type Manifest struct { + Sections []*ManifestSection +} + +type ManifestSection struct { + Key string + Entries []*ManifestEntry +} + +type ManifestEntry struct { + Key string + Value string +} + +func (s *Manifest) Set(sectionKey string, key string, value string) { + section := s.getOrCreateSection(sectionKey) + section.Set(key, value) +} + +func (s *Manifest) getOrCreateSection(key string) *ManifestSection { + for _, section := range s.Sections { + if section.Key == key { + return section + } + } + section := &ManifestSection{ + Key: key, + } + s.Sections = append(s.Sections, section) + return section +} + +func (s *Manifest) Render() string { + var b bytes.Buffer + + for i, section := range s.Sections { + if i != 0 { + b.WriteString("\n") + } + b.WriteString(section.Render()) + } + + return b.String() +} + +func (s *ManifestSection) Set(key string, value string) { + for _, entry := range s.Entries { + if entry.Key == key { + entry.Value = value + return + } + } + + entry := &ManifestEntry{ + Key: key, + Value: value, + } + s.Entries = append(s.Entries, entry) +} + +func (s *ManifestSection) Render() string { + var b bytes.Buffer + + b.WriteString("[" + s.Key + "]\n") + for _, entry := range s.Entries { + b.WriteString(entry.Key + "=" + entry.Value + "\n") + } + + return b.String() +}