Skip to content

Commit a437d1c

Browse files
patrickdillonvincepri
authored andcommitted
add runtime object asset interface
1 parent a06391b commit a437d1c

File tree

5 files changed

+96
-59
lines changed

5 files changed

+96
-59
lines changed

pkg/asset/asset.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/pkg/errors"
1010
"github.com/sirupsen/logrus"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
1112
)
1213

1314
const (
@@ -42,6 +43,16 @@ type WritableAsset interface {
4243
Load(FileFetcher) (found bool, err error)
4344
}
4445

46+
// WritableRuntimeAsset is a WriteableAsset that has files that can be written to disk,
47+
// in addition to a manifest file that contains the runtime object.
48+
type WritableRuntimeAsset interface {
49+
WritableAsset
50+
51+
// RuntimeFiles returns the manifest files along with their
52+
// instantiated runtime object.
53+
RuntimeFiles() []*RuntimeFile
54+
}
55+
4556
// File is a file for an Asset.
4657
type File struct {
4758
// Filename is the name of the file.
@@ -50,6 +61,13 @@ type File struct {
5061
Data []byte
5162
}
5263

64+
// RuntimeFile is a file that contains a manifest file and a runtime object.
65+
type RuntimeFile struct {
66+
File
67+
68+
Object client.Object `json:"-"`
69+
}
70+
5371
// PersistToFile writes all of the files of the specified asset into the specified
5472
// directory.
5573
func PersistToFile(asset WritableAsset, directory string) error {
@@ -107,3 +125,8 @@ func isDirEmpty(name string) (bool, error) {
107125
func SortFiles(files []*File) {
108126
sort.Slice(files, func(i, j int) bool { return files[i].Filename < files[j].Filename })
109127
}
128+
129+
// SortManifestFiles sorts the specified files by file name.
130+
func SortManifestFiles(files []*RuntimeFile) {
131+
sort.Slice(files, func(i, j int) bool { return files[i].Filename < files[j].Filename })
132+
}

pkg/asset/manifests/aws/cluster.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
"k8s.io/utils/ptr"
1212
capa "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
1313

14+
"github.com/openshift/installer/pkg/asset"
1415
"github.com/openshift/installer/pkg/asset/installconfig"
1516
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
1617
)
1718

1819
// GenerateClusterAssets generates the manifests for the cluster-api.
1920
func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID *installconfig.ClusterID) (*capiutils.GenerateClusterAssetsOutput, error) {
20-
manifests := capiutils.Manifests{}
21+
manifests := []*asset.RuntimeFile{}
2122
mainCIDR := capiutils.CIDRFromInstallConfig(installConfig)
2223

2324
zones, err := installConfig.AWS.AvailabilityZones(context.TODO())
@@ -200,7 +201,10 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
200201
}
201202
}
202203

203-
manifests = append(manifests, &capiutils.Manifest{Object: awsCluster, Filename: "02_infra-cluster.yaml"})
204+
manifests = append(manifests, &asset.RuntimeFile{
205+
Object: awsCluster,
206+
File: asset.File{Filename: "02_infra-cluster.yaml"},
207+
})
204208

205209
id := &capa.AWSClusterControllerIdentity{
206210
ObjectMeta: metav1.ObjectMeta{
@@ -213,7 +217,10 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
213217
},
214218
},
215219
}
216-
manifests = append(manifests, &capiutils.Manifest{Object: id, Filename: "01_aws-cluster-controller-identity-default.yaml"})
220+
manifests = append(manifests, &asset.RuntimeFile{
221+
Object: id,
222+
File: asset.File{Filename: "01_aws-cluster-controller-identity-default.yaml"},
223+
})
217224

218225
return &capiutils.GenerateClusterAssetsOutput{
219226
Manifests: manifests,

pkg/asset/manifests/azure/cluster.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
77
capz "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
88

9+
"github.com/openshift/installer/pkg/asset"
910
"github.com/openshift/installer/pkg/asset/installconfig"
1011
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
1112
"github.com/openshift/installer/pkg/asset/manifests/capiutils/cidr"
1213
)
1314

1415
// GenerateClusterAssets generates the manifests for the cluster-api.
1516
func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID *installconfig.ClusterID) (*capiutils.GenerateClusterAssetsOutput, error) {
16-
manifests := capiutils.Manifests{}
17+
manifests := []*asset.RuntimeFile{}
1718
mainCIDR := capiutils.CIDRFromInstallConfig(installConfig)
1819

1920
session, err := installConfig.Azure.Session()
@@ -28,7 +29,10 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
2829

2930
// CAPZ expects the capz-system to be created.
3031
azureNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "capz-system"}}
31-
manifests = append(manifests, &capiutils.Manifest{Object: azureNamespace, Filename: "00_azure-namespace.yaml"})
32+
manifests = append(manifests, &asset.RuntimeFile{
33+
Object: azureNamespace,
34+
File: asset.File{Filename: "00_azure-namespace.yaml"},
35+
})
3236

