Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable taints support in snow #2327

Merged
merged 3 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ mocks: ## Generate mocks
${GOPATH}/bin/mockgen -destination=pkg/diagnostics/interfaces/mocks/diagnostics.go -package=mocks -source "pkg/diagnostics/interfaces.go" DiagnosticBundle,AnalyzerFactory,CollectorFactory,BundleClient
${GOPATH}/bin/mockgen -destination=pkg/clusterapi/mocks/capiclient.go -package=mocks -source "pkg/clusterapi/manager.go" CAPIClient,KubectlClient
${GOPATH}/bin/mockgen -destination=pkg/clusterapi/mocks/client.go -package=mocks -source "pkg/clusterapi/resourceset_manager.go" Client
${GOPATH}/bin/mockgen -destination=pkg/clusterapi/mocks/fetch.go -package=mocks -source "pkg/clusterapi/fetch.go"
${GOPATH}/bin/mockgen -destination=pkg/crypto/mocks/crypto.go -package=mocks -source "pkg/crypto/certificategen.go" CertificateGenerator
${GOPATH}/bin/mockgen -destination=pkg/networking/cilium/mocks/clients.go -package=mocks -source "pkg/networking/cilium/client.go"
${GOPATH}/bin/mockgen -destination=pkg/networking/cilium/mocks/helm.go -package=mocks -source "pkg/networking/cilium/templater.go"
Expand Down
3 changes: 3 additions & 0 deletions pkg/clusterapi/apibuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ func KubeadmControlPlane(clusterSpec *cluster.Spec, infrastructureObject APIObje
InitConfiguration: &bootstrapv1.InitConfiguration{
NodeRegistration: bootstrapv1.NodeRegistrationOptions{
KubeletExtraArgs: SecureTlsCipherSuitesExtraArgs(),
Taints: clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Taints,
},
},
JoinConfiguration: &bootstrapv1.JoinConfiguration{
NodeRegistration: bootstrapv1.NodeRegistrationOptions{
KubeletExtraArgs: SecureTlsCipherSuitesExtraArgs(),
Taints: clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Taints,
},
},
PreKubeadmCommands: []string{},
Expand Down Expand Up @@ -193,6 +195,7 @@ func KubeadmConfigTemplate(clusterSpec *cluster.Spec, workerNodeGroupConfig v1al
JoinConfiguration: &bootstrapv1.JoinConfiguration{
NodeRegistration: bootstrapv1.NodeRegistrationOptions{
KubeletExtraArgs: map[string]string{},
Taints: workerNodeGroupConfig.Taints,
},
},
PreKubeadmCommands: []string{},
Expand Down
40 changes: 40 additions & 0 deletions pkg/clusterapi/apibuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func newApiBuilerTest(t *testing.T) apiBuilerTest {
Host: "1.2.3.4",
},
Count: 3,
Taints: []v1.Taint{
{
Key: "key1",
Value: "val1",
Effect: v1.TaintEffectNoExecute,
TimeAdded: nil,
},
},
},
KubernetesVersion: "1.21",
},
Expand All @@ -94,6 +102,14 @@ func newApiBuilerTest(t *testing.T) apiBuilerTest {
workerNodeGroupConfig := &v1alpha1.WorkerNodeGroupConfiguration{
Name: "wng-1",
Count: 3,
Taints: []v1.Taint{
{
Key: "key2",
Value: "val2",
Effect: v1.TaintEffectNoSchedule,
TimeAdded: nil,
},
},
}

kubeadmConfigTemplate := &bootstrapv1.KubeadmConfigTemplate{
Expand Down Expand Up @@ -231,11 +247,27 @@ func wantKubeadmControlPlane() *controlplanev1.KubeadmControlPlane {
InitConfiguration: &bootstrapv1.InitConfiguration{
NodeRegistration: bootstrapv1.NodeRegistrationOptions{
KubeletExtraArgs: tlsCipherSuitesArgs(),
Taints: []v1.Taint{
{
Key: "key1",
Value: "val1",
Effect: v1.TaintEffectNoExecute,
TimeAdded: nil,
},
},
},
},
JoinConfiguration: &bootstrapv1.JoinConfiguration{
NodeRegistration: bootstrapv1.NodeRegistrationOptions{
KubeletExtraArgs: tlsCipherSuitesArgs(),
Taints: []v1.Taint{
{
Key: "key1",
Value: "val1",
Effect: v1.TaintEffectNoExecute,
TimeAdded: nil,
},
},
},
},
PreKubeadmCommands: []string{},
Expand Down Expand Up @@ -283,6 +315,14 @@ func wantKubeadmConfigTemplate() *bootstrapv1.KubeadmConfigTemplate {
JoinConfiguration: &bootstrapv1.JoinConfiguration{
NodeRegistration: bootstrapv1.NodeRegistrationOptions{
KubeletExtraArgs: map[string]string{},
Taints: []v1.Taint{
{
Key: "key2",
Value: "val2",
Effect: v1.TaintEffectNoSchedule,
TimeAdded: nil,
},
},
},
},
PreKubeadmCommands: []string{},
Expand Down
47 changes: 47 additions & 0 deletions pkg/clusterapi/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package clusterapi

import (
"context"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
)

// KubeClient is a kubernetes API client
type KubeClient interface {
Get(ctx context.Context, name, namespace string, obj runtime.Object) error
}

func MachineDeploymentInCluster(ctx context.Context, kubeclient KubeClient, clusterSpec *cluster.Spec, workerNodeGroupConfig v1alpha1.WorkerNodeGroupConfiguration) (*clusterv1.MachineDeployment, error) {
md := &clusterv1.MachineDeployment{}
err := kubeclient.Get(ctx, MachineDeploymentName(clusterSpec, workerNodeGroupConfig), constants.EksaSystemNamespace, md)
if apierrors.IsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, err
}
return md, nil
}

func KubeadmConfigTemplateInCluster(ctx context.Context, kubeclient KubeClient, md *clusterv1.MachineDeployment) (*bootstrapv1.KubeadmConfigTemplate, error) {
if md == nil {
return nil, nil
}

kct := &bootstrapv1.KubeadmConfigTemplate{}
err := kubeclient.Get(ctx, md.Spec.Template.Spec.Bootstrap.ConfigRef.Name, constants.EksaSystemNamespace, kct)
if apierrors.IsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, err
}
return kct, nil
}
174 changes: 174 additions & 0 deletions pkg/clusterapi/fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package clusterapi_test

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/clusterapi"
"github.com/aws/eks-anywhere/pkg/clusterapi/mocks"
"github.com/aws/eks-anywhere/pkg/constants"
)

type fetchTest struct {
*WithT
ctx context.Context
kubeClient *mocks.MockKubeClient
clusterSpec *cluster.Spec
workerNodeGroupConfig v1alpha1.WorkerNodeGroupConfiguration
machineDeployment *clusterv1.MachineDeployment
}

func newFetchTest(t *testing.T) fetchTest {
ctrl := gomock.NewController(t)
kubeClient := mocks.NewMockKubeClient(ctrl)
wng := v1alpha1.WorkerNodeGroupConfiguration{
Name: "md-0",
}
md := &clusterv1.MachineDeployment{
Spec: clusterv1.MachineDeploymentSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
Bootstrap: clusterv1.Bootstrap{
ConfigRef: &v1.ObjectReference{
Name: "snow-test-md-0-1",
},
},
},
},
},
}
return fetchTest{
WithT: NewWithT(t),
ctx: context.Background(),
kubeClient: kubeClient,
clusterSpec: givenClusterSpec(),
workerNodeGroupConfig: wng,
machineDeployment: md,
}
}

