diff --git a/pkg/model/components/apiserver.go b/pkg/model/components/apiserver.go index a0f17052d1a53..488e40f56fdaa 100644 --- a/pkg/model/components/apiserver.go +++ b/pkg/model/components/apiserver.go @@ -34,11 +34,11 @@ type KubeAPIServerOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &KubeAPIServerOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &KubeAPIServerOptionsBuilder{} // BuildOptions is responsible for filling in the default settings for the kube apiserver -func (b *KubeAPIServerOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *KubeAPIServerOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.KubeAPIServer == nil { clusterSpec.KubeAPIServer = &kops.KubeAPIServerConfig{} } diff --git a/pkg/model/components/aws.go b/pkg/model/components/aws.go index b21bbb15acdc4..d823fa0d68391 100644 --- a/pkg/model/components/aws.go +++ b/pkg/model/components/aws.go @@ -26,10 +26,10 @@ type AWSOptionsBuilder struct { OptionsContext *OptionsContext } -var _ loader.OptionsBuilder = &AWSOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &AWSOptionsBuilder{} -func (b *AWSOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *AWSOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec aws := clusterSpec.CloudProvider.AWS if aws == nil { return nil diff --git a/pkg/model/components/awscloudcontrollermanager.go b/pkg/model/components/awscloudcontrollermanager.go index 159a30a714b44..9333cc1643e5e 100644 --- a/pkg/model/components/awscloudcontrollermanager.go +++ b/pkg/model/components/awscloudcontrollermanager.go @@ -29,11 +29,11 @@ type AWSCloudControllerManagerOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &AWSCloudControllerManagerOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &AWSCloudControllerManagerOptionsBuilder{} // BuildOptions generates the configurations used for the AWS cloud controller manager manifest -func (b *AWSCloudControllerManagerOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *AWSCloudControllerManagerOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.GetCloudProvider() != kops.CloudProviderAWS { return nil diff --git a/pkg/model/components/awsebscsidriver.go b/pkg/model/components/awsebscsidriver.go index 8576814fff101..bb6dff02b0b64 100644 --- a/pkg/model/components/awsebscsidriver.go +++ b/pkg/model/components/awsebscsidriver.go @@ -27,10 +27,10 @@ type AWSEBSCSIDriverOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &AWSEBSCSIDriverOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &AWSEBSCSIDriverOptionsBuilder{} -func (b *AWSEBSCSIDriverOptionsBuilder) BuildOptions(o interface{}) error { - aws := o.(*kops.ClusterSpec).CloudProvider.AWS +func (b *AWSEBSCSIDriverOptionsBuilder) BuildOptions(o *kops.Cluster) error { + aws := o.Spec.CloudProvider.AWS if aws == nil { return nil } diff --git a/pkg/model/components/calico.go b/pkg/model/components/calico.go index dd5aaab8d9644..c94ca44e4ada7 100644 --- a/pkg/model/components/calico.go +++ b/pkg/model/components/calico.go @@ -26,10 +26,10 @@ type CalicoOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &CalicoOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &CalicoOptionsBuilder{} -func (b *CalicoOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *CalicoOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec c := clusterSpec.Networking.Calico if c == nil { return nil diff --git a/pkg/model/components/cilium.go b/pkg/model/components/cilium.go index 95d16c2448602..eb3c38e87df0e 100644 --- a/pkg/model/components/cilium.go +++ b/pkg/model/components/cilium.go @@ -30,10 +30,10 @@ type CiliumOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &CiliumOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &CiliumOptionsBuilder{} -func (b *CiliumOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *CiliumOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec c := clusterSpec.Networking.Cilium if c == nil { return nil diff --git a/pkg/model/components/cloudconfiguration.go b/pkg/model/components/cloudconfiguration.go index b0b06a2cd1d93..e18f4ab1cc808 100644 --- a/pkg/model/components/cloudconfiguration.go +++ b/pkg/model/components/cloudconfiguration.go @@ -27,10 +27,10 @@ type CloudConfigurationOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &CloudConfigurationOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &CloudConfigurationOptionsBuilder{} -func (b *CloudConfigurationOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *CloudConfigurationOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec c := clusterSpec.CloudConfig if c == nil { c = &kops.CloudConfiguration{} diff --git a/pkg/model/components/cloudconfiguration_test.go b/pkg/model/components/cloudconfiguration_test.go index 4247a59c48ffe..b3dda7d59a8b5 100644 --- a/pkg/model/components/cloudconfiguration_test.go +++ b/pkg/model/components/cloudconfiguration_test.go @@ -93,7 +93,8 @@ func TestCloudConfigurationOptionsBuilder(t *testing.T) { }, } { t.Run(test.description, func(t *testing.T) { - spec := kopsapi.ClusterSpec{ + cluster := &kopsapi.Cluster{} + cluster.Spec = kopsapi.ClusterSpec{ CloudConfig: &kopsapi.CloudConfiguration{}, CloudProvider: kopsapi.CloudProviderSpec{ Openstack: &kopsapi.OpenstackSpec{ @@ -102,15 +103,15 @@ func TestCloudConfigurationOptionsBuilder(t *testing.T) { }, } if p := test.generalManageSCs; p != nil { - spec.CloudConfig.ManageStorageClasses = p + cluster.Spec.CloudConfig.ManageStorageClasses = p } if p := test.openStackManageSCs; p != nil { - spec.CloudProvider.Openstack.BlockStorage.CreateStorageClass = p + cluster.Spec.CloudProvider.Openstack.BlockStorage.CreateStorageClass = p } - if err := ob.BuildOptions(&spec); err != nil { + if err := ob.BuildOptions(cluster); err != nil { t.Fatalf("failed to build options: %v", err) } - if want, got := test.expectedGeneralManageSCs, spec.CloudConfig.ManageStorageClasses; (want == nil) != (got == nil) || (got != nil && *got != *want) { + if want, got := test.expectedGeneralManageSCs, cluster.Spec.CloudConfig.ManageStorageClasses; (want == nil) != (got == nil) || (got != nil && *got != *want) { switch { case want == nil: t.Errorf("spec.cloudConfig.manageStorageClasses: want nil, got %t", *got) diff --git a/pkg/model/components/clusterautoscaler.go b/pkg/model/components/clusterautoscaler.go index 36e9adbd0437e..64e49f1edfebd 100644 --- a/pkg/model/components/clusterautoscaler.go +++ b/pkg/model/components/clusterautoscaler.go @@ -28,10 +28,10 @@ type ClusterAutoscalerOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &ClusterAutoscalerOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &ClusterAutoscalerOptionsBuilder{} -func (b *ClusterAutoscalerOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *ClusterAutoscalerOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec cas := clusterSpec.ClusterAutoscaler if cas == nil || !fi.ValueOf(cas.Enabled) { return nil diff --git a/pkg/model/components/containerd.go b/pkg/model/components/containerd.go index 47487e8948655..e62ae97b05310 100644 --- a/pkg/model/components/containerd.go +++ b/pkg/model/components/containerd.go @@ -27,11 +27,11 @@ type ContainerdOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &ContainerdOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &ContainerdOptionsBuilder{} // BuildOptions is responsible for filling in the default setting for containerd daemon -func (b *ContainerdOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *ContainerdOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.Containerd == nil { clusterSpec.Containerd = &kops.ContainerdConfig{} diff --git a/pkg/model/components/containerd_test.go b/pkg/model/components/containerd_test.go index f9dce8905d646..fc472f90279a9 100644 --- a/pkg/model/components/containerd_test.go +++ b/pkg/model/components/containerd_test.go @@ -59,7 +59,7 @@ func Test_Build_Containerd_Supported_Version(t *testing.T) { }, } - err = ob.BuildOptions(&c.Spec) + err = ob.BuildOptions(c) if err != nil { t.Fatalf("unexpected error from BuildOptions: %v", err) } diff --git a/pkg/model/components/defaults.go b/pkg/model/components/defaults.go index 47f319aea01ae..d58c7a5e0ec46 100644 --- a/pkg/model/components/defaults.go +++ b/pkg/model/components/defaults.go @@ -26,11 +26,11 @@ type DefaultsOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &DefaultsOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &DefaultsOptionsBuilder{} // BuildOptions is responsible for cluster options -func (b *DefaultsOptionsBuilder) BuildOptions(o interface{}) error { - options := o.(*kops.ClusterSpec) +func (b *DefaultsOptionsBuilder) BuildOptions(o *kops.Cluster) error { + options := &o.Spec if options.ClusterDNSDomain == "" { options.ClusterDNSDomain = "cluster.local" diff --git a/pkg/model/components/discovery.go b/pkg/model/components/discovery.go index f6c4995cc486b..5494813b530d5 100644 --- a/pkg/model/components/discovery.go +++ b/pkg/model/components/discovery.go @@ -31,10 +31,10 @@ type DiscoveryOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &DiscoveryOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &DiscoveryOptionsBuilder{} -func (b *DiscoveryOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *DiscoveryOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.KubeAPIServer == nil { clusterSpec.KubeAPIServer = &kops.KubeAPIServerConfig{} diff --git a/pkg/model/components/etcd.go b/pkg/model/components/etcd.go index 254c6c9c8aeaf..0ac503ef9e2ae 100644 --- a/pkg/model/components/etcd.go +++ b/pkg/model/components/etcd.go @@ -26,15 +26,15 @@ type EtcdOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &EtcdOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &EtcdOptionsBuilder{} const ( DefaultEtcd3Version_1_22 = "3.5.13" ) // BuildOptions is responsible for filling in the defaults for the etcd cluster model -func (b *EtcdOptionsBuilder) BuildOptions(o interface{}) error { - spec := o.(*kops.ClusterSpec) +func (b *EtcdOptionsBuilder) BuildOptions(o *kops.Cluster) error { + spec := &o.Spec for i := range spec.EtcdClusters { c := &spec.EtcdClusters[i] diff --git a/pkg/model/components/etcdmanager/options.go b/pkg/model/components/etcdmanager/options.go index 1b6b4039faaff..595fae80f2247 100644 --- a/pkg/model/components/etcdmanager/options.go +++ b/pkg/model/components/etcdmanager/options.go @@ -34,11 +34,11 @@ type EtcdManagerOptionsBuilder struct { *components.OptionsContext } -var _ loader.OptionsBuilder = &EtcdManagerOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &EtcdManagerOptionsBuilder{} // BuildOptions generates the configurations used to create etcd manager manifest -func (b *EtcdManagerOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *EtcdManagerOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec for i := range clusterSpec.EtcdClusters { etcdCluster := &clusterSpec.EtcdClusters[i] diff --git a/pkg/model/components/gcpcloudcontrollermanager.go b/pkg/model/components/gcpcloudcontrollermanager.go index d9ca176c7e6a9..2e304b89d98aa 100644 --- a/pkg/model/components/gcpcloudcontrollermanager.go +++ b/pkg/model/components/gcpcloudcontrollermanager.go @@ -27,10 +27,10 @@ type GCPCloudControllerManagerOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = (*GCPCloudControllerManagerOptionsBuilder)(nil) +var _ loader.ClusterOptionsBuilder = (*GCPCloudControllerManagerOptionsBuilder)(nil) -func (b *GCPCloudControllerManagerOptionsBuilder) BuildOptions(options interface{}) error { - clusterSpec := options.(*kops.ClusterSpec) +func (b *GCPCloudControllerManagerOptionsBuilder) BuildOptions(options *kops.Cluster) error { + clusterSpec := &options.Spec if clusterSpec.GetCloudProvider() != kops.CloudProviderGCE { return nil diff --git a/pkg/model/components/gcppdcsidriver.go b/pkg/model/components/gcppdcsidriver.go index e86e8ea25f119..a055a39daa5a3 100644 --- a/pkg/model/components/gcppdcsidriver.go +++ b/pkg/model/components/gcppdcsidriver.go @@ -27,10 +27,10 @@ type GCPPDCSIDriverOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &GCPPDCSIDriverOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &GCPPDCSIDriverOptionsBuilder{} -func (b *GCPPDCSIDriverOptionsBuilder) BuildOptions(o interface{}) error { - gce := o.(*kops.ClusterSpec).CloudProvider.GCE +func (b *GCPPDCSIDriverOptionsBuilder) BuildOptions(o *kops.Cluster) error { + gce := o.Spec.CloudProvider.GCE if gce == nil { return nil } diff --git a/pkg/model/components/hetznercloudcontrollermanager.go b/pkg/model/components/hetznercloudcontrollermanager.go index 4818d390339e7..4c33ec8a1d6bb 100644 --- a/pkg/model/components/hetznercloudcontrollermanager.go +++ b/pkg/model/components/hetznercloudcontrollermanager.go @@ -27,11 +27,11 @@ type HetznerCloudControllerManagerOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &HetznerCloudControllerManagerOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &HetznerCloudControllerManagerOptionsBuilder{} // BuildOptions generates the configurations used for the Hetzner cloud controller manager manifest -func (b *HetznerCloudControllerManagerOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *HetznerCloudControllerManagerOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.GetCloudProvider() != kops.CloudProviderHetzner { return nil diff --git a/pkg/model/components/karpenter.go b/pkg/model/components/karpenter.go index e18bc714a031a..c49df14b8bea5 100644 --- a/pkg/model/components/karpenter.go +++ b/pkg/model/components/karpenter.go @@ -26,10 +26,10 @@ type KarpenterOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &KarpenterOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &KarpenterOptionsBuilder{} -func (b *KarpenterOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *KarpenterOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec c := clusterSpec.Karpenter if c == nil { return nil diff --git a/pkg/model/components/kubecontrollermanager.go b/pkg/model/components/kubecontrollermanager.go index 5d142c9f3e847..64ae3e9364e02 100644 --- a/pkg/model/components/kubecontrollermanager.go +++ b/pkg/model/components/kubecontrollermanager.go @@ -38,11 +38,11 @@ type KubeControllerManagerOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &KubeControllerManagerOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &KubeControllerManagerOptionsBuilder{} // BuildOptions generates the configurations used to create kubernetes controller manager manifest -func (b *KubeControllerManagerOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *KubeControllerManagerOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.KubeControllerManager == nil { clusterSpec.KubeControllerManager = &kops.KubeControllerManagerConfig{} } diff --git a/pkg/model/components/kubecontrollermanager_test.go b/pkg/model/components/kubecontrollermanager_test.go index 9abd5b263c9bd..752baec277168 100644 --- a/pkg/model/components/kubecontrollermanager_test.go +++ b/pkg/model/components/kubecontrollermanager_test.go @@ -55,7 +55,7 @@ func Test_Build_KCM_Builder(t *testing.T) { }, } - err := kcm.BuildOptions(&c.Spec) + err := kcm.BuildOptions(c) if err != nil { t.Fatalf("unexpected error from BuildOptions %s", err) } @@ -82,7 +82,7 @@ func Test_Build_KCM_Builder_Change_Duration(t *testing.T) { c.Spec.KubeControllerManager.AttachDetachReconcileSyncPeriod.Duration = time.Minute * 5 - err := kcm.BuildOptions(&c.Spec) + err := kcm.BuildOptions(c) if err != nil { t.Fatalf("unexpected error from BuildOptions %s", err) } @@ -156,7 +156,7 @@ func Test_Build_KCM_Builder_CIDR_Mask_Size(t *testing.T) { ClusterCIDR: tc.ClusterCIDR, } - err := kcm.BuildOptions(&c.Spec) + err := kcm.BuildOptions(c) require.NoError(t, err) assert.Equal(t, tc.ExpectedMaskSize, c.Spec.KubeControllerManager.NodeCIDRMaskSize) diff --git a/pkg/model/components/kubedns.go b/pkg/model/components/kubedns.go index 2c2a0bb93176f..3d2eb339c4061 100644 --- a/pkg/model/components/kubedns.go +++ b/pkg/model/components/kubedns.go @@ -29,11 +29,11 @@ type KubeDnsOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &KubeDnsOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &KubeDnsOptionsBuilder{} // BuildOptions fills in the kubedns model -func (b *KubeDnsOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *KubeDnsOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.KubeDNS == nil { clusterSpec.KubeDNS = &kops.KubeDNSConfig{} diff --git a/pkg/model/components/kubelet.go b/pkg/model/components/kubelet.go index e1b55031f7b45..11fb19f4fc34c 100644 --- a/pkg/model/components/kubelet.go +++ b/pkg/model/components/kubelet.go @@ -34,11 +34,11 @@ type KubeletOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &KubeletOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &KubeletOptionsBuilder{} // BuildOptions is responsible for filling the defaults for the kubelet -func (b *KubeletOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *KubeletOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.Kubelet == nil { clusterSpec.Kubelet = &kops.KubeletConfigSpec{} diff --git a/pkg/model/components/kubelet_test.go b/pkg/model/components/kubelet_test.go index 233cee1b32999..ad01c61cc3905 100644 --- a/pkg/model/components/kubelet_test.go +++ b/pkg/model/components/kubelet_test.go @@ -52,7 +52,7 @@ func buildOptions(cluster *kops.Cluster) error { }, } - err = builder.BuildOptions(&cluster.Spec) + err = builder.BuildOptions(cluster) if err != nil { return nil } diff --git a/pkg/model/components/kubeproxy.go b/pkg/model/components/kubeproxy.go index b35035714cb94..5fbc25ce788f9 100644 --- a/pkg/model/components/kubeproxy.go +++ b/pkg/model/components/kubeproxy.go @@ -29,10 +29,10 @@ type KubeProxyOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &KubeProxyOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &KubeProxyOptionsBuilder{} -func (b *KubeProxyOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *KubeProxyOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.KubeProxy == nil { clusterSpec.KubeProxy = &kops.KubeProxyConfig{} } diff --git a/pkg/model/components/kubescheduler.go b/pkg/model/components/kubescheduler.go index dab0591d4b031..229cbc466358f 100644 --- a/pkg/model/components/kubescheduler.go +++ b/pkg/model/components/kubescheduler.go @@ -27,10 +27,10 @@ type KubeSchedulerOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &KubeSchedulerOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &KubeSchedulerOptionsBuilder{} -func (b *KubeSchedulerOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *KubeSchedulerOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.KubeScheduler == nil { clusterSpec.KubeScheduler = &kops.KubeSchedulerConfig{} } diff --git a/pkg/model/components/kubescheduler_test.go b/pkg/model/components/kubescheduler_test.go index a73c7a61636e9..2e563175be084 100644 --- a/pkg/model/components/kubescheduler_test.go +++ b/pkg/model/components/kubescheduler_test.go @@ -45,7 +45,7 @@ func Test_Build_Scheduler(t *testing.T) { }, } - err = ks.BuildOptions(&c.Spec) + err = ks.BuildOptions(c) if err != nil { t.Fatalf("unexpected error from BuildOptions: %v", err) diff --git a/pkg/model/components/networking.go b/pkg/model/components/networking.go index 00e5964c1b4c6..78171b89c7da8 100644 --- a/pkg/model/components/networking.go +++ b/pkg/model/components/networking.go @@ -28,14 +28,12 @@ type NetworkingOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &NetworkingOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &NetworkingOptionsBuilder{} -func (b *NetworkingOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) - - options := o.(*kops.ClusterSpec) - if options.Kubelet == nil { - options.Kubelet = &kops.KubeletConfigSpec{} +func (b *NetworkingOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec + if clusterSpec.Kubelet == nil { + clusterSpec.Kubelet = &kops.KubeletConfigSpec{} } networking := &clusterSpec.Networking diff --git a/pkg/model/components/nodeproblemdetector.go b/pkg/model/components/nodeproblemdetector.go index 03b93b1a7df34..8287b5ef7837c 100644 --- a/pkg/model/components/nodeproblemdetector.go +++ b/pkg/model/components/nodeproblemdetector.go @@ -28,10 +28,10 @@ type NodeProblemDetectorOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &NodeProblemDetectorOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &NodeProblemDetectorOptionsBuilder{} -func (b *NodeProblemDetectorOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *NodeProblemDetectorOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.NodeProblemDetector == nil { return nil } diff --git a/pkg/model/components/nodeterminationhandler.go b/pkg/model/components/nodeterminationhandler.go index eaec602d72d05..cf04bf858e96c 100644 --- a/pkg/model/components/nodeterminationhandler.go +++ b/pkg/model/components/nodeterminationhandler.go @@ -28,10 +28,10 @@ type NodeTerminationHandlerOptionsBuilder struct { *OptionsContext } -var _ loader.OptionsBuilder = &NodeTerminationHandlerOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &NodeTerminationHandlerOptionsBuilder{} -func (b *NodeTerminationHandlerOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *NodeTerminationHandlerOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec if clusterSpec.CloudProvider.AWS == nil { return nil } diff --git a/pkg/model/components/openstack.go b/pkg/model/components/openstack.go index caaa82a459434..3d929a2bf1959 100644 --- a/pkg/model/components/openstack.go +++ b/pkg/model/components/openstack.go @@ -30,10 +30,10 @@ type OpenStackOptionsBuilder struct { Context *OptionsContext } -var _ loader.OptionsBuilder = &OpenStackOptionsBuilder{} +var _ loader.ClusterOptionsBuilder = &OpenStackOptionsBuilder{} -func (b *OpenStackOptionsBuilder) BuildOptions(o interface{}) error { - clusterSpec := o.(*kops.ClusterSpec) +func (b *OpenStackOptionsBuilder) BuildOptions(o *kops.Cluster) error { + clusterSpec := &o.Spec openstack := clusterSpec.CloudProvider.Openstack if openstack == nil { diff --git a/upup/pkg/fi/cloudup/populate_cluster_spec.go b/upup/pkg/fi/cloudup/populate_cluster_spec.go index 1111d3404a7a6..3274c4936e994 100644 --- a/upup/pkg/fi/cloudup/populate_cluster_spec.go +++ b/upup/pkg/fi/cloudup/populate_cluster_spec.go @@ -306,7 +306,7 @@ func (c *populateClusterSpec) run(ctx context.Context, clientset simple.Clientse AssetBuilder: c.assetBuilder, } - var codeModels []loader.OptionsBuilder + var codeModels []loader.ClusterOptionsBuilder { { // Note: DefaultOptionsBuilder comes first @@ -340,27 +340,23 @@ func (c *populateClusterSpec) run(ctx context.Context, clientset simple.Clientse } specBuilder := &SpecBuilder{ - OptionsLoader: loader.NewOptionsLoader(codeModels), + OptionsLoader: loader.NewClusterOptionsLoader(codeModels), } - completed, err := specBuilder.BuildCompleteSpec(&cluster.Spec) + completed, err := specBuilder.BuildCompleteSpec(cluster) if err != nil { return fmt.Errorf("error building complete spec: %v", err) } // TODO: This should not be needed... - completed.Networking.Topology = c.InputCluster.Spec.Networking.Topology + completed.Spec.Networking.Topology = c.InputCluster.Spec.Networking.Topology // completed.Topology.Bastion = c.InputCluster.Spec.Topology.Bastion - fullCluster := &kopsapi.Cluster{} - *fullCluster = *cluster - fullCluster.Spec = *completed - - if errs := validation.ValidateCluster(fullCluster, true, clientset.VFSContext()); len(errs) != 0 { + if errs := validation.ValidateCluster(completed, true, clientset.VFSContext()); len(errs) != 0 { return fmt.Errorf("completed cluster failed validation: %v", errs.ToAggregate()) } - c.fullCluster = fullCluster + c.fullCluster = completed return nil } diff --git a/upup/pkg/fi/cloudup/spec_builder.go b/upup/pkg/fi/cloudup/spec_builder.go index fd6abfdad763b..a1e797287fc65 100644 --- a/upup/pkg/fi/cloudup/spec_builder.go +++ b/upup/pkg/fi/cloudup/spec_builder.go @@ -25,22 +25,22 @@ import ( ) type SpecBuilder struct { - OptionsLoader *loader.OptionsLoader + OptionsLoader *loader.OptionsLoader[*kopsapi.Cluster] } -func (l *SpecBuilder) BuildCompleteSpec(clusterSpec *kopsapi.ClusterSpec) (*kopsapi.ClusterSpec, error) { - loaded, err := l.OptionsLoader.Build(clusterSpec) +func (l *SpecBuilder) BuildCompleteSpec(cluster *kopsapi.Cluster) (*kopsapi.Cluster, error) { + loaded, err := l.OptionsLoader.Build(cluster) if err != nil { return nil, err } - completed := &kopsapi.ClusterSpec{} - *completed = *(loaded.(*kopsapi.ClusterSpec)) + completed := &kopsapi.Cluster{} + *completed = *loaded // Control-plane kubelet config = (base kubelet config + control-plane kubelet config) controlPlaneKubelet := &kopsapi.KubeletConfigSpec{} - reflectutils.JSONMergeStruct(controlPlaneKubelet, completed.Kubelet) - reflectutils.JSONMergeStruct(controlPlaneKubelet, completed.ControlPlaneKubelet) - completed.ControlPlaneKubelet = controlPlaneKubelet + reflectutils.JSONMergeStruct(controlPlaneKubelet, completed.Spec.Kubelet) + reflectutils.JSONMergeStruct(controlPlaneKubelet, completed.Spec.ControlPlaneKubelet) + completed.Spec.ControlPlaneKubelet = controlPlaneKubelet klog.V(1).Infof("options: %s", fi.DebugAsJsonStringIndent(completed)) return completed, nil diff --git a/upup/pkg/fi/loader/options_loader.go b/upup/pkg/fi/loader/options_loader.go index a80879673660f..ca665a6d1e94b 100644 --- a/upup/pkg/fi/loader/options_loader.go +++ b/upup/pkg/fi/loader/options_loader.go @@ -21,30 +21,35 @@ import ( "reflect" "k8s.io/klog/v2" + "k8s.io/kops/pkg/apis/kops" "k8s.io/kops/util/pkg/reflectutils" ) const maxIterations = 10 -type OptionsLoader struct { - Builders []OptionsBuilder +type OptionsLoader[T any] struct { + Builders []OptionsBuilder[T] } -type OptionsBuilder interface { - BuildOptions(options interface{}) error +type OptionsBuilder[T any] interface { + BuildOptions(options T) error } -func NewOptionsLoader(builders []OptionsBuilder) *OptionsLoader { - l := &OptionsLoader{} - l.Builders = builders +type ClusterOptionsBuilder OptionsBuilder[*kops.Cluster] + +func NewClusterOptionsLoader(builders []ClusterOptionsBuilder) *OptionsLoader[*kops.Cluster] { + l := &OptionsLoader[*kops.Cluster]{} + for _, b := range builders { + l.Builders = append(l.Builders, b) + } return l } // iterate performs a single iteration of all the templates, executing each template in order -func (l *OptionsLoader) iterate(userConfig interface{}, current interface{}) (interface{}, error) { +func (l *OptionsLoader[T]) iterate(userConfig T, current T) (T, error) { t := reflect.TypeOf(current).Elem() - next := reflect.New(t).Interface() + next := reflect.New(t).Interface().(T) // Copy the current state before applying rules; they act as defaults reflectutils.JSONMergeStruct(next, current) @@ -54,7 +59,8 @@ func (l *OptionsLoader) iterate(userConfig interface{}, current interface{}) (in err := t.BuildOptions(next) if err != nil { - return nil, err + var defaultT T + return defaultT, err } } @@ -66,13 +72,14 @@ func (l *OptionsLoader) iterate(userConfig interface{}, current interface{}) (in // Build executes the options configuration templates, until they converge // It bails out after maxIterations -func (l *OptionsLoader) Build(userConfig interface{}) (interface{}, error) { +func (l *OptionsLoader[T]) Build(userConfig T) (T, error) { + var defaultT T options := userConfig iteration := 0 for { nextOptions, err := l.iterate(userConfig, options) if err != nil { - return nil, err + return defaultT, err } if reflect.DeepEqual(options, nextOptions) { @@ -81,7 +88,7 @@ func (l *OptionsLoader) Build(userConfig interface{}) (interface{}, error) { iteration++ if iteration > maxIterations { - return nil, fmt.Errorf("options did not converge after %d iterations", maxIterations) + return defaultT, fmt.Errorf("options did not converge after %d iterations", maxIterations) } options = nextOptions