Skip to content

Commit

Permalink
move file writes to a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ndbaker1 committed Jan 19, 2024
1 parent eaa86c3 commit da2af64
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 38 deletions.
7 changes: 1 addition & 6 deletions nodeadm/internal/containerd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"context"
_ "embed"
"os"
"path"
"text/template"

"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
Expand Down Expand Up @@ -33,10 +31,7 @@ func writeContainerdConfig(cfg *api.NodeConfig) error {
if err != nil {
return err
}
if err := os.MkdirAll(path.Dir(containerdConfigFile), containerdConfigPerm); err != nil {
return err
}
return os.WriteFile(containerdConfigFile, containerdConfig, containerdConfigPerm)
return util.WriteFileWithDir(containerdConfigFile, containerdConfig, containerdConfigPerm)
}

func generateContainerdConfig(cfg *api.NodeConfig) ([]byte, error) {
Expand Down
9 changes: 3 additions & 6 deletions nodeadm/internal/daemon/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"context"
"fmt"
"io/fs"
"os"
"path"

"github.com/awslabs/amazon-eks-ami/nodeadm/internal/util"
"github.com/coreos/go-systemd/dbus"
)

Expand Down Expand Up @@ -114,13 +114,10 @@ const servicesRoot = "/etc/systemd/system"

func WriteSystemdServiceUnitDropIn(serviceName, fileName, fileContent string, filePerms fs.FileMode) error {
dropInPath := path.Join(servicesRoot, getServiceUnitDropInDir(serviceName), fileName)
if err := os.MkdirAll(path.Dir(dropInPath), filePerms); err != nil {
return err
}
return os.WriteFile(dropInPath, []byte(fileContent), filePerms)
return util.WriteFileWithDir(dropInPath, []byte(fileContent), filePerms)
}

func WriteSystemdServiceUnit(serviceName, unitContent string, filePerms fs.FileMode) error {
serviceUnitPath := path.Join(servicesRoot, getServiceUnitName(serviceName))
return os.WriteFile(serviceUnitPath, []byte(unitContent), filePerms)
return util.WriteFileWithDir(serviceUnitPath, []byte(unitContent), filePerms)
}
8 changes: 2 additions & 6 deletions nodeadm/internal/kubelet/cert.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package kubelet

import (
"os"
"path"
"github.com/awslabs/amazon-eks-ami/nodeadm/internal/util"
)

const caCertificatePath = "/etc/kubernetes/pki/ca.crt"

// Write the cluster certifcate authority to the filesystem where
// both kubelet and kubeconfig can read it
func writeClusterCaCert(caCert []byte) error {
if err := os.MkdirAll(path.Dir(caCertificatePath), kubeletConfigPerm); err != nil {
return err
}
return os.WriteFile(caCertificatePath, caCert, kubeletConfigPerm)
return util.WriteFileWithDir(caCertificatePath, caCert, kubeletConfigPerm)
}
13 changes: 3 additions & 10 deletions nodeadm/internal/kubelet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/awslabs/amazon-eks-ami/nodeadm/internal/api"
featuregates "github.com/awslabs/amazon-eks-ami/nodeadm/internal/feature-gates"
"github.com/awslabs/amazon-eks-ami/nodeadm/internal/util"
)

