-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathcluster_server.go
226 lines (200 loc) · 8.06 KB
/
cluster_server.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package server
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
yaml "github.com/ghodss/yaml"
mcfginformers "github.com/openshift/client-go/machineconfiguration/informers/externalversions"
"github.com/openshift/machine-config-operator/internal/clients"
ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
corelisterv1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
clientcmdv1 "k8s.io/client-go/tools/clientcmd/api/v1"
"k8s.io/klog/v2"
v1 "github.com/openshift/client-go/machineconfiguration/listers/machineconfiguration/v1"
)
const (
//nolint:gosec
bootstrapTokenDir = "/etc/mcs/bootstrap-token"
caBundleFilePath = "/etc/kubernetes/kubelet-ca.crt"
cloudProviderCAPath = "/etc/kubernetes/static-pod-resources/configmaps/cloud-config/ca-bundle.pem"
additionalCAPath = "/etc/pki/ca-trust/source/anchors/openshift-config-user-ca-bundle.crt"
)
// ensure clusterServer implements the
// Server interface.
var _ = Server(&clusterServer{})
type clusterServer struct {
machineConfigPoolLister v1.MachineConfigPoolLister
machineConfigLister v1.MachineConfigLister
controllerConfigLister v1.ControllerConfigLister
configMapLister corelisterv1.ConfigMapLister
kubeconfigFunc kubeconfigFunc
apiserverURL string
}
const minResyncPeriod = 20 * time.Minute
func resyncPeriod() func() time.Duration {
return func() time.Duration {
// Disable gosec here to avoid throwing
// G404: Use of weak random number generator (math/rand instead of crypto/rand)
// #nosec
factor := rand.Float64() + 1
return time.Duration(float64(minResyncPeriod.Nanoseconds()) * factor)
}
}
// NewClusterServer is used to initialize the machine config
// server that will be used to fetch the requested MachineConfigPool
// objects from within the cluster.
// It accepts a kubeConfig, which is not required when it's
// run from within a cluster(useful in testing).
// It accepts the apiserverURL which is the location of the KubeAPIServer.
func NewClusterServer(kubeConfig, apiserverURL string) (Server, error) {
clientsBuilder, err := clients.NewBuilder(kubeConfig)
if err != nil {
return nil, fmt.Errorf("failed to create Kubernetes rest client: %w", err)
}
machineConfigClient := clientsBuilder.MachineConfigClientOrDie("machine-config-shared-informer")
kubeClient := clientsBuilder.KubeClientOrDie("kube-client-shared-informer")
sharedInformerFactory := mcfginformers.NewSharedInformerFactory(machineConfigClient, resyncPeriod()())
kubeNamespacedSharedInformer := informers.NewFilteredSharedInformerFactory(kubeClient, resyncPeriod()(), "openshift-machine-config-operator", nil)
mcpInformer, mcInformer, ccInformer, cmInformer :=
sharedInformerFactory.Machineconfiguration().V1().MachineConfigPools(),
sharedInformerFactory.Machineconfiguration().V1().MachineConfigs(),
sharedInformerFactory.Machineconfiguration().V1().ControllerConfigs(),
kubeNamespacedSharedInformer.Core().V1().ConfigMaps()
mcpLister, mcLister, ccLister, cmLister := mcpInformer.Lister(), mcInformer.Lister(), ccInformer.Lister(), cmInformer.Lister()
mcpListerHasSynced, mcListerHasSynced, ccListerHasSynced, cmListerHasSynced :=
mcpInformer.Informer().HasSynced,
mcInformer.Informer().HasSynced,
ccInformer.Informer().HasSynced,
cmInformer.Informer().HasSynced
var informerStopCh chan struct{}
go sharedInformerFactory.Start(informerStopCh)
go kubeNamespacedSharedInformer.Start(informerStopCh)
if !cache.WaitForCacheSync(informerStopCh, mcpListerHasSynced, mcListerHasSynced, ccListerHasSynced, cmListerHasSynced) {
return nil, errors.New("failed to wait for cache sync")
}
return &clusterServer{
machineConfigPoolLister: mcpLister,
machineConfigLister: mcLister,
controllerConfigLister: ccLister,
configMapLister: cmLister,
kubeconfigFunc: func() ([]byte, []byte, error) { return kubeconfigFromSecret(bootstrapTokenDir, apiserverURL, nil) },
apiserverURL: apiserverURL,
}, nil
}
// GetConfig fetches the machine config(type - Ignition) from the cluster,
// based on the pool request.
func (cs *clusterServer) GetConfig(cr poolRequest) (*runtime.RawExtension, error) {
mp, err := cs.machineConfigPoolLister.Get(cr.machineConfigPool)
if err != nil {
return nil, fmt.Errorf("could not fetch pool. err: %w", err)
}
// For new nodes, we roll out the latest if at least one node has successfully updated.
// This avoids deadlocks in situations where the old configuration broke somehow
// (e.g. pull secret expired)
// and also avoids provisioning a new node, only to update it not long thereafter.
var currConf string
if mp.Status.UpdatedMachineCount > 0 {
currConf = mp.Spec.Configuration.Name
} else {
currConf = mp.Status.Configuration.Name
}
mc, err := cs.machineConfigLister.Get(currConf)
if err != nil {
return nil, fmt.Errorf("could not fetch config %s, err: %w", currConf, err)
}
ignConf, err := ctrlcommon.ParseAndConvertConfig(mc.Spec.Config.Raw)
if err != nil {
return nil, fmt.Errorf("parsing Ignition config failed with error: %w", err)
}
// Update the kubelet cert bundle to the latest in the controllerconfig, in case the pool was paused
// This also means that the /etc/mcs-machine-config-content.json written to disk will be a lie
// TODO(jerzhang): improve this process once we have a proper cert management model
cc, err := cs.controllerConfigLister.Get(ctrlcommon.ControllerConfigName)
if err != nil {
return nil, fmt.Errorf("could not get controllerconfig: %w", err)
}
// we cannot mock this in a test env
if cs.configMapLister != nil {
cm, err := cs.configMapLister.ConfigMaps(ctrlcommon.MCONamespace).Get("kubeconfig-data")
if err != nil {
klog.Errorf("Could not get kubeconfig data: %v", err)
} else {
if caBundle, ok := cm.BinaryData["ca-bundle.crt"]; ok {
cs.kubeconfigFunc = func() ([]byte, []byte, error) {
return kubeconfigFromSecret(bootstrapTokenDir, cs.apiserverURL, caBundle)
}
}
}
}
// strip the kargs out if we're going back to a version that doesn't support it
if err := MigrateKernelArgsIfNecessary(&ignConf, mc, cr.version); err != nil {
return nil, fmt.Errorf("failed to migrate kernel args %w", err)
}
addDataAndMaybeAppendToIgnition(caBundleFilePath, cc.Spec.KubeAPIServerServingCAData, &ignConf)
addDataAndMaybeAppendToIgnition(cloudProviderCAPath, cc.Spec.CloudProviderCAData, &ignConf)
appenders := getAppenders(currConf, cr.version, cs.kubeconfigFunc, []string{}, "")
for _, a := range appenders {
if err := a(&ignConf, mc); err != nil {
return nil, err
}
}
rawConf, err := json.Marshal(ignConf)
if err != nil {
return nil, err
}
return &runtime.RawExtension{Raw: rawConf}, nil
}
// kubeconfigFromSecret creates a kubeconfig with the certificate
// and token files in secretDir. If caData is provided, it will instead
// use that to populate the kubeconfig
func kubeconfigFromSecret(secretDir, apiserverURL string, caData []byte) ([]byte, []byte, error) {
caFile := filepath.Join(secretDir, corev1.ServiceAccountRootCAKey)
tokenFile := filepath.Join(secretDir, corev1.ServiceAccountTokenKey)
if caData == nil {
var err error
caData, err = os.ReadFile(caFile)
if err != nil {
return nil, nil, fmt.Errorf("failed to read %s: %w", caFile, err)
}
}
token, err := os.ReadFile(tokenFile)
if err != nil {
return nil, nil, fmt.Errorf("failed to read %s: %w", tokenFile, err)
}
kubeconfig := clientcmdv1.Config{
Clusters: []clientcmdv1.NamedCluster{{
Name: "local",
Cluster: clientcmdv1.Cluster{
Server: apiserverURL,
CertificateAuthorityData: caData,
}},
},
AuthInfos: []clientcmdv1.NamedAuthInfo{{
Name: "kubelet",
AuthInfo: clientcmdv1.AuthInfo{
Token: string(token),
},
}},
Contexts: []clientcmdv1.NamedContext{{
Name: "kubelet",
Context: clientcmdv1.Context{
Cluster: "local",
AuthInfo: "kubelet",
},
}},
CurrentContext: "kubelet",
}
kcData, err := yaml.Marshal(kubeconfig)
if err != nil {
return nil, nil, err
}
return kcData, caData, nil
}