Skip to content

Commit

Permalink
Allow setting cluster-dns-ip for Bottlerocket
Browse files Browse the repository at this point in the history
  • Loading branch information
cPu1 authored and TiberiuGC committed Aug 24, 2023
1 parent 0e27abd commit 83f50e6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 51 deletions.
23 changes: 11 additions & 12 deletions pkg/apis/eksctl.io/v1alpha5/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,8 @@ func ValidateNodeGroup(i int, ng *NodeGroup, cfg *ClusterConfig) error {
if ng.OverrideBootstrapCommand != nil {
return fieldNotSupported("overrideBootstrapCommand")
}
if ng.Bottlerocket != nil {
if err := checkBottlerocketSettings(ng.Bottlerocket.Settings, path); err != nil {
if ng.Bottlerocket != nil && ng.Bottlerocket.Settings != nil {
if err := checkBottlerocketSettings(ng, path); err != nil {
return err
}
}
Expand Down Expand Up @@ -1484,17 +1484,13 @@ func (fps FargateProfileSelector) Validate() error {
return nil
}

func checkBottlerocketSettings(doc *InlineDocument, path string) error {
if doc == nil {
return nil
}

func checkBottlerocketSettings(ng *NodeGroup, path string) error {
overlapErr := func(key, ngField string) error {
return errors.Errorf("invalid Bottlerocket setting: use %s.%s instead (path=%s)", path, ngField, key)
}

// Dig into kubernetes settings if provided.
kubeVal, ok := (*doc)["kubernetes"]
kubeVal, ok := (*ng.Bottlerocket.Settings)["kubernetes"]
if !ok {
return nil
}
Expand All @@ -1505,10 +1501,9 @@ func checkBottlerocketSettings(doc *InlineDocument, path string) error {
}

checkMapping := map[string]string{
"node-labels": "labels",
"node-taints": "taints",
"max-pods": "maxPodsPerNode",
"cluster-dns-ip": "clusterDNS",
"node-labels": "labels",
"node-taints": "taints",
"max-pods": "maxPodsPerNode",
}

for checkKey, shouldUse := range checkMapping {
Expand All @@ -1518,6 +1513,10 @@ func checkBottlerocketSettings(doc *InlineDocument, path string) error {
}
}

if _, ok := kube["cluster-dns-ip"]; ok && ng.ClusterDNS != "" {
return fmt.Errorf("only one of %[1]s.bottlerocket.settings.kubernetes.cluster-dns-ip or %[1]s.clusterDNS can be set", path)
}

return nil
}

Expand Down
120 changes: 81 additions & 39 deletions pkg/apis/eksctl.io/v1alpha5/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1803,23 +1803,55 @@ var _ = Describe("ClusterConfig validation", func() {
Expect(err).To(MatchError(ContainSubstring(`bottlerocket config can only be used with amiFamily "Bottlerocket"`)))
})

It("returns an error with unsupported fields", func() {
cmd := "/usr/bin/some-command"
doc := api.InlineDocument{
"cgroupDriver": "systemd",
}
type bottlerocketEntry struct {
ng *api.NodeGroup
expectedErr string
}

ngs := map[string]*api.NodeGroup{
"PreBootstrapCommands": {
DescribeTable("field validation", func(be bottlerocketEntry) {
if be.ng.NodeGroupBase == nil {
be.ng.NodeGroupBase = &api.NodeGroupBase{}
}
be.ng.AMIFamily = api.NodeImageFamilyBottlerocket
err := api.ValidateNodeGroup(0, be.ng, api.NewClusterConfig())
if be.expectedErr != "" {
Expect(err).To(MatchError(be.expectedErr))
} else {
Expect(err).NotTo(HaveOccurred())
}
},
Entry("preBootstrapCommands", bottlerocketEntry{
ng: &api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{
PreBootstrapCommands: []string{"/usr/bin/env true"},
}},
"OverrideBootstrapCommand": {
},
},

expectedErr: "preBootstrapCommands is not supported for Bottlerocket nodegroups (path=nodeGroups[0].preBootstrapCommands)",
}),

Entry("overrideBootstrapCommand", bottlerocketEntry{
ng: &api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{
OverrideBootstrapCommand: &cmd,
}},
"KubeletExtraConfig": {KubeletExtraConfig: &doc},
"overlapping Bottlerocket settings": {
OverrideBootstrapCommand: aws.String("/usr/bin/some-command"),
},
},

expectedErr: "overrideBootstrapCommand is not supported for Bottlerocket nodegroups (path=nodeGroups[0].overrideBootstrapCommand)",
}),

Entry("kubeletExtraConfig", bottlerocketEntry{
ng: &api.NodeGroup{
KubeletExtraConfig: &api.InlineDocument{
"cgroupDriver": "systemd",
},
},

expectedErr: "kubeletExtraConfig is not supported for Bottlerocket nodegroups (path=nodeGroups[0].kubeletExtraConfig)",
}),

Entry("overlapping settings", bottlerocketEntry{
ng: &api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{
Bottlerocket: &api.NodeGroupBottlerocket{
Settings: &api.InlineDocument{
Expand All @@ -1832,39 +1864,49 @@ var _ = Describe("ClusterConfig validation", func() {
},
},
},
}

cfg := api.NewClusterConfig()
for name, ng := range ngs {
if ng.NodeGroupBase == nil {
ng.NodeGroupBase = &api.NodeGroupBase{}
}
ng.AMIFamily = api.NodeImageFamilyBottlerocket
err := api.ValidateNodeGroup(0, ng, cfg)
Expect(err).To(HaveOccurred(), "foo", name)
}
})
expectedErr: "invalid Bottlerocket setting: use nodeGroups[0].labels instead (path=nodeGroups[0].kubernetes.node-labels)",
}),

It("has no error with supported fields", func() {
x := 32
ngs := []*api.NodeGroup{
{NodeGroupBase: &api.NodeGroupBase{Labels: map[string]string{"label": "label-value"}}},
{NodeGroupBase: &api.NodeGroupBase{MaxPodsPerNode: x}},
{
Entry("both clusterDNS and cluster-dns-ip set", bottlerocketEntry{
ng: &api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{
ScalingConfig: &api.ScalingConfig{
MinSize: &x,
Bottlerocket: &api.NodeGroupBottlerocket{
Settings: &api.InlineDocument{
"kubernetes": map[string]interface{}{
"cluster-dns-ip": "10.100.0.10",
},
},
},
},
ClusterDNS: "10.100.0.10",
},
}

cfg := api.NewClusterConfig()
for i, ng := range ngs {
ng.AMIFamily = api.NodeImageFamilyBottlerocket
Expect(api.ValidateNodeGroup(i, ng, cfg)).To(Succeed())
}
})
expectedErr: "only one of nodeGroups[0].bottlerocket.settings.kubernetes.cluster-dns-ip or nodeGroups[0].clusterDNS can be set",
}),

Entry("labels", bottlerocketEntry{
ng: &api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{Labels: map[string]string{"label": "label-value"}},
},
}),

Entry("maxPods", bottlerocketEntry{
ng: &api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{MaxPodsPerNode: 32},
},
}),

Entry("maxPods", bottlerocketEntry{
ng: &api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{
ScalingConfig: &api.ScalingConfig{
MinSize: aws.Int(5),
},
},
},
}),
)
})

type kmsFieldCase struct {
Expand Down

0 comments on commit 83f50e6

Please sign in to comment.