Skip to content

Commit

Permalink
OCM-7268 | feat: Update create machinepool command to support kubelet…
Browse files Browse the repository at this point in the history
…configs
  • Loading branch information
robpblake committed May 14, 2024
1 parent 1947eeb commit 65c4c3f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/create/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var args struct {
version string
autorepair bool
tuningConfigs string
kubeletConfigs string
rootDiskSize string
securityGroupIds []string
nodeDrainGracePeriod string
Expand Down Expand Up @@ -207,6 +208,15 @@ func init() {
"This list will overwrite any modifications made to node tuning configs on an ongoing basis.",
)

flags.StringVar(
&args.kubeletConfigs,
"kubelet-configs",
"",
"Name of the kubelet configs to be applied to the machine pool. Format should be a comma-separated list. "+
"Kubelet config must already exist. "+
"This list will overwrite any modifications made to node kubelet configs on an ongoing basis.",
)

flags.StringVar(&args.rootDiskSize,
"disk-size",
"",
Expand Down
38 changes: 38 additions & 0 deletions cmd/create/machinepool/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,44 @@ func addNodePool(cmd *cobra.Command, clusterKey string, cluster *cmv1.Cluster, r
npBuilder.TuningConfigs(inputTuningConfig...)
}

var inputKubeletConfigs []string
kubeletConfigs := args.kubeletConfigs
// Get the list of available kubelet configs
availableKubeletConfigs, err := r.OCMClient.ListKubeletConfigNames(cluster.ID())
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
}
if kubeletConfigs != "" {
if len(availableKubeletConfigs) > 0 {
inputKubeletConfigs = strings.Split(kubeletConfigs, ",")
} else {
// Parameter will be ignored
r.Reporter.Warnf("No kubelet config available for cluster '%s'. "+
"Any kubelet config in input will be ignored", cluster.ID())
}
}
if interactive.Enabled() {
// Skip if no kubelet configs are available
if len(availableKubeletConfigs) > 0 {
inputKubeletConfigs, err = interactive.GetMultipleOptions(interactive.Input{
Question: "Kubelet configs",
Help: cmd.Flags().Lookup("kubelet-configs").Usage,
Options: availableKubeletConfigs,
Default: inputKubeletConfigs,
Required: false,
})
if err != nil {
r.Reporter.Errorf("Expected a valid value for kubelet configs: %s", err)
os.Exit(1)
}
}
}

if len(inputKubeletConfigs) != 0 {
npBuilder.KubeletConfigs(inputKubeletConfigs...)
}

npBuilder.AWSNodePool(createAwsNodePoolBuilder(instanceType, securityGroupIds, awsTags))

nodeDrainGracePeriod := args.nodeDrainGracePeriod
Expand Down
16 changes: 16 additions & 0 deletions pkg/ocm/kubeletconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ func toOCMKubeletConfig(args KubeletConfigArgs) (*cmv1.KubeletConfig, error) {
return kubeletConfig, nil
}

func (c *Client) ListKubeletConfigNames(clusterId string) ([]string, error) {
configs, err := c.ListKubeletConfigs(context.Background(), clusterId)
if err != nil {
return make([]string, 0), err
}

var names []string

if len(configs) > 0 {
for _, c := range configs {
names = append(names, c.Name())
}
}
return names, nil
}

func (c *Client) CreateKubeletConfig(clusterID string, args KubeletConfigArgs) (*cmv1.KubeletConfig, error) {

kubeletConfig, err := toOCMKubeletConfig(args)
Expand Down
35 changes: 35 additions & 0 deletions pkg/ocm/kubeletconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"time"

"slices"

. "github.com/onsi/ginkgo/v2/dsl/core"
. "github.com/onsi/ginkgo/v2/dsl/decorators"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -314,6 +316,39 @@ var _ = Describe("KubeletConfig", Ordered, func() {
Expect(kubeletConfig.PodPidsLimit()).To(Equal(10000))
})
})

Context("Get KubeletConfig names", func() {
It("Gets the name of available KubeletConfigs", func() {
apiServer.AppendHandlers(
RespondWithJSON(
http.StatusOK, createKubeletConfigList(false)))

names, err := ocmClient.ListKubeletConfigNames(clusterId)
Expect(err).NotTo(HaveOccurred())
Expect(slices.Contains(names, kubeletName)).To(BeTrue())
})

It("Returns an empty list if no kubeletconfigs available", func() {
apiServer.AppendHandlers(
RespondWithJSON(
http.StatusOK, createKubeletConfigList(true)))

names, err := ocmClient.ListKubeletConfigNames(clusterId)
Expect(err).NotTo(HaveOccurred())
Expect(names).To(BeEmpty())
})

It("Returns an error if failing to list KubeletConfig names", func() {
apiServer.AppendHandlers(
RespondWithJSON(
http.StatusInternalServerError, createKubeletConfigList(false)))

names, err := ocmClient.ListKubeletConfigNames(clusterId)
Expect(err).To(HaveOccurred())
Expect(names).To(BeEmpty())
})

})
})

func createKubeletConfig() (string, error) {
Expand Down

0 comments on commit 65c4c3f

Please sign in to comment.