Skip to content

Commit 24dbc57

Browse files
stephenfinMaysaMacedo
authored andcommitted
cluster-api: Create credentials for cloud
Signed-off-by: Stephen Finucane <[email protected]>
1 parent 84eacc1 commit 24dbc57

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

pkg/asset/machines/openstack/openstackmachines.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,9 @@ func generateMachineSpec(clusterID string, platform *openstack.Platform, mpool *
185185
spec := capo.OpenStackMachineSpec{
186186
CloudName: CloudName,
187187
Flavor: mpool.FlavorName,
188-
// TODO(stephenfin): Create credentials
189188
IdentityRef: &capo.OpenStackIdentityReference{
190189
Kind: "Secret",
191-
Name: "openstack-cloud-credentials",
190+
Name: clusterID + "-cloud-config",
192191
},
193192
// FIXME(stephenfin): We probably want a FIP for bootstrap?
194193
// TODO: This is an image name. Migrate to a filter with Name when API v1alpha8 is released.

pkg/asset/manifests/openstack/cluster.go

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ package openstack
22

33
import (
44
"fmt"
5+
"os"
56

7+
"github.com/gophercloud/utils/openstack/clientconfig"
68
corev1 "k8s.io/api/core/v1"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
capo "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
911
capi "sigs.k8s.io/cluster-api/api/v1beta1"
12+
"sigs.k8s.io/yaml"
1013

1114
"github.com/openshift/installer/pkg/asset"
1215
"github.com/openshift/installer/pkg/asset/installconfig"
1316
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
1417
)
1518

1619
const (
17-
CloudName = "openstack"
18-
CredentialsSecretName = "openstack-cloud-credentials"
20+
cloudName = "openstack"
1921
)
2022

23+
// GenerateClusterAssets generates the cluster manifests for the cluster-api.
2124
func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID *installconfig.ClusterID) (*capiutils.GenerateClusterAssetsOutput, error) {
2225
manifests := []*asset.RuntimeFile{}
2326
openstackInstallConfig := installConfig.Config.OpenStack
@@ -30,11 +33,10 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
3033
},
3134
},
3235
Spec: capo.OpenStackClusterSpec{
33-
CloudName: CloudName,
34-
// TODO(stephenfin): Create credentials
36+
CloudName: cloudName,
3537
IdentityRef: &capo.OpenStackIdentityReference{
3638
Kind: "Secret",
37-
Name: CredentialsSecretName,
39+
Name: clusterID.InfraID + "-cloud-config",
3840
},
3941
// We disable management of most networking resources since either
4042
// we (the installer) will create them, or the user will have
@@ -68,7 +70,24 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
6870
File: asset.File{Filename: "02_infra-cluster.yaml"},
6971
})
7072

71-
// TODO(stephenfin): Create credentials request/cloud secret
73+
cloudConfig, err := generateCloudConfig(installConfig)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
openStackIdentity := &corev1.Secret{
79+
ObjectMeta: metav1.ObjectMeta{
80+
Name: clusterID.InfraID + "-cloud-config",
81+
Namespace: capiutils.Namespace,
82+
},
83+
Data: cloudConfig,
84+
}
85+
openStackIdentity.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret"))
86+
87+
manifests = append(manifests, &asset.RuntimeFile{
88+
Object: openStackIdentity,
89+
File: asset.File{Filename: "02_openstack-cloud-config.yaml"},
90+
})
7291

7392
return &capiutils.GenerateClusterAssetsOutput{
7493
Manifests: manifests,
@@ -80,3 +99,45 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
8099
},
81100
}, nil
82101
}
102+
103+
func generateCloudConfig(installConfig *installconfig.InstallConfig) (map[string][]byte, error) {
104+
opts := new(clientconfig.ClientOpts)
105+
opts.Cloud = installConfig.Config.Platform.OpenStack.Cloud
106+
107+
cloud, err := clientconfig.GetCloudFromYAML(opts)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
// We need to replace the local cacert path with the one used by CAPO
113+
caCert := []byte{}
114+
if cloud.CACertFile != "" {
115+
caCert, err = os.ReadFile(cloud.CACertFile)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
// TODO: Verify this path. This is taken from CAPO directly
121+
// https://github.com/kubernetes-sigs/cluster-api-provider-openstack/blob/main/templates/env.rc
122+
cloud.CACertFile = "/etc/certs/cacert"
123+
}
124+
125+
clouds := make(map[string]map[string]*clientconfig.Cloud)
126+
clouds["clouds"] = map[string]*clientconfig.Cloud{
127+
cloudName: cloud,
128+
}
129+
130+
cloudsYAML, err := yaml.Marshal(clouds)
131+
if err != nil {
132+
return nil, err
133+
}
134+
135+
creds := map[string][]byte{
136+
"clouds.yaml": cloudsYAML,
137+
}
138+
if len(caCert) != 0 {
139+
creds["cacert"] = caCert
140+
}
141+
142+
return creds, nil
143+
}

0 commit comments

Comments
 (0)