func TestMachineDeploymentInCluster(t *testing.T) {
g := newFetchTest(t)
g.kubeClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0",
constants.EksaSystemNamespace,
&clusterv1.MachineDeployment{},
).
DoAndReturn(func(_ context.Context, _, _ string, obj *clusterv1.MachineDeployment) error {
g.machineDeployment.DeepCopyInto(obj)
return nil
})

got, err := clusterapi.MachineDeploymentInCluster(g.ctx, g.kubeClient, g.clusterSpec, g.workerNodeGroupConfig)
g.Expect(err).To(Succeed())
g.Expect(got).To(Equal(g.machineDeployment))
}

func TestMachineDeploymentInClusterNotExists(t *testing.T) {
g := newFetchTest(t)
g.kubeClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0",
constants.EksaSystemNamespace,
&clusterv1.MachineDeployment{},
).
Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, ""))

got, err := clusterapi.MachineDeploymentInCluster(g.ctx, g.kubeClient, g.clusterSpec, g.workerNodeGroupConfig)
g.Expect(err).To(Succeed())
g.Expect(got).To(BeNil())
}

func TestMachineDeploymentInClusterError(t *testing.T) {
g := newFetchTest(t)
g.kubeClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0",
constants.EksaSystemNamespace,
&clusterv1.MachineDeployment{},
).
Return(errors.New("get md error"))

got, err := clusterapi.MachineDeploymentInCluster(g.ctx, g.kubeClient, g.clusterSpec, g.workerNodeGroupConfig)
g.Expect(err).NotTo(Succeed())
g.Expect(got).To(BeNil())
}

func TestKubeadmConfigTemplateInCluster(t *testing.T) {
g := newFetchTest(t)
kct := &bootstrapv1.KubeadmConfigTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "kct-1",
},
}
g.kubeClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0-1",
constants.EksaSystemNamespace,
&bootstrapv1.KubeadmConfigTemplate{},
).
DoAndReturn(func(_ context.Context, _, _ string, obj *bootstrapv1.KubeadmConfigTemplate) error {
kct.DeepCopyInto(obj)
return nil
})

got, err := clusterapi.KubeadmConfigTemplateInCluster(g.ctx, g.kubeClient, g.machineDeployment)
g.Expect(err).To(Succeed())
g.Expect(got).To(Equal(kct))
}

func TestKubeadmConfigTemplateInClusterMachineDeploymentNil(t *testing.T) {
g := newFetchTest(t)
got, err := clusterapi.KubeadmConfigTemplateInCluster(g.ctx, g.kubeClient, nil)
g.Expect(err).To(Succeed())
g.Expect(got).To(BeNil())
}

func TestKubeadmConfigTemplateInClusterNotExists(t *testing.T) {
g := newFetchTest(t)
g.kubeClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0-1",
constants.EksaSystemNamespace,
&bootstrapv1.KubeadmConfigTemplate{},
).
Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, ""))

got, err := clusterapi.KubeadmConfigTemplateInCluster(g.ctx, g.kubeClient, g.machineDeployment)
g.Expect(err).To(Succeed())
g.Expect(got).To(BeNil())
}

func TestKubeadmConfigTemplateInClusterError(t *testing.T) {
g := newFetchTest(t)
g.kubeClient.EXPECT().
Get(
g.ctx,
"snow-test-md-0-1",
constants.EksaSystemNamespace,
&bootstrapv1.KubeadmConfigTemplate{},
).
Return(errors.New("get kct error"))

got, err := clusterapi.KubeadmConfigTemplateInCluster(g.ctx, g.kubeClient, g.machineDeployment)
g.Expect(err).NotTo(Succeed())
g.Expect(got).To(BeNil())
}
Loading