-
Notifications
You must be signed in to change notification settings - Fork 0
/
observe_cloudprovider.go
149 lines (126 loc) · 5.53 KB
/
observe_cloudprovider.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
package cloudprovider
import (
"fmt"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets"
configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/library-go/pkg/operator/configobserver"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resourcesynccontroller"
configlistersv1 "github.com/openshift/client-go/config/listers/config/v1"
)
const (
cloudProviderConfFilePath = "/etc/kubernetes/static-pod-resources/configmaps/cloud-config/%s"
configNamespace = "openshift-config"
)
// InfrastructureLister lists infrastrucre information and allows resources to be synced
type InfrastructureLister interface {
InfrastructureLister() configlistersv1.InfrastructureLister
ResourceSyncer() resourcesynccontroller.ResourceSyncer
}
// NewCloudProviderObserver returns a new cloudprovider observer for syncing cloud provider specific
// information to controller-manager and api-server.
func NewCloudProviderObserver(targetNamespaceName string, cloudProviderNamePath, cloudProviderConfigPath []string) configobserver.ObserveConfigFunc {
cloudObserver := &cloudProviderObserver{
targetNamespaceName: targetNamespaceName,
cloudProviderNamePath: cloudProviderNamePath,
cloudProviderConfigPath: cloudProviderConfigPath,
}
return cloudObserver.ObserveCloudProviderNames
}
type cloudProviderObserver struct {
targetNamespaceName string
cloudProviderNamePath []string
cloudProviderConfigPath []string
}
// ObserveCloudProviderNames observes the cloud provider from the global cluster infrastructure resource.
func (c *cloudProviderObserver) ObserveCloudProviderNames(genericListers configobserver.Listers, recorder events.Recorder, existingConfig map[string]interface{}) (ret map[string]interface{}, _ []error) {
defer func() {
ret = configobserver.Pruned(ret, c.cloudProviderConfigPath, c.cloudProviderNamePath)
}()
listers := genericListers.(InfrastructureLister)
var errs []error
observedConfig := map[string]interface{}{}
infrastructure, err := listers.InfrastructureLister().Get("cluster")
if errors.IsNotFound(err) {
recorder.Warningf("ObserveCloudProviderNames", "Required infrastructures.%s/cluster not found", configv1.GroupName)
return observedConfig, errs
}
if err != nil {
return existingConfig, append(errs, err)
}
cloudProvider := getPlatformName(infrastructure.Status.Platform, recorder)
if len(cloudProvider) > 0 {
if err := unstructured.SetNestedStringSlice(observedConfig, []string{cloudProvider}, c.cloudProviderNamePath...); err != nil {
errs = append(errs, err)
}
}
sourceCloudConfigMap := infrastructure.Spec.CloudConfig.Name
sourceCloudConfigNamespace := configNamespace
sourceLocation := resourcesynccontroller.ResourceLocation{
Namespace: sourceCloudConfigNamespace,
Name: sourceCloudConfigMap,
}
// we set cloudprovider configmap values only for some cloud providers.
validCloudProviders := sets.NewString("azure", "gce", "openstack", "vsphere")
if !validCloudProviders.Has(cloudProvider) {
sourceCloudConfigMap = ""
}
if len(sourceCloudConfigMap) == 0 {
sourceLocation = resourcesynccontroller.ResourceLocation{}
}
if err := listers.ResourceSyncer().SyncConfigMap(
resourcesynccontroller.ResourceLocation{
Namespace: c.targetNamespaceName,
Name: "cloud-config",
},
sourceLocation); err != nil {
return existingConfig, append(errs, err)
}
if len(sourceCloudConfigMap) == 0 {
return observedConfig, errs
}
// usually key will be simply config but we should refer it just in case
staticCloudConfFile := fmt.Sprintf(cloudProviderConfFilePath, infrastructure.Spec.CloudConfig.Key)
if err := unstructured.SetNestedStringSlice(observedConfig, []string{staticCloudConfFile}, c.cloudProviderConfigPath...); err != nil {
recorder.Warningf("ObserveCloudProviderNames", "Failed setting cloud-config : %v", err)
return existingConfig, append(errs, err)
}
existingCloudConfig, _, err := unstructured.NestedStringSlice(existingConfig, c.cloudProviderConfigPath...)
if err != nil {
errs = append(errs, err)
// keep going on read error from existing config
}
if !equality.Semantic.DeepEqual(existingCloudConfig, []string{staticCloudConfFile}) {
recorder.Eventf("ObserveCloudProviderNamesChanges", "CloudProvider config file changed to %s", staticCloudConfFile)
}
return observedConfig, errs
}
func getPlatformName(platformType configv1.PlatformType, recorder events.Recorder) string {
cloudProvider := ""
switch platformType {
case "":
recorder.Warningf("ObserveCloudProvidersFailed", "Required status.platform field is not set in infrastructures.%s/cluster", configv1.GroupName)
case configv1.AWSPlatformType:
cloudProvider = "aws"
case configv1.AzurePlatformType:
cloudProvider = "azure"
case configv1.VSpherePlatformType:
cloudProvider = "vsphere"
case configv1.BareMetalPlatformType:
case configv1.GCPPlatformType:
cloudProvider = "gce"
case configv1.LibvirtPlatformType:
case configv1.OpenStackPlatformType:
cloudProvider = "openstack"
case configv1.NonePlatformType:
case configv1.OvirtPlatformType:
default:
// the new doc on the infrastructure fields requires that we treat an unrecognized thing the same bare metal.
// TODO find a way to indicate to the user that we didn't honor their choice
recorder.Warningf("ObserveCloudProvidersFailed", fmt.Sprintf("No recognized cloud provider platform found in infrastructures.%s/cluster.status.platform", configv1.GroupName))
}
return cloudProvider
}