Skip to content

Commit

Permalink
BR limited settings validations for Kubelet configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mitalipaygude committed Jun 9, 2024
1 parent d485120 commit 58db504
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/validations/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"errors"
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
"sigs.k8s.io/yaml"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
Expand Down Expand Up @@ -267,3 +271,47 @@ func ValidateManagementComponentsVersionSkew(ctx context.Context, k KubectlClien
}
return nil
}

// ValidateBottlerocketKC validates bottlerocket settings for Kubelet Configuration.
func ValidateBottlerocketKC(spec *cluster.Spec) error {
cpKubeletConfig := spec.Cluster.Spec.ControlPlaneConfiguration.KubeletConfiguration
if err := validateKubeletConfiguration(cpKubeletConfig); err != nil {
return err
}

workerNodeGroupConfigs := spec.Cluster.Spec.WorkerNodeGroupConfigurations
for _, workerNodeGroupConfig := range workerNodeGroupConfigs {
wnKubeletConfig := workerNodeGroupConfig.KubeletConfiguration
if err := validateKubeletConfiguration(wnKubeletConfig); err != nil {
return err
}
}

return nil
}

func validateKubeletConfiguration(eksakubeconfig *unstructured.Unstructured) error {
if eksakubeconfig == nil {
return nil
}
var bottlerocketKC *v1beta1.BottlerocketKubernetesSettings

delete(eksakubeconfig.Object, "kind")
delete(eksakubeconfig.Object, "apiVersion")
kcString, err := yaml.Marshal(eksakubeconfig)
if err != nil {
return err

Check warning on line 303 in pkg/validations/cluster.go

View check run for this annotation

Codecov / codecov/patch

pkg/validations/cluster.go#L303

Added line #L303 was not covered by tests
}

_, err = yaml.YAMLToJSONStrict([]byte(kcString))
if err != nil {
return fmt.Errorf("unmarshaling the yaml, malformed yaml %v", err)

Check warning on line 308 in pkg/validations/cluster.go

View check run for this annotation

Codecov / codecov/patch

pkg/validations/cluster.go#L308

Added line #L308 was not covered by tests
}

err = yaml.UnmarshalStrict(kcString, &bottlerocketKC)
if err != nil {
return fmt.Errorf("unmarshaling KubeletConfiguration for %v", err)
}

return nil
}
115 changes: 115 additions & 0 deletions pkg/validations/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/eks-anywhere/internal/test"
Expand Down Expand Up @@ -742,3 +743,117 @@ func TestValidateManagementComponentsVersionSkew(t *testing.T) {
})
}
}

func TestValidateBottlerocketKC(t *testing.T) {
tests := []struct {
name string
spec *cluster.Spec
subErr error
}{
{
name: "cp config",
spec: &cluster.Spec{
Config: &cluster.Config{
Cluster: &anywherev1.Cluster{
Spec: anywherev1.ClusterSpec{
ControlPlaneConfiguration: anywherev1.ControlPlaneConfiguration{
KubeletConfiguration: &unstructured.Unstructured{
Object: map[string]interface{}{
"maxPods": 50,
},
},
},
},
},
},
},
subErr: nil,
},
{
name: "worker config",
spec: &cluster.Spec{
Config: &cluster.Config{
Cluster: &anywherev1.Cluster{
Spec: anywherev1.ClusterSpec{
WorkerNodeGroupConfigurations: []anywherev1.WorkerNodeGroupConfiguration{
{
KubeletConfiguration: &unstructured.Unstructured{
Object: map[string]interface{}{
"maxPods": 50,
},
},
},
},
},
},
},
},
subErr: nil,
},
{
name: "nil kc config",
spec: &cluster.Spec{
Config: &cluster.Config{
Cluster: &anywherev1.Cluster{
Spec: anywherev1.ClusterSpec{
ControlPlaneConfiguration: anywherev1.ControlPlaneConfiguration{},
},
},
},
},
subErr: nil,
},
{
name: "invalid cp config",
spec: &cluster.Spec{
Config: &cluster.Config{
Cluster: &anywherev1.Cluster{
Spec: anywherev1.ClusterSpec{
ControlPlaneConfiguration: anywherev1.ControlPlaneConfiguration{
KubeletConfiguration: &unstructured.Unstructured{
Object: map[string]interface{}{
"maxPodss": 50,
},
},
},
},
},
},
},
subErr: errors.New("unknown field \"maxPodss\""),
},
{
name: "invalid worker config",
spec: &cluster.Spec{
Config: &cluster.Config{
Cluster: &anywherev1.Cluster{
Spec: anywherev1.ClusterSpec{
WorkerNodeGroupConfigurations: []anywherev1.WorkerNodeGroupConfiguration{
{
KubeletConfiguration: &unstructured.Unstructured{
Object: map[string]interface{}{
"maxPodss": 50,
},
},
},
},
},
},
},
},
subErr: errors.New("unknown field \"maxPodss\""),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tt := newTest(t, withKubectl())

err := validations.ValidateBottlerocketKC(tc.spec)
if err != nil {
tt.Expect(err.Error()).To(ContainSubstring(tc.subErr.Error()))
} else {
tt.Expect(tc.subErr).To(BeNil())
}
})
}
}
15 changes: 15 additions & 0 deletions pkg/validations/createvalidations/preflightvalidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/config"
"github.com/aws/eks-anywhere/pkg/constants"
Expand Down Expand Up @@ -51,6 +52,20 @@ func (v *CreateValidations) PreflightValidations(ctx context.Context) []validati
},
}

if len(v.Opts.Spec.VSphereMachineConfigs) != 0 {
cpRef := v.Opts.Spec.Cluster.Spec.ControlPlaneConfiguration.MachineGroupRef.Name
if v.Opts.Spec.VSphereMachineConfigs[cpRef].Spec.OSFamily == v1alpha1.Bottlerocket {
createValidations = append(createValidations,
func() *validations.ValidationResult {
return &validations.ValidationResult{
Name: "validate cluster's kubelet configuration for Bottlerocket OS",
Remediation: "ensure that the settings confgiuraed for Kubelet Configuration are Bottlerocket supported",
Err: validations.ValidateBottlerocketKC(v.Opts.Spec),

Check warning on line 63 in pkg/validations/createvalidations/preflightvalidations.go

View check run for this annotation

Codecov / codecov/patch

pkg/validations/createvalidations/preflightvalidations.go#L56-L63

Added lines #L56 - L63 were not covered by tests
}
})

Check warning on line 65 in pkg/validations/createvalidations/preflightvalidations.go

View check run for this annotation

Codecov / codecov/patch

pkg/validations/createvalidations/preflightvalidations.go#L65

Added line #L65 was not covered by tests
}
}

if v.Opts.Spec.Cluster.IsManaged() {
createValidations = append(
createValidations,
Expand Down

0 comments on commit 58db504

Please sign in to comment.