Skip to content

Commit

Permalink
Merge pull request #2586 from fabriziopandini/clusterctl-all-image-ov…
Browse files Browse the repository at this point in the history
…errides

✨clusterctl: improve support for air-gapped environments all image overrides
  • Loading branch information
k8s-ci-robot authored Mar 9, 2020
2 parents 48be8c5 + 078d188 commit 5154da4
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 73 deletions.
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,6 @@ func defaultClusterFactory(configClient config.Client) func(kubeconfig string) (
// defaultRepositoryFactory is a RepositoryClientFactory func the uses the default client provided by the repository low level library.
func defaultRepositoryFactory(configClient config.Client) func(providerConfig config.Provider) (repository.Client, error) {
return func(providerConfig config.Provider) (repository.Client, error) {
return repository.New(providerConfig, configClient.Variables())
return repository.New(providerConfig, configClient)
}
}
36 changes: 18 additions & 18 deletions cmd/clusterctl/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestNewFakeClient(t *testing.T) {
WithProvider(repository1Config)

// create a fake repository with some YAML files in it (usually matching the list of providers defined in the config)
repository1 := newFakeRepository(repository1Config, config1.Variables()).
repository1 := newFakeRepository(repository1Config, config1).
WithPaths("root", "components").
WithDefaultVersion("v1.0").
WithFile("v1.0", "components.yaml", []byte("content"))
Expand Down Expand Up @@ -168,7 +168,7 @@ func newFakeCluster(kubeconfig string, configClient config.Client) *fakeClusterC
fake.internalclient = cluster.New("", configClient,
cluster.InjectProxy(fake.fakeProxy),
cluster.InjectPollImmediateWaiter(pollImmediateWaiter),
cluster.InjectRepositoryFactory(func(provider config.Provider, configVariablesClient config.VariablesClient, options ...repository.Option) (repository.Client, error) {
cluster.InjectRepositoryFactory(func(provider config.Provider, configClient config.Client, options ...repository.Option) (repository.Client, error) {
if _, ok := fake.repositories[provider.Name()]; !ok {
return nil, errors.Errorf("Repository for kubeconfig %q does not exists.", provider.Name())
}
Expand Down Expand Up @@ -300,24 +300,24 @@ func (f *fakeConfigClient) WithProvider(provider config.Provider) *fakeConfigCli
// newFakeRepository return a fake implementation of the client for low-level repository library.
// The implementation stores configuration settings in a map; you can use
// the WithPaths or WithDefaultVersion methods to configure the repository and WithFile to set the map values.
func newFakeRepository(provider config.Provider, configVariablesClient config.VariablesClient) *fakeRepositoryClient {
func newFakeRepository(provider config.Provider, configClient config.Client) *fakeRepositoryClient {
fakeRepository := test.NewFakeRepository()

if configVariablesClient == nil {
configVariablesClient = newFakeConfig().Variables()
if configClient == nil {
configClient = newFakeConfig()
}

return &fakeRepositoryClient{
Provider: provider,
configVariablesClient: configVariablesClient,
fakeRepository: fakeRepository,
Provider: provider,
configClient: configClient,
fakeRepository: fakeRepository,
}
}

type fakeRepositoryClient struct {
config.Provider
configVariablesClient config.VariablesClient
fakeRepository *test.FakeRepository
configClient config.Client
fakeRepository *test.FakeRepository
}

var _ repository.Client = &fakeRepositoryClient{}
Expand All @@ -333,9 +333,9 @@ func (f fakeRepositoryClient) GetVersions() ([]string, error) {
func (f fakeRepositoryClient) Components() repository.ComponentsClient {
// use a fakeComponentClient (instead of the internal client used in other fake objects) we can de deterministic on what is returned (e.g. avoid interferences from overrides)
return &fakeComponentClient{
provider: f.Provider,
fakeRepository: f.fakeRepository,
configVariablesClient: f.configVariablesClient,
provider: f.Provider,
fakeRepository: f.fakeRepository,
configClient: f.configClient,
}
}

Expand All @@ -344,7 +344,7 @@ func (f fakeRepositoryClient) Templates(version string) repository.TemplateClien
return &fakeTemplateClient{
version: version,
fakeRepository: f.fakeRepository,
configVariablesClient: f.configVariablesClient,
configVariablesClient: f.configClient.Variables(),
}
}

Expand Down Expand Up @@ -425,9 +425,9 @@ func (f *fakeMetadataClient) Get() (*clusterctlv1.Metadata, error) {

// fakeComponentClient provides a super simple ComponentClient (e.g. without support for local overrides)
type fakeComponentClient struct {
provider config.Provider
fakeRepository *test.FakeRepository
configVariablesClient config.VariablesClient
provider config.Provider
fakeRepository *test.FakeRepository
configClient config.Client
}

func (f *fakeComponentClient) Get(version, targetNamespace, watchingNamespace string) (repository.Components, error) {
Expand All @@ -441,5 +441,5 @@ func (f *fakeComponentClient) Get(version, targetNamespace, watchingNamespace st
return nil, err
}

return repository.NewComponents(f.provider, version, content, f.configVariablesClient, targetNamespace, watchingNamespace)
return repository.NewComponents(f.provider, version, content, f.configClient, targetNamespace, watchingNamespace)
}
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type clusterClient struct {
pollImmediateWaiter PollImmediateWaiter
}

type RepositoryClientFactory func(provider config.Provider, configVariablesClient config.VariablesClient, options ...repository.Option) (repository.Client, error)
type RepositoryClientFactory func(provider config.Provider, configClient config.Client, options ...repository.Option) (repository.Client, error)

// ensure clusterClient implements Client.
var _ Client = &clusterClient{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/cluster/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (i *providerInstaller) getProviderContract(providerInstanceContracts map[st
return "", err
}

providerRepository, err := i.repositoryClientFactory(configRepository, i.configClient.Variables())
providerRepository, err := i.repositoryClientFactory(configRepository, i.configClient)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/clusterctl/client/cluster/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func Test_providerInstaller_Validate(t *testing.T) {
configClient: configClient,
proxy: tt.fields.proxy,
providerInventory: newInventoryClient(tt.fields.proxy, nil),
repositoryClientFactory: func(provider config.Provider, configVariablesClient config.VariablesClient, options ...repository.Option) (repository.Client, error) {
return repository.New(provider, configVariablesClient, repository.InjectRepository(repositoryMap[provider.ManifestLabel()]))
repositoryClientFactory: func(provider config.Provider, configClient config.Client, options ...repository.Option) (repository.Client, error) {
return repository.New(provider, configClient, repository.InjectRepository(repositoryMap[provider.ManifestLabel()]))
},
installQueue: tt.fields.installQueue,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/cluster/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (u *providerUpgrader) getUpgradeComponents(provider UpgradeItem) (repositor
return nil, err
}

providerRepository, err := u.repositoryClientFactory(configRepository, u.configClient.Variables())
providerRepository, err := u.repositoryClientFactory(configRepository, u.configClient)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/cluster/upgrader_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (u *providerUpgrader) getUpgradeInfo(provider clusterctlv1.Provider) (*upgr
return nil, err
}

providerRepository, err := u.repositoryClientFactory(configRepository, u.configClient.Variables())
providerRepository, err := u.repositoryClientFactory(configRepository, u.configClient)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/clusterctl/client/cluster/upgrader_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func Test_providerUpgrader_getUpgradeInfo(t *testing.T) {

u := &providerUpgrader{
configClient: configClient,
repositoryClientFactory: func(provider config.Provider, configVariablesClient config.VariablesClient, options ...repository.Option) (repository.Client, error) {
return repository.New(provider, configVariablesClient, repository.InjectRepository(tt.fields.repository))
repositoryClientFactory: func(provider config.Provider, configClient config.Client, options ...repository.Option) (repository.Client, error) {
return repository.New(provider, configClient, repository.InjectRepository(tt.fields.repository))
},
}
got, err := u.getUpgradeInfo(tt.args.provider)
Expand Down
8 changes: 4 additions & 4 deletions cmd/clusterctl/client/cluster/upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ func Test_providerUpgrader_Plan(t *testing.T) {

u := &providerUpgrader{
configClient: configClient,
repositoryClientFactory: func(provider config.Provider, configVariablesClient config.VariablesClient, options ...repository.Option) (repository.Client, error) {
return repository.New(provider, configVariablesClient, repository.InjectRepository(tt.fields.repository[provider.ManifestLabel()]))
repositoryClientFactory: func(provider config.Provider, configClient config.Client, options ...repository.Option) (repository.Client, error) {
return repository.New(provider, configClient, repository.InjectRepository(tt.fields.repository[provider.ManifestLabel()]))
},
providerInventory: newInventoryClient(tt.fields.proxy, nil),
}
Expand Down Expand Up @@ -774,8 +774,8 @@ func Test_providerUpgrader_createCustomPlan(t *testing.T) {

u := &providerUpgrader{
configClient: configClient,
repositoryClientFactory: func(provider config.Provider, configVariablesClient config.VariablesClient, options ...repository.Option) (repository.Client, error) {
return repository.New(provider, configVariablesClient, repository.InjectRepository(tt.fields.repository[provider.Name()]))
repositoryClientFactory: func(provider config.Provider, configClient config.Client, options ...repository.Option) (repository.Client, error) {
return repository.New(provider, configClient, repository.InjectRepository(tt.fields.repository[provider.Name()]))
},
providerInventory: newInventoryClient(tt.fields.proxy, nil),
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/clusterctl/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Test_clusterctlClient_GetProviderComponents(t *testing.T) {
config1 := newFakeConfig().
WithProvider(capiProviderConfig)

repository1 := newFakeRepository(capiProviderConfig, config1.Variables()).
repository1 := newFakeRepository(capiProviderConfig, config1).
WithPaths("root", "components.yaml").
WithDefaultVersion("v1.0.0").
WithFile("v1.0.0", "components.yaml", componentsYAML("ns1"))
Expand Down Expand Up @@ -343,7 +343,7 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
config1 := newFakeConfig().
WithProvider(infraProviderConfig)

repository1 := newFakeRepository(infraProviderConfig, config1.Variables()).
repository1 := newFakeRepository(infraProviderConfig, config1).
WithPaths("root", "components").
WithDefaultVersion("v3.0.0").
WithFile("v3.0.0", "cluster-template.yaml", rawTemplate)
Expand Down
4 changes: 2 additions & 2 deletions cmd/clusterctl/client/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ func fakeClusterForDelete() *fakeClient {
WithProvider(capiProviderConfig).
WithProvider(bootstrapProviderConfig)

repository1 := newFakeRepository(capiProviderConfig, config1.Variables()).
repository1 := newFakeRepository(capiProviderConfig, config1).
WithPaths("root", "components.yaml").
WithDefaultVersion("v1.0.0").
WithFile("v1.0.0", "components.yaml", componentsYAML("ns1")).
WithFile("v1.1.0", "components.yaml", componentsYAML("ns1"))
repository2 := newFakeRepository(bootstrapProviderConfig, config1.Variables()).
repository2 := newFakeRepository(bootstrapProviderConfig, config1).
WithPaths("root", "components.yaml").
WithDefaultVersion("v2.0.0").
WithFile("v2.0.0", "components.yaml", componentsYAML("ns2")).
Expand Down
8 changes: 4 additions & 4 deletions cmd/clusterctl/client/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func fakeEmptyCluster() *fakeClient {
WithProvider(controlPlaneProviderConfig).
WithProvider(infraProviderConfig)

repository1 := newFakeRepository(capiProviderConfig, config1.Variables()).
repository1 := newFakeRepository(capiProviderConfig, config1).
WithPaths("root", "components.yaml").
WithDefaultVersion("v1.0.0").
WithFile("v1.0.0", "components.yaml", componentsYAML("ns1")).
Expand All @@ -408,7 +408,7 @@ func fakeEmptyCluster() *fakeClient {
{Major: 1, Minor: 1, Contract: "v1alpha3"},
},
})
repository2 := newFakeRepository(bootstrapProviderConfig, config1.Variables()).
repository2 := newFakeRepository(bootstrapProviderConfig, config1).
WithPaths("root", "components.yaml").
WithDefaultVersion("v2.0.0").
WithFile("v2.0.0", "components.yaml", componentsYAML("ns2")).
Expand All @@ -423,7 +423,7 @@ func fakeEmptyCluster() *fakeClient {
{Major: 2, Minor: 1, Contract: "v1alpha3"},
},
})
repository3 := newFakeRepository(controlPlaneProviderConfig, config1.Variables()).
repository3 := newFakeRepository(controlPlaneProviderConfig, config1).
WithPaths("root", "components.yaml").
WithDefaultVersion("v2.0.0").
WithFile("v2.0.0", "components.yaml", componentsYAML("ns3")).
Expand All @@ -438,7 +438,7 @@ func fakeEmptyCluster() *fakeClient {
{Major: 2, Minor: 1, Contract: "v1alpha3"},
},
})
repository4 := newFakeRepository(infraProviderConfig, config1.Variables()).
repository4 := newFakeRepository(infraProviderConfig, config1).
WithPaths("root", "components.yaml").
WithDefaultVersion("v3.0.0").
WithFile("v3.0.0", "components.yaml", componentsYAML("ns4")).
Expand Down
20 changes: 10 additions & 10 deletions cmd/clusterctl/client/repository/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type Client interface {
// repositoryClient implements Client.
type repositoryClient struct {
config.Provider
configVariablesClient config.VariablesClient
repository Repository
configClient config.Client
repository Repository
}

// ensure repositoryClient implements Client.
Expand All @@ -64,11 +64,11 @@ func (c *repositoryClient) GetVersions() ([]string, error) {
}

func (c *repositoryClient) Components() ComponentsClient {
return newComponentsClient(c.Provider, c.repository, c.configVariablesClient)
return newComponentsClient(c.Provider, c.repository, c.configClient)
}

func (c *repositoryClient) Templates(version string) TemplateClient {
return newTemplateClient(c.Provider, version, c.repository, c.configVariablesClient)
return newTemplateClient(c.Provider, version, c.repository, c.configClient.Variables())
}

func (c *repositoryClient) Metadata(version string) MetadataClient {
Expand All @@ -88,22 +88,22 @@ func InjectRepository(repository Repository) Option {
}

// New returns a Client.
func New(provider config.Provider, configVariablesClient config.VariablesClient, options ...Option) (Client, error) {
return newRepositoryClient(provider, configVariablesClient, options...)
func New(provider config.Provider, configClient config.Client, options ...Option) (Client, error) {
return newRepositoryClient(provider, configClient, options...)
}

func newRepositoryClient(provider config.Provider, configVariablesClient config.VariablesClient, options ...Option) (*repositoryClient, error) {
func newRepositoryClient(provider config.Provider, configClient config.Client, options ...Option) (*repositoryClient, error) {
client := &repositoryClient{
Provider: provider,
configVariablesClient: configVariablesClient,
Provider: provider,
configClient: configClient,
}
for _, o := range options {
o(client)
}

// if there is an injected repository, use it, otherwise use a default one
if client.repository == nil {
r, err := repositoryFactory(provider, configVariablesClient)
r, err := repositoryFactory(provider, configClient.Variables())
if err != nil {
return nil, errors.Wrapf(err, "failed to get repository client for the %s with name %s", provider.Type(), provider.Name())
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/clusterctl/client/repository/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func Test_newRepositoryClient_LocalFileSystemRepository(t *testing.T) {
dst1 := createLocalTestProviderFile(t, tmpDir, "bootstrap-foo/v1.0.0/bootstrap-components.yaml", "")
dst2 := createLocalTestProviderFile(t, tmpDir, "bootstrap-bar/v2.0.0/bootstrap-components.yaml", "")

configClient, err := config.New("", config.InjectReader(test.NewFakeReader()))
if err != nil {
t.Fatal(err)
}

type fields struct {
provider config.Provider
}
Expand All @@ -54,7 +59,7 @@ func Test_newRepositoryClient_LocalFileSystemRepository(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
repoClient, err := newRepositoryClient(tt.fields.provider, test.NewFakeVariableClient())
repoClient, err := newRepositoryClient(tt.fields.provider, configClient)
if err != nil {
t.Fatalf("got error %v when none was expected", err)
}
Expand Down
12 changes: 10 additions & 2 deletions cmd/clusterctl/client/repository/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ func (c *components) Yaml() ([]byte, error) {
// 3. Ensure all the ClusterRoleBinding which are referencing namespaced objects have the name prefixed with the namespace name
// 4. Set the watching namespace for the provider controller
// 5. Adds labels to all the components in order to allow easy identification of the provider objects
func NewComponents(provider config.Provider, version string, rawyaml []byte, configVariablesClient config.VariablesClient, targetNamespace, watchingNamespace string) (*components, error) {
func NewComponents(provider config.Provider, version string, rawyaml []byte, configClient config.Client, targetNamespace, watchingNamespace string) (*components, error) {
// inspect the yaml read from the repository for variables
variables := inspectVariables(rawyaml)

// Replace variables with corresponding values read from the config
yaml, err := replaceVariables(rawyaml, variables, configVariablesClient)
yaml, err := replaceVariables(rawyaml, variables, configClient.Variables())
if err != nil {
return nil, errors.Wrap(err, "failed to perform variable substitution")
}
Expand All @@ -199,6 +199,14 @@ func NewComponents(provider config.Provider, version string, rawyaml []byte, con
return nil, errors.Wrap(err, "failed to parse yaml")
}

// apply image overrides, if defined
objs, err = util.FixImages(objs, func(image string) (string, error) {
return configClient.ImageMeta().AlterImage(provider.ManifestLabel(), image)
})
if err != nil {
return nil, errors.Wrap(err, "failed to apply image overrides")
}

// inspect the list of objects for the images required by the provider component
images, err := util.InspectImages(objs)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions cmd/clusterctl/client/repository/components_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ type ComponentsClient interface {

// componentsClient implements ComponentsClient.
type componentsClient struct {
provider config.Provider
repository Repository
configVariablesClient config.VariablesClient
provider config.Provider
repository Repository
configClient config.Client
}

// ensure componentsClient implements ComponentsClient.
var _ ComponentsClient = &componentsClient{}

// newComponentsClient returns a componentsClient.
func newComponentsClient(provider config.Provider, repository Repository, configVariablesClient config.VariablesClient) *componentsClient {
func newComponentsClient(provider config.Provider, repository Repository, configClient config.Client) *componentsClient {
return &componentsClient{
provider: provider,
repository: repository,
configVariablesClient: configVariablesClient,
provider: provider,
repository: repository,
configClient: configClient,
}
}

Expand Down Expand Up @@ -74,5 +74,5 @@ func (f *componentsClient) Get(version, targetNamespace, watchingNamespace strin
log.V(1).Info("Using", "Override", path, "Provider", f.provider.ManifestLabel(), "Version", version)
}

return NewComponents(f.provider, version, file, f.configVariablesClient, targetNamespace, watchingNamespace)
return NewComponents(f.provider, version, file, f.configClient, targetNamespace, watchingNamespace)
}
Loading

0 comments on commit 5154da4

Please sign in to comment.