forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actuator.go
124 lines (104 loc) · 5.01 KB
/
actuator.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
// Copyright 2023 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 operatingsystemconfig
import (
"context"
_ "embed"
"fmt"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/gardener/gardener/extensions/pkg/controller/operatingsystemconfig"
oscommonactuator "github.com/gardener/gardener/extensions/pkg/controller/operatingsystemconfig/oscommon/actuator"
ostemplate "github.com/gardener/gardener/extensions/pkg/controller/operatingsystemconfig/oscommon/template"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
)
type actuator struct {
client client.Client
useGardenerNodeAgent bool
}
// NewActuator creates a new Actuator that updates the status of the handled OperatingSystemConfig resources.
func NewActuator(mgr manager.Manager, useGardenerNodeAgent bool) operatingsystemconfig.Actuator {
return &actuator{
client: mgr.GetClient(),
useGardenerNodeAgent: useGardenerNodeAgent,
}
}
var (
// TODO(rfranzke): Remove this after the UseGardenerNodeAgent feature gate has been promoted to GA.
//go:embed templates/cloud-init.template
cloudInitTemplateString string
cloudInitGenerator *ostemplate.CloudInitGenerator
)
func init() {
cloudInitTemplate, err := ostemplate.NewTemplate("cloud-init").Parse(cloudInitTemplateString)
runtime.Must(err)
cloudInitGenerator = ostemplate.NewCloudInitGenerator(
cloudInitTemplate,
ostemplate.DefaultUnitsPath,
"/usr/bin/env bash %s",
func(*extensionsv1alpha1.OperatingSystemConfig) (map[string]interface{}, error) {
return nil, nil
},
)
}
func (a *actuator) Reconcile(ctx context.Context, log logr.Logger, osc *extensionsv1alpha1.OperatingSystemConfig) ([]byte, *string, []string, []string, []extensionsv1alpha1.Unit, []extensionsv1alpha1.File, error) {
cloudConfig, cmd, err := oscommonactuator.CloudConfigFromOperatingSystemConfig(ctx, log, a.client, osc, cloudInitGenerator)
if err != nil {
return nil, nil, nil, nil, nil, nil, fmt.Errorf("could not generate cloud config: %w", err)
}
switch purpose := osc.Spec.Purpose; purpose {
case extensionsv1alpha1.OperatingSystemConfigPurposeProvision:
if !a.useGardenerNodeAgent {
return cloudConfig, cmd, oscommonactuator.OperatingSystemConfigUnitNames(osc), oscommonactuator.OperatingSystemConfigFilePaths(osc), nil, nil, nil
}
userData, err := a.handleProvisionOSC(ctx, osc)
return []byte(userData), nil, nil, nil, nil, nil, err
case extensionsv1alpha1.OperatingSystemConfigPurposeReconcile:
extensionUnits, extensionFiles, err := a.handleReconcileOSC(osc)
return cloudConfig, cmd, oscommonactuator.OperatingSystemConfigUnitNames(osc), oscommonactuator.OperatingSystemConfigFilePaths(osc), extensionUnits, extensionFiles, err
default:
return nil, nil, nil, nil, nil, nil, fmt.Errorf("unknown purpose: %s", purpose)
}
}
func (a *actuator) Delete(_ context.Context, _ logr.Logger, _ *extensionsv1alpha1.OperatingSystemConfig) error {
return nil
}
func (a *actuator) Migrate(ctx context.Context, log logr.Logger, osc *extensionsv1alpha1.OperatingSystemConfig) error {
return a.Delete(ctx, log, osc)
}
func (a *actuator) ForceDelete(ctx context.Context, log logr.Logger, osc *extensionsv1alpha1.OperatingSystemConfig) error {
return a.Delete(ctx, log, osc)
}
func (a *actuator) Restore(ctx context.Context, log logr.Logger, osc *extensionsv1alpha1.OperatingSystemConfig) ([]byte, *string, []string, []string, []extensionsv1alpha1.Unit, []extensionsv1alpha1.File, error) {
return a.Reconcile(ctx, log, osc)
}
func (a *actuator) handleProvisionOSC(ctx context.Context, osc *extensionsv1alpha1.OperatingSystemConfig) (string, error) {
writeFilesToDiskScript, err := operatingsystemconfig.FilesToDiskScript(ctx, a.client, osc.Namespace, osc.Spec.Files)
if err != nil {
return "", err
}
writeUnitsToDiskScript := operatingsystemconfig.UnitsToDiskScript(osc.Spec.Units)
return `#!/bin/bash
` + writeFilesToDiskScript + `
` + writeUnitsToDiskScript + `
systemctl daemon-reload
systemctl enable gardener-node-init && systemctl start gardener-node-init
`, nil
}
func (a *actuator) handleReconcileOSC(_ *extensionsv1alpha1.OperatingSystemConfig) ([]extensionsv1alpha1.Unit, []extensionsv1alpha1.File, error) {
// provider-local does not add any additional units or additional files
return nil, nil, nil
}