diff --git a/pkg/cluster/config/deepcopy.go b/pkg/cluster/config/deepcopy.go deleted file mode 100644 index ac1e209c0e..0000000000 --- a/pkg/cluster/config/deepcopy.go +++ /dev/null @@ -1,85 +0,0 @@ -/* -Copyright 2018 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 config - -// TODO(bentheelder): consider kubernetes deep-copy gen -// In the meantime the pattern is: -// - handle nil receiver -// - create a new(OutType) -// - *out = *in to copy plain fields -// - copy pointer fields by calling their DeepCopy -// - copy slices / maps by allocating a new one and performing a copy loop - -// DeepCopy returns a deep copy -func (in *Config) DeepCopy() *Config { - if in == nil { - return nil - } - out := new(Config) - *out = *in - out.NodeLifecycle = in.NodeLifecycle.DeepCopy() - return out -} - -// DeepCopy returns a deep copy -func (in *NodeLifecycle) DeepCopy() *NodeLifecycle { - if in == nil { - return nil - } - out := new(NodeLifecycle) - if in.PreBoot != nil { - out.PreBoot = make([]LifecycleHook, len(in.PreBoot)) - for i := range in.PreBoot { - out.PreBoot[i] = *(in.PreBoot[i].DeepCopy()) - } - } - if in.PreKubeadm != nil { - out.PreKubeadm = make([]LifecycleHook, len(in.PreKubeadm)) - for i := range in.PreKubeadm { - out.PreKubeadm[i] = *(in.PreKubeadm[i].DeepCopy()) - } - } - if in.PostKubeadm != nil { - out.PostKubeadm = make([]LifecycleHook, len(in.PostKubeadm)) - for i := range in.PostKubeadm { - out.PostKubeadm[i] = *(in.PostKubeadm[i].DeepCopy()) - } - } - if in.PostSetup != nil { - out.PostSetup = make([]LifecycleHook, len(in.PostSetup)) - for i := range in.PostSetup { - out.PostSetup[i] = *(in.PostSetup[i].DeepCopy()) - } - } - return out -} - -// DeepCopy returns a deep copy -func (in *LifecycleHook) DeepCopy() *LifecycleHook { - if in == nil { - return nil - } - out := new(LifecycleHook) - *out = *in - if in.Command != nil { - out.Command = make([]string, len(in.Command)) - for i := range in.Command { - out.Command[i] = in.Command[i] - } - } - return out -} diff --git a/pkg/cluster/config/deepcopy_test.go b/pkg/cluster/config/deepcopy_test.go deleted file mode 100644 index e2686c8bfe..0000000000 --- a/pkg/cluster/config/deepcopy_test.go +++ /dev/null @@ -1,77 +0,0 @@ -/* -Copyright 2018 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 config - -import ( - "reflect" - "testing" -) - -func TestDeepCopy(t *testing.T) { - cases := []struct { - TestName string - Config *Config - }{ - { - TestName: "Canonical config", - Config: New(), - }, - { - TestName: "Config with NodeLifecyle hooks", - Config: func() *Config { - cfg := New() - cfg.NodeLifecycle = &NodeLifecycle{ - PreBoot: []LifecycleHook{ - { - Command: []string{"ps", "aux"}, - }, - }, - PreKubeadm: []LifecycleHook{ - { - Name: "docker ps", - Command: []string{"docker", "ps"}, - }, - }, - PostKubeadm: []LifecycleHook{ - { - Name: "docker ps again", - Command: []string{"docker", "ps", "-a"}, - }, - }, - PostSetup: []LifecycleHook{ - { - Name: "docker ps again again", - Command: []string{"docker", "ps", "-a"}, - MustSucceed: true, - }, - }, - } - return cfg - }(), - }, - } - for _, tc := range cases { - original := tc.Config - deepCopy := tc.Config.DeepCopy() - if !reflect.DeepEqual(original, deepCopy) { - t.Errorf( - "case: '%s' deep copy did not equal original: %+v != %+v", - tc.TestName, original, deepCopy, - ) - } - } -} diff --git a/pkg/cluster/config/default.go b/pkg/cluster/config/default.go index ef002bfe47..e795a8b133 100644 --- a/pkg/cluster/config/default.go +++ b/pkg/cluster/config/default.go @@ -22,7 +22,7 @@ import ( // New returns a new default Config func addDefaultingFuncs(scheme *runtime.Scheme) error { - return RegisterDefaults(scheme) + return RegisterDefaults(scheme) } func SetDefaults_Config(obj *Config) { @@ -34,3 +34,13 @@ func SetDefaults_Config(obj *Config) { obj.NumNodes = 1 } } + +func (c *Config) ApplyDefaults() { + SetDefaults_Config(c) +} + +func New() *Config { + cfg := &Config{} + cfg.ApplyDefaults() + return cfg +} diff --git a/pkg/cluster/config/encoding/encoding.go b/pkg/cluster/config/encoding/encoding.go index 62c841e84e..5c60af9cdd 100644 --- a/pkg/cluster/config/encoding/encoding.go +++ b/pkg/cluster/config/encoding/encoding.go @@ -73,8 +73,8 @@ func detectVersion(raw []byte) (version string, err error) { } switch c.APIVersion { // default to the current api version if unspecified, or explicitly specified - case config.APIVersion, "": - return config.APIVersion, nil + case config.SchemeGroupVersion.String(), "": + return config.SchemeGroupVersion.String(), nil } return "", fmt.Errorf("invalid version: %v", c.APIVersion) } @@ -92,7 +92,7 @@ func Unmarshal(raw []byte) (config.Any, error) { // load version var cfg config.Any switch version { - case config.APIVersion: + case config.SchemeGroupVersion.String(): cfg = &config.Config{} } err = yaml.Unmarshal(raw, cfg) diff --git a/pkg/cluster/config/version.go b/pkg/cluster/config/version.go new file mode 100644 index 0000000000..c3c4d5e058 --- /dev/null +++ b/pkg/cluster/config/version.go @@ -0,0 +1,33 @@ +/* +Copyright 2018 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 config + +// APIVersion is the kubernetes-style API apiVersion for this Config package +const APIVersion = "kind.sigs.k8s.io/v1alpha1" + +// ConfigKind is the kubernetes-style API kind identifier for Config +const ConfigKind = "Config" + +// Kind returns the `kind:` for Config +func (c *Config) Kind() string { + return ConfigKind +} + +// APIVersion returns the `apiVersion:` for Config +func (c *Config) APIVersion() string { + return SchemeGroupVersion.String() +}