Skip to content

Commit 452539b

Browse files
Fedosinsoltysh
authored andcommitted
UPSTREAM: 89885: allow to read openstack cloud provider config from a secret
This patch brings back the downstream changes that were introduced to allow reading openstack cloud provider config from a secret. They are available in release-4.4, but were reverted in master with openshift/origin#24719 This change includes: - Ability to read metadata values for kubelet. Since the service does not have access to the secret to read the configuration, but it needs data to download (e.g. hostname or flavor), we are trying to get it from the metadata server. - Deprecation of kubeConfig parameter. Now we read the file that was provided with --kubeconfig option. Origin-commit: f95edc26155a29769b3c5b80c03755a01a87b5fc UPSTREAM: 89885: legacy-cloud-provider/openstack: include / prefix in instance ID output When we want to read an instance ID from the metadata service, cloud provider doesn't include "/" prefix, which is required for successful parsing of provider the ID later. This commit adds the missing "/" prefix to the output. UPSTREAM: 89885: SQUASH: Fix Cinder provisioning crashing on nil cloud provider OpenStack cloud provider must not use nil when provisioning a Cinder volume. UPSTREAM: 89885: SQUASH: Report OpenStack cloud initialization errors openshift-rebase(v1.24):source=dbe70e455ee UPSTREAM: <carry>: Set informer for openstack Set informer for the openstack cloud provider to ensure it is properly initialized when reading config from a secret. Upstream 89885 was closed in favor of 96750. Co-authored-by: Hemant Kumar <[email protected]> openshift-rebase(v1.24):source=d7ecbd903e2 UPSTREAM: 89885: SQUASH: Retry fetching clouds.conf The OpenStack secret is not guaranteed to be present at the time kube-controller-manager is initialised. Co-authored-by: Martin André <[email protected]> Co-authored-by: Pierre Prinetti <[email protected]> openshift-rebase(v1.24):source=8bc9dd29ef0 UPSTREAM: 89885: Fix panic in openstack.InstanceExistsByProviderID() ... when provider is uninitialised. This is a fix to downstream-only code which was originally proposed upstream as kubernetes#89885 but did not merge. It is therefore not relevant upstream. Given that we will replace the openstack legacy cloud provider in 4.12 we will not re-propose kubernetes#89885 or this fix to it. Causes all openstack.Instances() methods which require more than the local metadata service to return NotImplemented instead of crashing if the provider is not initialised.
1 parent 834af76 commit 452539b

File tree

4 files changed

+296
-60
lines changed

4 files changed

+296
-60
lines changed

plugin/pkg/admission/storage/persistentvolume/label/admission.go

