Skip to content

Commit

Permalink
Move OS specific cmds to snow provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayiwang7 committed May 31, 2022
1 parent 8e4e1fc commit 437421f
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 134 deletions.
20 changes: 0 additions & 20 deletions pkg/clusterapi/apibuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,8 @@ func KubeadmControlPlane(clusterSpec *cluster.Spec, infrastructureObject APIObje
},
}

if err := SetRegistryMirrorInKubeadmControlPlane(kcp, clusterSpec.Cluster.Spec.RegistryMirrorConfiguration); err != nil {
return nil, err
}

if err := SetProxyConfigInKubeadmControlPlane(kcp, clusterSpec.Cluster.Spec); err != nil {
return nil, err
}

SetIdentityAuthInKubeadmControlPlane(kcp, clusterSpec)

RestartContainerdInKubeadmControlPlane(kcp, clusterSpec.Cluster.Spec)

return kcp, nil
}

Expand Down Expand Up @@ -213,16 +203,6 @@ func KubeadmConfigTemplate(clusterSpec *cluster.Spec, workerNodeGroupConfig v1al
},
}

if err := SetRegistryMirrorInKubeadmConfigTemplate(kct, clusterSpec.Cluster.Spec.RegistryMirrorConfiguration); err != nil {
return nil, err
}

if err := SetProxyConfigInKubeadmConfigTemplate(kct, clusterSpec.Cluster.Spec); err != nil {
return nil, err
}

RestartContainerdInKubeadmConfigTemplate(kct, clusterSpec.Cluster.Spec)

return kct, nil
}

Expand Down
50 changes: 0 additions & 50 deletions pkg/clusterapi/apibuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import (
"github.com/aws/eks-anywhere/pkg/clusterapi"
)

var restartContainerdCommands = []string{
"sudo systemctl daemon-reload",
"sudo systemctl restart containerd",
}

