-
Notifications
You must be signed in to change notification settings - Fork 463
/
landscaper-gardenlet.go
148 lines (121 loc) · 5.61 KB
/
landscaper-gardenlet.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
// Copyright (c) 2021 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 app
import (
"context"
"errors"
"fmt"
"os"
"github.com/gardener/gardener/landscaper/pkg/gardenlet/apis/imports"
importsv1alpha1 "github.com/gardener/gardener/landscaper/pkg/gardenlet/apis/imports/v1alpha1"
importvalidation "github.com/gardener/gardener/landscaper/pkg/gardenlet/apis/imports/validation"
gardenlet "github.com/gardener/gardener/landscaper/pkg/gardenlet/controller"
gardenletconfig "github.com/gardener/gardener/pkg/gardenlet/apis/config"
gardenletconfigv1alpha1 "github.com/gardener/gardener/pkg/gardenlet/apis/config/v1alpha1"
landscaperconstants "github.com/gardener/landscaper/apis/deployer/container"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/component-base/version/verflag"
)
// in integration tests, we do not assume that the Gardenlet can be rolled out successfully,
// nor that the Seed can be registered.
// this is to provide an easy means of testing the landscaper component without requiring
// a fully functional Gardener control plane.
var isIntegrationTest bool
// NewCommandStartLandscaperGardenelet creates a *cobra.Command object with default parameters
func NewCommandStartLandscaperGardenelet(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "landscaper-gardenlet",
Short: "Launch the landscaper component for the Gardenlet.",
Long: "Gardener landscaper component for the Gardenlet. Sets up the Garden cluster and deploys the Gardenlet with TLS bootstrapping to automatically register the configured Seed cluster.",
RunE: func(cmd *cobra.Command, args []string) error {
verflag.PrintAndExitIfRequested()
if len(args) != 0 && !isIntegrationTest {
return errors.New("arguments are not supported. Please only set the path to the configuration file via environment variable \"IMPORTS_PATH\"")
}
return run(ctx)
},
}
cmd.Flags().BoolVar(
&isIntegrationTest,
"integration-test",
false,
"component executed during integration test. This disables certain checks that require Gardener API groups in the Garden cluster.")
// add version flag
flags := cmd.Flags()
verflag.AddFlags(flags)
return cmd
}
func run(ctx context.Context) error {
landscaperOperation, importPath, componentDescriptorPath, err := getLandscaperEnvironmentVariables()
if err != nil {
return err
}
imports, err := loadImportsFromFile(importPath)
if err != nil {
return fmt.Errorf("unable to load landscaper imports: %w", err)
}
if errs := importvalidation.ValidateLandscaperImports(imports); len(errs) > 0 {
return fmt.Errorf("errors validating the landscaper imports: %+v", errs)
}
landscaper, err := gardenlet.NewGardenletLandscaper(imports, landscaperOperation, componentDescriptorPath, isIntegrationTest)
if err != nil {
return err
}
return landscaper.Run(ctx)
}
func getLandscaperEnvironmentVariables() (string, string, string, error) {
var operation string
if operation = os.Getenv(landscaperconstants.OperationName); operation != string(landscaperconstants.OperationReconcile) && operation != string(landscaperconstants.OperationDelete) {
return "", "", "", fmt.Errorf("environment variable %q has to be set and must either be %q or %q", landscaperconstants.OperationName, landscaperconstants.OperationReconcile, landscaperconstants.OperationDelete)
}
var importPath, componentDescriptorPath string
if importPath = os.Getenv(landscaperconstants.ImportsPathName); importPath == "" {
return "", "", "", fmt.Errorf("environment variable %q has to be set and point to the file containing the configuration for the Gardenlet landscaper", landscaperconstants.ImportsPathName)
}
if componentDescriptorPath = os.Getenv(landscaperconstants.ComponentDescriptorPathName); componentDescriptorPath == "" {
return "", "", "", fmt.Errorf("environment variable %q has to be set and point to the file containing the component descriptor for the Gardenlet landscaper", landscaperconstants.ComponentDescriptorPathName)
}
return operation, importPath, componentDescriptorPath, nil
}
// loadImportsFromFile loads the content of file and decodes it as a
// imports object.
func loadImportsFromFile(file string) (*imports.Imports, error) {
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
landscaperImport := &imports.Imports{}
scheme := runtime.NewScheme()
codecs := serializer.NewCodecFactory(scheme)
if err := imports.AddToScheme(scheme); err != nil {
return nil, err
}
if err := importsv1alpha1.AddToScheme(scheme); err != nil {
return nil, err
}
// Adding internal and v1alpha1 Gardenlet types
// Required to parse the Gardenlet component config
if err := gardenletconfigv1alpha1.AddToScheme(scheme); err != nil {
return nil, err
}
if err := gardenletconfig.AddToScheme(scheme); err != nil {
return nil, err
}
if _, _, err := codecs.UniversalDecoder().Decode(data, nil, landscaperImport); err != nil {
return nil, err
}
return landscaperImport, nil
}