+24
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ import (
2424
"io"
2525
"sync"
2626

27+
genericadmissioninitializer "k8s.io/apiserver/pkg/admission/initializer"
28+
2729
v1 "k8s.io/api/core/v1"
2830
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2931
"k8s.io/apiserver/pkg/admission"
32+
"k8s.io/client-go/informers"
3033
cloudprovider "k8s.io/cloud-provider"
3134
cloudvolume "k8s.io/cloud-provider/volume"
3235
volumehelpers "k8s.io/cloud-provider/volume/helpers"
@@ -51,12 +54,14 @@ func Register(plugins *admission.Plugins) {
5154
}
5255

5356
var _ = admission.Interface(&persistentVolumeLabel{})
57+
var _ = genericadmissioninitializer.WantsExternalKubeInformerFactory(&persistentVolumeLabel{})
5458

5559
type persistentVolumeLabel struct {
5660
*admission.Handler
5761

5862
mutex sync.Mutex
5963
cloudConfig []byte
64+
sharedInformer informers.SharedInformerFactory
6065
awsPVLabeler cloudprovider.PVLabeler
6166
gcePVLabeler cloudprovider.PVLabeler
6267
azurePVLabeler cloudprovider.PVLabeler
@@ -86,6 +91,20 @@ func (l *persistentVolumeLabel) SetCloudConfig(cloudConfig []byte) {
8691
l.cloudConfig = cloudConfig
8792
}
8893

94+
func (l *persistentVolumeLabel) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
95+
secretInformer := f.Core().V1().Secrets()
96+
l.sharedInformer = f
97+
l.SetReadyFunc(secretInformer.Informer().HasSynced)
98+
}
99+
100+
// ValidateInitialization ensures lister is set.
101+
func (l *persistentVolumeLabel) ValidateInitialization() error {
102+
if l.sharedInformer == nil {
103+
return fmt.Errorf("missing shared informer")
104+
}
105+
return nil
106+
}
107+
89108
func nodeSelectorRequirementKeysExistInNodeSelectorTerms(reqs []api.NodeSelectorRequirement, terms []api.NodeSelectorTerm) bool {
90109
for _, req := range reqs {
91110
for _, term := range terms {
@@ -396,6 +415,11 @@ func (l *persistentVolumeLabel) getOpenStackPVLabeler() (cloudprovider.PVLabeler
396415
return nil, err
397416
}
398417

418+
cloudProviderWithInformer, ok := cloudProvider.(cloudprovider.InformerUser)
419+
if ok {
420+
cloudProviderWithInformer.SetInformers(l.sharedInformer)
421+
}
422+
399423
openStackPVLabeler, ok := cloudProvider.(cloudprovider.PVLabeler)
400424
if !ok {
401425
return nil, errors.New("OpenStack cloud provider does not implement PV labeling")

staging/src/k8s.io/legacy-cloud-providers/openstack/metadata.go

+65-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ const (
5454

5555
// configDriveID is used as an identifier on the metadata search order configuration.
5656
configDriveID = "configDrive"
57+
58+
// We have to use AWS compatible metadata for the next urls, because OpenStack doesn't
59+
// provide this information.
60+
61+
// instanceTypeURL contains url to get the instance type from metadata server.
62+
instanceTypeURL = "http://169.254.169.254/2009-04-04/meta-data/instance-type"
63+
64+
// localAddressURL contains url to get the instance local ip address from metadata server.
65+
localAddressURL = "http://169.254.169.254/2009-04-04/meta-data/local-ipv4"
66+
67+
// publicAddressURL contains url to get the instance public ip address from metadata server.
68+
publicAddressURL = "http://169.254.169.254/2009-04-04/meta-data/public-ipv4"
5769
)
5870

5971
// ErrBadMetadata is used to indicate a problem parsing data from metadata server
@@ -160,13 +172,64 @@ func getMetadataFromMetadataService(metadataVersion string) (*Metadata, error) {
160172
defer resp.Body.Close()
161173

162174
if resp.StatusCode != http.StatusOK {
163-
err = fmt.Errorf("unexpected status code when reading metadata from %s: %s", metadataURL, resp.Status)
164-
return nil, err
175+
return nil, fmt.Errorf("unexpected status code when reading metadata from %s: %s", metadataURL, resp.Status)
165176
}
166177

167178
return parseMetadata(resp.Body)
168179
}
169180

181+
func getIntanceType() (string, error) {
182+
klog.V(4).Infof("Attempting to fetch instance type from %s", instanceTypeURL)
183+
resp, err := http.Get(instanceTypeURL)
184+
if err != nil {
185+
return "", fmt.Errorf("error fetching %s: %v", instanceTypeURL, err)
186+
}
187+
defer resp.Body.Close()
188+
189+
if resp.StatusCode != http.StatusOK {
190+
return "", fmt.Errorf("unexpected status code when reading instance type from %s: %s", instanceTypeURL, resp.Status)
191+
}
192+
body, err := ioutil.ReadAll(resp.Body)
193+
if err != nil {
194+
return "", fmt.Errorf("cannot read the response body %s: %v", instanceTypeURL, err)
195+
}
196+
197+
return string(body), nil
198+
}
199+
200+
func getNodeAddress(url string) (string, error) {
201+
klog.V(4).Infof("Attempting to fetch instance address from %s", url)
202+
resp, err := http.Get(url)
203+
if err != nil {
204+
return "", fmt.Errorf("error fetching %s: %v", url, err)
205+
}
206+
defer resp.Body.Close()
207+
208+
if resp.StatusCode != http.StatusOK {
209+
return "", fmt.Errorf("unexpected status code when reading instance address from %s: %s", url, resp.Status)
210+
}
211+
body, err := ioutil.ReadAll(resp.Body)
212+
if err != nil {
213+
return "", fmt.Errorf("cannot read the response body %s: %v", url, err)
214+
}
215+
216+
return string(body), nil
217+
}
218+
219+
func getNodeAddresses() (string, string, error) {
220+
localAddess, err := getNodeAddress(localAddressURL)
221+
if err != nil {
222+
return "", "", err
223+
}
224+
225+
publicAddress, err := getNodeAddress(publicAddressURL)
226+
if err != nil {
227+
return "", "", err
228+
}
229+
230+
return localAddess, publicAddress, nil
231+
}
232+
170233
// Metadata is fixed for the current host, so cache the value process-wide
171234
var metadataCache *Metadata
172235

0 commit comments

Comments
 (0)