Skip to content

Commit 55e92ea

Browse files
authored
Merge pull request #5300 from asalkeld/operator-required-fixes-combined
🌱 Operator two small required changes
2 parents af2feb1 + ad98753 commit 55e92ea

File tree

5 files changed

+71
-15
lines changed

5 files changed

+71
-15
lines changed

cmd/clusterctl/client/repository/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func repositoryFactory(providerConfig config.Provider, configVariablesClient con
157157

158158
// if the url is a github repository
159159
if rURL.Scheme == httpsScheme && rURL.Host == githubDomain {
160-
repo, err := newGitHubRepository(providerConfig, configVariablesClient)
160+
repo, err := NewGitHubRepository(providerConfig, configVariablesClient)
161161
if err != nil {
162162
return nil, errors.Wrap(err, "error creating the GitHub repository client")
163163
}

cmd/clusterctl/client/repository/components.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ func (c *components) Yaml() ([]byte, error) {
144144
return utilyaml.FromUnstructured(c.objs)
145145
}
146146

147+
// ComponentsAlterFn defines the function that is used to alter the components.Objs().
148+
type ComponentsAlterFn func(objs []unstructured.Unstructured) ([]unstructured.Unstructured, error)
149+
150+
// AlterComponents provides a mechanism to alter the component.Objs from outside
151+
// the repository module.
152+
func AlterComponents(comps Components, alterFn ComponentsAlterFn) error {
153+
c, ok := comps.(*components)
154+
if !ok {
155+
return errors.New("could not alter components as Components is not of the correct type")
156+
}
157+
158+
alteredObjs, err := alterFn(c.Objs())
159+
if err != nil {
160+
return err
161+
}
162+
c.objs = alteredObjs
163+
return nil
164+
}
165+
147166
// ComponentsOptions represents specific inputs that are passed in to
148167
// clusterctl library. These are user specified inputs.
149168
type ComponentsOptions struct {

cmd/clusterctl/client/repository/components_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,3 +948,40 @@ func Test_addCommonLabels(t *testing.T) {
948948
})
949949
}
950950
}
951+
952+
func TestAlterComponents(t *testing.T) {
953+
c := &components{
954+
targetNamespace: "test-ns",
955+
objs: []unstructured.Unstructured{
956+
{
957+
Object: map[string]interface{}{
958+
"kind": "ClusterRole",
959+
},
960+
},
961+
},
962+
}
963+
want := []unstructured.Unstructured{
964+
{
965+
Object: map[string]interface{}{
966+
"kind": "ClusterRole",
967+
"metadata": map[string]interface{}{
968+
"labels": map[string]interface{}{
969+
clusterctlv1.ClusterctlLabelName: "",
970+
clusterv1.ProviderLabelName: "infrastructure-provider",
971+
},
972+
},
973+
},
974+
},
975+
}
976+
977+
alterFn := func(objs []unstructured.Unstructured) ([]unstructured.Unstructured, error) {
978+
// reusing addCommonLabels to do an example modification.
979+
return addCommonLabels(objs, config.NewProvider("provider", "", clusterctlv1.InfrastructureProviderType)), nil
980+
}
981+
982+
g := NewWithT(t)
983+
if err := AlterComponents(c, alterFn); err != nil {
984+
t.Errorf("AlterComponents() error = %v", err)
985+
}
986+
g.Expect(c.objs).To(Equal(want))
987+
}

cmd/clusterctl/client/repository/repository_github.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ func (g *gitHubRepository) GetFile(version, path string) ([]byte, error) {
116116
return files, nil
117117
}
118118

