Skip to content

Commit

Permalink
Use overridesFolder config to configure overrides
Browse files Browse the repository at this point in the history
Specify the overridesFolder property in the clusterctl config to
determine where the overrides live.
  • Loading branch information
Warren Fernandes committed Mar 16, 2020
1 parent 5252a7a commit 1c63815
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 60 deletions.
3 changes: 1 addition & 2 deletions cmd/clusterctl/client/config/variables_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ func Test_variables_Get(t *testing.T) {
wantErr: false,
},
{
name: "Returns error if the variable does exists",
name: "Returns error if the variable does not exist",
args: args{
key: "baz",
},
want: "",
wantErr: true,
},
}
Expand Down
34 changes: 1 addition & 33 deletions cmd/clusterctl/client/repository/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ limitations under the License.
package repository

import (
"io/ioutil"
"net/url"
"os"
"path/filepath"

"github.com/pkg/errors"
"k8s.io/client-go/util/homedir"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
)
Expand Down Expand Up @@ -72,7 +68,7 @@ func (c *repositoryClient) Templates(version string) TemplateClient {
}

func (c *repositoryClient) Metadata(version string) MetadataClient {
return newMetadataClient(c.Provider, version, c.repository)
return newMetadataClient(c.Provider, version, c.repository, c.configClient.Variables())
}

// Option is a configuration option supplied to New
Expand Down Expand Up @@ -168,31 +164,3 @@ func repositoryFactory(providerConfig config.Provider, configVariablesClient con

return nil, errors.Errorf("invalid provider url. there are no provider implementation for %q schema", rURL.Scheme)
}

const overrideFolder = "overrides"

