This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
forked from Mirantis/virtlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
166 lines (146 loc) · 3.97 KB
/
gen.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Copyright 2018 Mirantis
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 tools
import (
"errors"
"io"
"github.com/spf13/cobra"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/Mirantis/virtlet/pkg/config"
"github.com/Mirantis/virtlet/pkg/version"
)
const (
sourceYamlFile = "deploy/data/virtlet-ds.yaml"
virtletImage = "mirantis/virtlet"
)
// genCommand is used to generate Kubernetes YAML for Virtlet deployment
type genCommand struct {
out io.Writer
dev bool
compat bool
crd bool
tag string
}
// NewGenCmd returns a cobra.Command that generates Kubernetes YAML for Virtlet
// deployment.
func NewGenCmd(out io.Writer) *cobra.Command {
g := &genCommand{out: out}
cmd := &cobra.Command{
Use: "gen",
Short: "Generate Kubernetes YAML for Virtlet deployment",
Long: "This command produces YAML suitable for use with kubectl apply -f -",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New("This command does not accept arguments")
}
return g.Run()
},
}
cmd.Flags().BoolVar(&g.dev, "dev", false, "Development mode for use with kubeadm-dind-cluster")
cmd.Flags().BoolVar(&g.compat, "compat", false, "Produce YAML that's compatible with older Kubernetes versions")
cmd.Flags().BoolVar(&g.crd, "crd", false, "Dump CRD definitions only")
cmd.Flags().StringVar(&g.tag, "tag", version.Get().ImageTag, "Set virtlet image tag")
return cmd
}
func (g *genCommand) getYaml() ([]byte, error) {
var objs []runtime.Object
if !g.crd {
bs, err := Asset(sourceYamlFile)
if err != nil {
return bs, err
}
if objs, err = LoadYaml(bs); err != nil {
return nil, err
}
if len(objs) == 0 {
return nil, errors.New("source yaml is empty")
}
ds, ok := objs[0].(*apps.DaemonSet)
if !ok {
return nil, errors.New("the first object is not a DaemonSet")
}
if g.dev {
applyDev(ds)
}
if g.compat {
applyCompat(ds)
}
if g.tag != "" {
applyTag(ds, g.tag)
}
}
objs = append(objs, config.GetCRDDefinitions()...)
return ToYaml(objs)
}
// Run executes the command.
func (g *genCommand) Run() error {
bs, err := g.getYaml()
if err != nil {
return err
}
if _, err := g.out.Write(bs); err != nil {
return err
}
return nil
}
func walkContainers(ds *apps.DaemonSet, toCall func(c *v1.Container)) {
initContainers := ds.Spec.Template.Spec.InitContainers
for n := range initContainers {
toCall(&initContainers[n])
}
containers := ds.Spec.Template.Spec.Containers
for n := range containers {
toCall(&containers[n])
}
}
func walkMounts(ds *apps.DaemonSet, toCall func(m *v1.VolumeMount)) {
walkContainers(ds, func(c *v1.Container) {
for i := range c.VolumeMounts {
toCall(&c.VolumeMounts[i])
}
})
}
func applyDev(ds *apps.DaemonSet) {
ds.Spec.Template.Spec.Volumes = append(ds.Spec.Template.Spec.Volumes, v1.Volume{
Name: "dind",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/dind",
},
},
})
volMount := v1.VolumeMount{
Name: "dind",
MountPath: "/dind",
}
walkContainers(ds, func(c *v1.Container) {
c.VolumeMounts = append(c.VolumeMounts, volMount)
})
}
func applyCompat(ds *apps.DaemonSet) {
walkMounts(ds, func(v *v1.VolumeMount) {
if v.Name == "run" || v.Name == "k8s-pods-dir" {
v.MountPath += ":shared"
v.MountPropagation = nil
}
})
}
func applyTag(ds *apps.DaemonSet, tag string) {
walkContainers(ds, func(c *v1.Container) {
if c.Image == virtletImage {
c.Image += ":" + tag
}
})
}