const (
Expand Down Expand Up @@ -284,33 +285,25 @@ func (k *kubelet) GenerateKubeletConfig(cfg *api.NodeConfig) (*kubeletSubConfig,
// - kubeletConfigOverrides should be passed in the order of application
func (k *kubelet) writeKubeletConfigToFile(kubeletConfig []byte) error {
configPath := path.Join(kubeletConfigRoot, kubeletConfigFile)
if err := os.MkdirAll(path.Dir(configPath), kubeletConfigPerm); err != nil {
return err
}

k.additionalArguments["config"] = configPath

zap.L().Info("Writing kubelet config to file..", zap.String("path", configPath))
return os.WriteFile(configPath, kubeletConfig, kubeletConfigPerm)
return util.WriteFileWithDir(configPath, kubeletConfig, kubeletConfigPerm)
}

// WriteKubeletConfigToDir writes the kubelet config to a directory for drop-in
// directory support. This is only supported on kubelet versions >= 1.28.
// see: https://kubernetes.io/docs/tasks/administer-cluster/kubelet-config-file/#kubelet-conf-d
func (k *kubelet) writeKubeletConfigToDir(kubeletConfig []byte) error {
dirPath := path.Join(kubeletConfigRoot, kubeletConfigDir)
if err := os.MkdirAll(dirPath, kubeletConfigPerm); err != nil {
return err
}

k.additionalArguments["config-dir"] = dirPath

zap.L().Info("Enabling kubelet config drop-in dir..")
k.setEnv("KUBELET_CONFIG_DROPIN_DIR_ALPHA", "on")

filePath := path.Join(dirPath, "10-defaults.conf")
zap.L().Info("Writing kubelet config to drop-in file..", zap.String("path", filePath))
return os.WriteFile(filePath, kubeletConfig, kubeletConfigPerm)
return util.WriteFileWithDir(filePath, kubeletConfig, kubeletConfigPerm)
}

func getProviderId(availabilityZone, instanceId string) string {
Expand Down
6 changes: 2 additions & 4 deletions nodeadm/internal/kubelet/image-credential-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"text/template"

"github.com/awslabs/amazon-eks-ami/nodeadm/internal/api"
"github.com/awslabs/amazon-eks-ami/nodeadm/internal/util"
"go.uber.org/zap"
"golang.org/x/mod/semver"
)
Expand Down Expand Up @@ -48,10 +49,7 @@ func (k *kubelet) writeImageCredentialProviderConfig(cfg *api.NodeConfig) error
k.additionalArguments["image-credential-provider-bin-dir"] = path.Dir(ecrCredentialProviderBinPath)
k.additionalArguments["image-credential-provider-config"] = imageCredentialProviderConfigPath

if err := os.MkdirAll(imageCredentialProviderRoot, imageCredentialProviderPerm); err != nil {
return err
}
return os.WriteFile(imageCredentialProviderConfigPath, config, imageCredentialProviderPerm)
return util.WriteFileWithDir(imageCredentialProviderConfigPath, config, imageCredentialProviderPerm)
}

type imageCredentialProviderTemplateVars struct {
Expand Down
9 changes: 3 additions & 6 deletions nodeadm/internal/kubelet/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package kubelet
import (
"bytes"
_ "embed"
"os"
"path"
"text/template"

"github.com/awslabs/amazon-eks-ami/nodeadm/internal/api"
"github.com/awslabs/amazon-eks-ami/nodeadm/internal/util"
)

const (
Expand All @@ -30,18 +30,15 @@ func (k *kubelet) writeKubeconfig(cfg *api.NodeConfig) error {
if err != nil {
return err
}
if err := os.MkdirAll(kubeconfigRoot, kubeconfigPerm); err != nil {
return err
}
if enabled := cfg.Spec.Cluster.EnableOutpost; enabled != nil && *enabled {
// kubelet bootstrap kubeconfig uses aws-iam-authenticator with cluster id to authenticate to cluster
// - if "aws eks describe-cluster" is bypassed, for local outpost, the value of CLUSTER_NAME parameter will be cluster id.
// - otherwise, the cluster id will use the id returned by "aws eks describe-cluster".
k.additionalArguments["bootstrap-kubeconfig"] = kubeconfigBootstrapPath
return os.WriteFile(kubeconfigBootstrapPath, kubeconfig, kubeconfigPerm)
return util.WriteFileWithDir(kubeconfigBootstrapPath, kubeconfig, kubeconfigPerm)
} else {
k.additionalArguments["kubeconfig"] = kubeconfigPath
return os.WriteFile(kubeconfigPath, kubeconfig, kubeconfigPerm)
return util.WriteFileWithDir(kubeconfigPath, kubeconfig, kubeconfigPerm)
}
}

Expand Down
11 changes: 11 additions & 0 deletions nodeadm/internal/util/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ package util

import (
"errors"
"io/fs"
"os"
"os/exec"
"path"
"strconv"
"strings"
)

const trimChars = " \n\t"

// Wraps os.WriteFile to automatically create parent directories such that the
// caller does not need to ensure the existence of the file's directory
func WriteFileWithDir(filePath string, data []byte, perm fs.FileMode) error {
if err := os.MkdirAll(path.Dir(filePath), perm); err != nil {
return err
}
return os.WriteFile(filePath, data, perm)
}

func isHostPresent(host string) (bool, error) {
output, err := exec.Command("getent", "hosts", host).Output()
if err != nil {
Expand Down

0 comments on commit da2af64

Please sign in to comment.