// getLocalOverride return local override file from the config folder, if it exists.
// This is required for development purposes, but it can be used also in production as a workaround for problems on the official repositories
func getLocalOverride(provider config.Provider, version, path string) ([]byte, error) {
// local override files are searched at $home/.cluster-api/overrides/<provider-label>/<version>/<path>
homeFolder := filepath.Join(homedir.HomeDir(), config.ConfigFolder)
overridePath := filepath.Join(homeFolder, overrideFolder, provider.ManifestLabel(), version, path)

// it the local override exists, use it
_, err := os.Stat(overridePath)
if err == nil {
content, err := ioutil.ReadFile(overridePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read local override for %s/%s/%s", provider.ManifestLabel(), version, path)
}
return content, nil
}

// it the local override does not exists, return (so files from the provider's repository could be used)
if os.IsNotExist(err) {
return nil, nil
}

// blocks for any other error
return nil, err
}
7 changes: 6 additions & 1 deletion cmd/clusterctl/client/repository/components_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ func (f *componentsClient) Get(version, targetNamespace, watchingNamespace strin
path := f.repository.ComponentsPath()

// read the component YAML, reading the local override file if it exists, otherwise read from the provider repository
file, err := getLocalOverride(f.provider, version, path)
file, err := getLocalOverride(&newOverrideInput{
configVariablesClient: f.configClient.Variables(),
provider: f.provider,
version: version,
filePath: path,
})
if err != nil {
return nil, err
}
Expand Down
23 changes: 15 additions & 8 deletions cmd/clusterctl/client/repository/metadata_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ type MetadataClient interface {

// metadataClient implements MetadataClient.
type metadataClient struct {
provider config.Provider
version string
repository Repository
configVarClient config.VariablesClient
provider config.Provider
version string
repository Repository
}

// ensure metadataClient implements MetadataClient.
var _ MetadataClient = &metadataClient{}

// newMetadataClient returns a metadataClient.
func newMetadataClient(provider config.Provider, version string, repository Repository) *metadataClient {
func newMetadataClient(provider config.Provider, version string, repository Repository, config config.VariablesClient) *metadataClient {
return &metadataClient{
provider: provider,
version: version,
repository: repository,
configVarClient: config,
provider: provider,
version: version,
repository: repository,
}
}

Expand All @@ -60,7 +62,12 @@ func (f *metadataClient) Get() (*clusterctlv1.Metadata, error) {
version := f.version
name := "metadata.yaml"

file, err := getLocalOverride(f.provider, version, name)
file, err := getLocalOverride(&newOverrideInput{
configVariablesClient: f.configVarClient,
provider: f.provider,
version: version,
filePath: name,
})
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/clusterctl/client/repository/metadata_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ func Test_metadataClient_Get(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &metadataClient{
provider: tt.fields.provider,
version: tt.fields.version,
repository: tt.fields.repository,
configVarClient: test.NewFakeVariableClient(),
provider: tt.fields.provider,
version: tt.fields.version,
repository: tt.fields.repository,
}
got, err := f.Get()
if (err != nil) != tt.wantErr {
Expand Down
103 changes: 103 additions & 0 deletions cmd/clusterctl/client/repository/overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package repository

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"k8s.io/client-go/util/homedir"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
)

const (
overrideFolder = "overrides"
overrideFolderKey = "overridesFolder"
)

// Overrider provides behavior to determine the overrides layer.
type Overrider interface {
Path() string
}

// overrides implements the Overrider interface.
type overrides struct {
configVariablesClient config.VariablesClient
providerLabel string
version string
filePath string
}

type newOverrideInput struct {
configVariablesClient config.VariablesClient
provider config.Provider
version string
filePath string
}

// newOverride returns an Overrider.
func newOverride(o *newOverrideInput) Overrider {
return &overrides{
configVariablesClient: o.configVariablesClient,
providerLabel: o.provider.ManifestLabel(),
version: o.version,
filePath: o.filePath,
}
}

// Path returns the fully formed path to the file within the specified
// overrides config.
func (o *overrides) Path() string {
basepath := filepath.Join(homedir.HomeDir(), config.ConfigFolder, overrideFolder)
f, err := o.configVariablesClient.Get(overrideFolderKey)
if err == nil && len(strings.TrimSpace(f)) != 0 {
basepath = f
}

return filepath.Join(
basepath,
o.providerLabel,
o.version,
o.filePath,
)
}

// getLocalOverride return local override file from the config folder, if it exists.
// This is required for development purposes, but it can be used also in production as a workaround for problems on the official repositories
func getLocalOverride(info *newOverrideInput) ([]byte, error) {
overridePath := newOverride(info).Path()
// it the local override exists, use it
_, err := os.Stat(overridePath)
if err == nil {
content, err := ioutil.ReadFile(overridePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read local override for %s", overridePath)
}
return content, nil
}

// it the local override does not exists, return (so files from the provider's repository could be used)
if os.IsNotExist(err) {
return nil, nil
}

// blocks for any other error
return nil, err
}
108 changes: 108 additions & 0 deletions cmd/clusterctl/client/repository/overrides_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package repository

import (
"os"
"path/filepath"
"testing"

. "github.com/onsi/gomega"
"k8s.io/client-go/util/homedir"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
)

func TestOverrides(t *testing.T) {
tests := []struct {
name string
configVarClient config.VariablesClient
expectedPath string
}{
{
name: "returns default overrides path if no config provided",
configVarClient: test.NewFakeVariableClient(),
expectedPath: filepath.Join(homedir.HomeDir(), config.ConfigFolder, overrideFolder, "infrastructure-myinfra", "v1.0.1", "infra-comp.yaml"),
},
{
name: "returns default overrides path if config variable is empty",
configVarClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, ""),
expectedPath: filepath.Join(homedir.HomeDir(), config.ConfigFolder, overrideFolder, "infrastructure-myinfra", "v1.0.1", "infra-comp.yaml"),
},
{
name: "returns default overrides path if config variable is whitespace",
configVarClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, " "),
expectedPath: filepath.Join(homedir.HomeDir(), config.ConfigFolder, overrideFolder, "infrastructure-myinfra", "v1.0.1", "infra-comp.yaml"),
},
{
name: "uses overrides folder from the config variables",
configVarClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, "/Users/foobar/workspace/releases"),
expectedPath: "/Users/foobar/workspace/releases/infrastructure-myinfra/v1.0.1/infra-comp.yaml",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
provider := config.NewProvider("myinfra", "", clusterctlv1.InfrastructureProviderType)
override := newOverride(&newOverrideInput{
configVariablesClient: tt.configVarClient,
provider: provider,
version: "v1.0.1",
filePath: "infra-comp.yaml",
})

g.Expect(override.Path()).To(Equal(tt.expectedPath))
})
}
}

func TestGetLocalOverrides(t *testing.T) {
t.Run("returns contents of file successfully", func(t *testing.T) {
g := NewWithT(t)
tmpDir := createTempDir(t)
defer os.RemoveAll(tmpDir)

createLocalTestProviderFile(t, tmpDir, "infrastructure-myinfra/v1.0.1/infra-comp.yaml", "foo: bar")

info := &newOverrideInput{
configVariablesClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, tmpDir),
provider: config.NewProvider("myinfra", "", clusterctlv1.InfrastructureProviderType),
version: "v1.0.1",
filePath: "infra-comp.yaml",
}

b, err := getLocalOverride(info)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(string(b)).To(Equal("foo: bar"))
})

t.Run("doesn't return error if file does not exist", func(t *testing.T) {
g := NewWithT(t)

info := &newOverrideInput{
configVariablesClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, "do-not-exist"),
provider: config.NewProvider("myinfra", "", clusterctlv1.InfrastructureProviderType),
version: "v1.0.1",
filePath: "infra-comp.yaml",
}

_, err := getLocalOverride(info)
g.Expect(err).ToNot(HaveOccurred())
})
}
7 changes: 6 additions & 1 deletion cmd/clusterctl/client/repository/template_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ func (c *templateClient) Get(flavor, targetNamespace string, listVariablesOnly b
name = fmt.Sprintf("%s.yaml", name)

// read the component YAML, reading the local override file if it exists, otherwise read from the provider repository
rawYaml, err := getLocalOverride(c.provider, version, name)
rawYaml, err := getLocalOverride(&newOverrideInput{
configVariablesClient: c.configVariablesClient,
provider: c.provider,
version: version,
filePath: name,
})
if err != nil {
return nil, err
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/clusterctl/test/run-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ cat <<EOF > "clusterctl-settings.json"
EOF

# Create a local filesystem repository for the docker provider and update clusterctl.yaml
LOCAL_CAPD_REPO_PATH="${ARTIFACTS}/testdata/docker"
LOCAL_CAPD_REPO_PATH="${ARTIFACTS}/testdata/infrastructure-docker"
mkdir -p "${LOCAL_CAPD_REPO_PATH}"
cp -r "${REPO_ROOT_ABS}/cmd/clusterctl/test/testdata/docker/${CAPD_VERSION}" "${LOCAL_CAPD_REPO_PATH}"
# We build the infrastructure-components.yaml from the capd folder and put in local repo folder
kustomize build "${REPO_ROOT_ABS}/test/infrastructure/docker/config/default/" > "${LOCAL_CAPD_REPO_PATH}/${CAPD_VERSION}/infrastructure-components.yaml"
export CLUSTERCTL_CONFIG="${ARTIFACTS}/testdata/clusterctl.yaml"
export CLUSTERCTL_CONFIG="${ARTIFACTS}/testdata/clusterctl.yaml"
cat <<EOF > "${CLUSTERCTL_CONFIG}"
providers:
- name: docker
url: ${LOCAL_CAPD_REPO_PATH}/${CAPD_VERSION}/infrastructure-components.yaml
type: InfrastructureProvider
overridesFolder:${ARTIFACTS}/testdata
DOCKER_SERVICE_DOMAIN: "cluster.local"
DOCKER_SERVICE_CIDRS: "10.128.0.0/12"
DOCKER_POD_CIDRS: "192.168.0.0/16"
DOCKER_SERVICE_CIDRS: "10.128.0.0/12"
DOCKER_POD_CIDRS: "192.168.0.0/16"
EOF

export KIND_CONFIG_FILE="${ARTIFACTS}/kind-cluster-with-extramounts.yaml"
Expand Down
Loading

0 comments on commit 1c63815

Please sign in to comment.