-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package amifamily | ||
|
||
import ( | ||
"github.com/samber/lo" | ||
v1 "k8s.io/api/core/v1" | ||
corev1beta1 "sigs.k8s.io/karpenter/pkg/apis/v1beta1" | ||
"sigs.k8s.io/karpenter/pkg/cloudprovider" | ||
|
||
"github.com/aws/karpenter-provider-aws/pkg/apis/v1beta1" | ||
"github.com/aws/karpenter-provider-aws/pkg/providers/amifamily/bootstrap" | ||
) | ||
|
||
type AL2023 struct { | ||
DefaultFamily | ||
*Options | ||
} | ||
|
||
func (a AL2023) DefaultAMIs(version string) []DefaultAMIOutput { | ||
// TODO: SSM parameters not yet available | ||
return []DefaultAMIOutput{} | ||
} | ||
|
||
func (a AL2023) UserData(kubeletConfig *corev1beta1.KubeletConfiguration, taints []v1.Taint, labels map[string]string, caBundle *string, _ []*cloudprovider.InstanceType, customUserData *string, _ *v1beta1.InstanceStorePolicy) bootstrap.Bootstrapper { | ||
return bootstrap.Nodeadm{ | ||
Options: bootstrap.Options{ | ||
ClusterName: a.Options.ClusterName, | ||
ClusterEndpoint: a.Options.ClusterEndpoint, | ||
ClusterCIDR: a.Options.ClusterCIDR, | ||
KubeletConfig: kubeletConfig, | ||
Taints: taints, | ||
Labels: labels, | ||
CABundle: caBundle, | ||
CustomUserData: customUserData, | ||
}, | ||
} | ||
} | ||
|
||
// DefaultBlockDeviceMappings returns the default block device mappings for the AMI Family | ||
func (a AL2023) DefaultBlockDeviceMappings() []*v1beta1.BlockDeviceMapping { | ||
return []*v1beta1.BlockDeviceMapping{{ | ||
DeviceName: a.EphemeralBlockDevice(), | ||
EBS: &DefaultEBS, | ||
}} | ||
} | ||
|
||
func (a AL2023) EphemeralBlockDevice() *string { | ||
return lo.ToPtr("/dev/xvda") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package bootstrap | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"fmt" | ||
"mime/multipart" | ||
"net/textproto" | ||
"reflect" | ||
"strings" | ||
|
||
admapi "github.com/awslabs/amazon-eks-ami/nodeadm/api" | ||
"github.com/awslabs/amazon-eks-ami/nodeadm/api/v1alpha1" | ||
"github.com/samber/lo" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
) | ||
|
||
const nodeConfigContentType = "application/" + admapi.GroupName | ||
|
||
// const shellConfigContentType = `text/x-shellscript; charset="us-ascii"` | ||
|
||
type Nodeadm struct { | ||
Options | ||
} | ||
|
||
func (n Nodeadm) Script() (string, error) { | ||
nodeadmConfig, err := n.nodeadmConfig() | ||
if err != nil { | ||
return "", fmt.Errorf("generating NodeConfig, %w", err) | ||
} | ||
userData, err := n.mergeUserData(lo.Compact([]string{nodeadmConfig, lo.FromPtr(n.CustomUserData)})...) | ||
if err != nil { | ||
return "", err | ||
} | ||
// The mime/multipart package adds carriage returns, while the rest of our logic does not. Remove all | ||
// carriage returns for consistency. | ||
return base64.StdEncoding.EncodeToString([]byte(strings.ReplaceAll(userData, "\r", ""))), nil | ||
} | ||
|
||
func (n Nodeadm) mergeUserData(userDatas ...string) (string, error) { | ||
var outputBuffer bytes.Buffer | ||
writer := multipart.NewWriter(&outputBuffer) | ||
if err := writer.SetBoundary(Boundary); err != nil { | ||
return "", fmt.Errorf("defining boundary for merged user data %w", err) | ||
} | ||
outputBuffer.WriteString(MIMEVersionHeader + "\n") | ||
outputBuffer.WriteString(fmt.Sprintf(MIMEContentTypeHeaderTemplate, Boundary) + "\n\n") | ||
for _, userData := range userDatas { | ||
mimedUserData, err := n.mimeify(userData) | ||
if err != nil { | ||
return "", err | ||
} | ||
if err := copyCustomUserDataParts(writer, mimedUserData); err != nil { | ||
return "", err | ||
} | ||
} | ||
writer.Close() | ||
return outputBuffer.String(), nil | ||
} | ||
|
||
func (n Nodeadm) nodeadmConfig() (string, error) { | ||
config := &v1alpha1.NodeConfig{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "NodeConfig", | ||
APIVersion: admapi.GroupName + "/v1alpha1", | ||
}, | ||
Spec: v1alpha1.NodeConfigSpec{ | ||
Cluster: v1alpha1.ClusterDetails{ | ||
Name: n.ClusterName, | ||
APIServerEndpoint: n.ClusterEndpoint, | ||
CIDR: n.ClusterCIDR, | ||
}, | ||
}, | ||
} | ||
if n.CABundle != nil { | ||
ca, err := base64.StdEncoding.DecodeString(*n.CABundle) | ||
if err != nil { | ||
return "", err | ||
} | ||
config.Spec.Cluster.CertificateAuthority = ca | ||
} | ||
inlineConfig, err := n.generateInlineKubeletConfiguration() | ||
if err != nil { | ||
return "", err | ||
} | ||
if len(inlineConfig) != 0 { | ||
config.Spec.Kubelet.Config = inlineConfig | ||
} | ||
if labelArg := n.nodeLabelArg(); labelArg != "" { | ||
config.Spec.Kubelet.Flags = []string{labelArg} | ||
} | ||
|
||
configJSON, err := json.Marshal(config) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(configJSON), nil | ||
} | ||
|
||
func (n Nodeadm) generateInlineKubeletConfiguration() (map[string]runtime.RawExtension, error) { | ||
config := map[string]runtime.RawExtension{} | ||
if n.KubeletConfig != nil { | ||
t := reflect.TypeOf(n.KubeletConfig).Elem() | ||
rv := reflect.Indirect(reflect.ValueOf(n.KubeletConfig)) | ||
for i := 0; i < t.NumField(); i++ { | ||
field := t.Field(i) | ||
name := "" | ||
if tags := strings.Split(field.Tag.Get("json"), ","); len(tags) > 0 { | ||
name = tags[0] | ||
} else { | ||
return nil, fmt.Errorf("failed to serialize KubeletConfiguration, field %q doesn't specify a json tag", field.Name) | ||
} | ||
val := rv.FieldByName(field.Name).Interface() | ||
if reflect.DeepEqual(val, reflect.Zero(field.Type).Interface()) { | ||
// don't attempt to serialize zero values | ||
continue | ||
} | ||
jsonVal, err := json.Marshal(val) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to serialize KubeletConfiguration, %w", err) | ||
} | ||
config[name] = runtime.RawExtension{Raw: jsonVal} | ||
} | ||
} | ||
if len(n.Taints) != 0 { | ||
taintsJSON, err := json.Marshal(n.Taints) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to serialize KubeletConfiguration, %w", err) | ||
} | ||
config["registerWithTaints"] = runtime.RawExtension{Raw: taintsJSON} | ||
} | ||
if _, ok := config["maxPods"]; !n.AWSENILimitedPodDensity && !ok { | ||
config["maxPods"] = runtime.RawExtension{Raw: []byte("110")} | ||
} | ||
return config, nil | ||
} | ||
|
||
// TODO: attempt to parse as yaml / json then fall back to shell-script? Or only support yaml / json in non-mime format | ||
// mimeify returns userData in a mime format | ||
// if the userData passed in is already in a mime format, then the input is returned without modification | ||
func (n Nodeadm) mimeify(customUserData string) (string, error) { | ||
if strings.HasPrefix(strings.TrimSpace(customUserData), "MIME-Version:") || | ||
strings.HasPrefix(strings.TrimSpace(customUserData), "Content-Type:") { | ||
return customUserData, nil | ||
} | ||
var outputBuffer bytes.Buffer | ||
writer := multipart.NewWriter(&outputBuffer) | ||
outputBuffer.WriteString(MIMEVersionHeader + "\n") | ||
outputBuffer.WriteString(fmt.Sprintf(MIMEContentTypeHeaderTemplate, writer.Boundary()) + "\n\n") | ||
partWriter, err := writer.CreatePart(textproto.MIMEHeader{ | ||
"Content-Type": []string{nodeConfigContentType}, | ||
}) | ||
if err != nil { | ||
return "", fmt.Errorf("creating multi-part section from custom user-data: %w", err) | ||
} | ||
_, err = partWriter.Write([]byte(customUserData)) | ||
if err != nil { | ||
return "", fmt.Errorf("writing custom user-data input: %w", err) | ||
} | ||
writer.Close() | ||
return outputBuffer.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters