Skip to content

Commit

Permalink
Merge pull request #2254 from fabriziopandini/clusterctl-make-templat…
Browse files Browse the repository at this point in the history
…e-generic

✨clusterctl: make template object more generic
  • Loading branch information
k8s-ci-robot authored Feb 5, 2020
2 parents 5ce39e9 + 2dab84b commit 12ddd37
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 139 deletions.
35 changes: 0 additions & 35 deletions cmd/clusterctl/pkg/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
}

type templateValues struct {
name string
url string
providerType clusterctlv1.ProviderType
version string
flavor string
variables []string
targetNamespace string
yaml []byte
Expand All @@ -353,11 +348,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
},
want: templateValues{
name: "infra",
url: "url",
providerType: clusterctlv1.InfrastructureProviderType,
version: "v3.0.0",
flavor: "",
variables: []string{"CLUSTER_NAME"}, // variable detected
targetNamespace: "ns1",
yaml: templateYAML("ns1", "test"), // original template modified with target namespace and variable replacement
Expand All @@ -376,11 +366,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
},
want: templateValues{
name: "infra",
url: "url",
providerType: clusterctlv1.InfrastructureProviderType,
version: "v3.0.0",
flavor: "",
variables: []string{"CLUSTER_NAME"}, // variable detected
targetNamespace: "ns1",
yaml: templateYAML("ns1", "test"), // original template modified with target namespace and variable replacement
Expand All @@ -399,11 +384,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
},
want: templateValues{
name: "infra",
url: "url",
providerType: clusterctlv1.InfrastructureProviderType,
version: "v3.0.0",
flavor: "",
variables: []string{"CLUSTER_NAME"}, // variable detected
targetNamespace: "default",
yaml: templateYAML("default", "test"), // original template modified with target namespace and variable replacement
Expand All @@ -420,21 +400,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
return
}

