forked from gardener-attic/gardener-extensions
/
actuator_reconcile.go
145 lines (120 loc) · 4 KB
/
actuator_reconcile.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
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 coreos
import (
"context"
"encoding/base64"
"fmt"
"strconv"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var coreOSCloudInitCommand = fmt.Sprintf("/usr/bin/coreos-cloudinit --from-file=")
func (c *actuator) reconcile(ctx context.Context, config *extensionsv1alpha1.OperatingSystemConfig) ([]byte, *string, []string, error) {
cloudConfig, units, err := c.cloudConfigFromOperatingSystemConfig(ctx, config)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not generate cloud config: %v", err)
}
var command *string
if path := config.Spec.ReloadConfigFilePath; path != nil {
cmd := coreOSCloudInitCommand + *path
command = &cmd
}
return []byte(cloudConfig), command, units, nil
}
func (c *actuator) cloudConfigFromOperatingSystemConfig(ctx context.Context, config *extensionsv1alpha1.OperatingSystemConfig) (string, []string, error) {
cloudConfig := &CloudConfig{
CoreOS: Config{
Update: Update{
RebootStrategy: "off",
},
Units: []Unit{
{
Name: "update-engine.service",
Mask: true,
},
{
Name: "locksmithd.service",
Mask: true,
},
},
},
}
// blacklist sctp kernel module
if config.Spec.Purpose == extensionsv1alpha1.OperatingSystemConfigPurposeReconcile {
cloudConfig.WriteFiles = []File{
{
Encoding: "b64",
Content: base64.StdEncoding.EncodeToString([]byte("install sctp /bin/true")),
Owner: "root",
Path: "/etc/modprobe.d/sctp.conf",
RawFilePermissions: "0644",
},
}
}
unitNames := make([]string, 0, len(config.Spec.Units))
for _, unit := range config.Spec.Units {
unitNames = append(unitNames, unit.Name)
u := Unit{Name: unit.Name}
if unit.Command != nil {
u.Command = *unit.Command
}
if unit.Enable != nil {
u.Enable = *unit.Enable
}
if unit.Content != nil {
u.Content = *unit.Content
}
for _, dropIn := range unit.DropIns {
u.DropIns = append(u.DropIns, UnitDropIn{
Name: dropIn.Name,
Content: dropIn.Content,
})
}
cloudConfig.CoreOS.Units = append(cloudConfig.CoreOS.Units, u)
}
for _, file := range config.Spec.Files {
f := File{
Path: file.Path,
}
permissions := extensionsv1alpha1.OperatingSystemConfigDefaultFilePermission
if p := file.Permissions; p != nil {
permissions = *p
}
f.RawFilePermissions = strconv.FormatInt(int64(permissions), 8)
if file.Content.Inline != nil {
f.Encoding = file.Content.Inline.Encoding
f.Content = file.Content.Inline.Data
}
if file.Content.SecretRef != nil {
var secret corev1.Secret
if err := c.client.Get(ctx, client.ObjectKey{Name: file.Content.SecretRef.Name, Namespace: config.Namespace}, &secret); err != nil {
return "", nil, err
}
data, ok := secret.Data[file.Content.SecretRef.DataKey]
if !ok {
return "", nil, fmt.Errorf("could not find key %q in data of secret %q", file.Content.SecretRef.DataKey, file.Content.SecretRef.Name)
}
f.Encoding = "b64"
f.Content = base64.StdEncoding.EncodeToString(data)
}
cloudConfig.WriteFiles = append(cloudConfig.WriteFiles, f)
}
data, err := cloudConfig.String()
if err != nil {
return "", nil, err
}
return data, unitNames, nil
}