type apiBuilerTest struct {
*WithT
clusterSpec *cluster.Spec
Expand Down Expand Up @@ -262,36 +257,6 @@ func TestKubeadmControlPlane(t *testing.T) {
tt.Expect(got).To(Equal(want))
}

func TestKubeadmControlPlaneWithRegistryMirror(t *testing.T) {
for _, tt := range registryMirrorTests {
t.Run(tt.name, func(t *testing.T) {
g := newApiBuilerTest(t)
g.clusterSpec.Cluster.Spec.RegistryMirrorConfiguration = tt.registryMirrorConfig
got, err := clusterapi.KubeadmControlPlane(g.clusterSpec, g.providerMachineTemplate)
g.Expect(err).To(Succeed())
want := wantKubeadmControlPlane()
want.Spec.KubeadmConfigSpec.Files = tt.wantFiles
want.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(wantRegistryMirrorCommands(), restartContainerdCommands...)
g.Expect(got).To(Equal(want))
})
}
}

func TestKubeadmControlPlaneWithProxyConfig(t *testing.T) {
for _, tt := range proxyTests {
t.Run(tt.name, func(t *testing.T) {
g := newApiBuilerTest(t)
g.clusterSpec.Cluster.Spec.ProxyConfiguration = tt.proxy
got, err := clusterapi.KubeadmControlPlane(g.clusterSpec, g.providerMachineTemplate)
g.Expect(err).To(Succeed())
want := wantKubeadmControlPlane()
want.Spec.KubeadmConfigSpec.Files = tt.wantFiles
want.Spec.KubeadmConfigSpec.PreKubeadmCommands = tt.wantCmd
g.Expect(got).To(Equal(want))
})
}
}

func wantKubeadmConfigTemplate() *bootstrapv1.KubeadmConfigTemplate {
return &bootstrapv1.KubeadmConfigTemplate{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -337,21 +302,6 @@ func TestKubeadmConfigTemplate(t *testing.T) {
tt.Expect(got).To(Equal(want))
}

func TestKubeadmConfigTemplateWithRegistryMirror(t *testing.T) {
for _, tt := range registryMirrorTests {
t.Run(tt.name, func(t *testing.T) {
g := newApiBuilerTest(t)
g.clusterSpec.Cluster.Spec.RegistryMirrorConfiguration = tt.registryMirrorConfig
got, err := clusterapi.KubeadmConfigTemplate(g.clusterSpec, *g.workerNodeGroupConfig)
g.Expect(err).To(Succeed())
want := wantKubeadmConfigTemplate()
want.Spec.Template.Spec.Files = tt.wantFiles
want.Spec.Template.Spec.PreKubeadmCommands = append(wantRegistryMirrorCommands(), restartContainerdCommands...)
g.Expect(got).To(Equal(want))
})
}
}

func TestMachineDeployment(t *testing.T) {
tt := newApiBuilerTest(t)
got := clusterapi.MachineDeployment(tt.clusterSpec, *tt.workerNodeGroupConfig, tt.kubeadmConfigTemplate, tt.providerMachineTemplate)
Expand Down
3 changes: 0 additions & 3 deletions pkg/clusterapi/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ var proxyTests = []struct {
name string
proxy *v1alpha1.ProxyConfiguration
wantFiles []bootstrapv1.File
wantCmd []string
}{
{
name: "proxy config nil",
proxy: nil,
wantFiles: []bootstrapv1.File{},
wantCmd: []string{},
},
{
name: "with proxy, pods cidr, service cidr, cp endpoint",
Expand All @@ -42,7 +40,6 @@ Environment="HTTPS_PROXY=1.2.3.4:8888"
Environment="NO_PROXY=1.2.3.4/5,1.2.3.4/5,1.2.3.4/0,1.2.3.5/0,localhost,127.0.0.1,.svc,1.2.3.4"`,
},
},
wantCmd: restartContainerdCommands,
},
}

Expand Down
15 changes: 5 additions & 10 deletions pkg/clusterapi/registry_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func registryMirrorConfigContent(registryAddress, registryCert string, insecureS
return string(config), nil
}

func registryMirrorConfig(registryMirrorConfig *v1alpha1.RegistryMirrorConfiguration) (files []bootstrapv1.File, preKubeadmCommands []string, err error) {
func registryMirrorConfig(registryMirrorConfig *v1alpha1.RegistryMirrorConfiguration) (files []bootstrapv1.File, err error) {
registryAddress := net.JoinHostPort(registryMirrorConfig.Endpoint, registryMirrorConfig.Port)
registryConfig, err := registryMirrorConfigContent(registryAddress, registryMirrorConfig.CACertContent, registryMirrorConfig.InsecureSkipVerify)
if err != nil {
return nil, nil, err
return nil, err
}
files = []bootstrapv1.File{
{
Expand All @@ -53,24 +53,20 @@ func registryMirrorConfig(registryMirrorConfig *v1alpha1.RegistryMirrorConfigura
})
}

preKubeadmCommands = []string{
"cat /etc/containerd/config_append.toml >> /etc/containerd/config.toml",
}
return files, preKubeadmCommands, nil
return files, nil
}

func SetRegistryMirrorInKubeadmControlPlane(kcp *controlplanev1.KubeadmControlPlane, mirrorConfig *v1alpha1.RegistryMirrorConfiguration) error {
if mirrorConfig == nil {
return nil
}

containerdFiles, containerdCommands, err := registryMirrorConfig(mirrorConfig)
containerdFiles, err := registryMirrorConfig(mirrorConfig)
if err != nil {
return fmt.Errorf("setting registry mirror configuration: %v", err)
}

kcp.Spec.KubeadmConfigSpec.Files = append(kcp.Spec.KubeadmConfigSpec.Files, containerdFiles...)
kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands, containerdCommands...)

return nil
}
Expand All @@ -80,13 +76,12 @@ func SetRegistryMirrorInKubeadmConfigTemplate(kct *bootstrapv1.KubeadmConfigTemp
return nil
}

containerdFiles, containerdCommands, err := registryMirrorConfig(mirrorConfig)
containerdFiles, err := registryMirrorConfig(mirrorConfig)
if err != nil {
return fmt.Errorf("setting registry mirror configuration: %v", err)
}

kct.Spec.Template.Spec.Files = append(kct.Spec.Template.Spec.Files, containerdFiles...)
kct.Spec.Template.Spec.PreKubeadmCommands = append(kct.Spec.Template.Spec.PreKubeadmCommands, containerdCommands...)

return nil
}
8 changes: 0 additions & 8 deletions pkg/clusterapi/registry_mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ var registryMirrorTests = []struct {
},
}

func wantRegistryMirrorCommands() []string {
return []string{
"cat /etc/containerd/config_append.toml >> /etc/containerd/config.toml",
}
}

func TestSetRegistryMirrorInKubeadmControlPlane(t *testing.T) {
for _, tt := range registryMirrorTests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -100,7 +94,6 @@ func TestSetRegistryMirrorInKubeadmControlPlane(t *testing.T) {
g.Expect(clusterapi.SetRegistryMirrorInKubeadmControlPlane(got, tt.registryMirrorConfig)).To(Succeed())
want := wantKubeadmControlPlane()
want.Spec.KubeadmConfigSpec.Files = tt.wantFiles
want.Spec.KubeadmConfigSpec.PreKubeadmCommands = wantRegistryMirrorCommands()
g.Expect(got).To(Equal(want))
})
}
Expand All @@ -114,7 +107,6 @@ func TestSetRegistryMirrorInKubeadmConfigTemplate(t *testing.T) {
g.Expect(clusterapi.SetRegistryMirrorInKubeadmConfigTemplate(got, tt.registryMirrorConfig)).To(Succeed())
want := wantKubeadmConfigTemplate()
want.Spec.Template.Spec.Files = tt.wantFiles
want.Spec.Template.Spec.PreKubeadmCommands = wantRegistryMirrorCommands()
g.Expect(got).To(Equal(want))
})
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/clusterapi/kubeadm_commands.go → pkg/clusterapi/systemctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@ import (
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)

var buildContainerdConfigCommands = []string{
"cat /etc/containerd/config_append.toml >> /etc/containerd/config.toml",
}

var restartContainerdCommands = []string{
"sudo systemctl daemon-reload",
"sudo systemctl restart containerd",
}

func CreateContainerdConfigFileInKubeadmControlPlane(kcp *controlplanev1.KubeadmControlPlane, cluster v1alpha1.ClusterSpec) {
if cluster.RegistryMirrorConfiguration != nil {
kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands, buildContainerdConfigCommands...)
}
}

func CreateContainerdConfigFileInKubeadmConfigTemplate(kct *bootstrapv1.KubeadmConfigTemplate, cluster v1alpha1.ClusterSpec) {
if cluster.RegistryMirrorConfiguration != nil {
kct.Spec.Template.Spec.PreKubeadmCommands = append(kct.Spec.Template.Spec.PreKubeadmCommands, buildContainerdConfigCommands...)
}
}

func RestartContainerdInKubeadmControlPlane(kcp *controlplanev1.KubeadmControlPlane, cluster v1alpha1.ClusterSpec) {
if restartContainerdNeeded(cluster) {
kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(kcp.Spec.KubeadmConfigSpec.PreKubeadmCommands, restartContainerdCommands...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
"github.com/aws/eks-anywhere/pkg/clusterapi"
)

var kubeadmCommandsTests = []struct {
var restartContainerdCommands = []string{
"sudo systemctl daemon-reload",
"sudo systemctl restart containerd",
}

var restartContainerdTests = []struct {
name string
cluster v1alpha1.ClusterSpec
want []string
Expand Down Expand Up @@ -49,7 +54,7 @@ var kubeadmCommandsTests = []struct {
}

func TestRestartContainerdInKubeadmControlPlane(t *testing.T) {
for _, tt := range kubeadmCommandsTests {
for _, tt := range restartContainerdTests {
t.Run(tt.name, func(t *testing.T) {
g := newApiBuilerTest(t)
got := wantKubeadmControlPlane()
Expand All @@ -62,7 +67,7 @@ func TestRestartContainerdInKubeadmControlPlane(t *testing.T) {
}

func TestRestartContainerdInKubeadmConfigTemplate(t *testing.T) {
for _, tt := range kubeadmCommandsTests {
for _, tt := range restartContainerdTests {
t.Run(tt.name, func(t *testing.T) {
g := newApiBuilerTest(t)
got := wantKubeadmConfigTemplate()
Expand All @@ -73,3 +78,56 @@ func TestRestartContainerdInKubeadmConfigTemplate(t *testing.T) {
})
}
}

var buildContainerdConfigCommands = []string{
"cat /etc/containerd/config_append.toml >> /etc/containerd/config.toml",
}

var createContainerdConfigTests = []struct {
name string
cluster v1alpha1.ClusterSpec
want []string
}{
{
name: "registry mirror exists",
cluster: v1alpha1.ClusterSpec{
RegistryMirrorConfiguration: &v1alpha1.RegistryMirrorConfiguration{
Endpoint: "1.2.3.4",
},
},
want: buildContainerdConfigCommands,
},
{
name: "registry mirror nil",
cluster: v1alpha1.ClusterSpec{
RegistryMirrorConfiguration: nil,
},
want: []string{},
},
}

func TestCreateContainerdConfigFileInKubeadmControlPlane(t *testing.T) {
for _, tt := range createContainerdConfigTests {
t.Run(tt.name, func(t *testing.T) {
g := newApiBuilerTest(t)
got := wantKubeadmControlPlane()
clusterapi.CreateContainerdConfigFileInKubeadmControlPlane(got, tt.cluster)
want := wantKubeadmControlPlane()
want.Spec.KubeadmConfigSpec.PreKubeadmCommands = tt.want
g.Expect(got).To(Equal(want))
})
}
}

func TestCreateContainerdConfigFileInKubeadmConfigTemplate(t *testing.T) {
for _, tt := range createContainerdConfigTests {
t.Run(tt.name, func(t *testing.T) {
g := newApiBuilerTest(t)
got := wantKubeadmConfigTemplate()
clusterapi.CreateContainerdConfigFileInKubeadmConfigTemplate(got, tt.cluster)
want := wantKubeadmConfigTemplate()
want.Spec.Template.Spec.PreKubeadmCommands = tt.want
g.Expect(got).To(Equal(want))
})
}
}
27 changes: 25 additions & 2 deletions pkg/providers/snow/apibuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func CAPICluster(clusterSpec *cluster.Spec, snowCluster *snowv1.AWSSnowCluster,
func KubeadmControlPlane(clusterSpec *cluster.Spec, snowMachineTemplate *snowv1.AWSSnowMachineTemplate) (*controlplanev1.KubeadmControlPlane, error) {
kcp, err := clusterapi.KubeadmControlPlane(clusterSpec, snowMachineTemplate)
if err != nil {
return nil, fmt.Errorf("failed generating KubeadmControlPlane: %v", err)
return nil, fmt.Errorf("generating KubeadmControlPlane: %v", err)
}

// TODO: support unstacked etcd
Expand All @@ -50,13 +50,24 @@ func KubeadmControlPlane(clusterSpec *cluster.Spec, snowMachineTemplate *snowv1.
fmt.Sprintf("/etc/eks/bootstrap-after.sh %s %s", clusterSpec.VersionsBundle.Snow.KubeVip.VersionedImage(), clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Endpoint.Host),
)

if err := clusterapi.SetRegistryMirrorInKubeadmControlPlane(kcp, clusterSpec.Cluster.Spec.RegistryMirrorConfiguration); err != nil {
return nil, err
}

if err := clusterapi.SetProxyConfigInKubeadmControlPlane(kcp, clusterSpec.Cluster.Spec); err != nil {
return nil, err
}

clusterapi.CreateContainerdConfigFileInKubeadmControlPlane(kcp, clusterSpec.Cluster.Spec)
clusterapi.RestartContainerdInKubeadmControlPlane(kcp, clusterSpec.Cluster.Spec)

return kcp, nil
}

func kubeadmConfigTemplate(clusterSpec *cluster.Spec, workerNodeGroupConfig v1alpha1.WorkerNodeGroupConfiguration) (*bootstrapv1.KubeadmConfigTemplate, error) {
kct, err := clusterapi.KubeadmConfigTemplate(clusterSpec, workerNodeGroupConfig)
if err != nil {
return nil, fmt.Errorf("failed generating KubeadmConfigTemplate: %v", err)
return nil, fmt.Errorf("generating KubeadmConfigTemplate: %v", err)
}

joinConfigKubeletExtraArg := kct.Spec.Template.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs
Expand All @@ -65,6 +76,18 @@ func kubeadmConfigTemplate(clusterSpec *cluster.Spec, workerNodeGroupConfig v1al
kct.Spec.Template.Spec.PreKubeadmCommands = append(kct.Spec.Template.Spec.PreKubeadmCommands,
"/etc/eks/bootstrap.sh",
)

if err := clusterapi.SetRegistryMirrorInKubeadmConfigTemplate(kct, clusterSpec.Cluster.Spec.RegistryMirrorConfiguration); err != nil {
return nil, err
}

if err := clusterapi.SetProxyConfigInKubeadmConfigTemplate(kct, clusterSpec.Cluster.Spec); err != nil {
return nil, err
}

clusterapi.CreateContainerdConfigFileInKubeadmConfigTemplate(kct, clusterSpec.Cluster.Spec)
clusterapi.RestartContainerdInKubeadmConfigTemplate(kct, clusterSpec.Cluster.Spec)

return kct, nil
}

Expand Down
Loading

0 comments on commit 437421f

Please sign in to comment.