if got.Name() != tt.want.name {
t.Errorf("Name() got = %v, want %v", got.Name(), tt.want.name)
}
if got.URL() != tt.want.url {
t.Errorf("URL() got = %v, want %v", got.URL(), tt.want.url)
}
if got.Type() != tt.want.providerType {
t.Errorf("Type() got = %v, want %v", got.Type(), tt.want.providerType)
}
if got.Version() != tt.want.version {
t.Errorf("Version() got = %v, want %v", got.Version(), tt.want.version)
}
if got.Flavor() != tt.want.flavor {
t.Errorf("Flavor() got = %v, want %v", got.Flavor(), tt.want.flavor)
}
if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
t.Errorf("Variables() got = %v, want %v", got.Variables(), tt.want.variables)
}
Expand Down
46 changes: 6 additions & 40 deletions cmd/clusterctl/pkg/client/repository/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ import (
// 1. Checks for all the variables in the cluster template YAML file and replace with corresponding config values
// 2. Ensure all the cluster objects are deployed in the target namespace
type Template interface {
// configuration of the provider the template belongs to.
config.Provider

// Version of the provider the template belongs to.
Version() string

// Flavor implemented by the template (empty means default flavor).
// A flavor is a variant of cluster template supported by the provider, like e.g. Prod, Test.
Flavor() string

// Variables required by the template.
// This value is derived by the template YAML.
Variables() []string
Expand All @@ -55,9 +45,6 @@ type Template interface {

// template implements Template.
type template struct {
config.Provider
version string
flavor string
variables []string
targetNamespace string
objs []unstructured.Unstructured
Expand All @@ -66,14 +53,6 @@ type template struct {
// Ensures template implements the Template interface.
var _ Template = &template{}

func (t *template) Version() string {
return t.version
}

func (t *template) Flavor() string {
return t.flavor
}

func (t *template) Variables() []string {
return t.variables
}
Expand All @@ -90,22 +69,12 @@ func (t *template) Yaml() ([]byte, error) {
return util.FromUnstructured(t.objs)
}

// newTemplateOptions carries the options supported by newTemplate
type newTemplateOptions struct {
provider config.Provider
version string
flavor string
rawYaml []byte
configVariablesClient config.VariablesClient
targetNamespace string
}

// newTemplate returns a new objects embedding a cluster template YAML file.
func newTemplate(options newTemplateOptions) (*template, error) {
// NewTemplate returns a new objects embedding a cluster template YAML file.
func NewTemplate(rawYaml []byte, configVariablesClient config.VariablesClient, targetNamespace string) (*template, error) {
// Inspect variables and replace with values from the configuration.
variables := inspectVariables(options.rawYaml)
variables := inspectVariables(rawYaml)

yaml, err := replaceVariables(options.rawYaml, variables, options.configVariablesClient)
yaml, err := replaceVariables(rawYaml, variables, configVariablesClient)
if err != nil {
return nil, errors.Wrap(err, "failed to perform variable substitution")
}
Expand All @@ -119,14 +88,11 @@ func newTemplate(options newTemplateOptions) (*template, error) {
// Ensures all the template components are deployed in the target namespace (applies only to namespaced objects)
// This is required in order to ensure a cluster and all the related objects are in a single namespace, that is a requirement for
// the clusterctl move operation (and also for many controller reconciliation loops).
objs = fixTargetNamespace(objs, options.targetNamespace)
objs = fixTargetNamespace(objs, targetNamespace)

return &template{
Provider: options.provider,
version: options.version,
flavor: options.flavor,
variables: variables,
targetNamespace: options.targetNamespace,
targetNamespace: targetNamespace,
objs: objs,
}, nil
}
9 changes: 1 addition & 8 deletions cmd/clusterctl/pkg/client/repository/template_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,5 @@ func (c *templateClient) Get(flavor, targetNamespace string) (Template, error) {
}
}

return newTemplate(newTemplateOptions{
provider: c.provider,
version: version,
flavor: flavor,
rawYaml: rawYaml,
configVariablesClient: c.configVariablesClient,
targetNamespace: targetNamespace,
})
return NewTemplate(rawYaml, c.configVariablesClient, targetNamespace)
}
21 changes: 0 additions & 21 deletions cmd/clusterctl/pkg/client/repository/template_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ func Test_templates_Get(t *testing.T) {
targetNamespace string
}
type want struct {
provider config.Provider
version string
flavor string
variables []string
targetNamespace string
}
Expand All @@ -70,9 +67,6 @@ func Test_templates_Get(t *testing.T) {
targetNamespace: "ns1",
},
want: want{
provider: p1,
version: "v1.0",
flavor: "",
variables: []string{variableName},
targetNamespace: "ns1",
},
Expand All @@ -94,9 +88,6 @@ func Test_templates_Get(t *testing.T) {
targetNamespace: "ns1",
},
want: want{
provider: p1,
version: "v1.0",
flavor: "prod",
variables: []string{variableName},
targetNamespace: "ns1",
},
Expand Down Expand Up @@ -130,18 +121,6 @@ func Test_templates_Get(t *testing.T) {
return
}

if got.Name() != tt.want.provider.Name() {
t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name())
}

if got.Type() != tt.want.provider.Type() {
t.Errorf("got.Type() = %v, want = %v ", got.Type(), tt.want.provider.Type())
}

if got.Version() != tt.want.version {
t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version)
}

if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
t.Errorf("got.Variables() = %v, want = %v ", got.Variables(), tt.want.variables)
}
Expand Down
36 changes: 1 addition & 35 deletions cmd/clusterctl/pkg/client/repository/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"reflect"
"testing"

clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/test"
)
Expand All @@ -37,20 +36,12 @@ var templateMapYaml = []byte("apiVersion: v1\n" +
" name: manager")

func Test_newTemplate(t *testing.T) {
p1 := config.NewProvider("p1", "", clusterctlv1.BootstrapProviderType)

type args struct {
provider config.Provider
version string
flavor string
rawYaml []byte
configVariablesClient config.VariablesClient
targetNamespace string
}
type want struct {
provider config.Provider
version string
flavor string
variables []string
targetNamespace string
}
Expand All @@ -63,17 +54,11 @@ func Test_newTemplate(t *testing.T) {
{
name: "variable is replaced and namespace fixed",
args: args{
provider: p1,
version: "v1.2.3",
flavor: "flavor",
rawYaml: templateMapYaml,
configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue),
targetNamespace: "ns1",
},
want: want{
provider: p1,
version: "v1.2.3",
flavor: "flavor",
variables: []string{variableName},
targetNamespace: "ns1",
},
Expand All @@ -82,33 +67,14 @@ func Test_newTemplate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := newTemplate(newTemplateOptions{
provider: tt.args.provider,
version: tt.args.version,
flavor: tt.args.flavor,
rawYaml: tt.args.rawYaml,
configVariablesClient: tt.args.configVariablesClient,
targetNamespace: tt.args.targetNamespace,
})
got, err := NewTemplate(tt.args.rawYaml, tt.args.configVariablesClient, tt.args.targetNamespace)
if (err != nil) != tt.wantErr {
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return
}

if got.Name() != tt.want.provider.Name() {
t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name())
}

if got.Type() != tt.want.provider.Type() {
t.Errorf("got.Type() = %v, want = %v ", got.Type(), tt.want.provider.Type())
}

if got.Version() != tt.want.version {
t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version)
}

if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
t.Errorf("got.Variables() = %v, want = %v ", got.Variables(), tt.want.variables)
}
Expand Down

0 comments on commit 12ddd37

Please sign in to comment.