3337
azureCluster := &capz.AzureCluster{
3438
ObjectMeta: metav1.ObjectMeta{
@@ -79,7 +83,10 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
7983
},
8084
},
8185
}
82-
manifests = append(manifests, &capiutils.Manifest{Object: azureCluster, Filename: "02_azure-cluster.yaml"})
86+
manifests = append(manifests, &asset.RuntimeFile{
87+
Object: azureCluster,
88+
File: asset.File{Filename: "02_azure-cluster.yaml"},
89+
})
8390

8491
azureClientSecret := &corev1.Secret{
8592
ObjectMeta: metav1.ObjectMeta{
@@ -90,7 +97,10 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
9097
"clientSecret": session.Credentials.ClientSecret,
9198
},
9299
}
93-
manifests = append(manifests, &capiutils.Manifest{Object: azureClientSecret, Filename: "01_azure-client-secret.yaml"})
100+
manifests = append(manifests, &asset.RuntimeFile{
101+
Object: azureClientSecret,
102+
File: asset.File{Filename: "01_azure-client-secret.yaml"},
103+
})
94104

95105
id := &capz.AzureClusterIdentity{
96106
ObjectMeta: metav1.ObjectMeta{
@@ -108,7 +118,10 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
108118
TenantID: session.Credentials.TenantID,
109119
},
110120
}
111-
manifests = append(manifests, &capiutils.Manifest{Object: id, Filename: "01_aws-cluster-controller-identity-default.yaml"})
121+
manifests = append(manifests, &asset.RuntimeFile{
122+
Object: id,
123+
File: asset.File{Filename: "01_aws-cluster-controller-identity-default.yaml"},
124+
})
112125

113126
return &capiutils.GenerateClusterAssetsOutput{
114127
Manifests: manifests,

pkg/asset/manifests/capiutils/manifest.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,12 @@ package capiutils
22

33
import (
44
corev1 "k8s.io/api/core/v1"
5-
"sigs.k8s.io/controller-runtime/pkg/client"
6-
)
7-
8-
// Manifests is a list of Manifests sortable by filename.
9-
type Manifests []*Manifest
10-
11-
func (m Manifests) Len() int {
12-
return len(m)
13-
}
14-
15-
func (m Manifests) Less(i, j int) bool {
16-
return m[i].Filename < m[j].Filename
17-
}
18-
19-
func (m Manifests) Swap(i, j int) {
20-
m[i], m[j] = m[j], m[i]
21-
}
225

23-
// Manifest is a wrapper for a CAPI manifest.
24-
type Manifest struct {
25-
Object client.Object
26-
Filename string
27-
}
6+
"github.com/openshift/installer/pkg/asset"
7+
)
288

299
// GenerateClusterAssetsOutput is the output of GenerateClusterAssets.
3010
type GenerateClusterAssetsOutput struct {
31-
Manifests Manifests
11+
Manifests []*asset.RuntimeFile
3212
InfrastructureRef *corev1.ObjectReference
3313
}

pkg/asset/manifests/clusterapi/cluster.go

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package clusterapi
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
6-
"sort"
77

88
"github.com/pkg/errors"
99
corev1 "k8s.io/api/core/v1"
@@ -30,13 +30,12 @@ const (
3030
manifestDir = "cluster-api"
3131
)
3232

33-
var _ asset.WritableAsset = (*Cluster)(nil)
33+
var _ asset.WritableRuntimeAsset = (*Cluster)(nil)
3434

3535
// Cluster generates manifests for target cluster
3636
// creation using CAPI.
3737
type Cluster struct {
38-
FileList []*asset.File
39-
Manifests capiutils.Manifests `json:"-"`
38+
FileList []*asset.RuntimeFile
4039
}
4140

4241
// Name returns a human friendly name for the operator.
@@ -74,15 +73,18 @@ func (c *Cluster) Generate(dependencies asset.Parents) error {
7473
return nil
7574
}
7675

77-
c.FileList = []*asset.File{}
78-
c.Manifests = capiutils.Manifests{}
76+
if err := os.MkdirAll(filepath.Dir(manifestDir), 0755); err != nil {
77+
return err
78+
}
79+
80+
c.FileList = []*asset.RuntimeFile{}
7981

8082
namespace := &corev1.Namespace{
8183
ObjectMeta: metav1.ObjectMeta{
8284
Name: capiutils.Namespace,
8385
},
8486
}
85-
c.Manifests = append(c.Manifests, &capiutils.Manifest{Object: namespace, Filename: "00_capi-namespace.yaml"})
87+
c.FileList = append(c.FileList, &asset.RuntimeFile{Object: namespace, File: asset.File{Filename: "00_capi-namespace.yaml"}})
8688

