Skip to content

Commit 2febc3a

Browse files
clusterctl add local override
1 parent a26471f commit 2febc3a

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

cmd/clusterctl/pkg/client/config/reader_viper.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import (
2626
"k8s.io/klog"
2727
)
2828

29+
// ConfigFolder defines the name of the config folder under $home
30+
const ConfigFolder = "cluster-api"
31+
2932
// viperReader implements Reader using viper as backend for reading from environment variables
3033
// and from a clusterctl config file.
3134
type viperReader struct {
@@ -44,7 +47,7 @@ func (v *viperReader) Init(path string) error {
4447
} else {
4548
// Configure for searching cluster-api/.clusterctl{.extension} in home directory
4649
viper.SetConfigName(".clusterctl")
47-
viper.AddConfigPath(filepath.Join(homedir.HomeDir(), "cluster-api"))
50+
viper.AddConfigPath(filepath.Join(homedir.HomeDir(), ConfigFolder))
4851
}
4952

5053
// Configure for reading environment variables as well, and more specifically:

cmd/clusterctl/pkg/client/repository/client.go

+33
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ limitations under the License.
1717
package repository
1818

1919
import (
20+
"io/ioutil"
2021
"net/url"
22+
"os"
23+
"path/filepath"
2124

2225
"github.com/pkg/errors"
26+
"k8s.io/client-go/util/homedir"
2327
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config"
2428
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/test"
2529
)
@@ -151,3 +155,32 @@ func repositoryFactory(providerConfig config.Provider, configVariablesClient con
151155

152156
return nil, errors.Errorf("invalid provider url. there are no provider implementation for %q schema", rURL.Scheme)
153157
}
158+
159+
const overrideFolder = "overrides"
160+
161+
// getLocalOverride return local override file from the config folder, if it exists.
162+
// This is required for development purposes, but it can be used also in production as a workaround for problems on the official repositories
163+
func getLocalOverride(provider config.Provider, version, path string) ([]byte, error) {
164+
165+
// local override files are searched at $home/cluster-api/overrides/<provider-name>/<version>/<path>
166+
homeFolder := filepath.Join(homedir.HomeDir(), config.ConfigFolder)
167+
overridePath := filepath.Join(homeFolder, overrideFolder, provider.Name(), version, path)
168+
169+
// it the local override exists, use it
170+
_, err := os.Stat(overridePath)
171+
if err == nil {
172+
content, err := ioutil.ReadFile(overridePath)
173+
if err != nil {
174+
return nil, errors.Wrapf(err, "failed to read local override for %s/%s/%s", provider.Name(), version, path)
175+
}
176+
return content, nil
177+
}
178+
179+
// it the local override does not exists, return (so files from the provider's repository could be used)
180+
if os.IsNotExist(err) {
181+
return nil, nil
182+
}
183+
184+
// blocks for any other error
185+
return nil, err
186+
}

cmd/clusterctl/pkg/client/repository/components_client.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,17 @@ func (f *componentsClient) Get(version, targetNamespace, watchingNamespace strin
5555
// retrieve the path where the path is stored
5656
path := f.repository.ComponentsPath()
5757

58-
// read from the components path.
59-
// If file is a path, the entire content of the path is returned.
60-
file, err := f.repository.GetFile(version, path)
58+
// read the component YAML, reading the local override file if it exists, otherwise read from the provider repository
59+
file, err := getLocalOverride(f.provider, version, path)
6160
if err != nil {
62-
return nil, errors.Wrapf(err, "failed to read %q from the repository for provider %q", path, f.provider.Name())
61+
return nil, err
62+
}
63+
64+
if file == nil {
65+
file, err = f.repository.GetFile(version, path)
66+
if err != nil {
67+
return nil, errors.Wrapf(err, "failed to read %q from provider's repository %q", path, f.provider.Name())
68+
}
6369
}
6470

6571
return newComponents(f.provider, version, file, f.configVariablesClient, targetNamespace, watchingNamespace)

0 commit comments

Comments
 (0)