119-
// newGitHubRepository returns a gitHubRepository implementation.
120-
func newGitHubRepository(providerConfig config.Provider, configVariablesClient config.VariablesClient, opts ...githubRepositoryOption) (*gitHubRepository, error) {
119+
// NewGitHubRepository returns a gitHubRepository implementation.
120+
func NewGitHubRepository(providerConfig config.Provider, configVariablesClient config.VariablesClient, opts ...githubRepositoryOption) (Repository, error) {
121121
if configVariablesClient == nil {
122122
return nil, errors.New("invalid arguments: configVariablesClient can't be nil")
123123
}

cmd/clusterctl/client/repository/repository_github_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func Test_githubRepository_newGitHubRepository(t *testing.T) {
112112
g := NewWithT(t)
113113
resetCaches()
114114

115-
gitHub, err := newGitHubRepository(tt.field.providerConfig, tt.field.variableClient)
115+
gitHub, err := NewGitHubRepository(tt.field.providerConfig, tt.field.variableClient)
116116
if tt.wantErr {
117117
g.Expect(err).To(HaveOccurred())
118118
return
@@ -212,7 +212,7 @@ func Test_githubRepository_getFile(t *testing.T) {
212212
g := NewWithT(t)
213213
resetCaches()
214214

215-
gitHub, err := newGitHubRepository(providerConfig, configVariablesClient, injectGithubClient(client))
215+
gitHub, err := NewGitHubRepository(providerConfig, configVariablesClient, injectGithubClient(client))
216216
g.Expect(err).NotTo(HaveOccurred())
217217

218218
got, err := gitHub.GetFile(tt.release, tt.fileName)
@@ -268,10 +268,10 @@ func Test_gitHubRepository_getVersions(t *testing.T) {
268268
g := NewWithT(t)
269269
resetCaches()
270270

271-
gitHub, err := newGitHubRepository(tt.field.providerConfig, configVariablesClient, injectGithubClient(client))
271+
gitHub, err := NewGitHubRepository(tt.field.providerConfig, configVariablesClient, injectGithubClient(client))
272272
g.Expect(err).NotTo(HaveOccurred())
273273

274-
got, err := gitHub.getVersions()
274+
got, err := gitHub.(*gitHubRepository).getVersions()
275275
if tt.wantErr {
276276
g.Expect(err).To(HaveOccurred())
277277
return
@@ -357,7 +357,7 @@ func Test_gitHubRepository_getLatestContractRelease(t *testing.T) {
357357
g := NewWithT(t)
358358
resetCaches()
359359

360-
gRepo, err := newGitHubRepository(tt.field.providerConfig, configVariablesClient, injectGithubClient(client))
360+
gRepo, err := NewGitHubRepository(tt.field.providerConfig, configVariablesClient, injectGithubClient(client))
361361
g.Expect(err).NotTo(HaveOccurred())
362362

363363
got, err := latestContractRelease(gRepo, tt.contract)
@@ -443,7 +443,7 @@ func Test_gitHubRepository_getLatestRelease(t *testing.T) {
443443
g := NewWithT(t)
444444
resetCaches()
445445

446-
gRepo, err := newGitHubRepository(tt.field.providerConfig, configVariablesClient, injectGithubClient(client))
446+
gRepo, err := NewGitHubRepository(tt.field.providerConfig, configVariablesClient, injectGithubClient(client))
447447
g.Expect(err).NotTo(HaveOccurred())
448448

449449
got, err := latestRelease(gRepo)
@@ -453,7 +453,7 @@ func Test_gitHubRepository_getLatestRelease(t *testing.T) {
453453
}
454454
g.Expect(err).NotTo(HaveOccurred())
455455
g.Expect(got).To(Equal(tt.want))
456-
g.Expect(gRepo.defaultVersion).To(Equal(tt.want))
456+
g.Expect(gRepo.(*gitHubRepository).defaultVersion).To(Equal(tt.want))
457457
})
458458
}
459459
}
@@ -525,7 +525,7 @@ func Test_gitHubRepository_getLatestPatchRelease(t *testing.T) {
525525
g := NewWithT(t)
526526
resetCaches()
527527

528-
gRepo, err := newGitHubRepository(tt.field.providerConfig, configVariablesClient, injectGithubClient(client))
528+
gRepo, err := NewGitHubRepository(tt.field.providerConfig, configVariablesClient, injectGithubClient(client))
529529
g.Expect(err).NotTo(HaveOccurred())
530530

531531
got, err := latestPatchRelease(gRepo, tt.major, tt.minor)
@@ -584,10 +584,10 @@ func Test_gitHubRepository_getReleaseByTag(t *testing.T) {
584584
g := NewWithT(t)
585585
resetCaches()
586586

587-
gRepo, err := newGitHubRepository(providerConfig, configVariablesClient, injectGithubClient(client))
587+
gRepo, err := NewGitHubRepository(providerConfig, configVariablesClient, injectGithubClient(client))
588588
g.Expect(err).NotTo(HaveOccurred())
589589

590-
got, err := gRepo.getReleaseByTag(tt.args.tag)
590+
got, err := gRepo.(*gitHubRepository).getReleaseByTag(tt.args.tag)
591591
if tt.wantErr {
592592
g.Expect(err).To(HaveOccurred())
593593
return
@@ -690,10 +690,10 @@ func Test_gitHubRepository_downloadFilesFromRelease(t *testing.T) {
690690
g := NewWithT(t)
691691
resetCaches()
692692

693-
gRepo, err := newGitHubRepository(providerConfig, configVariablesClient, injectGithubClient(client))
693+
gRepo, err := NewGitHubRepository(providerConfig, configVariablesClient, injectGithubClient(client))
694694
g.Expect(err).NotTo(HaveOccurred())
695695

696-
got, err := gRepo.downloadFilesFromRelease(tt.args.release, tt.args.fileName)
696+
got, err := gRepo.(*gitHubRepository).downloadFilesFromRelease(tt.args.release, tt.args.fileName)
697697
if tt.wantErr {
698698
g.Expect(err).To(HaveOccurred())
699699
return

0 commit comments

Comments
 (0)