8789
cluster := &clusterv1.Cluster{
8890
ObjectMeta: metav1.ObjectMeta{
@@ -91,7 +93,7 @@ func (c *Cluster) Generate(dependencies asset.Parents) error {
9193
},
9294
Spec: clusterv1.ClusterSpec{},
9395
}
94-
c.Manifests = append(c.Manifests, &capiutils.Manifest{Object: cluster, Filename: "01_capi-cluster.yaml"})
96+
c.FileList = append(c.FileList, &asset.RuntimeFile{Object: cluster, File: asset.File{Filename: "01_capi-cluster.yaml"}})
9597

9698
// Gather the ignition files, and store them in a secret.
9799
{
@@ -100,9 +102,9 @@ func (c *Cluster) Generate(dependencies asset.Parents) error {
100102
if err != nil {
101103
return errors.Wrap(err, "unable to inject installation info")
102104
}
103-
c.Manifests = append(c.Manifests,
104-
&capiutils.Manifest{
105-
Filename: "01_ignition-secret-master.yaml",
105+
c.FileList = append(c.FileList,
106+
&asset.RuntimeFile{
107+
File: asset.File{Filename: "01_ignition-secret-master.yaml"},
106108
Object: &corev1.Secret{
107109
ObjectMeta: metav1.ObjectMeta{
108110
Name: fmt.Sprintf("%s-%s", clusterID.InfraID, "master"),
@@ -117,8 +119,8 @@ func (c *Cluster) Generate(dependencies asset.Parents) error {
117119
},
118120
},
119121
},
120-
&capiutils.Manifest{
121-
Filename: "01_ignition-secret-bootstrap.yaml",
122+
&asset.RuntimeFile{
123+
File: asset.File{Filename: "01_ignition-secret-bootstrap.yaml"},
122124
Object: &corev1.Secret{
123125
ObjectMeta: metav1.ObjectMeta{
124126
Name: fmt.Sprintf("%s-%s", clusterID.InfraID, "bootstrap"),
@@ -164,26 +166,39 @@ func (c *Cluster) Generate(dependencies asset.Parents) error {
164166
return fmt.Errorf("failed to generate manifests: cluster.Spec.InfrastructureRef was never set")
165167
}
166168

169+
// Append the infrastructure manifests.
170+
c.FileList = append(c.FileList, out.Manifests...)
171+
167172
// Create the infrastructure manifests.
168-
sort.Sort(c.Manifests)
169-
for _, m := range c.Manifests {
173+
for _, m := range c.FileList {
170174
objData, err := yaml.Marshal(m.Object)
171175
if err != nil {
172176
return errors.Wrapf(err, "failed to marshal infrastructure manifest %s", m.Filename)
173177
}
178+
m.Data = objData
174179

175-
c.FileList = append(c.FileList, &asset.File{
176-
Filename: filepath.Join(manifestDir, m.Filename),
177-
Data: objData,
178-
})
180+
// If the filename is already a path, do not append the manifestDir.
181+
if filepath.Dir(m.Filename) == manifestDir {
182+
continue
183+
}
184+
m.Filename = filepath.Join(manifestDir, m.Filename)
179185
}
180-
asset.SortFiles(c.FileList)
181186

187+
asset.SortManifestFiles(c.FileList)
182188
return nil
183189
}
184190

185191
// Files returns the files generated by the asset.
186192
func (c *Cluster) Files() []*asset.File {
193+
files := []*asset.File{}
194+
for _, f := range c.FileList {
195+
files = append(files, &f.File)
196+
}
197+
return files
198+
}
199+
200+
// RuntimeFiles returns the files generated by the asset.
201+
func (c *Cluster) RuntimeFiles() []*asset.RuntimeFile {
187202
return c.FileList
188203
}
189204

@@ -204,10 +219,7 @@ func (c *Cluster) Load(f asset.FileFetcher) (bool, error) {
204219
fileList := append(yamlFileList, ymlFileList...) //nolint:gocritic
205220
fileList = append(fileList, jsonFileList...)
206221

207-
c.FileList = append(c.FileList, fileList...)
208-
asset.SortFiles(c.FileList)
209-
210-
for _, file := range c.FileList {
222+
for _, file := range fileList {
211223
u := &unstructured.Unstructured{}
212224
if err := yaml.Unmarshal(file.Data, u); err != nil {
213225
return false, errors.Wrap(err, "failed to unmarshal file")
@@ -219,12 +231,14 @@ func (c *Cluster) Load(f asset.FileFetcher) (bool, error) {
219231
if err := clusterapi.Scheme.Convert(u, obj, nil); err != nil {
220232
return false, errors.Wrap(err, "failed to convert object")
221233
}
222-
c.Manifests = append(c.Manifests, &capiutils.Manifest{
223-
Filename: file.Filename,
224-
Object: obj.(client.Object),
234+
c.FileList = append(c.FileList, &asset.RuntimeFile{
235+
File: asset.File{
236+
Filename: file.Filename,
237+
Data: file.Data},
238+
Object: obj.(client.Object),
225239
})
226240
}
227241

228-
sort.Sort(c.Manifests)
242+
asset.SortManifestFiles(c.FileList)
229243
return len(c.FileList) > 0, nil
230244
}

0 commit comments

Comments
 (0)