Skip to content

Commit

Permalink
just populate k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
bwagner5 committed Jul 8, 2022
1 parent 35e78dc commit f02d47e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 63 deletions.
5 changes: 1 addition & 4 deletions pkg/cloudprovider/aws/amifamily/bootstrap/bottlerocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ type Bottlerocket struct {
}

func (b Bottlerocket) Script() (string, error) {
s, err := NewBottlerocketSettings(b.CustomUserData)
s, err := NewBottlerocketConfig(b.CustomUserData)
if err != nil {
return "", fmt.Errorf("invalid UserData %w", err)
}
// Karpenter will overwrite settings present inside custom UserData
// based on other fields specified in the provisioner
if s.Settings.Kubernetes == nil {
s.Settings.Kubernetes = &kubernetes{}
}
s.Settings.Kubernetes.ClusterName = &b.ClusterName
s.Settings.Kubernetes.APIServer = &b.ClusterEndpoint
s.Settings.Kubernetes.ClusterCertificate = b.CABundle
Expand Down
99 changes: 40 additions & 59 deletions pkg/cloudprovider/aws/amifamily/bootstrap/bottlerocketsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ limitations under the License.
package bootstrap

import (
"reflect"
"strings"

"github.com/pelletier/go-toml/v2"
)

func NewBottlerocketSettings(userdata *string) (*config, error) {
c := &config{}
func NewBottlerocketConfig(userdata *string) (*BottlerocketConfig, error) {
c := &BottlerocketConfig{}
if userdata == nil {
return c, nil
}
Expand All @@ -32,56 +29,56 @@ func NewBottlerocketSettings(userdata *string) (*config, error) {
return c, nil
}

// config is the root of the bottlerocket config, see more here https://github.com/bottlerocket-os/bottlerocket#using-user-data
type config struct {
// BottlerocketConfig is the root of the bottlerocket config, see more here https://github.com/bottlerocket-os/bottlerocket#using-user-data
type BottlerocketConfig struct {
SettingsRaw map[string]interface{} `toml:"settings"`
Settings settings `toml:"-"`
Settings BottlerocketSettings `toml:"-"`
}

// This is a subset of all configuration in https://github.com/bottlerocket-os/bottlerocket/blob/develop/sources/models/src/aws-k8s-1.22/mod.rs
// BottlerocketSettings is a subset of all configuration in https://github.com/bottlerocket-os/bottlerocket/blob/develop/sources/models/src/aws-k8s-1.22/mod.rs
// These settings apply across all K8s versions that karpenter supports.
type settings struct {
Kubernetes *kubernetes `toml:"kubernetes"`
type BottlerocketSettings struct {
Kubernetes BottlerocketKubernetes `toml:"kubernetes"`
}

// kubernetes specific configuration for bottlerocket api
type kubernetes struct {
APIServer *string `toml:"api-server"`
ClusterCertificate *string `toml:"cluster-certificate"`
ClusterName *string `toml:"cluster-name"`
ClusterDNSIP *string `toml:"cluster-dns-ip,omitempty"`
NodeLabels map[string]string `toml:"node-labels,omitempty"`
NodeTaints map[string][]string `toml:"node-taints,omitempty"`
MaxPods *int `toml:"max-pods,omitempty"`
StaticPods map[string]staticPod `toml:"static-pods,omitempty"`
EvictionHard map[string]string `toml:"eviction-hard,omitempty"`
KubeReserved map[string]string `toml:"kube-reserved,omitempty"`
SystemReserved map[string]string `toml:"system-reserved,omitempty"`
AllowedUnsafeSysctls []string `toml:"allowed-unsafe-sysctls,omitempty"`
ServerTLSBootstrap *bool `toml:"server-tls-bootstrap,omitempty"`
RegistryQPS *int `toml:"registry-qps,omitempty"`
RegistryBurst *int `toml:"registry-burst,omitempty"`
EventQPS *int `toml:"event-qps,omitempty"`
EventBurst *int `toml:"event-burst,omitempty"`
KubeAPIQPS *int `toml:"kube-api-qps,omitempty"`
KubeAPIBurst *int `toml:"kube-api-burst,omitempty"`
ContainerLogMaxSize *string `toml:"container-log-max-size,omitempty"`
ContainerLogMaxFiles *int `toml:"container-log-max-files,omitempty"`
CPUManagerPolicy *string `toml:"cpu-manager-policy,omitempty"`
CPUManagerReconcilePeriod *string `toml:"cpu-manager-reconcile-period,omitempty"`
TopologyManagerScope *string `toml:"topology-manager-scope,omitempty"`
TopologyManagerPolicy *string `toml:"topology-manager-policy,omitempty"`
// BottlerocketKubernetes is k8s specific configuration for bottlerocket api
type BottlerocketKubernetes struct {
APIServer *string `toml:"api-server"`
ClusterCertificate *string `toml:"cluster-certificate"`
ClusterName *string `toml:"cluster-name"`
ClusterDNSIP *string `toml:"cluster-dns-ip,omitempty"`
NodeLabels map[string]string `toml:"node-labels,omitempty"`
NodeTaints map[string][]string `toml:"node-taints,omitempty"`
MaxPods *int `toml:"max-pods,omitempty"`
StaticPods map[string]BottlerocketStaticPod `toml:"static-pods,omitempty"`
EvictionHard map[string]string `toml:"eviction-hard,omitempty"`
KubeReserved map[string]string `toml:"kube-reserved,omitempty"`
SystemReserved map[string]string `toml:"system-reserved,omitempty"`
AllowedUnsafeSysctls []string `toml:"allowed-unsafe-sysctls,omitempty"`
ServerTLSBootstrap *bool `toml:"server-tls-bootstrap,omitempty"`
RegistryQPS *int `toml:"registry-qps,omitempty"`
RegistryBurst *int `toml:"registry-burst,omitempty"`
EventQPS *int `toml:"event-qps,omitempty"`
EventBurst *int `toml:"event-burst,omitempty"`
KubeAPIQPS *int `toml:"kube-api-qps,omitempty"`
KubeAPIBurst *int `toml:"kube-api-burst,omitempty"`
ContainerLogMaxSize *string `toml:"container-log-max-size,omitempty"`
ContainerLogMaxFiles *int `toml:"container-log-max-files,omitempty"`
CPUManagerPolicy *string `toml:"cpu-manager-policy,omitempty"`
CPUManagerReconcilePeriod *string `toml:"cpu-manager-reconcile-period,omitempty"`
TopologyManagerScope *string `toml:"topology-manager-scope,omitempty"`
TopologyManagerPolicy *string `toml:"topology-manager-policy,omitempty"`
}

type staticPod struct {
type BottlerocketStaticPod struct {
Enabled *bool `toml:"enabled,omitempty"`
Manifest *string `toml:"manifest,omitempty"`
}

func (c *config) UnmarshalTOML(data []byte) error {
func (c *BottlerocketConfig) UnmarshalTOML(data []byte) error {
// unmarshal known settings
s := struct {
Settings settings `toml:"settings"`
Settings BottlerocketSettings `toml:"settings"`
}{}
if err := toml.Unmarshal(data, &s); err != nil {
return err
Expand All @@ -94,26 +91,10 @@ func (c *config) UnmarshalTOML(data []byte) error {
return nil
}

func (c *config) MarshalTOML() ([]byte, error) {
func (c *BottlerocketConfig) MarshalTOML() ([]byte, error) {
if c.SettingsRaw == nil {
c.SettingsRaw = map[string]interface{}{}
}
settings := reflect.ValueOf(c.Settings)
for i := 0; i < settings.NumField(); i++ {
field := settings.Field(i)
tField := settings.Type().Field(i)
if !tField.IsExported() {
continue
}
tomlKey := tField.Name
tag, ok := tField.Tag.Lookup("toml")
if ok {
parts := strings.Split(tag, ",")
if parts[0] != "" {
tomlKey = parts[0]
}
}
c.SettingsRaw[tomlKey] = field.Interface()
}
c.SettingsRaw["kubernetes"] = c.Settings.Kubernetes
return toml.Marshal(c)
}

0 comments on commit f02d47e

Please sign in to comment.