This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
manifests.go
124 lines (113 loc) · 3.36 KB
/
manifests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2018 Datadog, Inc.
package setup
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"text/template"
"github.com/golang/glog"
defaultTemplates "github.com/DataDog/pupernetes/pkg/setup/templates"
)
func createManifest(filePath string, content []byte) error {
glog.V(4).Infof("Creating default template in %s", filePath)
err := ioutil.WriteFile(filePath, content, 0444)
if err != nil {
glog.Errorf("Cannot create manifest file: %s")
return err
}
return nil
}
func (e *Environment) populateDefaultTemplates() error {
glog.V(4).Infof("Creating default templates, if needed ...")
for _, manifest := range defaultTemplates.Manifests {
filePath := path.Join(e.manifestTemplatesABSPath, manifest.Destination, fmt.Sprintf("%s.yaml", manifest.Name))
_, err := os.Stat(filePath)
if err == nil {
glog.V(4).Infof("Default template already here, not creating: %s", filePath)
continue
}
err = createManifest(filePath, manifest.Content)
if err != nil {
glog.Errorf("Cannot create manifest %s: %v", manifest.Name, err)
return err
}
}
return nil
}
// TODO keep the information
func (e *Environment) isStaticPod(filename string) bool {
for _, manifest := range defaultTemplates.Manifests {
if manifest.Destination == "/manifest-files" {
glog.V(5).Infof("This is marked as API pod: %s.yaml", manifest.Name)
continue
}
if filename == fmt.Sprintf("%s.yaml", manifest.Name) {
glog.V(5).Infof("This is marked as static pod: %s.yaml", manifest.Name)
return true
}
}
return false
}
func (e *Environment) renderTemplates(category string) error {
sourceDir := path.Join(e.manifestTemplatesABSPath, category)
files, err := ioutil.ReadDir(sourceDir)
if err != nil {
glog.Errorf("Cannot list the content of: %s, %v", sourceDir, err)
return err
}
b, err := json.Marshal(&e.templateMetadata)
if err != nil {
glog.Errorf("Cannot marshal template metadata: %v", err)
return err
}
glog.V(4).Infof("Rendering templates with the following metadata: %v", string(b))
for _, f := range files {
p := path.Join(sourceDir, f.Name())
b, err := ioutil.ReadFile(p)
if err != nil {
glog.Errorf("Cannot read the file %s: %v", p, err)
return err
}
tmpl, err := template.New(f.Name()).Parse(string(b))
if err != nil {
glog.Errorf("Cannot parse template from %s: %v", f.Name(), err)
return err
}
destPath := path.Join(e.rootABSPath, category, f.Name())
glog.V(4).Infof("Rendering manifest %s to %s", f.Name(), destPath)
dest, err := os.OpenFile(destPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0444)
if err != nil {
glog.Errorf("Cannot openfile %s: %v", destPath, err)
return err
}
err = tmpl.Execute(dest, e.templateMetadata)
if err != nil {
glog.Errorf("Cannot render template %s: %v", f.Name(), err)
return err
}
}
glog.V(4).Infof("Successfully render all templates")
return nil
}
func (e *Environment) setupManifests() error {
err := e.populateDefaultTemplates()
if err != nil {
return err
}
for _, t := range []string{
defaultTemplates.ManifestStaticPod,
defaultTemplates.ManifestConfig,
defaultTemplates.ManifestAPI,
} {
err = e.renderTemplates(t)
if err != nil {
return err
}
}
return nil
}